/* Section pour les mail*/
var onMailClick = function(sourceElement){
    var mailDiv = document.createElement("div");
    mailDiv.id = "idOfMailDiv";
	mailDiv.style.position = "absolute";
	mailDiv.style.left = findLeft(sourceElement)+"px";
	mailDiv.style.top = findTop(sourceElement)+"px";
	mailDiv.style.display = "block";
	mailDiv.innerHTML = "<form name=\"mailform\"><table class=\"mail_table\">"+
	"<tr><td class=\"mail_title_td\" colspan=\"2\"><table><tr><td>Envoyer nous un message</td><td class=\"popup_close_td\"><img class=\"close_img\" alt=\"Close\" title=\"Close\" src=\"resource/img/mail/closebtn.png\" onclick=\"closeMailForm();\" onmouseover=\"closeOver(this);\" onmouseout=\"closeOut(this);\"></td></tr></table><hr/></td></tr>"+
	"<tr><td class=\"form_text_top_right_td\"><b>Nom</b>:</td><td><input type=\"text\" name=\"mail_name\" style=\"width:200px;\" /></td></tr>"+
	"<tr><td class=\"form_text_top_right_td\"><b>Courriel</b>:</td><td><input type=\"text\" name=\"mail_from\" style=\"width:300px;\" /></td></tr>"+
	"<tr><td class=\"form_text_top_right_td\"><b>Sujet</b>:</td><td><input type=\"text\" name=\"mail_subject\" style=\"width:400px;\" /></td></tr>"+
	"<tr><td class=\"form_text_top_right_td\"><b>Message</b>:</td><td><textarea name=\"mail_content\" cols=\"60\" rows=\"10\" wrap=\"soft\"></textarea></td></tr>"+
	"<tr><td colspan=\"2\" class=\"form_text_top_right_td\"><input onclick=\"callMailPage();\" type=\"button\" value=\"Envoyer\" /><input type=\"reset\" value=\"Effacer\" /></td></tr>"+
	"</table></form>";
	document.body.appendChild(mailDiv);
	document.getElementById("idOfMailImage").disabled = "disabled";
	document.forms["mailform"].mail_name.focus();
}
var closeOver = function(imgTarget){
    imgTarget.className = "over_close_img";
}
var closeOut = function(imgTarget){
    imgTarget.className = "close_img";
}
var closeMailForm = function(){
    document.body.removeChild(document.getElementById("idOfMailDiv"));
	var imgMailTag = document.getElementById("idOfMailImage");
	imgMailTag.disabled = "";
	imgMailTag.className = "mail_img";
	imgMailTag.src = "resource/img/mail/enveloppe.png";
}
var onMailOver = function(sourceElement){
    sourceElement.className = "over_mail_img";
	sourceElement.src = "resource/img/mail/enveloppeover.png";
}
var onMailOut = function(sourceElement){
    sourceElement.className = "mail_img";
	sourceElement.src = "resource/img/mail/enveloppe.png";
}
var onAlbumHomeOver = function(sourceElement){
    sourceElement.src = "resource/img/galerie/album_home_over.png";
}
var onAlbumHomeOut = function(sourceElement){
    sourceElement.src = "resource/img/galerie/album_home.png";
}



var onImgMouseOver = function(imgSrc){
    var fbDiv = document.createElement("div");
    fbDiv.id = "idOfFacebookDiv";
	fbDiv.style.position = "absolute";
	fbDiv.style.left = findLeft(imgSrc)-10+"px";
	fbDiv.style.top = findTop(imgSrc)-20+"px";
	fbDiv.style.display = "block";
	fbDiv.innerHTML = "<table class=\"tfaceb\"><tr><td><a href=\"http://www.facebook.com/pages/precision-custom/121044021268471\" target=\"_blank\"><img class=\"imgfacebmsg\" src=\"resource/img/menu/facebookmsg.png\" /></a></td><td>&nbsp;</td><td valign=\"top\">Joignez notre &eacute;quipe sur <b style=\"color:#3B5999;\">Facebook</b> afin de suivre les chroniques et aventures de <b>PR&Eacute;CISION CUSTOM</b>. Ainsi, vous serez toujours inform&eacute;s des promotions et des &eacute;v&egrave;nements &agrave; venirs.</td></tr></table>";
	document.body.appendChild(fbDiv);
	fbDiv.onmouseout = function(){
	    var theDiv = document.getElementById("idOfFacebookDiv");
		if (theDiv){
	        document.body.removeChild(theDiv);
		}
    }
}
/*var leave_popup_fb = function(){
}*/

