
//JQuery body onload equivalent - but better. Be sure Jquery library is loaded first
$(document).ready(function() {
	//Facebook stuff
	FB.init({appId: '118723894868971', status: true,cookie: true, xfbml: true, oauth: true});
	
	//CLOSING POPUP  
	//Click the x event!  
	$("#popupFacebookAlertClose").click(function(){  
		disablePopupAsso(true);  
	});
	
	$("#popup_ok_button").click(function(){  
		disablePopupAsso(true);
	});
	
	$("#popup_birthday_ok_button").click(function(){  
		
		if ($("#popup_birthday_ok_button").attr("href") != "javascript:getFriendsAlert();"){
			window.location = '/account.php?sp=birthdays';
		disablePopupAsso(false);
			}
	});
	
	$("#popup_birthday_cancel_button").click(function(){  
		disablePopupBirthdayAlert();
	});
	
	$("#popup_fail_button").click(function(){  
		disablePopupAsso(true);
		fb_logout();
	});
	
	$("#popup_create_asso_button").click(function(){  
		createFBAsso();  
	});
	
	$("#popup_cancel_button").click(function(){  
		cancelFBAsso();  
	});
});

function fb_login(){
FB.login(function(response) {
if(response.authResponse)
{
$.get("/FB_login.php",
		function(data){
		
			var result = eval("(" + data + ")");
			
			if (result){
				code = result['code'];
				
				switch(code){
				
					case 201 : 
							//OK. Association
							
							centerPopupAsso();  
							setPopupAssoText(result['text']);
							loadPopupAsso();
							getFriendsInfo();
							break;
							
					case 202 : 
							
							centerPopupAsso();  
							setPopupAssoText(result['text']);
							loadPopupAssoConfirm();
							getFriendsInfo();
							break;
							
					case 200 : 
							//Ok.
							getFriendsInfo();
							if (window.location.pathname == "/login.php" || window.location.pathname == "/registration.php") //if login, redirect to my account
								document.location.href = '/account.php?sp=profile';
							else if (window.location.pathname == "/regCommand.php" )
								document.location.href = '/command.php';
							else if (window.location.pathname == "/Facebook_photos.php")
								launchAlbumRetrieving();
							else window.location.reload();
							
							break;
							
					case 401 :
					case 402 :
					case 403 :
							centerPopupAsso();  
							setPopupAssoText(result['text']);
							loadPopupFail();
							break;
					
					default:break;
				
				}
				
			}
			//window.location.reload();
			//else alert(data);
			
		});
		
	
}
},{scope:'user_photos, email, friends_birthday'});
}

function fb_relogin(){

}

function fb_logout(){
	FB.logout();
}

function getFriendsInfo(){

$.post("/FB_friends_info.php", { 'choices[]': ["Jon", "Susan"] }, function(data){

});

}

function createFBAsso(){

$.get("/FB_login_asso.php",{create:"1"},
		function(data){
		
		var result = eval("(" + data + ")");
			
			if (result){
				code = result['code'];
				
				switch(code){
				
					case 201 : 
							centerPopupAsso();  
							setPopupAssoText(result['text']);
							loadPopupAsso();
							break;
							
					default:break;
				}
			}
		

});

}

function cancelFBAsso(){

$.get("/FB_login_asso.php",{create:"0"},
		function(data){
		
		var result = eval("(" + data + ")");
			
			if (result){
				code = result['code'];
				
				switch(code){
				
					case 201 : 
							centerPopupAsso();  
							setPopupAssoText(result['text']);
							loadPopupAsso();
							break;
							
					default:break;
				}
			}
			
		fb_logout();

});

}

function birthdays_alert_selection(){

	if ($('input[type=radio][name=pop_chk]:checked').attr('value') == 1){
	
	
	$.post("/FB_friends_alert.php", "friends=all", function(data){
	
		$("#popupFacebookAlert").width(300);
		$("#popupFacebookAlert").height(200);
		centerPopupAsso();  
		setPopupAssoText(decodeURIComponent( encodeURIComponent('Your selection has been validated!')));
		loadPopupBirthday();
		
	});
	
	}
	else {
	
	$.get('/account.php',{ sp: "birthdays", cmd: "get_friends" }, function(data){
	
	
		var result = eval("(" + data + ")");
			
			if (result){
				setPopupWithFriends(result);
			}
	
	
	});
	
	}
}

