﻿
$(document).ready(function() {

//  odblokowanie formularza wysylkowego
	$("#EFFsendtypeS").focus( function() { 
			$("#EFFsubmit").val('Wyślij z automatu');
			$("#EFFsubmit").removeAttr("disabled");
			$("#EFFtimesend").show();	
			$("#EFFnotifyofsend").removeAttr("disabled");
	});
	$("#EFFsendtypeL").focus( function() { 		
			$("#EFFsubmit").val('Wygeneruj link(i) do kartki');
			$("#EFFsubmit").removeAttr("disabled");
			$("#EFFtimesend").hide();
			$("#EFFnotifyofsend").attr("disabled","disabled");			
	});	
	
// listy nawigacyjne	
	$("#EcGalNaviCatList").change( function() { 
		location=this.value;
	});
	$("#EcGalNaviPageList").change( function() { 
		location=this.value;
	});	
	$("#AmountList").change( function() { 
		location=this.value;
	});			

// preview tekstu ekartki
	$("a.textprev").click(function(){
    	var title = this.title;
		var nr = title.substring(39);
		var idx = nr - 1;
		var email = $("#EFFemail"+idx).val();
		var head = $("#EFFhead"+idx).val();
		var place = $("#EFFplace").val();
		var bodyecard = $("#EFFbodyecard").val();
		var checkstring = bodyecard.indexOf("\n",0);
		while (checkstring>=0) {
				bodyecard = bodyecard.replace("\n", "<br />");
				checkstring = bodyecard.indexOf("\n",0);			
			};		
		var expression = $("#EFFexpression").val();
		var signature = $("#EFFsignature").val();		
		var timetypeecard = $("#EFFtimetypeecard").val();		
		var windowtitle = 'Treść kartki dla: '+email
		if (timetypeecard == 'oldtypeecard') {
			var ecardtemplate = $("#EFFecardtemplate").val();
			var url = 'http://' + siteurl + '/index.php?zone=kartki&module=podglad&action=textpreview&ecardtemplate=' + ecardtemplate + '&head=' + head + '&place=' + place + '&bodyecard=' + bodyecard + '&expression=' + expression + '&signature=' + signature; 
			if(ecardtemplate == 'A') {
		        GB_show(windowtitle,url,365,535);	
			}
			else if (ecardtemplate == 'B') {
		        GB_show(windowtitle,url,510,445);	
			}			
		}
		else {		
			var formcard = $("#EFFformcard").val();
			var typography = $("#EFFtypography").val();
			var orientation = $("#EFForientation").val();
			var url = 'http://' + siteurl + '/index.php?zone=ecards&module=textview&formcard=' + formcard + '&typography=' + typography + '&head=' + head + '&place=' + place + '&bodyecard=' + bodyecard + '&expression=' + expression + '&signature=' + signature + '&orientation=' + orientation; 
	        GB_show(windowtitle,url,609,584);
		}	
        return false;
	});

// full preview kartki
	$("#EFFfullprev").click(function(){
		var head = $("#EFFhead0").val();
		var place = $("#EFFplace").val();
		var bodyecard = $("#EFFbodyecard").val();
		var checkstring = bodyecard.indexOf("\n",0);
		while (checkstring>=0) {
				bodyecard = bodyecard.replace("\n", "<br />");
				checkstring = bodyecard.indexOf("\n",0);			
			};
		var expression = $("#EFFexpression").val();
		var signature = $("#EFFsignature").val();	
		var amount = $("#EFFamount").val();
		var orientation = $("#EFForientation").val();
    	var windowtitle = 'Podgląd eKartki Ajlawju.pl';	
		var timetypeecard = $("#EFFtimetypeecard").val();
		if (timetypeecard == 'oldtypeecard') {
			var ecardmodelid = $("#EFFecardmodelid").val();			
			var ecardtemplate = $("#EFFecardtemplate").val();
			var amount = $("#EFFamount").val();
			var envelope = $("#EFFenvelope").val();
			var url = 'http://' + siteurl + '/index.php?zone=kartki&module=podglad&action=fullpreview&ecardmodelid=' + ecardmodelid + '&ecardtemplate=' + ecardtemplate + '&head=' + head + '&place=' + place + '&bodyecard=' + bodyecard + '&expression=' + expression + '&signature=' + signature + '&puzzle=' + 0 + '&amount=' + amount + '&envelope=' + envelope;		
	        GB_show(windowtitle,url,532,754);	
		}
		else {		
			var scenario = $("#EFFscenario").val();
			var envelope = $("#EFFenvelope").val();
			var formcard = $("#EFFformcard").val();
			var typography = $("#EFFtypography").val();		
			var ecardthemeid = $("#EFFecardthemeid").val();
			var orientation = $("#EFForientation").val();			
			var url = 'http://' + siteurl + '/index.php?zone=ecards&module=view&theme=' + ecardthemeid + '&param=formpreview&scenario=' + scenario + '&envelope=' + envelope + '&formcard=' + formcard + '&typography=' + typography + '&head=' + head + '&place=' + place + '&bodyecard=' + bodyecard + '&expression=' + expression + '&signature=' + signature + '&amount=' + amount + '&orientation=' + orientation;
		//	alert(url);			
	        GB_show(windowtitle,url,612,740);
		}			
        return false;
	});	

//	wybor papeteriii
	$("select#EFFstationery").change( function() { 
		var valStationery = $("#EFFstationery").val();
		var sOne = valStationery.indexOf("a");
		var sTwo = valStationery.indexOf("b");
		var sThree = valStationery.indexOf("c");
		var valTemplate = valStationery.substring(0,sOne);
		var valEnvelope = valStationery.substring(sOne+1,sTwo);
		var valFormCard = valStationery.substring(sTwo+1,sThree);
		var valTypography = valStationery.substring(sThree+1);						
		$("#EFFtemplate").val(valTemplate);
		$("#EFFenvelope").val(valEnvelope);
		$("#EFFformCard").val(valFormCard);
	//	$("#EFFtypography").val(valTypography);
	}); 	


// sprawdzenie formularza wysylkowego
	$("#EcFillForm").submit( function() {	
		var amount = $("#EFFamount").val(); 
		var count = 0;
		var EmailVal = ""; 
		var ErrorCount = 0;
		do {
			EmailVal = $("#EFFemail"+count).val();	
			EmailVal = EmailVal.toLowerCase();
			EmailVal = ignoreSpaces(EmailVal);
			$("#EFFemail"+count).val(EmailVal);
			if (count==0 && $("#EFFemail"+count).val()=="") {
				$("#EFFalert").text("Musisz podać adres e-mail przynajmniej pierwszego adresata.");
				$("#EFFalert").show();
				return false;
			}
			else if ($("#EFFemail"+count).val()!=""){
				if ($("#EFFemail"+count).val().indexOf('@')==-1 || $("#EFFemail"+count).val().indexOf('.')==-1){			
					$("#EFFalert").text("Format adresu e-mail: '"+$("#EFFemail"+count).val()+"' jest nieprawidłowy. Popraw.");
					$("#EFFalert").show();
					return false;
				}			
			}	
			count++;	
		}
		while (count < amount)
		
		$("#EFFsubmit").val("Czekaj, wysyłam");
		$("#EFFsubmit").attr("disabled","disabled");
		$("EFFsubmit").addClass("CursorWait")
		return true;

	});
	
	
// sprawdzenie formularza wysylkowego
	$("#EcPrimaAprilisForm").submit( function() {	
		var amount = $("#EPAamount").val(); 
		var count = 0;
		var EmailVal = ""; 
		var ErrorCount = 0;
		do {
			EmailVal = $("#EPAemail"+count).val();	
			EmailVal = EmailVal.toLowerCase();
			EmailVal = ignoreSpaces(EmailVal);
			$("#EPAemail"+count).val(EmailVal);
			if (count==0 && $("#EPAemail"+count).val()=="") {
				$("#EPAalert").text("Musisz podać adres e-mail przynajmniej pierwszego adresata.");
				$("#EPAalert").show();
				return false;
			}
			else if ($("#EPAemail"+count).val()!=""){
				if ($("#EPAemail"+count).val().indexOf('@')==-1 || $("#EPAemail"+count).val().indexOf('.')==-1){			
					$("#EPAalert").text("Format adresu e-mail: '"+$("#EPAemail"+count).val()+"' jest nieprawidłowy. Popraw.");
					$("#EPAalert").show();
					return false;
				}			
			}	
			count++;	
		}
		while (count < amount)
		
		$("#EPAsubmit").val("Czekaj, wysyłam");
		$("#EPAsubmit").attr("disabled","disabled");
		$("EPAsubmit").addClass("CursorWait")
		return true;

	});


function ignoreSpaces(string) 
	{
		var temp = "";
		string = '' + string;
		splitstring = string.split(" ");
		for(i = 0; i < splitstring.length; i++)
		temp += splitstring[i];
		return temp;
	}	
	
	
	
			
});



		