﻿window.Guldeken = {};

Guldeken.Class = {
	create: function() {
		return function() {
		this.initialize.apply(this, arguments);
		}
	}
};

var _arrOpenAlerts = new Array(); //Alert
var _intOpenAlertIndex = 0; //Alert

var _strActionPath;

var _strAlertLayer = "<div class=\"alert\" id=\"divAlert_#{SrcElement}\">"
_strAlertLayer += "<div class=\"top\">&nbsp;</div>";
_strAlertLayer += "<div class=\"middle\">";
_strAlertLayer += "<div class=\"close\"><img onclick=\"Milou.Util.DOM.LayerVisibility('divAlert_#{SrcElement}','hidden')\" src=\"public/images/global_alert_close.gif\" alt=\"Stäng\" class=\"button\"></div>";
_strAlertLayer += "<img src=\"public/images/global_alert_icon_1.gif\" alt=\"\"><div id=\"divAlertContent\">#{Text}</div>";
if(Milou.Util.User.browser == 'Explorer' && Milou.Util.User.version < 7)
{
	_strAlertLayer += "<iframe id=\"iframeAlertDummy\"></iframe>";
}
_strAlertLayer += "</div>";
_strAlertLayer += "<div class=\"bottom\">&nbsp;</div>";
_strAlertLayer += "</div>";



Guldeken.Tickets = {
	Order: function() {
		strPath = $F('hidPath');
		Guldeken.Utils.SwapObject($('btnSend'), strPath+'images/btn_send_loader.gif')
		Guldeken.Alert.Clear();
		var strCompany = $F('txtCompany');
		if($('txtCompany').defaultValue == $F('txtCompany'))
			strCompany = "";
		
		new Ajax.Request(strPath + 'AjaxTasks.aspx', {
			parameters: {
				name: $F('txtName'),
				company: strCompany,
				address: $F('txtAddress'),
				phone: $F('txtPhone'),
				email: $F('txtEmail'),
				guests: $F('cmbGuests'),
				task: 'tickets'
			},
			onSuccess: function(strResult) {
				if(strResult.responseText.strip() == "true")
				{
					new Effect.Fade('ContainerContentMainOrderTickets', {duration:0.5, from:1.0, to:0});
					$('ContainerContentMainOrderTicketsConfirm').setOpacity(0);
					$('ContainerContentMainOrderTicketsConfirm').show();
					setTimeout("new Effect.Opacity('ContainerContentMainOrderTicketsConfirm', {duration:0.5, from:0, to:1.0})",500)
				}
			},
			onFailure: function(strResult) {
				Guldeken.Alert.Open("Något gick snett (Errorcode 1), var god försök igen eller kontakta support@milou.se","btnSend",(-30),(-70))
			}
		});
	}
};

