// JavaScript Document
include("/js/lang_js.asp");

function addCart(type,idProd) {
	// si le navigateur n'accepte pas les cookies, on redire l'utilisateur vers la page d'inscription / log
/*	Set_Cookie( 'test', 'none', '', '/', '', '' );
	if ( Get_Cookie( 'test' ) ){}else{
		if (isEmptySessionUser=="true"){
			window.location.href="/"+langueJs+"/connect.asp?referer="+window.location.href;return false;}}*/
	
	if (type=="sample")
	boxCommand('addcart',type,idProd,strCommandSample);
	else if (type=="wind")
	windCommand('addcart',idProd,strRepackage);
	else if (type=="kit")
	ajaxCommand('addcart',type);
	else
	boxCommand('addcart',type,idProd,strCommand);
}
function windCommand(action,idProd,strMessage){
	boxDialog.open(strMessage);
	var fct = function(){boxDialog.SendAjax("action=rec"+action+"&pty=wind&pid="+idProd+"&pnb="+document.getElementById("nbprod").value,function(){boxDialog.close();location.reload();})}
	boxDialog.RequestAjax("action="+action+"&pty=wind&pid="+idProd,fct);
}
function ajaxCommand(action,type){
	boxDialog.open(strYourCart);
	document.getElementById("dialog_body").innerHTML = "<b>"+strKitAdded+"</b>";
	var elms = document.getElementsByTagName("DIV");
	for (var i=0;i<elms.length;i++){
		if (elms[i].className=="kit_o"){
			var objTab=elms[i].id.split('-');
			boxDialog.SendAjax("action=rec"+action+"&pty="+objTab[0]+"&pid="+objTab[1]+"&pnb="+objTab[2]+"&pi="+objTab[3]+"&pt="+objTab[4],function(){});
		}
	}
	countCart();
	setTimeout("boxDialog.close()",5000);
}
function countCart() {
	var cart = document.getElementById("cart.value");
	// requete ajax
	if(window.XMLHttpRequest) // FIREFOX
	  xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // IE
	  xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
	else 
	  return(false);
	xhr.onreadystatechange  = function(){ 
		if(xhr.readyState == 4) {
		if(xhr.status == 200){
			cart.innerHTML = xhr.responseText;
			refreshMenuCart();
			return(true);}
		else{alert('Error ' + xhr.status + ' -- ' + xhr.statusText + '\n'+xhr.responseText);
			return(false);}
		}
	};
	xhr.open("GET", "/js/action_js.asp?action=countcart&no_chache="+Math.floor((Math.random()*999999)), true);
	xhr.setRequestHeader("Cache-Control","no-cache");
	xhr.send(null);
}
function refreshMenuCart(){
	var cartmenu=document.getElementById("content_cart_menu");
	if (cartmenu!=undefined){
	// requete ajax
	if(window.XMLHttpRequest) // FIREFOX
	  xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject) // IE
	  xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
	else 
	  return(false);
	xhr.onreadystatechange  = function(){ 
		if(xhr.readyState == 4) {
		if(xhr.status == 200){
			cartmenu.innerHTML = xhr.responseText;
			return(true);}
		else{alert('Error ' + xhr.status + ' -- ' + xhr.statusText + '\n'+xhr.responseText);
			return(false);}
		}
	};
	xhr.open("GET", "/js/action_js.asp?action=refreshcartmenu&no_chache="+Math.floor((Math.random()*999999)), true);
	xhr.setRequestHeader("Cache-Control","no-cache");
	xhr.send(null);
	}
}
function modifyQuantity(type, idProd, quantity){
	var nArgs = modifyQuantity.arguments.length;
	var reloadPage=false;
	if (nArgs>3) reloadPage = modifyQuantity.arguments[3];
	boxDialog.SendAjax("action=recaddcart&pty="+type+"&pid="+idProd+"&pnb="+quantity,function(){if(reloadPage){location.reload();}else{refreshMenuCart();}});
}
function delCart(elm,type,idProd){
	var nArgs = delCart.arguments.length;
	while(elm.tagName.toLowerCase()!="tbody"){
		elm = elm.parentNode;}
	var reloadPage=false;
	if (nArgs>3) reloadPage = delCart.arguments[3];
	boxDialog.SendAjax("action=delcart&pid="+idProd+"&pty="+type,function(){/*elm.parentNode.removeChild(elm);*/if(reloadPage){location.reload();}else{countCart();}});
}
function boxCommand(action,type,idProd,strMessage){
	boxDialog.open(strMessage);
	var fct = function(){boxDialog.SendAjax("action=rec"+action+"&pty="+type+"&pid="+idProd+"&pnb="+document.getElementById("nbprod").value,function(){boxDialog.close();countCart();})}
	boxDialog.RequestAjax("action="+action+"&pty="+type+"&pid="+idProd,fct);
}
function boxDialogPopup(){
	this.writeContent=function(textHTML,fct) {
		document.getElementById("dialog_body").innerHTML = textHTML;
		var fct1=function(){boxDialog.close();}
		document.getElementById("dialog_body").appendChild(this.clear());
		document.getElementById("dialog_body").appendChild(this.createBtn(strCancel,fct1,"inputbutton inputaux inputright"));
		if ((textHTML.toLowerCase().indexOf("error")==-1)&&(fct!=null)){
		document.getElementById("dialog_body").appendChild(this.createBtn(strValidate,fct,"inputsubmit inputright marg5r"));
		}
		document.getElementById("dialog_body").appendChild(this.clear());
	}
	this.clear = function(){var e1=document.createElement("div");
		e1.className="clearb";
		return e1;
	}
	this.createBtn=function(txtBtn,fct,classN){
		var btn = document.createElement("input");
		btn.type = "button";
		btn.value = txtBtn;
		btn.className = classN;
		btn.onclick = function(){fct();}
		return btn;
	}
	this.RequestAjax=function(data,fct){
		// requete ajax
		if(window.XMLHttpRequest) // FIREFOX
		  xhr = new XMLHttpRequest(); 
		else if(window.ActiveXObject) // IE
		  xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
		else 
		  return(false);
		xhr.onreadystatechange  = function(){ 
			if(xhr.readyState == 4) {
			if(xhr.status == 200){
				boxDialog.writeContent(xhr.responseText,fct);
				return(true);}
			else{alert('Error ' + xhr.status + ' -- ' + xhr.statusText + '\n'+xhr.responseText);
				return(false);}
			}
		};
		xhr.open("GET", "/js/action_js.asp?"+data+"&no_chache="+Math.floor((Math.random()*999999)), true);
		xhr.setRequestHeader("Cache-Control","no-cache");
		xhr.send(null);
	}
	this.SendAjax=function(data,fct){
		// requete ajax
		if(window.XMLHttpRequest) // FIREFOX
		  xhr = new XMLHttpRequest(); 
		else if(window.ActiveXObject) // IE
		  xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
		else 
		  return(false);
		xhr.onreadystatechange  = function(){ 
			if(xhr.readyState == 4) {
			if(xhr.status == 200){
				if (xhr.responseText.toLowerCase().indexOf("ok")!=-1){
				// Reponse Ok
					fct();
				return(true);}
				else{// Reponse ko
				return(false);}
			}
			else{alert('Error ' + xhr.status + ' -- ' + xhr.statusText + '\n'+xhr.responseText);
				return(false);}
			}
		};
		xhr.open("GET", "/js/action_js.asp?"+data+"&no_chache="+Math.floor((Math.random()*999999)), false);
		xhr.setRequestHeader("Cache-Control","no-cache");
		xhr.send(null);
	}
	this.close=function(){
		var ep = document.getElementById("pop_dialog");
		if (ep!=undefined)
			ep.parentNode.removeChild(ep);
	}
	this.loader=function(){
		var myDiv = document.createElement("DIV");
		
		var myLoader = document.createElement("IMG");
		myLoader.src="/images/ajax_loader.gif";
		myLoader.style.verticalAlign="middle";
		
		var myStr = document.createElement("SPAN");
		myStr.innerHTML = strLoadingProgress;
		myStr.style.paddingLeft="10px";
		
		myDiv.appendChild(myLoader);
		myDiv.appendChild(myStr);
		
		return myDiv;
	}
	this.open=function(stringTextTitleBox) {
		this.close();
		var childToAppend=this.loader();
		var NodeNew_content = document.createElement("div");
		NodeNew_content.className = "pop_dialog";
		var NodeNew_a = document.createElement("div");
		NodeNew_a.className = "dialog_popup";
		var NodeNew_b = document.createElement("div");
		NodeNew_b.className = "pop_container";
		var NodeNew_c = document.createElement("div");
		NodeNew_c.className = "pop_content";
		var NodeNew_h = document.createElement("img");
		NodeNew_h.src = "/images/close.png";
		NodeNew_h.onclick = function(){boxDialog.close()};
		var NodeNew_d = document.createElement("H6");
		NodeNew_d.className = "dialog_title";
		var NodeNew_f = document.createElement("span");
		NodeNew_f.innerHTML = stringTextTitleBox;
		var NodeNew_e = document.createElement("div");
		NodeNew_e.className = "dialog_content";
		var NodeNew_g = document.createElement("div");
		NodeNew_g.className = "dialog_body";
		NodeNew_g.id = "dialog_body";
		NodeNew_content.id = "pop_dialog";

		// construct
		NodeNew_d.appendChild(NodeNew_f);
		NodeNew_d.appendChild(NodeNew_h);
		NodeNew_c.appendChild(NodeNew_d);
		NodeNew_e.appendChild(NodeNew_g);
		NodeNew_c.appendChild(NodeNew_e);
		NodeNew_b.appendChild(NodeNew_c);
		NodeNew_g.appendChild(childToAppend);
		NodeNew_a.appendChild(NodeNew_b);
		NodeNew_content.appendChild(NodeNew_a);
		document.body.insertBefore(NodeNew_content,document.body.firstChild);
		
		this.windowCenter('pop_dialog');
	}
	this.windowCenter = function(myId){
		var result=this.returnSizeScreen();
		var myWidth=result[0];
		var myHeight=result[1];
		var elementWidth=document.getElementById(myId).offsetWidth;
		var elementHeight=document.getElementById(myId).offsetHeight;
	
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			var myScrollTop=window.pageYOffset;
			var myScrollLeft=window.pageXOffset;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			var myScrollTop=document.documentElement.scrollTop;
			var myScrollLeft=document.documentElement.scrollLeft;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			var myScrollTop=document.documentElement.scrollTop;
			var myScrollLeft=document.documentElement.scrollLeft;
		}
		//var posY=myScrollTop+myHeight-(myHeight/2)-(elementHeight/2);
		var posY=myScrollTop-(elementHeight/2);
		//var posX=-(elementWidth/2);
		document.getElementById(myId).style.top=posY+"px";
		//document.getElementById(myId).style.left=posX+"px";
	}
	this.returnSizeScreen = function(){
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		}
		var resultats = new Array(myWidth,myHeight);
		return resultats;
	}
}
function sendCoordBankByEmail(elm, elmCmd){
	elm.disabled=true;
	elm.style.backgroundColor="#666";
	boxDialog.SendAjax("action=sendcoordbankbyemail&cmd="+elmCmd,function(){elm.style.display='none'; window.location.href='/';});
//	elm.style.display="none";
}
var boxDialog=new boxDialogPopup();
function gId(idName){return document.getElementById(idName);}
function include(fileName){document.write("<script type='text/javascript' src='"+fileName+"'></script>" );} 

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


function Get_Cookie( 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;
	}
}
