var ua = navigator.userAgent;
var browser;

var x=0;
var y=0;
var isDragging = false;
var imgWidth;
var imgHeight;
var imgCoords;
var widthZoom;
var ratio;
var previewWidth = previewHeight = 250;
var axe;
var xObjmid=0;
var yObjmid=0;
var objectToDrag;

var imgX=0;
var imgY=0;
var imgHeight=0;
var imgWidth=0;
var imgCoords=[0,0];
var cursor;
var nav="";

		
function getPositionCurseur(e){

  var DocRef;    // Variable pour IE uniquement

  // L'événement est passée à la fonction
  // donc tous sauf IE…
  if(e){                     // Dans ce cas on obtient directement la position dans la page
    x = e.pageX;
    y = e.pageY;
  }
  
  if(y>=0 && x>=0)
  {}else{
  nav="ie";
    x = event.clientX;
    y = event.clientY;

    //-- Il faut traiter le CAS des DOCTYPE sous IE
    if( document.documentElement && document.documentElement.clientWidth) // Donc DOCTYPE
      DocRef = document.documentElement;   // Dans ce cas c'est documentElement qui est réfèrence
    else
      DocRef = document.body;                    // Dans ce cas c'est body qui est réfèrence

    //-- On rajoute la position liée aux ScrollBars
    x += DocRef.scrollLeft;
    y += DocRef.scrollTop;
  }
}
		
function findPos(obj){
	var curleft = curtop = 0;
	curleft = obj.offsetLeft;
	curtop = obj.offsetTop;
	return [curleft,curtop];
}
		
function showZoom(url){
if(!document.getElementById('cursor'))
{
	var html='<div id="cursor" onmouseover="showZoom(document.getElementById(\'urlZoom\').value);" onmousemove="drag(event);" onmouseout="hideZoom();" style="background-color:#FFF; display:none; filter:alpha(opacity=50); -moz-opacity:0.5; opacity:0.5; z-index:2; position:absolute; border:2px solid #000000;cursor:crosshair;" > </div>';
	document.getElementById('master').innerHTML+=html;
}

	if(document.getElementById("img1")){
		imgWidth = document.getElementById("img1").offsetWidth;
		imgHeight = document.getElementById("img1").offsetHeight;
		imgCoords = findPos(document.getElementById("img1"));
		widthZoom = document.getElementById("wImgZoom").value;
		ratio = widthZoom/imgWidth;
		document.getElementById("cursor").style.display="block";
		
		if(ratio>0)
		{
		document.getElementById("cursor").style.width = (previewWidth/ratio)+"px";
		document.getElementById("cursor").style.height = (previewHeight/ratio)+"px";
		}
	//	beginDrag(document.getElementById("cursor"),null);
	}
	
	var imgbwidth = parseInt(document.getElementById("wImgb").value,10);
	
	loadZoom(url);
	
	var incBorder=parseInt(document.getElementById("img1").offsetWidth-document.getElementById("img1").width);
	document.getElementById("zoom").style.width=previewWidth+"px";
	document.getElementById("zoom").style.height=previewHeight+"px";
	document.getElementById("zoom").style.left = (imgCoords[0]+imgbwidth+5+incBorder)+"px";
	document.getElementById("zoom").style.top = imgCoords[1]+"px";
	document.getElementById("zoom").style.display="block";
}

function loadZoom(url){
	document.getElementById("urlZoom").value=url; 
	document.getElementById("imgZoom").src = document.getElementById("urlZoom").value;
	
	document.getElementById("wImgZoom").value=document.getElementById("imgZoom").width;
	widthZoom = document.getElementById("wImgZoom").value;
	ratio = widthZoom/imgWidth;
	
	if(ratio==1)
	{
		document.getElementById("imgZoom").width=document.getElementById("imgZoom").width*2;
		if(nav!="ie")document.getElementById("imgZoom").height=document.getElementById("imgZoom").height*2;
		document.getElementById("wImgZoom").value=document.getElementById("imgZoom").width;
		widthZoom = document.getElementById("wImgZoom").value;
		ratio = widthZoom/imgWidth;
	//	alert(ratio);
		document.getElementById("cursor").style.width = (previewWidth/ratio)+"px";
		document.getElementById("cursor").style.height = (previewHeight/ratio)+"px";
	}
}
		
