var show_error_firstname;
var show_error_lastname;
var show_error_email;
var show_error_street;
var show_error_postcode;
var show_error_city;
var show_error_gender;
var show_error_pass;
var show_error_datensg;
var dialogHeight = 800;
var identifyer;
function setIdentyfyer(identity)
{
  identifyer = identity;
}
 function zurueck()
 {
   $("#gott").fadeTo("slow", 0.0, function()
    {
    $("#gott").hide();
 	$("#createAccountUserForm").fadeTo("slow", 1.0);
 	$(".ui-dialog-content").height(680);
   });
 }
function encode_utf8(rohtext) {
 alert("first"+rohtext);
            // dient der Normalisierung des Zeilenumbruchs
           rohtext = rohtext.replace(/\r\n/g,"\n");
           var utftext = "";
           for(var n=0; n<rohtext.length; n++)
              {
                // ermitteln des Unicodes des  aktuellen Zeichens
              var c=rohtext.charCodeAt(n);
                 // alle Zeichen von 0-127 => 1byte
                if (c<128)
                     utftext += String.fromCharCode(c);
               // alle Zeichen von 127 bis 2047 => 2byte
                 else if((c>127) && (c<2048)) {
                     utftext += String.fromCharCode((c>>6)|192);
                     utftext += String.fromCharCode((c&63)|128);}
                // alle Zeichen von 2048 bis 66536 => 3byte
                else {
                    utftext += String.fromCharCode((c>>12)|224);
                    utftext += String.fromCharCode(((c>>6)&63)|128);
                    utftext += String.fromCharCode((c&63)|128);}
                }
                alert("second"+utftext);
             return utftext;
        }