function setPopupWithFriends(friends){

code = '<table>';

$("#popupFacebookAlert").width(650);
$("#popupFacebookAlert").height(600);
centerPopupAsso();


$("#popup_birthday_ok_button").attr("href","javascript:getFriendsAlert();");

$("#popupFacebookAlertText").html(decodeURIComponent(encodeURIComponent('Please select the friends to add to the alert<br><br>'))); 

$("#popupFacebookAlert").css("font-size","12px");

$("#popupFacebookAlertContent").css("height","480px");


$("#popupFacebookAlertHeader").css("background-color","#627AAD");
$("#popupFacebookAlertText").css("color","#FFFFFF");
$("#popupFacebookAlertText").css("font-weight","bold");
$("#popupFacebookAlertText").css("margin","auto");
$("#popupFacebookAlert").css("border","2px solid #939594");
$("#popupFacebookAlertFooter").css("text-align","right");
$("#popup_birthday_ok_button_image").attr("src","img/facebook_ok.jpg");
$("#popup_birthday_cancel_button").show();

code='<tr>';
for(i = 0; i < friends.length; i++) {

if (i%3==0)
	code += '</tr><tr>';
	
code += '<td><br><input id="chk_'+friends[i].fbid+'" name="chk_friend" value="'+friends[i].fbid+'" type="checkbox"';

if (friends[i].in_alert)
code+=' checked';

code+='></input><img src="http://graph.facebook.com/'+friends[i].fbid+'/picture" \> '+friends[i].first_name+' '+friends[i].last_name+'</td>';

}

code += '</tr>';


$("#popupFacebookAlertContent").html(code);

$("#popupFacebookAlert").scrollTo("0px");

loadPopupBirthday();
}

function getFriendsAlert(){
	
	friends = new Array();
	i=0;
	$("input:checkbox[name=chk_friend]:checked").each(function()
{
    friends[i] = $(this).val();
	i++;
});

$.post("/FB_friends_alert.php", "friends="+array2json(friends), function(data){
	
		$("#popupFacebookAlertContent").html('');
		$("#popupFacebookAlert").css("font-size","16px");
		$("#popupFacebookAlertContent").css("height","30px");
		$("#popupFacebookAlert").width(300);
		$("#popupFacebookAlert").height(200);
		$("#popup_birthday_ok_button").attr("href","javascript:nothing();");
		$("#popupFacebookAlertHeader").css("background-color","");
		$("#popupFacebookAlertText").css("color","");
		$("#popupFacebookAlertText").css("font-weight","");
		$("#popupFacebookAlertText").css("margin","");
		$("#popupFacebookAlert").css("border","2px solid #FF7C00");
		$("#popupFacebookAlertFooter").css("text-align","");
		$("#popup_birthday_cancel_button").hide();
		$("#popup_birthday_ok_button_image").attr("src","/img/popup_fb_ok.gif");
		
		centerPopupAsso();  
		setPopupAssoText(decodeURIComponent( encodeURIComponent('Your selection has been validated')));
		loadPopupBirthday();
	});

}

//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var popupStatus = 0;  

//loading popup with jQuery magic!  
function loadPopupAsso(){  
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#backgroundPopup").css({  
			"opacity": "0.7"  
		});
		$("#popupFacebookAlertClose").show();
		$("#popup_create_asso_button").hide();
		$("#popup_cancel_button").hide();
		$("#popup_birthday_ok_button").hide();
		$("#popup_birthday_cancel_button").hide();
		$("#popup_fail_button").hide();
		$("#popup_ok_button").show();
		$("#backgroundPopup").show();  
		$("#popupFacebookAlert").show();  
		popupStatus = 1;  
	}
	else {
		$("#popupFacebookAlertClose").show();
		$("#popup_create_asso_button").hide();
		$("#popup_cancel_button").hide();
		$("#popup_birthday_ok_button").hide();
		$("#popup_birthday_cancel_button").hide();
		$("#popup_fail_button").hide();
		$("#popup_ok_button").show();
	
	}
}