function hideZoom(){
	document.getElementById("zoom").style.display="none";
	document.getElementById("cursor").style.display="none";
}
		
function drag(e){

	var newPosX = 0;
	var newPosY = 0;
	
	var curW=(previewWidth/ratio);
	var curH=(previewHeight/ratio);
	
	getPositionCurseur(e);
			
	var imgCoordsimage = Array();
	var imgCoordsmaster = findPos(document.getElementById("master"));
	imgCoordsimage["deb"] = findPos(document.getElementById("img1"));
	imgCoordsimagediv = findPos(document.getElementById("image"));
	/*
	var borderLeftW=0;
	if(document.getElementById("img1").borderLeftWidth) borderLeftW=parseInt(document.getElementById("img1").borderLeftWidth);
	
	var borderTopW=0;
	if(document.getElementById("img1").borderLeftWidth) borderTopW=parseInt(document.getElementById("img1").borderTopWidth);
		*/
		
	var incBorder=parseInt(document.getElementById("img1").offsetWidth-document.getElementById("img1").width)/2;
	
	imgCoordsimage["deb"][0] = imgCoordsimage["deb"][0] + incBorder;
	imgCoordsimage["deb"][1] = imgCoordsimage["deb"][1] + incBorder;
	
//	imgCoordsimage["deb"][1] += parseInt(document.getElementById("img1").offsetWidth-document.getElementById("img1").width);
		
	imgCoordsimage["fin"] = findPos(document.getElementById("img1"));
	imgCoordsimage["fin"][0] = imgCoordsimage["fin"][0]  + parseInt(document.getElementById("img1").width)+incBorder;
	imgCoordsimage["fin"][1] = imgCoordsimage["fin"][1]  + parseInt(document.getElementById("img1").height)+incBorder;

	newPosY = y - imgCoordsmaster[1] - curH/2;
	newPosX = x - imgCoordsmaster[0] - curW/2;
				
	if(newPosY<=imgCoordsimage["deb"][1])
	{
		newPosY=imgCoordsimage["deb"][1];
		
	}else if(newPosY+curH>=imgCoordsimage["fin"][1])
	{
		newPosY=imgCoordsimage["fin"][1]-curH-4;
	}
	
	if(newPosX<=imgCoordsimage["deb"][0])
	{
		newPosX=imgCoordsimage["deb"][0];
		
	}else if(newPosX+curW>=imgCoordsimage["fin"][0]){
		newPosX=imgCoordsimage["fin"][0]-curW-4;
	}

	var left=(newPosX)+"px";
	var top=(newPosY)+"px";
	
	document.getElementById("cursor").style.left = left;
	document.getElementById("cursor").style.top = top;
		
	if(document.getElementById("cursor").style.display=="block")
	{
		document.getElementById("imgZoom").style.left = (((imgCoordsimage["deb"][0] - newPosX)*ratio)-incBorder*2)+"px";
		document.getElementById("imgZoom").style.top = (((imgCoordsimage["deb"][1] - newPosY)*ratio)-incBorder*2)+"px";
	}
}

