var lang = Object();

lang.get = function(key){
	var reg=new RegExp("[\.]+", "g");
	var arKey = key.split(reg);
	var returnVal = '';
	var prekey = '';
	var finalKey = ''; 
	var textKey = ''; 
	for(var i =0;i<(arKey.length-2);i++){
		prekey = prekey+arKey[i];
                if(i<(arKey.length-3)){
                    prekey = prekey+'.'
                }
	}
		finalKey = arKey[(i)] ;
		textKey = arKey[(i+1)] ;
	try{
		dojo.requireLocalization(prekey, finalKey);
		var nlsLibFirst = dojo.i18n.getLocalization(prekey,finalKey) ;
		returnVal = (nlsLibFirst[textKey] || key);
	}
  catch(err){
		returnVal = key;
	}
	return returnVal ;
}

function preLoadPicture(url){
  var img = new Image();
  img.src=url;
  return img;
}

var img1 = preLoadPicture("./images/signup/signup_left.png");
var img2 = preLoadPicture("./images/signup/signup_left_over.png");
var img3 = preLoadPicture("./images/signup/signup_right.png");
var img4 = preLoadPicture("./images/signup/signup_right_over.png");

// detection du navigateur
var browser = detectBrowser();

//Supprimer les espaces début et finaux de châine de caractère.
function Trim(txtVar){
	return txtVar.replace(/(^\s*)|(\s*$)/g,"");
} 

function Init(){
	var tabParam = extractUrlParams();
	if (tabParam['confirm'])
    activateAccount(tabParam['confirm']);
}

function clearErrorLogin(){
  document.getElementById('msgErrorPwd').innerHTML = "";
}

function extractUrlParams(){
	var t = location.search.substring(1).split('&');
	var f = [];
	for (var i=0; i<t.length; i++){
		var x = t[ i ].split('=');
		f[x[0]]=x[1];
	}
	return f;
}

function centerpopup(page,largeur,hauteur){
	var top=(screen.height-hauteur)/2;
	var left=(screen.width-largeur)/2;
	window.open(page,"","menubar=no, scrollbars=yes, status=no, location=no, top="+top+",left="+left+",width="+largeur+",height="+hauteur);
} 

function detectBrowser(){
    var version;
    var strChUserAgent = navigator.userAgent;
    var intSplitStart = strChUserAgent.indexOf("(",0);
    var intSplitEnd = strChUserAgent.indexOf(")",0);
    var strChStart = strChUserAgent.substring(0,intSplitStart);
    var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
    var strChEnd = strChUserAgent.substring(strChEnd);
    
    if(strChMid.indexOf("MSIE 7") != -1)
      version = 'IE7';
    else if(strChMid.indexOf("MSIE 8") != -1)
      version = 'IE8';
    else if(strChEnd.indexOf("Firefox") != -1)
      version =  'FF'; 
    else if(strChEnd.indexOf("Netscape") != -1)
      version = 'NS';
    else if(strChStart.indexOf("Opera") != -1)
      version = 'O';
    else if(strChStart.indexOf("Safari") != -1)
      version = 'SAF';
    else if(strChStart.indexOf("Chrome") != -1)
      version = 'CHROME';
    else
      version = 'OTHER';
    return version;
}

function clearFormSignUp(){
  dojo.byId("textEmail").value = '';
  dojo.byId("pwdPasswordSignUp").value = '';
  dojo.byId("pwdPasswordConfirm").value = '';
  dojo.byId("cboBrand").value = '';
  dojo.byId("chkTermsOfAgreement").checked = false;
  dojo.byId("ptitcaptcha_entry").value = '';
  dojo.byId("divModelList").innerHTML = '<table><tr><td>&nbsp;</td></tr></table>';
  dojo.byId("signInError").innerHTML = '&nbsp;';
}

function openDialog(dialog){
  if (document.getElementById('txtLogin').value)
    document.getElementById('forgotTxtLogin').value = document.getElementById('txtLogin').value;
  document.getElementById('emailNotFound').innerHTML = '&nbsp;';  
  showDialog(dialog);
}

function showDialog(dlgVar) {   
	var dlg = dijit.byId(dlgVar);
	document.getElementById(dlgVar).style.display = 'block';
	dlg.duration = 1;
	dlg.show();
}
function hideDialog(dlgVar1,dlgVar2){
	var dlg = dijit.byId(dlgVar1);
	dlg.duration = 1;
	dlg.hide();
	if (dlgVar2) {
		showDialog(dlgVar2);
	}
}

