/***************************************/
/* Launch/Common Product
/***************************************/
$(document).ready(function(){

	//Cache les boites de dialogue
	setTimeout("$('div#response_message_pop').hide();", 0);
	setTimeout("$('div#response_ok_pop').hide();", 0);
	setTimeout("$('div#response_ko_pop').hide();", 0);
	
	//Lance la boite de dialogue
	$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			width:600,
			modal: true,
			close: function() {
				$(this).html('');
			}
	});	
	
	//Lance la lytebox
	$('#gallery1 a').lightBox();	
});

/***************************************/
/* Lance la boite de dialogue
/***************************************/
function dialog_laodForm_sendFriend(typeProduct,url,lang)
{
	
	$.ajax({
		type: "POST",
		url: url+"/inc/product/sendFriend.inc.php",
		data: "show=1&action=showFormPopIn&typeProduct="+typeProduct+"&url="+url,
		dataType:"text",
		success: function(message){
			if(message!=0)
			{
				if(lang=='fr')
				{
					$('div#dialog').dialog('option', 'buttons', { 'Envoyer': function() { validateFormSendFriend('form_sendFriend_to');}, 'Fermer': function() { $(this).dialog('close');} });
				}
				else if(lang=='en')
				{
					$('div#dialog').dialog('option', 'buttons', { 'Send': function() { validateFormSendFriend('form_sendFriend_to');}, 'Close': function() { $(this).dialog('close');} });
				}
				else if(lang=='es')
				{
					$('div#dialog').dialog('option', 'buttons', { 'Enviar': function() { validateFormSendFriend('form_sendFriend_to');}, 'Cerrar': function() { $(this).dialog('close');} });
				}
				else
				{
					$('div#dialog').dialog('option', 'buttons', { 'Send': function() { validateFormSendFriend('form_sendFriend_to');}, 'Close': function() { $(this).dialog('close');} });
				}				

				$("div#form_sendFriend_to").html(message);			
				
				$('#dialog').dialog('open');			
			}
			else
			{
			}		
		}
	});	
}


