function doSubmit(param) {
  if (param != ''){
   var hFrm = window.document.getElementById(param);
   if (param == 'img1' && check('photo'))
       showProgress();

   if (param == 'rcv' && !checkReceiver())
       return;

   if (param == 'sponsoring_prog' && !checkSponsoringProgram())
       return;

   if (param == 'audiotel' && !checkAForm())
      return;

   if (param == 'regForm' && !checkRegistrationForm())
      return;

   if (param == 'loginForm' && !checkLoginForm())
      return;

   if (param == 'lgnForm') {
      var hWnd = window.document.getElementsByName("option");
      for (var i=0; i < hWnd.length; i++) {
	 if ((hWnd[i].type.toLowerCase() == 'radio') && (hWnd[i].checked)) {
	    if (hWnd[i].value == 1) {
		continue;
	    } else if (hWnd[i].value == 2) {
		if (!checkLoginForm()) return;
	    }
 	 }
      }
   }

   if (param == 'prForm' && !checkForm())
      return;

   if (param == 'user' && !checkUserForm())
      return;

   if (param == 'changePasswdForm' && !checkPasswd())
      return;

   if (param == 'byr') {
     _hideErrors();
     if (!checkRegCmdForm()){
	 //window.scrollTo(0,0);
	 return;
     }
     else if (!checkCBox()){
	 //window.scrollTo(0,0);
	 return;
     }
     /*
     else if (!checkTemplateType())
	 {
	     window.scrollTo(0,250);
	     return;
	 }
    */
     else if (!checkPaymentType()){
	 window.scrollTo(0,0);
       return;
     }
     showProgress();
     
   }

   if (param == 'shpmnt') {
	if(!checkShpmntRadio())
		return;
   }

   hFrm.submit();
  }
}

function checkShpmntRadio() {
	var hFrm = window.document.getElementById('shpmnt');
	if(!hFrm.stype[0].checked && !hFrm.stype[1].checked) {
		alert('Veuillez choisir une option d\'envoi');
		return false;
	}
	return true;
}

function checkOrangeField(id) {
    var field = window.document.getElementById(id);
    //alert("color: "+field.style.color);
    //if (field.style.color == "#ff7f00")
	field.style.color="#000000";

}

function showProgress() {
   var hDiv = window.document.getElementById('progress');

   hDiv.style.left = ""+(screen.availWidth/2)-(196)+"px";
   hDiv.style.top = "1000px";
   hDiv.style.visibility = 'visible';
   hDiv.style.display = 'block';
   setTimeout('document.images["upl"].src = "http://dev.popcarte.com/api/service/petitsfreres/img/petits/popup_telechargement.gif"', 200);
   //hDiv.innerHTML= '<img id="upl" src="images/popup_telechargement.gif" />';
}

function check(id) {
   var hObj = window.document.getElementById(id);

   if (hObj.value != '')
        return true;

   return false;
}

function checkReceiver() {
   var hInputs = window.document.getElementsByTagName("INPUT");
   var ret = true;
   var gret = false;
   var reqGroup = new Object;
   for (var i=0; i<hInputs.length; i++) {
	if ((hInputs[i].getAttribute("isRequired") == '1') && (hInputs[i].type != 'checkbox')){
           if (!(hInputs[i].value != "")) {
              window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	      _showError("errText");
	      ret=false;
           } else {
              window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
           }
        }
	if(hInputs[i].getAttribute("groupRequired")) {
		if(reqGroup[hInputs[i].getAttribute("groupRequired")])
			reqGroup[hInputs[i].getAttribute("groupRequired")].push(hInputs[i]);
		else {
			reqGroup[hInputs[i].getAttribute("groupRequired")] = new Array();
			reqGroup[hInputs[i].getAttribute("groupRequired")].push(hInputs[i]);
		}
	}
   }
   for(var gr in reqGroup) {
	gret = false;
	for(var i = 0; i < reqGroup[gr].length; i++) {
		if(reqGroup[gr][i].value != '') {
			gret = true;
		}
	        window.document.getElementById(reqGroup[gr][i].name+"_label").style.color = "#333333";
	}
	if(!gret) {
	      	_showError("errText");
	      	ret=false;
		for(var i = 0; i < reqGroup[gr].length; i++) {
	              window.document.getElementById(reqGroup[gr][i].name+"_label").style.color = "#ff7f00";
		}
	}
   }
   return ret;
}