function showModelList() {
	if (dojo.byId("cboBrand").value != 1) {
		dojo.rawXhrPost({
		
    // The following URL must match that used to test the server.
    url: "./ajaxrequest.php",
    handleAs: "text",
    postData: "search=modelList&brandId=" + dojo.byId("cboBrand").value,
    
    timeout: 60000, // Time in milliseconds
    // The LOAD function will be called on a successful response.
    load: function(response, ioArgs){      	
        dojo.byId("divModelList").innerHTML = "<table><tr><td width='320'>"+lang.get("padnls.home.interface.welcome.selectModel")+"</td><td>" + response + "</td></tr></table>";
        if (dojo.byId("customHeight")) {
      		dojo.byId("customHeight").value = "";
      		dojo.byId("customWidth").value = "";
      	}
    	return response;
    },
    
    // The ERROR function will be called in an error case.
    error: function(response, ioArgs){
    	console.error("HTTP status code: ", ioArgs.xhr.status);
    	return response;
    }
		});
	}
	else {
		dojo.byId("divModelList").innerHTML = "<table><tr><td>"+lang.get("padnls.home.interface.welcome.displayResoW")+" <input type='text' id='customWidth' class='FieldInput' name='customWidth' maxlength='4' value='800'> - "+lang.get("padnls.home.interface.welcome.displayResoH")+" <input type='text' id='customHeight' name='customHeight' class='FieldInput' maxlength='4' value='600'/> "+lang.get("padnls.home.interface.welcome.displayResoPix")+"</td></tr></table>";
	}
}

function showCustomDimension() {

	dojo.rawXhrPost( { 
		
		// The following URL must match that used to test the server.
		url: "./ajaxrequest.php", 
		handleAs: "text",
		postData: "search=modelDimension&modelId="+dojo.byId('cboModel').value,
		timeout: 60000, // Time in milliseconds
		
		// The LOAD function will be called on a successful response.
		load: function(response, ioArgs) {
		var arg1 = response;
    arg2=arg1.split('|');
    if(dojo.byId('cboModel').value == 1){
    	dojo.byId("divModelList").innerHTML = "<table><tr><td>"+lang.get("padnls.home.interface.welcome.displayResoW")+" <input type='text' class='FieldInput' name='customWidth' id='customWidth' maxlength='4'/> - "+lang.get("padnls.home.interface.welcome.displayResoH")+" <input type='text' class='FieldInput' name='customHeight' id='customHeight' maxlength='4'> "+lang.get("padnls.home.interface.welcome.displayResoPix")+"</td></tr></table>";
    }
    else {  
      var selectedOption =  dojo.byId("cboModel").selectedIndex;
      dojo.byId("divModelList").innerHTML = dojo.byId("divModelList").innerHTML+"<input type='hidden' name='customHeight' id='customHeight'><input type='hidden' name='customWidth' id='customWidth'/>";
      dojo.byId("cboModel").selectedIndex = selectedOption;
    }
    dojo.byId("customHeight").value = arg2[0];
    dojo.byId("customWidth").value = arg2[1];
    return response;
    },
		
		// The ERROR function will be called in an error case.
		error: function(response, ioArgs) {
    console.error("HTTP status code: ", ioArgs.xhr.status);
    return response;
		}
	});
}

function InsertUser() {
	var tabParam = paramInArray('frmFormSignIn');
	dojo.byId("signInError").innerHTML = "";
	//if (aryForm[1]['chkTermsOfAgreement'] == true) {
		dojo.rawXhrPost({
		
    // The following URL must match that used to test the server.
    url: "./ajaxrequest.php",
    handleAs: "text",
    postData: "search=SaveUser&"+tabParam[0],
    timeout: 60000, // Time in milliseconds
    // The LOAD function will be called on a successful response.
    load: function(response, ioArgs){
    	if (response == 1) {
    		hideDialog('formSignIn', 'confirmSendEmail');
    		clearFormSignUp();
    	}
    	else {
    		dojo.byId("signInError").innerHTML = response;
    	}
    	return response;
    },

    // The ERROR function will be called in an error case.
    error: function(response, ioArgs){
    	console.error("HTTP status code: ", ioArgs.xhr.status);
    	return response;
    }
		});
}
	
function activateAccount(md5Email) {
	if(md5Email){
		dojo.rawXhrPost( { 

    // The following URL must match that used to test the server.
    url: "./ajaxrequest.php", 
    handleAs: "text",
    postData: "search=Confirm&txtEmail="+md5Email,
    
    timeout: 60000, // Time in milliseconds
    
    // The LOAD function will be called on a successful response.
    load: function(response, ioArgs) {
    	if(response == "activationDone")
    		showDialog("confirmActive");	
    	else if (response == "activationFailed")
    	  showDialog("alreadyConfirm");
    },
      // The ERROR function will be called in an error case.
      error: function(response, ioArgs) {
    	console.error("HTTP status code: ", ioArgs.xhr.status);
    	return response;
    }
		});
	}
	return false;
}