function quantity(type,obj){
	if(type=='moins' && obj.value>1)
		obj.value=parseInt(obj.value)-1;
	else if(type=='plus')
		obj.value=parseInt(obj.value)+1;
}
function checkEmail(stringIn) {
	if (stringIn.indexOf("@") < 1){
		return false;
	}else{
		st = stringIn.substr(stringIn.indexOf("@"));
		if(st.indexOf(".") == -1){
			return false;
		}else{
			if(!checkNormalChar(stringIn)){
				return false;
			}else{
				return true;
			}
		}
	}
}
function checkNormalChar(stringIn) {
	 retval = false 
	 var i;
     for (i=0;i<=stringIn.length-1;i++) { 
     	 //cho phep nhung ky tu tu A->Z, a->z, 0->9,va cac ky tu @,_
	      if (((stringIn.charCodeAt(i) >= 48)&&(stringIn.charCodeAt(i) <= 57)) || ((stringIn.charCodeAt(i) >= 64)&&(stringIn.charCodeAt(i) <= 90)) || ((stringIn.charCodeAt(i) >= 97)&&(stringIn.charCodeAt(i) <= 122)) ||(stringIn.charCodeAt(i)==95) ||(stringIn.charCodeAt(i)==46) ||(stringIn.charCodeAt(i)==45)) { 
                retval = true;
      	  }else{
      	  		retval = false;
      	  		break;
         }
     }
	 return retval;
}
function change_zoom(id,pid,nzoom,newid,newvalue,ids,ids2){
	if(document.getElementById(id)){
		if(document.getElementById(id).style.display == "inline"){
			document.getElementById(id).style.display = "none";
			document.getElementById(id).style.position = "";
			if(newid && newvalue){
				if(document.getElementById('change_pdzoom').value=='1'){
					document.getElementById('pd_zoom').style.display = 'none';
				}else{
					document.getElementById('pd_zoom').style.display = '';
				}
			}
			if(ids){
				hide_all(ids);
			}
			if(newid){
				document.getElementById(newid).value = 'product_zoom';
				if(document.getElementById('pdtid_defaut'))	document.getElementById('change_img_hd').value = document.getElementById('pdtid_defaut').value;
			}
		}else{
			if(ids && ids2){
				hide_all(ids);
				hide_all(ids2);
			}
			document.getElementById(id).style.display = "inline";
			document.getElementById(id).style.position = "absolute";
			if(newid && newvalue){
				document.getElementById(newid).value = newvalue;
			}
			if(pid){
				document.getElementById('change_img_hd').value = pid;
				if(document.getElementById('pid_'+pid)){
					document.getElementById('pdtimg_'+pid).src=document.getElementById('pid_'+pid).value;
				}
			}
			if(document.getElementById('pd_zoom')){
				if(nzoom=='1'){
					document.getElementById('pd_zoom').style.display = 'none';
				}else{
					document.getElementById('pd_zoom').style.display = '';
				}
			}
		}
	}
}
function hideshow_id(id,type){
	if(document.getElementById(id)){
		if(type==1)
			document.getElementById(id).style.display = '';
		else
			document.getElementById(id).style.display = 'none';
	}
}
function getElementsByName_iefix(tag, name) {
	 var elem = document.getElementsByTagName(tag);
	 var arr = new Array();
	 for(i = 0,iarr = 0; i < elem.length; i++) {
		  att = elem[i].getAttribute("name");
		  if(att == name) {
			   arr[iarr] = elem[i];
			   iarr++;
		  }
	 }
	 return arr;
}
function hide_all(id){
	for(var i=0;i<(getElementsByName_iefix('div',id).length);i++){
		getElementsByName_iefix('div',id)[i].style.display = "none";
		getElementsByName_iefix('div',id)[i].style.position = "";
	}
}
function hideall_class(type,id){
	for(var i=0;i<(getElementsByName_iefix(type,id).length);i++){
		getElementsByName_iefix(type,id)[i].className = "";
	}
}
function hideall(nzoom,newid,pid,ids,ids2){
	if(ids && ids2){
		hide_all(ids);
		hide_all(ids2);
	}
	document.getElementById(newid).value = 'product_zoom';
	document.getElementById('change_img_hd').value = pid;
	if(document.getElementById('pd_zoom')){
		if(nzoom=='1'){
			document.getElementById('pd_zoom').style.display = 'none';
		}else{
			document.getElementById('pd_zoom').style.display = '';
		}
	}
}

function writeCookie(nom,valeur,jours) {
var expDate = new Date()
expDate.setTime(expDate.getTime() + (jours * 24 * 3600 * 1000))
document.cookie = nom + "=" + escape(valeur)+ ";expires=" + expDate.toGMTString()
}
// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );
function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}	

if(getCookie( 'user_resolution' ) == null){
	writeCookie('user_resolution',screen.width +"x"+ screen.height,30);
}
/* LOGO RECEIVE&PAY in left.php*/
function popupcalc(merchantID,montant){ 		
	var win2 = window.open("http://www.receiveandpay.com/wallet/customer/calculette.php?MerchantID="+merchantID+"&Amount="+montant ,'popup','height=503,width=600,status=yes,scrollbars=yes,menubar=no,resizable=yes'); 
}
function popupRnp(name,url,width,height){
  var var_popuprnp = window.open(url, name ,'height='+height+',width='+width+',status=yes,scrollbars=yes,menubar=no,resizable=yes');
}

function popupWin(name,url,height){ 		
	window.open(url ,name,'height=500,width=600,status=yes,scrollbars=auto,menubar=no,resizable=yes'); 
}

function sf_innerHTML(id,value){
	if(document.getElementById(id)){
		document.getElementById(id).innerHTML = value;
	}
}