function loadPopupAssoConfirm(){  
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#backgroundPopup").css({  
			"opacity": "0.7"  
		});
		$("#popup_ok_button").hide();
		$("#popupFacebookAlertClose").hide();
		$("#popup_birthday_cancel_button").hide();
		$("#popup_fail_button").hide();
		$("#popup_birthday_ok_button").hide();
		$("#popup_create_asso_button").show();
		$("#popup_cancel_button").show();		
		$("#backgroundPopup").show();  
		$("#popupFacebookAlert").show();  
		popupStatus = 1;  
	}  
}

function loadPopupFail(){  
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#backgroundPopup").css({  
			"opacity": "0.7"  
		});
		$("#popupFacebookAlertClose").hide();
		$("#popup_create_asso_button").hide();
		$("#popup_birthday_ok_button").hide();
		$("#popup_birthday_cancel_button").hide();
		$("#popup_cancel_button").hide();
		$("#popup_fail_button").show();
		$("#popup_ok_button").hide();
		$("#backgroundPopup").show();  
		$("#popupFacebookAlert").show();  
		popupStatus = 1;  
	}
	else {
		$("#popupFacebookAlertClose").hide();
		$("#popup_create_asso_button").hide();
		$("#popup_birthday_ok_button").hide();
		$("#popup_birthday_cancel_button").hide();
		$("#popup_cancel_button").hide();
		$("#popup_fail_button").show();
		$("#popup_ok_button").hide();
	
	}
} 

function loadPopupBirthday(){  
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#backgroundPopup").css({  
			"opacity": "0.7"  
		});
		$("#popupFacebookAlertClose").hide();
		$("#popup_create_asso_button").hide();
		$("#popup_cancel_button").hide();
		$("#popup_fail_button").hide();
		$("#popup_birthday_cancel_button").hide();
		//$("#popup_birthday_ok_button").attr("href","javascript:nothing();");
		$("#popup_birthday_ok_button").show();
		$("#popup_ok_button").hide();
		//$("#backgroundPopup").fadeIn("slow");  
		//$("#popupFacebookAlert").fadeIn("quick");
		$("#backgroundPopup").show();  
		$("#popupFacebookAlert").show();
		
		if ($("#popup_birthday_ok_button").attr("href")=="javascript:getFriendsAlert();"){
			$("#popupFacebookAlertClose").hide();
			$("#popup_birthday_cancel_button").show();
		}
		
		popupStatus = 1;  
	}
}

function setPopupAssoText(text){

$("#popupFacebookAlertText").html(text);

}

//disabling popup with jQuery magic!  
function disablePopupAsso(reload){  
	//disables popup only if it is enabled  
	if(popupStatus==1){  
		//$("#backgroundPopup").fadeOut("slow");  
		//$("#popupFacebookAlert").fadeOut("slow");  
		$("#backgroundPopup").hide();  
		$("#popupFacebookAlert").hide();  
		popupStatus = 0;  
	}
	if (reload)
		location.reload();
}  

function disablePopupBirthdayAlert(){  
	//disables popup only if it is enabled  
	if(popupStatus==1){  
		$("#backgroundPopup").hide();  
		$("#popupFacebookAlert").hide(); 
			$("#popupFacebookAlertContent").html('');
		$("#popupFacebookAlert").css("font-size","16px");
		$("#popupFacebookAlertContent").css("height","30px");
		$("#popupFacebookAlert").width(300);
		$("#popupFacebookAlert").height(200);
		$("#popup_birthday_ok_button").attr("href","javascript:nothing();");
		$("#popupFacebookAlertHeader").css("background-color","");
		$("#popupFacebookAlertText").css("color","");
		$("#popupFacebookAlertText").css("font-weight","");
		$("#popupFacebookAlertText").css("margin","");
		$("#popupFacebookAlert").css("border","2px solid #FF7C00");
		$("#popup_birthday_cancel_button").hide();
		$("#popup_birthday_ok_button_image").attr("src","/img/popup_fb_ok.gif");
		$("#popupFacebookAlertFooter").css("text-align","");
		popupStatus = 0;  
	}
	
}  