function EMailcheck(s)
{
 var a = false;
 var res = false;
 if(typeof(RegExp) == 'function')
 {
  var b = new RegExp('abc');
  if(b.test('abc') == true){a = true;}
  }

 if(a == true)
 {
  reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
                   '(\\@)([a-zA-Z0-9\\-\\.]+)'+
                   '(\\.)([a-zA-Z]{2,4})$');
  res = (reg.test(s));
 }
 else
 {
  res = (s.search('@') >= 1 &&
         s.lastIndexOf('.') > s.search('@') &&
         s.lastIndexOf('.') >= s.length-5)
 }
 return(res);
}
 function checkAccountCreateForm()
 { 
 	newsletter =    $("input[name='newsletter']:checked").val();
 	gender =        $("input[name='gender']:checked").val();
 	country =       $("option:selected").val();
 	telephone =     $("input[name='telephone']").val();
 	password =      $("input[name ='passwordd']").val();
 	confirmation =  $("input[name='confirmation']").val();
 	datensg =       $("input[name='datensg']:checked").val();
 	firstname =     $("input[name='firstname']").val();
 	lastname =      $("input[name='lastname']").val();
 	email =         $("input[name='email_address_create_account']").val();
 	street =        $("input[name='street_address']").val();
 	postcode =      $("input[name='postcode']").val();
 	city =          $("input[name='city']").val();
	accountstatus = $("input[name='createUser']:checked").val();
 	if(newsletter == undefined)
 	{
 		newsletter = 0;
 	}
 	if(accountstatus == undefined)
 	{
 		
		
		if (accountstatus == undefined)
		{
		$("input[name='createUser']").css({background: "#ffabab"});
 			$(".ui-dialog-content").height(dialogHeight+30);
 			dialogHeight = dialogHeight+30;
 			$("#accountError").slideDown("slow");
			show_error_accountstatus = 1;
		}
		accountCheck			= false;
 	}
	else 
	{
	if (show_error_accountstatus = 1)
		{
		
 			$(".ui-dialog-content").height(dialogHeight-30);
 			dialogHeight = dialogHeight-30;
 			$("#accountError").slideUp("slow");
			$("input[name='firstname']").css({background: "#ffabab"});
			show_error_accountstatus = 0
		}
		accountCheck			= true;
	}
 	//////////////////////////////////////////////////////////////////////
 	if(firstname.length < 3)
 	{
 		
 		if (show_error_firstname == 0)
 		{
 			$("input[name='firstname']").css({background: "#ffabab"});
 			$(".ui-dialog-content").height(dialogHeight+30);
 			dialogHeight = dialogHeight+30;
 			$("#firstNameError").slideDown("slow");	
 			show_error_firstname = 1;
 			
 		}
 		firstnameCheck = false;
 	}
 	else
 	{	
 		if (show_error_firstname == 1)
 		{
 			$(".ui-dialog-content").height(dialogHeight-30);
 			dialogHeight = dialogHeight-30;
 			$("#firstNameError").slideUp("slow");
 			$("input[name='firstname']").css({background: "#fff"});	
 			show_error_firstname = 0;
 			
 		}
 		firstnameCheck = true;
 	}
 	////////////////////////////////////////////////////////////////////////
 	if(lastname.length < 3)
 	{
 		if (show_error_lastname == 0)
 		{
 			$(".ui-dialog-content").height(dialogHeight+30);
 			dialogHeight = dialogHeight+30;
 			$("input[name='lastname']").css({background: "#ffabab"});
 			$("#lastNameError").slideDown("slow");
 			show_error_lastname = 1;
  			
 		}
 		lastnameCheck = false;
 	}
 	else
 	{
 		if (show_error_lastname == 1)
 		{
 			$(".ui-dialog-content").height(dialogHeight-30);
 			dialogHeight = dialogHeight-30;
 			$("#lastNameError").slideUp("slow");
 			$("input[name='lastname']").css({background: "#fff"});
 			show_error_lastname = 0;		
  			
 		}
 		lastnameCheck = true;
 	}
 	////////////////////////////////////////////////////////////////////////
 	if(!EMailcheck(email))
 	{
 		if (show_error_email == 0)
 		{
 			$(".ui-dialog-content").height(dialogHeight+30);
 			dialogHeight = dialogHeight+30;
 			$("input[name='email_address_create_account']").css({background: "#ffabab"});
 			$("#emailError").slideDown("slow");	
 			show_error_email = 1;
  			
 		}
 		emailCheck = false;
 	}
 	else
 	{
 		if (show_error_email == 1)
 		{
 			$(".ui-dialog-content").height(dialogHeight-30);
 			dialogHeight = dialogHeight-30;
 			$("#emailError").slideUp("slow");
 			$("input[name='email_address_create_account']").css({background: "#fff"});
 			show_error_email = 0;
  			
 		}
 		emailCheck = true;
 	}
 	////////////////////////////////////////////////////////////////////////
 	if(street.length < 5)
 	{
 		if (show_error_street == 0)
 		{
 			$(".ui-dialog-content").height(dialogHeight+20);
 			dialogHeight = dialogHeight+20;
 			$("input[name='street_address']").css({background: "#ffabab"});
 			$("#streetError").slideDown("slow");
 			show_error_street = 1;
  			
 			
 		}
 		streetCheck = false;
 	}
 	else
 	{  
 		 if (show_error_street == 1)
 		{
 			$(".ui-dialog-content").height(dialogHeight-20);
 			dialogHeight = dialogHeight-20;
 			$("#streetError").slideUp("slow");
 			$("input[name='street_address']").css({background: "#fff"});
 			show_error_street = 0;	
  			
 		}
 		streetCheck = true;
 	}
 	////////////////////////////////////////////////////////////////////////
 	if(postcode.length < 4)
 	{
 		if (show_error_postcode == 0)
 		{
 			$(".ui-dialog-content").height(dialogHeight+20);
 			dialogHeight = dialogHeight+20;
 			$("#postcodeError").slideDown("slow");
 			$("input[name='postcode']").css({background: "#ffabab"});	
 			show_error_postcode = 1;
 		}
  		postcodeCheck = false;
 	}
 	else
 	{
 		 if (show_error_postcode == 1)
 		{
 			$(".ui-dialog-content").height(dialogHeight-20);
 			dialogHeight = dialogHeight-20;
 			$("#postcodeError").slideUp("slow");
 			$("input[name='postcode']").css({background: "#fff"});
 			show_error_postcode = 0;
 		}
  		postcodeCheck = true;
 	}
 	////////////////////////////////////////////////////////////////////////
 	if(city.length < 3)
 	{
 		if (show_error_city == 0)
 		{
 			$(".ui-dialog-content").height(dialogHeight+20);
 			dialogHeight = dialogHeight+20;
 			$("input[name='city']").css({background: "#ffabab"});
 			$("#cityError").slideDown("slow");	
 			show_error_city = 1;
 		}
  		cityCheck = false;
 	}
 	else
 	{
 		 if (show_error_city == 1)
 		{
 			$(".ui-dialog-content").height(dialogHeight-20);
 			dialogHeight = dialogHeight-20;
 			$("#cityError").slideUp("slow");
 			$("input[name='city']").css({background: "#fff"});	
 			show_error_city = 0;
 		}
  		cityCheck = true;
 	}
 	////////////////////////////////////////////////////////////////////////////
 	if(gender != "m" && gender !="f")
 	{
 		if (show_error_gender == 0)
 		{
 			$(".ui-dialog-content").height(dialogHeight+20);
 			dialogHeight = dialogHeight+20;
 			$("#genderError").slideDown("slow");	
 			show_error_gender = 1;
 		}
  		genderCheck = false;
 	}
 	else
 	{
 		 if (show_error_gender == 1)
 		{
 			$(".ui-dialog-content").height(dialogHeight-20);
 			dialogHeight = dialogHeight-20;
 			$("#genderError").slideUp("slow");
 			show_error_gender = 0;
 		}
  		genderCheck = true;
 	}
 	//////////////////////////////////////////////////////////////////////////////
 	if( identifyer == "createKunde")
 	{
 	if(password != confirmation || password < 4 || confirmation < 4 )
 	{
 		if (show_error_pass == 0)
 		{
 			$(".ui-dialog-content").height(dialogHeight+20);
 			dialogHeight = dialogHeight+20;
 			$("#passError").slideDown("slow");	
 			show_error_pass = 1;
 		}
  		passCheck = false;
 	}
 	else
 	{
 		 if (show_error_pass == 1)
 		{
 			$(".ui-dialog-content").height(dialogHeight-20);
 			dialogHeight = dialogHeight-20;
 			$("#passError").slideUp("slow");
 			show_error_pass = 0;
 		}
  		passCheck = true;
 	}
 	}
 	//////////////////////////////////////////////////////////////////////////////
 	if(datensg != "datensg")
 	{
 		if (show_error_datensg == 0)
 		{
 			$(".ui-dialog-content").height(dialogHeight+20);
 			dialogHeight = dialogHeight+20;
 			$("#datensgError").slideDown("slow");	
 			show_error_datensg = 1;
			
 			datensgvalue = false;
 		}
  		datensgCheck = false;
 	}
 	else
 	{
 		 if (show_error_datensg == 1)
 		{
 			$(".ui-dialog-content").height(dialogHeight-20);
 			dialogHeight = dialogHeight-20;
 			$("#datensgError").slideUp("slow");
 			show_error_datensg = 0;
 			datensgvalue = true;
 		}
  		datensgCheck = true;
 	}
 	

 	if(accountCheck &&genderCheck &&  datensgCheck && firstnameCheck && lastnameCheck && emailCheck && streetCheck && postcodeCheck && cityCheck && identifyer == "createGuest")
 	{
 	
 		$.ajax({
   		type: "POST",
   		url: "https://www.paulitoys.de/create_guest_account.php",
   		data: "action=process&newsletter="+newsletter+"&gender="+gender+"&firstname="+firstname+"&lastname="+lastname+"&email_address_create_account="+email+"&street_address="+street+"&postcode="+postcode+"&city="+city+"&country="+country+"&telephone="+telephone+"",
   		success: function(msg){
     	if(msg == "true")
     	{
		window.location.replace('https://www.paulitoys.de/checkout_shipping.php'); 
     	/*$("#createAccountUserForm").fadeTo("slow", 0.0, function()
     	{
     	$("#createAccountUserForm").empty();
     	$("#createAccountUserForm").append("<div style='margin-left:150px;'><div style='background: url(templates/WebStore/img/ok.png) no-repeat top left; padding:16px 0 0 60px; height:38px; font-weight:bold; color:#79ce19'>Sie haben sich erfolgreich als Gast angemeldet!<br /><br /><br /></div><div style='margin-left:75px; padding-right:10px; float:left;'><img src='templates/WebStore/img/kasse.jpg' /></div><br /><br /><br /><a href='https://www.paulitoys.de/checkout_shipping.php'>Weiter zur Kasse</a></div>");
     	$(".ui-dialog-content").height(180);
     	$("#createAccountUserForm").fadeTo("slow", 1.0);
     	$("#createNewGuest").removeAttr("href");
     	$("#createNewGuest").removeAttr("onclick");
     	$("#createNewGuest").append("Sie haben sich bereits als Gast angemeldet!");

     	});*/

     	}
     	else
     	{
   
     	$("#createAccountUserForm").fadeTo("slow", 0.0, function()
     	{
     	test = msg.indexOf("<td class=\"messageStackError\">")
     	test2 = msg.indexOf("</td>", test);
     	test3 = msg.slice(test, test2+5);
		$(".ui-dialog-content").height(100);
		$("#gott").empty();
     	$("#gott").append(test3+"<br /><a href=\"#\" onclick=\"zurueck(); return false\">zurueck</a>");
     	$("#gott").show();
     	$("#gott").fadeTo("slow",1.0);
     	  	});
     	}
   }
 });
 	}
 	 	if(accountCheck && passCheck && datensgCheck && genderCheck && firstnameCheck && lastnameCheck && emailCheck && streetCheck && postcodeCheck && cityCheck && identifyer == "createKunde")
 	{
 		$.ajax({
   		type: "POST",
   		url: "https://www.paulitoys.de/create_account.php",
   		data: "action=process&newsletter="+newsletter+"&datensg="+datensgvalue+"&password="+password+"&confirmation="+confirmation+"&gender="+gender+"&firstname="+firstname+"&lastname="+lastname+"&email_address_create_account="+email+"&street_address="+street+"&postcode="+postcode+"&city="+city+"&country="+country+"&telephone="+telephone+"",
   		success: function(msg){
     	if(msg == "true")
     	{
		window.location.replace('https://www.paulitoys.de/checkout_shipping.php'); 
     	/*$("#createAccountUserForm").fadeTo("slow", 0.0, function()
     	{
     	$("#createAccountUserForm").empty();
     	$("#createAccountUserForm").append("<div style='margin-left:150px'><div style=\" background: url(templates/WebStore/img/ok.png) no-repeat top left; padding:16px 0 0 60px; height:38px; font-weight:bold; color:#79ce19\">Sie haben sich erfolgreich als Neukunde angemeldet!<br /><br /><br /></div>" +
     	"<div style='margin-left:75px; padding-right:10px; float:left;'><img src='templates/WebStore/img/kasse.jpg' /></div><br /><br /><br /><a href='https://www.paulitoys.de/checkout_shipping.php'>Weiter zur Kasse</a></div>");
     	$(".ui-dialog-content").height(180);
     	$("#createAccountUserForm").fadeTo("slow", 1.0);
     	$("#createNewGuest").removeAttr("href");
     	$("#createNewGuest").removeAttr("onclick");
     	$("#createNewGuest").append("Sie haben sich bereits als Kunde angemeldet!");
     	});*/
     	
     	}
     	else
     	{
     	$("#createAccountUserForm").fadeTo("slow", 0.0, function()
     	{
     	test = msg.indexOf("<td class=\"messageStackError\">")
     	test2 = msg.indexOf("</td>", test);
     	test3 = msg.slice(test, test2+5);
		$(".ui-dialog-content").height(100);
		$("#gott").empty();
     	$("#gott").append(test3+"<br /><a href=\"#\" onclick=\"zurueck(); return false\">zurueck</a>");
     	$("#gott").show();
     	$("#gott").fadeTo("slow",1.0);	
     	});	
     	}
   }
 });
 	}
 }

 function checkAccountCreateStatus(value)
 {
 	if (value == "createGuest")
 	{   
 		 if ((dialogHeight -100) >= 650)
 		{
 			$(".ui-dialog-content").height(dialogHeight - 100);
 			dialogHeight = dialogHeight - 100;
 			showpw = 0;
 		}
 		$("#inputPassword").slideUp("slow");
 	}
 	if (value == "createKunde")
 	{
 		if (showpw == 0)
 		{
 			$(".ui-dialog-content").height(dialogHeight + 100);
 			dialogHeight = dialogHeight + 100;
 		}
 	   $("#inputPassword").slideDown("slow");
 	   showpw = 1;
 	}
 }
 function createNewAccount()
 {
 	if(offen == 0)
 	{
 	$(".ui-dialog-content").height(650);	
 	offen=1;
	$("#createNewGuest").append("<img id=\"ajaxloader\" src=\"templates/WebStore/img/ajax-loader.gif\" alt=\"loading...\" />");
		$.ajax({
  		type: "GET",
 		url: "https://www.paulitoys.de/create_guest_account.php",
  		dataType: "html",
  		success: function(msg)
  			{
  				
		  		show_error_firstname 	= 0;
		  		show_error_lastname 	= 0;
		  		show_error_email 		= 0;
		  		show_error_street 		= 0;
		  		show_error_postcode 	= 0;
		  		show_error_city 		= 0;
		  		show_error_gender 		= 0;
		  		show_error_pass 		= 0;
		  		show_error_datensg 		= 0;
				show_error_accountstatus= 0;
		  		
		  		firstnameCheck 			= false;
		  		lastnameCheck 			= false;
		  		emailCheck 				= false;
		  		streetCheck 			= false;
		  		postcodeCheck 			= false;
		  		cityCheck 				= false;
		  		genderCheck 			= false;
		  		passCheck 				= false;
		  		datensgCheck 			= false;
		  		datensgvalue 			= false;
				accountCheck			= false;
		  		
		  		showpw 					= 0;
		  		
		  		$("#newuserform").empty();
		  		$("#newuserform").append(msg);
		  		$("#firstNameError").hide();
		  		$("#inputPassword").hide();
		  		$("#lastNameError").hide();
		  		$("#lastNameError").hide();
		  		$("#emailError").hide();
		  		$("#postcodeError").hide();
		  		$("#streetError").hide();
		  		$("#cityError").hide();
		  		$("#passError").hide();
		  		$("#genderError").hide();
		  		$("#datensgError").hide();
				$("#accountError").hide();
				
		  		$("#gott").hide();
		  		$("#ajaxloader").remove();
				$("#RegisterNew").fadeOut("slow",function()
				{
				$("#newuserform").slideDown("slow");
				}
				);
		  		
				$(".ui-dialog-content").height(650);
  		}
		});	
 	}
 }