var magnifyImg = function(imgTarget){
    if (typeof(imgTarget) != 'undefined'){
		var aDivOverImg = document.getElementById("idofbanneroverdiv");
		var aImgInDiv = document.getElementById("idofbanneroverimg");
		
	    aImgInDiv.src = imgTarget.src;
	    aImgInDiv.height = imgTarget.height + 20;
		aImgInDiv.style.width = 'auto';
		aImgInDiv.title = imgTarget.title;
		aImgInDiv.alt = imgTarget.alt;
		aImgInDiv.parentNode.href = imgTarget.parentNode.href;
		
		aDivOverImg.style.position = "absolute";
		aDivOverImg.style.left = findLeft(imgTarget)+"px";
		aDivOverImg.style.top = findTop(imgTarget)-10+"px";
		aDivOverImg.style.display = "block";
	}
}
var unmagnify = function(){
    var aDivOverImg = document.getElementById("idofbanneroverdiv");
	aDivOverImg.style.display = "none";
}
var findLeft = function(pElem){
    var aLeft = 0;
	if (pElem.offsetParent){
	    while(true){
		    aLeft += pElem.offsetLeft;
		    if(!pElem.offsetParent){
			    break;
		    }
			pElem = pElem.offsetParent;
		}
	    
	}else if (pElem.x){
	    aLeft += pElem.x;
	}
	return aLeft;
}

var findTop = function(pElem){
    var aTop = 0;
	if (pElem.offsetParent){
	    while(true){
		    aTop += pElem.offsetTop;
		    if(!pElem.offsetParent){
			    break;
		    }
			pElem = pElem.offsetParent;
		}
	    
	}else if (pElem.y){
	    aTop += pElem.y;
	}
	return aTop;
}


var setSrcToImage = function(imgId, imgSrc){
    var imgTag = document.getElementById(imgId);
	imgTag.src = imgSrc;
}

var changeImages = function(imgLeftId, imgRightId, curentPage){
    var left = document.getElementById(imgLeftId);
    var right = document.getElementById(imgRightId);
	var temp = left.src;
	
	left.src = right.src;
	right.src = temp;
	
	temp = left.title;
	left.title = right.title;
	right.title = temp;
	
	temp = left.alt;
	left.alt = right.alt;
	right.alt = temp;
	
	temp = left.name;
	left.name = right.name;
	right.name = temp;	
}

function resetScroll() {
  if( typeof( window.pageYOffset ) == 'number' ) {
    window.pageYOffset = 0;
    window.pageXOffset = 0;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    document.body.scrollTop = 0;
    document.body.scrollLeft = 0;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    document.documentElement.scrollTop = 0;
    document.documentElement.scrollLeft = 0;
  }
}

var onLoginClick = function(sourceElement){
    var loginDiv = document.createElement("div");
    loginDiv.id = "idOfLoginDiv";
	loginDiv.style.position = "absolute";
	loginDiv.style.left = findLeft(sourceElement)+"px";
	loginDiv.style.top = findTop(sourceElement)+"px";
	loginDiv.style.display = "block";
	loginDiv.innerHTML = "<form action=\"fc/page/login\" name=\"the_login_form\" method=\"post\"><table class=\"login_table\">"+
	"<tr><td class=\"login_title_td\" colspan=\"2\"><table><tr><td>Authentification</td><td  class=\"popup_close_td\"><img class=\"close_img\" alt=\"Close\" title=\"Close\" src=\"resource/img/mail/closebtn.png\" onclick=\"closeLoginForm();\" onmouseover=\"closeOver(this);\" onmouseout=\"closeOut(this);\"></td></tr></table><hr/></td></tr>"+
	"<tr><td class=\"form_text_top_right_td\">Username:</td><td><input type=\"text\" name=\"login_user\" style=\"width:150px;\" /></td></tr>"+
	"<tr><td class=\"form_text_top_right_td\">Password :</td><td><input type=\"password\" name=\"login_pswd\" style=\"width:150px;\" /></td></tr>"+
	"<tr><td colspan=\"2\" class=\"form_text_top_right_td\"><input type=\"submit\" value=\"Authentifier\" /><input type=\"reset\" value=\"Effacer\" /></td></tr>"+
	"</table></form>";	
	document.body.appendChild(loginDiv);
	document.getElementById("idOfLoginImg").disabled = "disabled";
	document.forms["the_login_form"].login_user.focus();
}
var closeLoginForm = function(){
    document.body.removeChild(document.getElementById("idOfLoginDiv"));
	var imgLoginTag = document.getElementById("idOfLoginImg");
	imgLoginTag.disabled = "";
	imgLoginTag.className = "login_img";
	imgLoginTag.src = "resource/img/portal/login.png";
}