function checkForm() {

   var hInputs = window.document.getElementsByTagName("INPUT");
   var ret = true;
   for (var i=0; i<hInputs.length; i++) {
	if ((hInputs[i].getAttribute("isRequired") == '1')){
           if (!(hInputs[i].value != "")) {
              window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	      _showError("errText");
	      ret=false;
           } else if ( (hInputs[i].name=="rEmail") && !(check_email(hInputs[i].value)) ){
	      window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	      ret=false;
	      alert("Votre adresse email n'est pas correcte");
	      hInputs[i].focus();
           } else {
              window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
           }
        }
   }
   return ret;
}

function checkRegCmdForm() {

    var hInputs = window.document.getElementsByTagName("INPUT");
    var ret = true;
    var emailObj = "";
    var focus = false;
    
    for (var i=0; i<hInputs.length; i++) {
	if ((hInputs[i].getAttribute("isRequired") == '1')){
	    if (!(hInputs[i].value != "")) {
		window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
		//window.scroll(window.document.getElementById(hInputs[i].name+"_label").offsetLeft,window.document.getElementById(hInputs[i].name+"_label").offsetTop);
		if (!focus){
		    hInputs[i].focus();		
		    alert ('Les informations en orange sont requises pour l\'envoi de votre message');
		    focus = true;
		}
		
		//_showError("errText");
		ret=false;
		
	    } else if ( hInputs[i].name=="rEmail") {
		if (!(check_email(hInputs[i].value))) {
		    window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
		    ret=false;
		    //PfP special one
		    if (!focus){
		    hInputs[i].focus();
		    }
		    alert("Votre adresse email n'est pas correcte");
		    hInputs[i].focus();
		}
		emailObj = hInputs[i];

	    } else if ( hInputs[i].name=="rEmailConf" && hInputs[i].value != emailObj.value ){
		window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
		ret=false;
		_showError("errEmailConf");
		hInputs[i].focus();
		
	    } else if ( (hInputs[i].name=="rPasswd") && (hInputs[i].value != hInputs[i+1].value) ) {
		window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
		//window.document.getElementById(hInputs[i+1].name+"_label").style.color = "#ff7f00";
		ret=false;
		_showError("errPasswd");
	    } else {
		window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
	    }
        }
    }
    return ret;
}

function checkLoginForm() {

   var hInputs = window.document.getElementsByTagName("INPUT");
   var ret = true;

   for (var i=0; i<hInputs.length; i++) {
	if ((hInputs[i].getAttribute("isRequired") == '1')){
           if (!(hInputs[i].value != "")) {
              window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	     _showError("errText");
	      ret=false;
           } else {
              window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
           }
        }
   }
   return ret;
}

function checkTemplateType(){

    var template = window.document.getElementsByName('tid');
    var photoinput = window.document.getElementById('photo');

    for (i=0; i<template.length; i++)
	{
	    if (template[i].checked==true) { t = template[i].value; }
	}

    
    if (t < 0 && photoinput.value != '')
      {
	  alert('Votre s\351lection de carte ne permet pas d\'utiliser votre photo, veuillez changer votre s\351lection');
	  return false;
      }

    else if (t > 0 && photoinput.value == '' && this.photoLoaded!=1)
	{
	    alert('Veuillez s\351lectionner une photo');
	    return false;
	}

    return true;
}