//centering popup  
function centerPopupAsso(){  
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $("#popupFacebookAlert").height();  
	var popupWidth = $("#popupFacebookAlert").width();  
	//centering  
	$("#popupFacebookAlert").css({  
		"position": "absolute",  
		"top": windowHeight/2-popupHeight/2,  
		"left": windowWidth/2-popupWidth/2  
	});  
	//only need force for IE6  

	$("#backgroundPopup").css({  
		"height": windowHeight  
	});  

}

function launchAlbumRetrieving(){
$("#album_fb_loader").show();
$.get("/Facebook_photos.php",
		function(data){
			$("#album_fb_loader").hide();
			window.location = "/Facebook_photos.php";
		});

}

function disconnect_fb_account(){

$.get("/FB_disconnect_account.php",
		function(data){
			window.location.reload();
		});
}

function nothing(){}

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())
       return;
     else if (!checkCBox())
       return;
     else if (!checkPaymentType())
       return;
	   
   }

   if (param == 'shpmnt') {
	if (!checkShpmnt())
		return;
   }

   if (param == 'frmtForm'){
       if (!checkFrmt())
		return;
   }
   hFrm.submit();
  }
}

function checkShpmnt() {
	hFrm = window.document.getElementById('stype');
	if(hFrm.value!="rcv" && hFrm.value!="sback"){
		alert('Please select a shipment method');
		return false;
	}
	return true;
}

function checkFrmt() {
		if(format_clicked<0){
		alert('Please select format');
		return false;
	}
	return true;
}