var validateForm = function(formName){
    var theFormElements = document.forms[formName].elements;
    for(var i = 0, lg = theFormElements.length ; i < lg ; i++){
        if(theFormElements[i].value == ""){
		    alert("Tous les champs sont obligatoire");
			return false;
        }
    }
	return true;
}




var showGaleryImage = function(imgSource, entity, id, size){
    if (typeof(imgSource) != 'undefined'){
		var aDiv = document.getElementById("idOfGalerieDiv");
		var aImg = document.getElementById("idOfGalerieImg");
		var aEnglobingDiv = document.getElementById("idOfGaleryEnglobatorDiv");
		
	    aImg.src = "resource/image?entity="+entity+"&id="+id+"&size="+size;
	    aImg.title = imgSource.title;
		aImg.alt = imgSource.alt;
		aImg.className = "galerie_display_img";
		
		
		aDiv.style.position = "absolute";
		aDiv.style.left = findLeft(aEnglobingDiv)+"px";
		aDiv.style.top = findTop(aEnglobingDiv)+"px";
		aDiv.style.display = "block";
		aDiv.style.zIndex = 1000;
	}
}
var onClickGalerieDisplayedImage = function(){
    var aDiv = document.getElementById("idOfGalerieDiv");
    var aImg = document.getElementById("idOfGalerieImg");
    aDiv.style.display = "none";
	aImg.src="";
	
}


var isNumber = function(s) { 
    return String(s).search(/^\s*\d+\s*$/) != -1
}
var validatePromotionForm = function(){
    var theFormElements = document.forms["promotion_add"].elements;
	var aDate, aYear, aMonth, aDay, aCadPrice, aCadUnit, aCadDecimal;
    for(var i = 0, lg = theFormElements.length ; i < lg ; i++){
        if ((theFormElements[i].name == "promotion_number" ||
			theFormElements[i].name == "promotion_name" ||
			theFormElements[i].name == "promotion_fabriquant" ||
			theFormElements[i].name == "promotion_small_image" ||
			theFormElements[i].name == "promotion_large_image") && theFormElements[i].value == ""){
		    alert("Les champs: Numero, Nom, Fabricant, Petite image et garnde image, sont obligatoires.");
			return false;
        }else if(theFormElements[i].name == "promotion_number" && theFormElements[i].value != ""  && !isNumber(theFormElements[i].value)){
		    alert("Le champ:Numero, est obligatoire et doit etre compose de chiffres uniquement.");
			return false;
		}else if ((theFormElements[i].name == "promotion_year_date" ||
			       theFormElements[i].name == "promotion_month_date" ||
			       theFormElements[i].name == "promotion_day_date") && theFormElements[i].value != ""  && !isNumber(theFormElements[i].value)){
		    alert("Le champ: Date limite, n'est pas obligatoire, mais doit etre du format AAAA/MM/JJ.");
			return false;
		}else if ((theFormElements[i].name == "promotion_cad_unity_price" ||
			       theFormElements[i].name == "promotion_cad_decimal_price") && theFormElements[i].value != "" && !isNumber(theFormElements[i].value)){
		    alert("Le champ: Prix (CAD), n'est pas obligatoire, mais doit etre compose de chiffres.");
			return false;
		}
    }
	var aDiv = document.getElementById("idofPromotionHiddens");
	var priceElement = "";
	var dateElement = "";
	aCadUnit  = document.forms['promotion_add'].promotion_cad_unity_price.value; 
    aCadDecimal  = document.forms['promotion_add'].promotion_cad_decimal_price.value; 
	aCadPrice = "";
	if (aCadUnit.length > 0 && aCadDecimal.length > 0){
	    aCadPrice = aCadUnit+"."+aCadDecimal;
    }else if (aCadUnit.length > 0){
	    aCadPrice = aCadUnit+".00";
	}
	if (aCadPrice.length > 0){
	    priceElement = '<input type="hidden" name="promotion_cad_price" value="'+aCadPrice+'" />'+"";
	    /*document.forms['promotion_add'].promotion_cad_price.value = aCadPrice;*/
    }
	
	aYear  = document.forms['promotion_add'].promotion_year_date.value; 
    aMonth  = document.forms['promotion_add'].promotion_month_date.value; 
    aDay  = document.forms['promotion_add'].promotion_day_date.value;
	aDate = aYear+"-"+aMonth+"-"+aDay;
    if (aDate.length > 2){
	    dateElement = '<input type="hidden" name="promotion_end_date" value="'+aDate+'" />'+"";
	    /*document.forms['promotion_add'].promotion_end_date.value = aDate;*/
    }
	aDiv.innerHTML = priceElement+dateElement+"";
    return true;
}