function checkCBox() {
   var hC2 = window.document.getElementById('rCheck1');

   if (hC2 && !hC2.checked) {
      window.document.getElementById("rCheck1_label").style.color = "#ff7f00";
      alert("Vous devez accepter nos conditions de vente pour terminer votre commande");

      hC2.focus();
      return false;
   }

   return true;
}

function checkPaymentType() {
   var hR = window.document.getElementsByName('payment_type');
   if(hR.length) {
	   for (var i =0; i<hR.length; i++) {
	     if (hR[i].checked == true) return true;
	   }
	   _showError("errPay");
//   window.document.getElementById("payment_type_label").style.color = "#ff7f00";

	   return false;
  }
  return true;
}

function _showError(err) {
   var hObj = window.document.getElementById(err);
   if (hObj)
   	hObj.style.display = "block";
}

function _hideError(err) {
   var hObj = window.document.getElementById(err);
   if (hObj)
   	hObj.style.display = "none";
}

function _hideErrors() {
   window.document.getElementById("errText").style.display = "none";
   window.document.getElementById("errPay").style.display = "none";
   window.document.getElementById("errPasswd").style.display = "none";
   window.document.getElementById("errEmailConf").style.display = "none";
}

function showPopup(cmd,params) {
   window.open("popup.php?act="+cmd+params,"_blank","width=500,height=400,toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1");
}

function showPopup2(image, width, height) {
   window.open(image,"_blank","width="+width+",height="+height+",toolbar=0,location=0,directories=0,status=0,menuBar=0,scrollBars=1,resizable=1");
}

function maxLen(field,maxlimit){
   var hField = document.getElementById(field);
   if (hField.value.length > maxlimit) {
	hField.value = hField.value.substring(0,maxlimit);
   }
}


function isChecked(id, message) {
   var hC2 = window.document.getElementById(id);
   if (!hC2.checked) {
      window.document.getElementById(id+"_label").style.color = "#ff7f00";
      alert(message);
      hC2.focus();
      return false;
   }
   return true;
}

function checkRegistrationForm() {
   var hInputs = window.document.getElementsByTagName("INPUT");
   var ret = true;
  var emailObj = "";

   for (var i=0; i<hInputs.length; i++) {
	if ((hInputs[i].getAttribute("isRequired") == '1') && (hInputs[i].type != 'checkbox')){
           if (!(hInputs[i].value != "")) {
              window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	      _showError("errText");
	      ret=false;
           } else if (hInputs[i].name=="rEmail") {
	      if (!check_email(hInputs[i].value)) {
	         window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	         ret=false;
	         _showError("errEmail");
	     } else {
	         window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
		 _hideError("errEmail");
	     }
				emailObj = hInputs[i];
			} else if (hInputs[i].name=="rEmailConf") {
				if (hInputs[i].value != emailObj.value) {
					window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
					window.document.getElementById(emailObj.name+"_label").style.color = "#ff7f00";
					ret=false;
					_showError("errEmailConf");
				} else {
					window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
					window.document.getElementById(emailObj.name+"_label").style.color = "#333333";
					_hideError("errEmailConf");
				}
           } else if (hInputs[i].name=="rPasswd") {
	      if (hInputs[i].value != hInputs[i+1].value) {
	          window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	          //window.document.getElementById(hInputs[i+1].name+"_label").style.color = "#ff7f00";
	          ret=false;
	          _showError("errPasswd");
	      } else {
	         window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
		  _hideError("errPasswd");
	      }
   	   } else if (hInputs[i].name=="rPasswdConf") {
	      if (hInputs[i].value != hInputs[i-1].value) {
	          window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	          //window.document.getElementById(hInputs[i+1].name+"_label").style.color = "#ff7f00";
	          ret=false;
	          _showError("errPasswd");
	      } else {
	         window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
		  _hideError("errPasswd");
	      }
           } else {
              window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
           }
        }
   }

   if (ret && !isChecked('rCheck1',"Vous devez accepter nos conditions de vente pour valider votre inscription"))
      return false;

   return ret;
}