Guldeken.Registration = {
	Send: function() {
		strPath = $F('hidPath');
		Guldeken.Utils.SwapObject($('btnSend'), strPath+'images/btn_send_loader.gif')
		Guldeken.Alert.Clear();
		new Ajax.Request(strPath + 'AjaxTasks.aspx', {
			parameters: {
				category: $F('cmbCategory'),
				reg_name: $F('txtItem'),
				reg_orderer: $F('txtOrderer'),
				company: $F('txtCompany'),
				name: $F('txtName'),
				address: $F('txtAddress'),
				phone: $F('txtPhone'),
				email: $F('txtEmail'),
				producent: $F('txtProducent'),
				description: $F('txtDescription'),
				task: 'registration'
			},
			onSuccess: function(strResult) {
				if(strResult.responseText.strip() > 0)
				{
					Guldeken.Registration.Upload(strResult.responseText.strip());
				}
				else
				{
					Guldeken.Alert.Open("Något gick snett (Errorcode 2), var god försök igen eller kontakta support@milou.se","txtPhone",(-30),(-10))
					$('btnSend').src = $('btnSend').src.replace("btn_send_loader","btn_send");
				}
			},
			onFailure: function(strResult) {
				Guldeken.Alert.Open("Något gick snett (Errorcode 3), var god försök igen eller kontakta support@milou.se","txtPhone",(-30),(-10))
				$('btnSend').src = $('btnSend').src.replace("btn_send_loader","btn_send");
			}
		});
	},
	Upload: function(intId) {
		if($('frmUpload').action.indexOf("?id") == -1)
			_strActionPath = $('frmUpload').action;
			
		$('frmUpload').action = _strActionPath + '?id='+intId;
		$('frmUpload').submit();
	},
	Success: function(intId) {
		new Ajax.Request(strPath + 'AjaxTasks.aspx', {
			parameters: {
				name: $F('txtName'),
				email: $F('txtEmail'),
				id: intId,
				task: 'registrationmail'
			},
			onSuccess: function(strResult) {
				$('ContainerContentMainRegistrationConfirm').setOpacity(0);
				$('ContainerContentMainRegistrationConfirm').show();
				new Effect.Fade('ContainerContentMainRegistration', {duration:0.5, from:1.0, to:0});
				setTimeout("new Effect.Opacity('ContainerContentMainRegistrationConfirm', {duration:0.5, from:0, to:1.0})",500)
				$('btnSend').src = $('btnSend').src.replace("btn_send_loader","btn_send");
			},
			onFailure: function(strResult) {
				Guldeken.Alert.Open("Något gick snett (Errorcode 4), var god försök igen eller kontakta support@milou.se","txtPhone",(-30),(-10))
				$('btnSend').src = $('btnSend').src.replace("btn_send_loader","btn_send");
			}
		});
	},
	Failure: function(strMessage) {
		Guldeken.Alert.Open("Något gick snett (Errorcode 5), var god försök igen eller kontakta support@milou.se","txtPhone",(-30),(-10))
		$('btnSend').src = $('btnSend').src.replace("btn_send_loader","btn_send");
	},
	TooLarge: function(strSize) {
		Guldeken.Alert.Open("Filer som laddas upp får max vara 20Mb, filen som laddades upp var " + (parseInt(strSize/1000000)) + "Mb. Kontakta sara@eventparlamentet.se om du inte kan minska storleken på filen.","fileAttachment",(-30),(-82))
		$('btnSend').src = $('btnSend').src.replace("btn_send_loader","btn_send");
	}
};

Guldeken.Utils = {
	AlertShow: function(objMessage) {
		Guldeken.Alert.Open(objMessage.Message,objMessage.ElementName,(-30),(-60));
		$(objMessage.ElementName).select();
		if($(objMessage.ElementName).viewportOffset()[1] < 100)
			window.scrollBy(0,($(objMessage.ElementName).viewportOffset()[1]-100)); 
	},
	SwapObject: function (objElement,strSrc) {
		if(objElement && objElement.src.indexOf('btn_send_loader') == -1)
		{
			objElement.src = strSrc;
		}
	}
};

Guldeken.Alert = {
	Open: function(strText,strSourceElement,intPosX,intPosY) {
		Guldeken.Alert.Clear();
		var templateAlert = new Template(_strAlertLayer).evaluate({SrcElement: strSourceElement,Text: strText});
		
		new Insertion.Before($(strSourceElement), templateAlert);
		//$(strSourceElement).insert(templateAlert,'before');
		
		var calunit = Milou.Util.User.browser == "Netscape"? "" : "px";
	    $("divAlert_"+strSourceElement).absolutize();
		$("divAlert_"+strSourceElement).style.left = intPosX+calunit;
		$("divAlert_"+strSourceElement).style.top = intPosY+calunit;		
		Milou.Util.DOM.LayerVisibility('divAlert_' + strSourceElement,'visible');
		_arrOpenAlerts[_intOpenAlertIndex] = "divAlert_"+strSourceElement;
		_intOpenAlertIndex ++;
    },
    Clear: function() {
		for(var i=0; i<_arrOpenAlerts.length; i++)
		{
			$(_arrOpenAlerts[i]).remove();
		}
		_arrOpenAlerts.clear();
		_intOpenAlertIndex = 0;
    }
};