var testpopin=false;

function reglement() {
	var url='SIMPLY_MARKET_REGLEMENT_JEU_AOUT_09.pdf';
 	window.open(url,'download');  
}

function inscription() {
	window.location.href = "jeu.html";  
}

function _(o) {
	if (!o) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById(o)) return false;
	return document.getElementById(o);
}

function xhrRequest(type) {
	var xhrSend;
	if (!type) {
		type = 'text';
	}
	if (window.ActiveXObject) {
		try {
			xhrSend = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xhrSend = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	} else if (window.XMLHttpRequest) {
		xhrSend = new XMLHttpRequest();
		if (xhrSend.overrideMimeType) {
			xhrSend.overrideMimeType('text/' + type);
		}
	}
	return (xhrSend);
}

function getWidth() {
  myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
	myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	myWidth = document.body.clientWidth;
  }
  return myWidth;
}	

function setLeft() {
	myWidth=getWidth();
	myWidth=(myWidth-903)/2;
	if (myWidth<0) myWidth=0;
	myWidth=myWidth+"px";
	_('content').style.left=myWidth;
}

function getHeight() {
  myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
	myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function showPopin() {
	myDiv=_('myPopin');
	lenom=_('id_nom_athos').value;
	leno=_('id_no_athos').value;
	if (lenom=="") {
		alert('Veuillez renseigner votre nom.');
		return false;
	} else if (leno=="") {
		alert('Veuillez renseigner votre numéro de carte de fidélité.');
		return false;	
	} else {
		if (myDiv && !testpopin) {
			get_data_from_athos(lenom,leno);
		}
	}
}

function get_data_from_athos(nom, no) {
	
	myDiv=_('myPopin');
	var xhrRec = xhrRequest('html');
	xhrRec.onreadystatechange = function() {
		if(xhrRec.readyState == 4 && xhrRec.status == 200) {
			if (xhrRec.responseText=='err') {
				alert('Le nom ou le numéro de carte n\'est pas valide ');
//				alert(xhrRec.responseText);
				return false;
			} else {			

				_('retour').innerHTML = xhrRec.responseText;
			
				testpopin=true;
				myWidth=getWidth(); 
				_('wrapper').style.width=myWidth+'px';
				myWidth=(myWidth-345)/2; if (myWidth<0) myWidth=0; myWidth=myWidth+'px';
				myHeight=getHeight();
				_('wrapper').style.height=myHeight+'px';
				myHeight=(myHeight-419)/2; if (document.all) myHeight=(myHeight-10); if (myHeight<0) myHeight=0; myHeight=myHeight+'px';
				_('wrapper').style.opacity='0.5';
				_('wrapper').style.display='block';
				myDiv.style.top=myHeight;
				myDiv.style.left=myWidth;
				myDiv.style.display='block';
				
				_('popincodCarte').value=_('popincodCarteret').value;
				_('popinnom').value=_('popinnomret').value;
				_('popinprenom').value=_('popinprenomret').value;
				_('popinadresse1').value=_('popinadresse1ret').value;
				_('popinadresse2').value=_('popinadresse2ret').value;
				_('popinadresse3').value=_('popinadresse3ret').value;
				_('popinadresse4').value=_('popinadresse4ret').value;
				_('popincp').value=_('popincpret').value;
				_('popinville').value=_('popinvilleret').value;
				_('popinpays').value=_('popinpaysret').value;
				_('popinemail').value=_('popinemailret').value;
				
				if ( _('popinoptinret').value=="true") {
					_('popinoptin0').checked = false;
					_('popinoptin1').checked = true;
				} else {
					_('popinoptin0').checked = true;
					_('popinoptin1').checked = false;
				}			
			}
		}
	}

	xhrRec.open("POST",'ajax_return.php',true);
	xhrRec.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrRec.send("MODE=4&nom="+nom+"&no="+no);	


//	xhrRec.open("POST",'ajax_return.php',true);
//	xhrRec.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
//	xhrRec.send("MODE=2&nom="+nom+"&no="+no);			


}



function get_verif_from_athos(no) {
	myDiv=_('myPopin');
	var xhrRec = xhrRequest('html');
	xhrRec.onreadystatechange = function() {
		if(xhrRec.readyState == 4 && xhrRec.status == 200) {
			if (xhrRec.responseText=='err') {
				alert('Le numéro de carte saisi n\'est pas valide !');
				//alert(xhrRec.responseText);
			} else {
				_('retour').innerHTML = xhrRec.responseText;
				testpopin=true;
				myWidth=getWidth(); 
				_('wrapper').style.width=myWidth+'px';
				myWidth=(myWidth-345)/2; if (myWidth<0) myWidth=0; myWidth=myWidth+'px';
				myHeight=getHeight();
				_('wrapper').style.height=myHeight+'px';
				myHeight=(myHeight-419)/2; if (document.all) myHeight=(myHeight-10); if (myHeight<0) myHeight=0; myHeight=myHeight+'px';
				_('wrapper').style.opacity='0.5';
				_('wrapper').style.display='block';
				myDiv.style.top=myHeight;
				myDiv.style.left=myWidth;
				myDiv.style.display='block';
				
				_('popincodCarte').value=_('popincodCarteret').value;
				_('popinnom').value=_('popinnomret').value;
				_('popinprenom').value=_('popinprenomret').value;
				_('popinadresse1').value=_('popinadresse1ret').value;
				_('popinadresse2').value=_('popinadresse2ret').value;
				_('popinadresse3').value=_('popinadresse3ret').value;
				_('popinadresse4').value=_('popinadresse4ret').value;
				_('popincp').value=_('popincpret').value;
				_('popinville').value=_('popinvilleret').value;
				_('popinpays').value=_('popinpaysret').value;
				_('popinemail').value=_('popinemailret').value;
				
				if ( _('popinoptinret').value=="true") {
					_('popinoptin0').checked = false;
					_('popinoptin1').checked = true;
				} else {
					_('popinoptin0').checked = true;
					_('popinoptin1').checked = false;
				}			
			}
		}
	}

	xhrRec.open("POST",'ajax_return.php',false);
	xhrRec.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhrRec.send("MODE=2&nom="+nom+"&no="+no);	
	
	myretour = xhrRec.responseText;
	if (myretour=='err') {
		alert('Le numéro de carte saisi n\'est pas valide !');
	}
	return myretour;
//	xhrRec.open("POST",'ajax_return.php',true);
//	xhrRec.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
//	xhrRec.send("MODE=2&nom="+nom+"&no="+no);			


}



function closePopin() {
	myDiv=_('myPopin');
	if (myDiv && testpopin) {
		testpopin=false;
		myDiv.style.display='none';
		_('wrapper').style.display='none';				
	}
}

function submit_form() {
	lenom = _('nom').value;
	leprenom = _('prenom').value;
	lemail = _('email').value;
	lesimplymarket = _('simplymarket').value;
	op1 = eval(_('optin1').checked);
	op2 = eval(_('optin0').checked);
	lepas_de_carte= _('pas_de_carte').checked;
	leid_no_athos = _('id_no_athos').value;
	
	if (lenom=="" || leprenom=="" || lemail=="" ||  lesimplymarket=="" ||  ( _('optin1').checked ==  _('optin0').checked)) {
		alert('Veuillez renseigner tous les champs obligatoires.');
		return false;
	} else if (isEmail(lemail)==false) {
		alert('L\'Email n\'est pas valide.');
		return false;
	} else if (lepas_de_carte==false && leid_no_athos.trim() == '') {
		alert('Merci de cocher la case \'Je n’ai pas de carte de fidélité mais je souhaite participer au jeu\' si vous n\'avez pas de numéro de carte.');
		return false;
	} else {
		if (leid_no_athos.trim() != '' && lepas_de_carte==false) {
			myretour = get_verif_from_athos(leid_no_athos);
			if (myretour=='err') {
				return;
			} else {
				get_data_from_athos(myretour, leid_no_athos);
			}
		}
		var optin = 0;
		if (op1) {optin=1;}
		if (op2) {optin=0;}
		var xhrRec = xhrRequest('html');
		xhrRec.onreadystatechange = function() {
			if(xhrRec.readyState == 4 && xhrRec.status == 200) {
				if (xhrRec.responseText=='ok') {
/*					_('formulaire').innerHTML = '<img src="images/bg_retour_ok.gif" alt="" title="" /><br />';*/
					_('page1').style.display='none';
					_('page2').style.display='block';
					
				} else {
					_('erreur').style.display = 'block';
					_('erreur').innerHTML = xhrRec.responseText;
				}
			}
		}
		xhrRec.open("POST",'ajax_return.php',true);
		xhrRec.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhrRec.send("MODE=1&email="+lemail+"&nom="+lenom+"&prenom="+leprenom+"&optin="+optin+"&simplymarket="+lesimplymarket+"&carte="+leid_no_athos);		
	}
}

function validPopin() {

	lecode = _('popincodCarte').value;
	lenom = _('popinnom').value;
	leprenom = _('popinprenom').value;
	ladresse1 = _('popinadresse1').value;
	ladresse2 = _('popinadresse2').value;
	ladresse3 = _('popinadresse3').value;
	ladresse4 = _('popinadresse4').value;
	lecp = _('popincp').value;
	laville = _('popinville').value;
	lepays = _('popinpays').value;
	lemail = _('popinemail').value;
	
	op1 = eval(_('popinoptin1').checked);
	op2 = eval(_('popinoptin0').checked);
	
	if (lenom=="" || laville=="" || lemail=="" ||  ( _('popinoptin1').checked ==  _('popinoptin0').checked)) {
		alert('Veuillez renseigner tous les champs obligatoires.');
		return false;
	} else if (isEmail(lemail)==false) {
		alert('L\'Email n\'est pas valide.');
		return false;
	} else {
		var optin = 0;
		if (op1) optin=1;
		if (op2) optin=0;
		
		var xhrRec = xhrRequest('html');
		xhrRec.onreadystatechange = function() {
			if(xhrRec.readyState == 4 && xhrRec.status == 200) {
				if (xhrRec.responseText=='ok') {
					_('formulaire').innerHTML = '<img src="images/bg_retour_ok.gif" alt="" title="" /><br />';
				} else {
					_('erreur').style.display = 'block';
					_('erreur').innerHTML = xhrRec.responseText;
				}
			}
		}
		xhrRec.open("POST",'ajax_return.php',true);
		xhrRec.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		xhrRec.send("MODE=3&email="+lemail+"&nom="+lenom+"&prenom="+leprenom+"&optin="+optin+"&ladresse1="+ladresse1+"&ladresse2="+ladresse2+"&ladresse3="+ladresse3+"&ladresse4="+ladresse4+"&lecp="+lecp+"&laville="+laville+"&lepays="+lepays+"&lecode="+lecode);		
	}




	/* ENVOYER LE FORMULAIRE + TRAVAIL SUR LES CHAMPS */
	closePopin();
	//_('formulaire').innerHTML = '<img src="images/bg_retour_ok.gif" alt="" title="" /><br />';
}



isEmail = function(emailStr) {
	var checkTLD = 1;
	var knownDomsPat = /^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|cat|jobs|mobi|travel|info|pro|museum|fr)$/;
	var emailPat = /^(.+)@(.+)$/;
	var specialChars = "\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars = "\[^\\s" + specialChars + "\]";
	var quotedUser = "(\"[^\"]*\")";
	var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom = validChars + '+';
	var word = "(" + atom + "|" + quotedUser + ")";
	var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray = emailStr.match(emailPat);
	if (matchArray == null) { return false; }
	var user = matchArray[1];
	var domain = matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i) > 127) { return false; }
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i) > 127) { return false; }
	}
	if (user.match(userPat) == null) { return false; }
	var IPArray=domain.match(ipDomainPat);
	if (IPArray != null) {
		for (var i=1; i<=4; i++) {
			if (IPArray[i] > 255) { return false; }
		}
		return true;
	}
	var atomPat = new RegExp("^" + atom + "$");
	var domArr = domain.split(".");
	var len = domArr.length;
	for (i=0; i<len; i++) {
		if (domArr[i].search(atomPat) == -1) { return false; }
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) { return false; }
	if (len < 2) { return false; }
	return true;
}




var regExpBeginning = /^\s+/;
var regExpEnd = /\s+$/;  

// Supprime les espaces inutiles en début et fin de la chaîne passée en paramètre.
function trim(aString) {
    return aString.replace(regExpBeginning, "").replace(regExpEnd, "");
}
// Supprime les espaces inutiles en début de la chaîne passée en paramètre.
function ltrim(aString) {
    return aString.replace(regExpBeginning, "");
}
// Supprime les espaces inutiles en fin de la chaîne passée en paramètre.
function rtrim(aString) {
    return aString.replace(regExpEnd, "");
}

String.prototype.trim = function(){
	return this.replace(/^\s+/, "").replace(/\s+$/, "");
}