function showProgress() {
   var hDiv = window.document.getElementById('progress');

   hDiv.style.left = (screen.availWidth/2)-(196);
   hDiv.style.visibility = 'visible';
   hDiv.style.display = 'block';
   setTimeout('document.images["upl"].src = "/images/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 = "";
  
   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") {
				if (!(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();
				}
        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 checkCBox() {
   var hC2 = window.document.getElementById('rCheck1');

   if (hC2 && !hC2.checked) {
      window.document.getElementById("rCheck1_label").style.color = "#ff7f00";
      alert("Please agree to our terms and conditions to complete your order");

      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',"You must agree to our terms and conditions to complete your registration"))
      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("Informations in orange are required to save your profile");
   return ret;
}

function check_email(email) {

 var filter=/^.+@.+\..{2,4}$/

 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);
	}
}
	
function hideFlash(){
    /*flash = document.getElementsByTagName('embed');
    for (var i = 0; i < flash.length; i++)
	{
	    flash[i].style.visibility='hidden';
	    }*/
    
}

//functions for send mode choice

var send_mode_clicked=-1;

function shipmentSwitch(id, highlight){

    var div = window.document.getElementById(id);
    var stype = window.document.getElementById('stype');
    if (send_mode_clicked < 0){
    if (highlight){
	    div.style.backgroundColor = "#FFCC66";
	    div.style.border = "1px solid #FFCC66";
	}
	else {
	    div.style.backgroundColor = "#f7f7f7";
	    div.style.border = "1px solid #feb74c";
	    stype.value="";	    
	}
    }
	}

function clickSendMode(mode){
    send_mode_clicked = mode;
    var stype = window.document.getElementById('stype');
    if (mode == 1){
	window.document.getElementById('shipDiv1').style.backgroundColor = "#feb74c";
	window.document.getElementById('shipDiv1').style.border = "1px solid #FF9900";
	stype.value="rcv";
        window.document.getElementById('imgTick1').style.display = "block";

	window.document.getElementById('shipDiv2').style.backgroundColor = "#f7f7f7";
	window.document.getElementById('shipDiv2').style.border = "1px solid #feb74c";
	window.document.getElementById('imgTick2').style.display = "none";
    }
    else {
	window.document.getElementById('shipDiv2').style.backgroundColor = "#feb74c";
	window.document.getElementById('shipDiv2').style.border = "1px solid #FF9900";
	stype.value="sback";
  window.document.getElementById('imgTick2').style.display = "block";

	window.document.getElementById('shipDiv1').style.backgroundColor = "#f7f7f7";
	window.document.getElementById('shipDiv1').style.border = "1px solid #feb74c";
	window.document.getElementById('imgTick1').style.display = "none";
    }
}


//functions for choose format page

var descriptions = new Array();
descriptions[1]='Our classic postcard, 4x6 inches, 10 ounces, printed on laminated glossy paper. <br />Price decreases depending on quantity and mailing method. Prices from $0.49. <br />';
descriptions[2]='Our elegant square card, 6x6 inches, 10 ounces, printed on laminated glossy paper. <br />Price decreases depending on quantity and mailing method. Prices from $0.49. <br />';
descriptions[4]='Our beautiful folded card, 5x7 inches, 10 ounces,  printed on laminated glossy paper.<br />Price decreases depending on quantity and mailing method. Prices from $1.65.<br />';
descriptions[5]='Our giant card, 8x12 inches, 10 ounces, printed on laminated glossy paper. <br />Price decreases depending on quantity and mailing method. Prices from $0.49. <br />';


var format_clicked = -1;
var first_mouseover = 1;
var default_format = 0;


function is_numeric(input){
    return typeof(input)=='number';
  }

function setDefaultFormat(format, elt, url){
default_format = format;
document.getElementById('desc').innerHTML=descriptions[format];
elt.style.background='#e7e7e7';
elt.style.border="1px #feb74c solid";
document.getElementById('prevImage').src="img/pix.png";
document.getElementById('prevImage').src=url[format];
}

function overFormat(format, elt, url){
    elt.style.cursor='pointer';

    if (format_clicked<0){
	if (first_mouseover){
		outFormat(default_format, document.getElementById('select_'+default_format));
		first_mouseover = 0;
	}
	document.getElementById('desc').innerHTML=descriptions[format];
	elt.style.background='#e7e7e7';
	elt.style.border="1px #feb74c solid";
    	document.getElementById('prevImage').src="img/pix.png";
	document.getElementById('prevImage').src=url[format];
    }
}

function outFormat(format, elt){

    if (format_clicked<0){
	elt.style.backgroundColor='';
	elt.style.border="";
	}
}
	

function clickFormat(formats, format, elt, url){
    for (var f in formats){
	    var _f = document.getElementById('select_'+f);
if (_f != null){	    
_f.style.background='';
	    _f.style.border='';
}
	}
    format_clicked = format;
    elt.style.background='#e7e7e7';
    elt.style.border="1px #feb74c solid";
    document.getElementById('fid').value=format;
    document.getElementById('desc').innerHTML=descriptions[format];
    document.getElementById('prevImage').src=url[format];
}	

function toComDetails(){

    document.getElementById('com_main').style.display="none";
    document.getElementById('com_main').style.visibility="hidden";
    document.getElementById('com_details').style.display="";
    document.getElementById('com_details').style.visibility="";

}

function toComMain(){

    document.getElementById('com_details').style.display="none";
    document.getElementById('com_details').style.visibility="hidden";
    document.getElementById('com_main').style.display="";
    document.getElementById('com_main').style.visibility="";
}

function country_shipper_check(){

   var shipper = document.getElementById('country_select_sendback');
   var box0 = document.getElementById('shipping_method_p');
   var box1 = document.getElementById('shipping_method_div1');
   var box2 = document.getElementById('shipping_method_div2');
   
   if (shipper.value==840)
   {
      box0.style.display = 'block';
      box1.style.display = 'block';
      box2.style.display = 'block';
   }
   else
   {
      box0.style.display = 'none';
      box1.style.display = 'none';
      box2.style.display = 'none';
   }

   
}

function select_font_alignement(choice){

    //alert('1');

    var left = document.getElementById('alignement_left');
    var center = document.getElementById('alignement_center');
    var right = document.getElementById('alignement_right');

    //alert('2');

    if (choice=='left')
    {
      left.src='/img/align-left_up.gif';
      center.src='/img/align-center.gif';
      right.src='/img/align-right.gif';
    }
    if (choice=='center')
    {
      left.src='/img/align-left.gif';
      center.src='/img/align-center_up.gif';
      right.src='/img/align-right.gif';
    }
    if (choice=='right')
    {
      left.src='/img/align-left.gif';
      center.src='/img/align-center.gif';
      right.src='/img/align-right_up.gif';
    }

    //alert('3');

}

function select_font_color(item_selected){

    //alert('1');

    var c1 = document.getElementById('font_color1');
    var c2 = document.getElementById('font_color2');
    var c3 = document.getElementById('font_color3');
    var c4 = document.getElementById('font_color4');
    var c5 = document.getElementById('font_color5');
    var c6 = document.getElementById('font_color6');
    var c7 = document.getElementById('font_color7');
    var c8 = document.getElementById('font_color8');
    var c9 = document.getElementById('font_color9');
    var c10 = document.getElementById('font_color10');

	var color = document.getElementById('color');
	var color_return = document.getElementById('colorForReturn');
    //alert('2');

    if (item_selected=='font_color1'){
	c1.src='/img/font_color1_up.gif';
	color.value="#000000";
	color_return.value=color.value;
	}
    else {c1.src='/img/font_color1.gif';}

    if (item_selected=='font_color2'){
	c2.src='/img/font_color2_up.gif';
	color.value="#070789";
	color_return.value=color.value;
	}
    else {c2.src='/img/font_color2.gif';}

    if (item_selected=='font_color3'){
	c3.src='/img/font_color3_up.gif';
	color.value="#C2011E";
	color_return.value=color.value;
	}
    else {c3.src='/img/font_color3.gif';}
    
    if (item_selected=='font_color4'){
	c4.src='/img/font_color4_up.gif';
	color.value="#007E08";
	color_return.value=color.value;
	}
    else {c4.src='/img/font_color4.gif';}
    
    if (item_selected=='font_color5'){
	c5.src='/img/font_color5_up.gif';
	color.value="#FFC208";
	color_return.value=color.value;
	}
    else {c5.src='/img/font_color5.gif';}
    
    if (item_selected=='font_color6'){
	c6.src='/img/font_color6_up.gif';
	color.value="#7B7B7B";
	color_return.value=color.value;
	}
    else {c6.src='/img/font_color6.gif';}
    
    if (item_selected=='font_color7'){
	c7.src='/img/font_color7_up.gif';
	color.value="#A4CBE8";
	color_return.value=color.value;
	}
    else {c7.src='/img/font_color7.gif';}
    
    if (item_selected=='font_color8'){
	c8.src='/img/font_color8_up.gif';
	color.value="#D411AD";
	color_return.value=color.value;
	}
    else {c8.src='/img/font_color8.gif';}
    
    if (item_selected=='font_color9'){
	c9.src='/img/font_color9_up.gif';
	color.value="#F25208";
	color_return.value=color.value;
	}
    else {c9.src='/img/font_color9.gif';}
    
    if (item_selected=='font_color10'){
	c10.src='/img/font_color10_up.gif';
	color.value="#ED83AB";
	color_return.value=color.value;
	}
    else {c10.src='/img/font_color10.gif';}

    //alert('3');

}

function select_font_size(){
var size = document.getElementById('font_size').value;
document.getElementById('sizeForReturn').value = size;
}

function select_font(item_selected){
var font = document.getElementById('font').value;
document.getElementById('fontForReturn').value = font;
}


function linker(link)
{
  var new_link = link.replace(/!/g,"/");
  var final_link = new_link.replace(/@/g,".");
  window.location = final_link;
}



//jours feries  (jourFeries.js)
//syntax : d/m/yyyy (without initial 0)
var jourFeries = new Array(
"9/9/2008",
"12/9/2008",
"23/9/2008",
"14/10/2008"
);



//popup.js

var isPopDefined = false;

var xoffset = 15;
var yoffset = 10;
pic1= new Image(30,29);
pic1.src="/images/rouegrise.gif";
pic2= new Image(30,29);
pic2.src="/images/rouevide.gif";

document.onmousemove = function(e) {
	if (!isPopDefined)
		return;

  var x, y, right, bottom;

  try { x = e.pageX; y = e.pageY; yyy = 0; } // FF
  catch(e) { x = event.x; y = event.y; yyy = document.documentElement.scrollTop} // IE

  right = (document.documentElement.clientWidth || document.body.clientWidth || document.body.scrollWidth);
  bottom = (window.scrollY || document.documentElement.scrollTop || document.body.scrollTop) + (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight);

  x += xoffset;
  y += yoffset;

  if(x > right-pop.offsetWidth)
    x = right-pop.offsetWidth;

  if(y+yyy > bottom-pop.offsetHeight)
    y = bottom-pop.offsetHeight-yyy;

  pop.style.top = y+yyy+'px';
  pop.style.left = x+'px';

}

function popup(elt) {
	if (!isPopDefined) {
		isPopDefined = true;
		pop = document.getElementById('popup');
	}
  document.getElementById('popup_img_over').innerHTML = "";
  //document.getElementById('popup_img').setAttribute('src', '/images/rouegrise.gif');
  document.getElementById('popup_img_over').innerHTML = "<img src='" + elt.getAttribute('srcBig') + "' align='center'>";
  document.getElementById('popup_span').innerHTML = elt.getAttribute('itemName');
  pop.style.display = 'block';
}

function popout() {
  pop.style.display = 'none';
}

  function editorSaveStart(msg) {
    window.onbeforeunload = function() {
      return msg;
    }
  }
  function editorSaveEnd() {
    window.onbeforeunload = null;
  }
  
  function orderStatusSwitchDetails(elt){
  
  if (document.getElementById('details').style.display=="none")
	document.getElementById('details').style.display="block";
else document.getElementById('details').style.display="none";
  
  }
  
  function facebook_popup(){
//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $("#facebook_popup").height();  
	var popupWidth = $("#facebook_popup").width();  
	//centering  
	$("#facebook_popup").css({  
		"position": "absolute",  
		"top": windowHeight/2-popupHeight/2,  
		"left": windowWidth/2-popupWidth/2  
	});  
	//only need force for IE6  

	$("#backgroundPopup").css({  
		"height": windowHeight  
	});  
	
$('#facebook_popup').show();
$('#backgroundPopup').show();
}

function facebook_popup_close(){
$('#facebook_popup').hide();
$('#backgroundPopup').hide();
}

//POPUP CREATING FUNCTION
function pop_the_popup(url,height,width) {
	newwindow=window.open(url,'name','height='+height+',width='+width);
	if (window.focus) {newwindow.focus()}
	return false;
}

function array2json(arr) {
	var parts = [];
	var is_list = (Object.prototype.toString.apply(arr) === '[object Array]');

	for(var key in arr) {
		var value = arr[key];
		if(typeof value == "object") { //Custom handling for arrays
			if(is_list) parts.push(array2json(value)); /* :RECURSION: */
			else parts[key] = array2json(value); /* :RECURSION: */
		} else {
			var str = "";
			if(!is_list) str = '"' + key + '":';
 
			//Custom handling for multiple data types
			if(typeof value == "number") str += value; //Numbers
			else if(value === false) str += 'false'; //The booleans
			else if(value === true) str += 'true';
			else str += '"' + convertString(String(value)) + '"'; //All other things
			// :TODO: Is there any more datatype we should be in the lookout for? (Functions?)

			parts.push(str);
		}
	}
	var json = parts.join(",");
	
	if(is_list) return '[' + json + ']';//Return numerical JSON
	return '{' + json + '}';//Return associative JSON
}

function convertString(string){
  string = string.replace(/"/g,"'");
  string = string.replace(/&/g,"%26");
  string = string.replace(/\\/g,"\\\\");
  return string;
}