/***************************************/
/* Vérification du formulaire d'envoi
/***************************************/
function validateFormSendFriend(idform,url)
{
	//Variables
	var name = $("#form_send_name"),
		email = $("#form_send_email"),
		name_rec = $("#form_send_name_rec"),
		email_rec = $("#form_send_email_rec"),
		message = $("#form_send_mess"),
			
		allFields = $([]).add(name).add(email).add(name_rec).add(email_rec).add(message),
		
		tips = $("#validateTips");
		
		var bValid = true;
		allFields.removeClass('ui-state-error');

		//Name			
		bValid = bValid && checkLength(name,"Prénom et nom",2,40);
		bValid = bValid && checkRegexp(name,/^[a-zA-Zçéèëêàäâôöùûüïî\'\-\ ]+$/,"Prénom et nom ne comporte que des lettres avec la possibilité de mettre des tirets ou des espaces");

		//Email
		bValid = bValid && checkLength(email,"E-mail",6,80);
		bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"L'Email doit respecter le format suivant : xxxx@xxx.xx");

		//Name			
		bValid = bValid && checkLength(name_rec,"Prénom et nom",2,40);
		bValid = bValid && checkRegexp(name_rec,/^[a-zA-Zçéèëêàäâôöùûüïî\'\-\ ]+$/,"Prénom et nom ne comporte que des lettres avec la possibilité de mettre des tirets ou des espaces");

		//Email
		bValid = bValid && checkLength(email_rec,"E-mail",6,80);
		bValid = bValid && checkRegexp(email_rec,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"L'Email doit respecter le format suivant : xxxx@xxx.xx");
		
		//Message
		bValid = bValid && checkEmpty(message,"Message");
		
		
		//si tout est ok alors on peut lancer le script
		if (bValid) 
		{
			//alert("ok");
			ajax_enr_sendFriend(idform);
		}
	
	/**************************/
	//Fonctions de vérification
	/**************************/
	
	function updateTips(t) 
	{
		tips.text(t).effect("highlight",{},3000);
	}
	
	function checkLength(o,n,min,max) 
	{
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			o.focus();
			updateTips("Le champs ne doit être vide et/ou la taille de votre " + n + " doit être entre "+min+" et "+max+" caractères.");
			return false;
		} else {
			return true;
		}
	}
	
	function checkRegexp(o,regexp,n) 
	{
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			o.focus();
			updateTips(n);
			return false;
		} else {
			return true;
		}
	}
	
	function checkSame(o,p,n) 
	{
		if ( o.val() != p.val() ) {
			p.addClass('ui-state-error');
			p.focus();
			updateTips("Les champs "+ n +" ne sont pas identiques");
			return false;
		} else {
			return true;
		}
	}

	function checkEmpty(o,n) 
	{
		if ( o.val() == "" ) {
			o.addClass('ui-state-error');
			o.focus();
			updateTips("Le champ "+ n +" ne doit pas être vide");
			return false;
		} else {
			return true;
		}
	}
	function ckeckCheck(o)
	{
		if (o.attr('checked')) 
		{
     		return true;
		}
		else
		{
			o.addClass('ui-state-error');
			o.focus();
			updateTips("Vous devez accepter les conditions générales de location");
			return false;		
		}	
	}							
}

/***************************************/
/* Enregistrement du message
/***************************************/
function ajax_enr_sendFriend(idform){

	var dataString="";
	
	$("p#validateTips").html("");

	$("form#"+idform).find("input,select,textarea").each(function(){
		dataString+="&"+$(this).attr("name")+"="+$(this).val();
		
	});
	
	$.ajax({
		type: "POST",
		url: $("input#form_send_url").val()+"/inc/product/sendFriend.inc.php",
		data: "show=1&action=add"+dataString,
		dataType:"xml",
		success: function(xml){

			$(xml).find('informations').each(function(){
	
				var status = $(this).attr('status');
				var message = $(this).attr('message');
				var messageerror = $(this).attr('messageerror');
			
				if(status==1)
				{
					setTimeout("$('#response_ok_pop').show('fast');", 0);
					setTimeout("$('#response_ko_pop').hide('fast');", 0);
					//$("#zoneGlobalMessageUnder_1_pop").html("Enregistrement : OK");
				}
				else
				{
					setTimeout("$('#response_ok_pop').hide('fast');", 0);
					setTimeout("$('#response_ko_pop').show('fast');", 0);
					//$("#zoneGlobalMessageUnder_1_pop").html("Problème lors de l'enregistrement");
				}
				setTimeout("$('#response_message_pop').show('fast');", 0);
				$("#zoneGlobalMessage_1_pop").html(message);
				//$("#zoneGlobalMessageUnder_1_pop").html(messageerror);
				
				setTimeout("$('#response_message_pop').hide('fast');", 5000);
				setTimeout("$('#response_ok_pop').hide('fast');", 5000);
				setTimeout("$('#response_ko_pop').hide('fast');", 5000);
				setTimeout("$('#dialog').dialog('close');", 6000);		

			});	
		}
	});
	//return false;	
}

/***************************************/
/* Module de correspondance des tailles pour la sélection sur la fiche produit
/***************************************/
function changeSelectSize(tabSize,typeSexe,marque,lang,value)
{

	//Tableau de corrspondances des tailles 
	var tabUS = new Array('12c',1,2,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10,10.5,11,11.5,12,13,14,15);
	var tabEUR = new Array('30',32.5,33.5,35,35.5,36,36.5,37,37.5,38,38.5,39,40,41,41.5,42,42.5,43,44,45,45.5,46,47,48,49);
	var tabUK = new Array(0,13,1,2,2.5,3,3.5,4,4.5,5,5.5,6,6.5,7,7.5,8,8.5,9,9.5,10,10.5,11,12,13,14);
	
	var tabUSGirl = new Array(5,6,6.5,7,7.5,8,8.5,9,9.5,10);
	var tabEURGirl = new Array(35,36,37,37.5,38,38.5,39,40,41,42);
	var tabUKGirl = new Array(2.5,3.5,4,4.5,5,5.5,6,6.5,7,7.5);
	
	var tabLakai = new Array(0,0,0,0,0,0,0,37,38,38.5,39,40,40.5,41,42,42.5,43,44,44.5,45,46,47,48.5,49.5,0);
	

	//En fonction des type et des marques, on choisi les tableaux de correspondances
	if((marque=="lakai" || marque=="adio") && lang=="EUR")
	{
		var tab = tabLakai;
	}
	else
	{
		if(typeSexe=="F")
		{
			if(lang=="US")
			{
				var tab = tabUSGirl;
			}
			else if(lang=="EUR")
			{
				var tab = tabEURGirl;
			}
			else if (lang=="UK")
			{
				var tab = tabUKGirl;
			}		
		}
		else
		{
			if(lang=="US")
			{
				var tab = tabUS;
			}
			else if(lang=="EUR")
			{
				var tab = tabEUR;
			}
			else if (lang=="UK")
			{
				var tab = tabUK;
			}			
		}
	}

	//Je recherchage les options du select du choix des tailles
	var newTabOfSize = tabSize.split(",");
	for (var i=0; i<newTabOfSize.length; i++)
	{
		if(typeSexe=="F")
		{
			for (var j=0; j<tabUSGirl.length; j++)
			{
				if(newTabOfSize[i]==tabUSGirl[j])
				{
					if(newTabOfSize[i]==value)
					{
						document.getElementById("sizeProd").options[i] = new Option(tab[j],newTabOfSize[i],false,true); 
					}
					else
					{
						document.getElementById("sizeProd").options[i] = new Option(tab[j],newTabOfSize[i],false,false); 
					} 
				}
			}		
		}
		else
		{

			for (var j=0; j<tabUS.length; j++)
			{
				if(newTabOfSize[i]==tabUS[j])
				{
					if(newTabOfSize[i]==value)
					{
						document.getElementById("sizeProd").options[i] = new Option(tab[j],newTabOfSize[i],false,true); 
					}
					else
					{
						document.getElementById("sizeProd").options[i] = new Option(tab[j],newTabOfSize[i],false,false); 
					}
					//alert(tab[j]);   
				}
			}	
		}	
	}
}

/***************************************/
/* Mise à jour du prix du caddy
/***************************************/
function MAJSmallcart(url)
{
	//form=document.getElementById(idform);

	var xhr_object = null;
	if(window.XMLHttpRequest) // Firefox
	{
		xhr_object = new XMLHttpRequest();
	}
	else if(window.ActiveXObject) // Internet Explorer
	{
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else // XMLHttpRequest non supporté par le navigateur
	{
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		return;
	}
	xhr_object.open("POST", url+"/inc/gestionCaddy.inc.php", true);
	xhr_object.onreadystatechange = function thefunction() 
	{
		if(xhr_object.readyState == 4)
		{
			var flux='';
			var docXML= xhr_object.responseXML;
			//var docTXT = xhr_object.responseText;
			
			var dataVars = docXML.getElementsByTagName("vars")[0].attributes;
			document.getElementById("at8_front_caddy_items").innerHTML=dataVars[1].nodeValue;
			document.getElementById("at8_front_caddy_price").innerHTML=dataVars[2].nodeValue;
		}
	}
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "show=1&relaodSmallCaddy=1"+"&url="+url;	
	//var data = "";
	xhr_object.send(data);
	
}

/***************************************/
/* L'ajout au caddy
/***************************************/
function addcart(idform,lang,url,template,label,pdt_reference,pdt_id,pdt_price)
{
	//form=document.getElementById(idform);

	var xhr_object = null;
	if(window.XMLHttpRequest) // Firefox
	{
		xhr_object = new XMLHttpRequest();
	}
	else if(window.ActiveXObject) // Internet Explorer
	{
		xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else // XMLHttpRequest non supporté par le navigateur
	{
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest...");
		return;
	}
	xhr_object.open("POST", url+"/inc/gestionCaddy.inc.php", true);
	xhr_object.onreadystatechange = function thefunction() 
	{
		if(xhr_object.readyState == 4)
		{
			
			var flux='';
			var docXML= xhr_object.responseXML;
			//var docTXT = xhr_object.responseText;
			var dataVars = docXML.getElementsByTagName("vars")[0].attributes;
			MAJSmallcart(dataVars[0].nodeValue);
			//document.getElementById("at8_change_btn_addcart").innerHTML="<a href=\""+dataVars[0].nodeValue+"/"+lang+"/caddy\" title=\""+label+"\"><img src=\""+dataVars[0].nodeValue+"/smarty/templates/"+template+"/images/"+lang+"/seecaddy.gif\" width=\"\" height=\"\" alt=\""+label+"\" title=\""+label+"\"/></a>";
			
			$('div#at8_header_logo_caddy').effect("pulsate",{},200,function(){window.location.href = dataVars[0].nodeValue+"/"+dataVars[1].nodeValue+"/caddy";});
			
			window.location.href=dataVars[0].nodeValue+"/"+dataVars[1].nodeValue+"/caddy";
			
			//setTimeout(window.location.href=dataVars[0].nodeValue+"/"+dataVars[1].nodeValue+"/caddy", 5000);
			//setTimeout("window.location.href = '"+dataVars[0].nodeValue+"/"+dataVars[1].nodeValue+"/caddy';", 5500);
		}
	}
	
	//document.getElementById("zoneGlobalLoadCat").innerHTML="<img src=\"../smarty/templates/admin/images/ajaxloader.gif\" alt=\"Loading\" title=\"Loading\"/>";
	
	xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var data = "show=1"+"&actionCaddy=add"+"&language="+lang+"&pdt_reference="+pdt_reference+"&pdt_id="+pdt_id+"&pdt_size="+document.getElementById("sizeProd").value+"&pdt_quantity="+document.getElementById("quantProd").value+"&pdt_price="+pdt_price+"&url="+url;	
	//var data = "";
	xhr_object.send(data);
	
}