//new sponsoring program (this function checks mandatory email)
function checkSponsoringProgram() 
{
   var hInputs = window.document.getElementsByTagName("INPUT");
   var ret = true;

   for (var i=0; i<hInputs.length; i++) 
	 {
			if ((hInputs[i].getAttribute("isRequired") == '1') && (hInputs[i].type != 'checkbox'))
			{
           if (!(hInputs[i].value != "")) 
					 {
              window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	      			_showError("errText");
	      			ret=false;
           } 
					 else if (hInputs[i].name=="rEmail") 
					 {
	      	 	 if (!check_email(hInputs[i].value)) 
						 {
	         		 window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	         		 ret=false;
	         		 _showError("errEmail");
	     			 } 
						 else 
						 {
	         		 window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
		 					 _hideError("errEmail");
	     			 }
           }

      } 
			else 
			{
        window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
      }
   }
   return ret;
}



function checkPasswd() {
   var hInputs = window.document.getElementsByTagName("INPUT");
   var ret = true;

   for (var i=0; i<hInputs.length; i++) {
	if ((hInputs[i].getAttribute("isRequired") == '1') && (hInputs[i].type != 'checkbox')){
           if (!(hInputs[i].value != "")) {
              window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	      _showError("errText");
	      ret=false;
           } else if ( (hInputs[i].name=="rPasswd") && (hInputs[i].value != hInputs[i+1].value) ) {
	      window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	      window.document.getElementById(hInputs[i+1].name+"_label").style.color = "#ff7f00";
	      ret=false;
	      _showError("errPasswd");
           } else {
              window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
           }
        }
   }
   return ret;
}

function checkAForm() {

   var hInputs = window.document.getElementsByTagName("INPUT");
   var ret = true;
   for (var i=0; i<hInputs.length; i++) {
	if ((hInputs[i].getAttribute("isRequired") == '1')){
           if (!(hInputs[i].value != "")) {
	      alert("Vous n'avez pas entr� les 2 codes");
	      return false;
           }
        }
   }
   return true;
}

function checkUserForm() {

   var hInputs = window.document.getElementsByTagName("INPUT");
   var ret = true;
   for (var i=0; i<hInputs.length; i++) {
	if ((hInputs[i].getAttribute("isRequired") == '1')){
           if (!(hInputs[i].value != "")) {
	      window.document.getElementById(hInputs[i].name+"_label").style.color = "#ff7f00";
	      ret = false;
           } else {
	      window.document.getElementById(hInputs[i].name+"_label").style.color = "#333333";
	   }
        }
   }
   if (!ret)
	alert("Les informations en orange sont requises pour la modification de votre profil");
   return ret;
}

function check_email(email) {

 var filter=/^.+@.+\..{2,3}$/

 if (email=='') return false;

 return (filter.test(email)) ? true : false;
}

function checkEmail(fieldId) {
    var hField = window.document.getElementById(fieldId);
    if (!check_email(hField.value)){
	alert("Votre adresse email n'est pas correcte");
        return false;
    }

    return true;
}

function changeOption(src, id) {
   var hWnd = window.document.getElementById(id);

   if (hWnd && src) {
	if (src.value != '') {
	   hWnd.checked = true;
        }
   }
}

var currentPage=1;
var maxPage=1;
function showMinPage(nb) {

	currentPage = nb;
	for (i=1; i <= maxPage; i++)
		if (i == nb) {
			document.getElementById('page' + i).style.display = "block";
			document.getElementById('pageLnk' + i).style.fontWeight = "bold";
		} else {
			document.getElementById('page' + i).style.display = "none";
			document.getElementById('pageLnk' + i).style.fontWeight = "normal";
		}            
	scrollTo(0,0);
}
function showPagePrev() {
	if (currentPage > 1) {
		currentPage--;
		showMinPage(currentPage);
	}
}
function showPageNext() {
	if (currentPage < maxPage) {
		currentPage++;
		showMinPage(currentPage);
	}
}
	