function forgotPassword(frmForm){
  var tabFormParam = paramInArray(frmForm);
  if (tabFormParam[1]['forgotTxtLogin']){
      	dojo.rawXhrPost({
          // The following URL must match that used to test the server.
          url: "./ajaxrequest.php",
          handleAs: "text",
          postData: "search=ForgotPassword&email="+tabFormParam[1]['forgotTxtLogin'],
          timeout: 60000, // Time in milliseconds
          // The LOAD function will be called on a successful response.
          load: function(response, ioArgs){
          		if(response == 'newPwdSent'){
                hideDialog('formForgotPassword','confirmNewPwd');
                document.getElementById('forgotTxtLogin').value = '';
          		}
          		else if(response == 'accountNotExist'){
                document.getElementById('forgotTxtLogin').value = '';
    		        document.getElementById('emailNotFound').innerHTML = lang.get("padnls.home.interface.welcome.errorForgotPwd");
          		}
          },
          // The ERROR function will be called in an error case.
          error: function(response, ioArgs){
          	console.error("HTTP status code: ", ioArgs.xhr.status);
          	return response;
          }
    		});
  }
	return false;
}
	
function checkAccount(frmForm, fct) {
	var tabFormParam = paramInArray(frmForm);
  // cas de connexion a padeleon
	if(tabFormParam[1]['txtLogin'] && tabFormParam[1]['pwdPassword']) {
		dojo.rawXhrPost({		
      // The following URL must match that used to test the server.
      url: "./ajaxrequest.php",
      handleAs: "text",
      postData: "search=CheckUser&"+tabFormParam[0],
      timeout: 60000, // Time in milliseconds
      // The LOAD function will be called on a successful response.
      load: function(response, ioArgs){
      	if (response == "logOn") {
      		document.forms[frmForm].submit();
      	}
      	else if(response == "accountNotConfirm") {
      	  document.getElementById('txtLogin').value = '';
      	  document.getElementById('pwdPassword').value = '';
      	  showDialog('accountNotConfirm');
      	}
      	else {
      	  // ici ds le cas mauvais login/pwd
      		document.getElementById('pwdPassword').value = '';
      		document.getElementById('msgErrorPwd').innerHTML = lang.get("padnls.home.interface.welcome.errorIncorrectEmail");
      	}
      },
  
      // The ERROR function will be called in an error case.
      error: function(response, ioArgs){
      	console.error("HTTP status code: ", ioArgs.xhr.status);
      	return response;
      }
		});
	}
	return false;
}

function ClearForm(idForm){
	var i, j;
	var maform = document.forms[idForm];//on appel maform chaque formulaire
	for (j = 0; j < maform.elements.length; j++) {
		try {
      if (maform.elements[j].type == "checkbox") {
      	maform.elements[j].checked = "";
      }
      else if (maform.elements[j].type == "text"||maform.elements[j].type == "password") {
      		maform.elements[j].value="";
      }
    } 
    catch (ex) {}
  }
}

function paramInArray(idForm){
	var i, j;
	var txtForm=0;
	var tabParam = new Array();
	tabParam[1] = new Array();
	var maform = document.forms[idForm];//on appel maform chaque formulaire
	for (j = 0; j < maform.elements.length; j++) {
		try {
      if (maform.elements[j].type == "checkbox") {
      	if (txtForm != 0) {
      		txtForm += "&" + maform.elements[j].name + "=" + maform.elements[j].checked;
      	}
      	else {
      		txtForm = maform.elements[j].name + "=" + maform.elements[j].checked;
      	}
      	tabParam[1][maform.elements[j].name]=maform.elements[j].checked;
      }
      else {
      	var value = Trim(maform.elements[j].value);
      	if (txtForm != 0) {
      		txtForm += "&" + maform.elements[j].name + "=" + value;
      	}
      	else {
      		txtForm = maform.elements[j].name + "=" + value;
      	}
      	tabParam[1][maform.elements[j].name]=value;
      }
		} 
		catch (ex) {}
	}
	tabParam[0] = txtForm;
	return tabParam;
}

function fieldToLowerCase(field){
	field = document.getElementById(field);
	field.value = field.value.toLowerCase();
}

function playFlash(){
  var lang = dojo.config.lang;
  document.getElementById('playFlash').innerHTML = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="640" height="400"><param name="movie" value="./faq/flash/take_tour_'+lang+'.swf" /><param name="quality" value="high" /><embed src="./faq/flash/take_tour_'+lang+'.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="640" height="400"></embed></object>';
}
