function replaceSubstring (inputString, badString,
                          goodString, caseSensitive) {
  fixedReplace = "";
  UI = inputString;
  UB = badString;
  if ((caseSensitive != 1) && (caseSensitive != true)) {
  UI = inputString.toUpperCase();
     UB = badString.toUpperCase();
     }
  badEnd = -1;
  badLoc = UI.indexOf(UB);
  if (badLoc != -1) {
     for (x=1; (badLoc != -1); x++) {
        fixedReplace = fixedReplace + 
                       inputString.substring((badEnd +
                       1), badLoc) + goodString
        badEnd = badLoc + UB.length - 1;
        badLoc = UI.indexOf(UB, (badLoc + 1)); }
     fixedReplace = fixedReplace + 
                    inputString.substring((badEnd + 1),
                    inputString.length); }
     else { fixedReplace = inputString;    }
return fixedReplace;
}


function eliminar(ruta) {
	conf=confirm("Êtez-vous certain de vouloir supprimer cet élément?");
	if(conf)
		location.href=ruta;	
}
function FitPic() {
	var arrTemp=self.location.href.split("?"); 
	var picUrl = (arrTemp.length>0)?arrTemp[1]:""; 
	var NS = (navigator.appName=="Netscape")?true:false; 	 
	iWidth = (NS)?window.innerWidth:document.body.clientWidth; 
	iHeight = (NS)?window.innerHeight:document.body.clientHeight; 
	iWidth = (document.images[0].width + 100) - iWidth; 
	iHeight = (document.images[0].height +100)- iHeight; 
	window.resizeBy(iWidth, iHeight); 
	self.focus(); 
}; 

function popImage(imageURL,imageTitle)
{
// Set the horizontal and vertical position for the popup
PositionX = 100;
PositionY = 100;
// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)
defaultWidth  = 500;
defaultHeight = 500;
// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows

var AutoClose = false;

// Do not edit below this line...
// ================================
if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;	
if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}
with (imgWin.document){
writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
writeln('width=120-(document.body.clientWidth-document.images[0].width);');
writeln('height=100-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
else writeln('</head><body scroll="no" onload="reSizeToImage();doTitle(); self.focus()" onblur="self.close()">');
writeln('<img name="George" src="'+imageURL+'" style="display:block"></body></html>');
close();		
}}


function OpenNewWindow(url,name,w,h) 
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	var v = window.open(url,name,'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width='+w+',height='+h+',top='+wint+',left='+winl);
}

function calcular(precio,cantidad)
{
 var valor = precio * cantidad;
 
 x=cantidad.split('.');
 if(x.length > 1)
 {
  if(x[1].length > 2)  	
   document.frm_carro.txt_cantidad.value=redondear(cantidad);
 }
 document.frm_carro.txt_stotal.value=redondear(valor); 	
}

function calcular2(cantidad)
{
 var precio=document.getElementById('idTarifs').value;
 var valor = precio * cantidad;
 
 x=cantidad.split('.');
 if(x.length > 1)
 {
  if(x[1].length > 2)  	
   document.frm_carro.txt_cantidad.value=redondear(cantidad);
 }
 
 //document.frm_carro.txt_stotal.value=redondear(valor);
 document.getElementById("txt_stotal").value=redondear(valor);
 //document.getElementById('subTotal').innerHTML=redondear(valor);
 
}

function calcular_group(precio,cantidad,indice,estado)
{
 var valor = precio * cantidad;
 
 x=cantidad.split('.');
 if(x.length > 1)
 {
  if(x[1].length > 2)
   document.getElementById('cant'+indice).value = redondear(cantidad);
 } 
 document.getElementById('stotal'+indice).value=redondear(valor);
}

function teclear(e,valor) 
{
tecla=(document.all) ? e.keyCode : e.which;
if(tecla==46)
 if(valor.indexOf('.') != "-1")
  return false;
	 
if(tecla<46 || tecla>57 || tecla==47)
return false;

}

function teclear_punto(e) 
{
tecla=(document.all) ? e.keyCode : e.which;
if((tecla>0 && tecla<46) || tecla>57 || tecla==47)
return false;
}

function cargar_region(pais)
{
  cadena=pais;
  jsrsExecute("./include/cargar_region.php",enviar_region,"confirmar",cadena);
}

/*function radio_button_checker(form)
{
var radio_choice = -1;

for (counter = 0; counter < form.pagos.length; counter++) {
 if (form.pagos[counter].checked){
  radio_choice = form.pagos[counter].value; 
 }
}

return radio_choice;
}

function radio_button_checker(form){
	var radio_choice = -1;
	for (counter = 0; counter < form.pagos.length; counter++) {
		if (form.pagos[counter].checked){
			radio_choice = form.pagos[counter].value; 
		}
	}
	return radio_choice;
}*/
function radio_button_checker(form){
	var radio_choice = -1;
	if(form.pagos!=null){//si existe
		if(form.pagos.length==undefined){
			radio_choice = form.pagos.value;
		}else{
			for(counter = 0; counter < form.pagos.length; counter++){
				if(form.pagos[counter].checked){
					radio_choice = form.pagos[counter].value; 
				}
			}
		}
	}
	return radio_choice;
}

function radio_button_checker1(form)
{
	var radio_choice = -1;
	
	for (counter = 0; counter < form.envios.length; counter++) 
	{
	  if (form.envios[counter].checked) 
	   radio_choice = form.envios[counter].value; 
	}

return radio_choice;
}

function metodos_envio(pago,region,lang,pais,cpostal)
{
  cadena=pago+","+region+","+lang+","+pais+","+cpostal;
  document.frm_inscripcion.pagosx.value=pago;
  jsrsExecute("./include/metodos_envio.php",fin_metodo_envio,"confirmar",cadena);
}

function fin_metodo_envio(cadena)
{
 cadena = cadena.split('-');
 document.frm_inscripcion.envios.value = cadena[0];
 document.frm_inscripcion.cantenvios.value = cadena[1];
}

function metodos_envio2(pago,region,lang,pais,cpostal){
	cadena=pago+","+region+","+lang+","+pais+","+cpostal;
  	document.frm_inscripcion.pagosx.value=pago;
  	jsrsExecute("./include/metodos_envio.php",fin_metodo_envio2,"confirmar",cadena);
}

function fin_metodo_envio2(cadena){
	cadena = cadena.split('-');
	document.frm_inscripcion.envios.value = cadena[0];
	document.frm_inscripcion.cantenvios.value = cadena[1];
	verificar_promocion();
}

function verificar_partner()
{
  cadena=document.frm_inscripcion.txt_codepartner.value;
  
  if(cadena)
   jsrsExecute("./include/verificar_partner.php",fin_verificar,"confirmar",cadena);
  else
   fin_verificar("y");
}

function fin_verificar(cadena)
{
if(cadena=="y")
{
	form=document.frm_inscripcion;
	email = form.txt_email.value;
	bmail = form.txt_bmail.value;
	cadena=email+","+bmail;
	jsrsExecute("./include/verificar_mail.php",fin_mail,"confirmar",cadena);
}
else {
 alert(document.frm_inscripcion.alert_codepartner.value);	 
}
}

function fin_mail(cadena)
{
if(cadena=="y")
{
form=document.frm_inscripcion;
if(validar_inscripcion()) {
  form.submit();	
}
}
else {
 alert(document.frm_inscripcion.alert_exemail.value);	 
}
}



function verificar_promocion()
{
	
  cadena=document.frm_inscripcion.txt_promocion.value;
  
  var detect=navigator.userAgent.toLowerCase();  
  if(detect.indexOf("msie")!= -1){
   jsrsExecute("verificar_codigo.php",finalizar_pago,"confirmar",cadena);
  }else{
   jsrsExecute("verificar_codigo.php",finalizar_pago,"confirmar",cadena);
  }
}

function finalizar_pago(cadena){
	cadena = cadena.split('-');
	var cadenas="";
	form=document.frm_inscripcion;	
	if(cadena[0]=="y"){
		/*var option=radio_button_checker(form);
		if(document.frm_inscripcion.cantenvios.value > 1){
			document.frm_inscripcion.action="midcompte.php";
		}else{*/
			if(document.frm_inscripcion.pagosx.value==1) {
          		//ruta que debe estar activa cuando PAYPAL está en modo producción
				document.frm_inscripcion.action="https://www.paypal.com/cgi-bin/webscr";
				
				//ruta que debe estar activa cuando PAYPAL está en modo test
				//document.frm_inscripcion.action="https://www.sandbox.paypal.com/cgi-bin/webscr";
			}else{
		  		document.frm_inscripcion.action="endcompte.php";
			}
		/*}*/
		if(validar_pedido()){
			if(cadena[1] > 0){//si monto > 0
				//document.frm_inscripcion.MONTANT.value = cadena[1];
				document.frm_inscripcion.amount.value = cadena[2];
				alert(document.frm_inscripcion.amount.value);
				cadenas = document.frm_inscripcion.custom.value;
				//cadenas = replaceSubstring (cadenas,"[region]",document.frm_inscripcion.slt_region.value,false)
				document.frm_inscripcion.custom.value = cadenas;
				var cp = document.frm_inscripcion.txt_codigopostal.value;
			}
			if(document.frm_inscripcion.envios.value=="1" && cp.substring(0,2)!='75' && document.frm_inscripcion.pagosx.value==1){
				alert("La livraison est seulement disponible à Paris et au code postal 75xxx");
			}else{
				cadenas = document.frm_inscripcion.custom.value;
				cadenas = replaceSubstring(cadenas,"[pagosx]",document.frm_inscripcion.pagosx.value,false);
				cadenas = replaceSubstring(cadenas,"[txt_promocion]",document.frm_inscripcion.txt_promocion.value,false);
				cadenas = replaceSubstring(cadenas,"[envios]",document.frm_inscripcion.envios.value,false);
				cadenas = replaceSubstring(cadenas,"[pais]",document.frm_inscripcion.slt_pais.value,false);
				//cadenas = replaceSubstring (cadenas,"[region]",document.frm_inscripcion.slt_region.value,false)
				cadenas = replaceSubstring(cadenas,"[villa]",document.frm_inscripcion.txt_villa.value,false);
				cadenas = replaceSubstring(cadenas,"[direccion]",document.frm_inscripcion.txt_direccion.value,false);
				cadenas = replaceSubstring(cadenas,"[cpostal]",document.frm_inscripcion.txt_codigopostal.value,false);
				cadenas = replaceSubstring(cadenas,"[telefono]",document.frm_inscripcion.txt_telefono.value,false);
				document.frm_inscripcion.custom.value = cadenas;

				document.frm_inscripcion.submit();
			}
	
		}
	}else{
		alert(document.frm_inscripcion.alert_promocion.value);	 
	}
}

function finalizar_pago2(){

	form=document.frm_inscripcion;
	var option=radio_button_checker1(form);
	document.frm_inscripcion.RETOUR.value = document.frm_inscripcion.RETOUR.value + option;
	if(form.pagos.value==1) {
		//ruta que debe estar activa cuando k&P está en modo producción
		//form.action="https://www.klikandpay.com/paiement/check.pl";
		
		//ruta que debe estar activa cuando k&P está en modo test
		form.action="https://www.klikandpay.com/paiementtest/check.pl";

	}else {		
		form.action="endcompte.php";
	}
	if(validar_pedido()){ 
    		var cp = document.frm_inscripcion.txt_codigopostal.value;	
		if(radio_button_checker1(document.frm_inscripcion)=="1" && cp.substring(0,2)!='75' && form.pagos.value==1)
			alert("La livraison est seulement disponible à Paris et au code postal 75xxx ");
		else
	 		form.submit(); 

	}
}
/**/
function validar_fecha_reserva(){
	if(document.frm_carro.reserva.value==1){
		fechini=format_fecha(document.getElementById("fecinicio").value);
		fechfin=format_fecha(document.getElementById("fecfin").value);
		freserva=document.frm_carro.txtFreserva.value;
		if(Trim(document.frm_carro.txtFreserva.value)==""){
				alert('Vous debez entrer a date');
				document.frm_carro.txtFreserva.focus();
				return false;
		}else{
			cadena=fechini+","+fechfin+","+freserva;
			alert(cadena);
			jsrsExecute("./include/validar_fecha_reserva.php",fin_fecha_reserva,"confirmar",cadena);
			/*var detect=navigator.userAgent.toLowerCase();  
  			if(detect.indexOf("msie")!= -1){  
			   jsrsExecute("validar_fecha_reserva.php",fin_fecha_reserva,"confirmar",cadena);
			}else{
			   jsrsExecute("./include/validar_fecha_reserva.php",fin_fecha_reserva,"confirmar",cadena);
			}*/
		}
	}else{
		if(validar_carro()){
			document.frm_carro.submit();	
		}
	}
}
function fin_fecha_reserva(cadena){
	/*fechini=document.frm_carro.fecinicio.value;
	fechfin=document.frm_carro.fecfin.value;*/
	alert(cadena);
	/*if(cadena!=0){
		if(cadena==1){
			alert("Vous debez entrer a date compris entre "+fechini+" et "+fechfin);
			document.frm_carro.txtFreserva.focus();
			return false;
		}else{
			alert("Debe ingresar la misma fecha de inicio");
			document.frm_carro.txtFreserva.focus();
			return false;
		}
	}else{
		if(validar_carro()){
			document.frm_carro.submit();	
		}
	}*/
}
/***/
function enviar_region(cadena)
{
 var form = document.frm_inscripcion;
 
  region=cadena.split(",");
 
  cantidad=region.length;
  
  form.slt_region.options.length=1;
  if(cantidad==1)
  {
   otraopcion=new Option("---------","");
   form.slt_region.options[0]= otraopcion;
  }

 for(x=0; x < cantidad-1; x++)
 { 
  	otraopcion=new Option(region[x],region[x]);
	form.slt_region.options[x]= otraopcion;
 }
}

function redondear(num)
{
	num *= 100;
    num = Math.round(num);
    num /= 100;
	
	return num;
}

function enviar_compra(data)
{
  if(validar_carrogroup())
   {
    if(Trim(document.frm_carro.idusr.value)=="")
	 document.frm_carro.action="loginc.php?"+data;
	else
     document.frm_carro.action="compte.php?"+data;
    document.frm_carro.submit();
   }
}

function check_limit(prodid,cantidad,pos,estado,divp,divr)
{
  cadena=prodid+","+cantidad+","+pos+","+estado+","+divp+","+divr;
  jsrsExecute("./include/check_limit.php",end_check,"confirmar",cadena);	
}

function end_check(cadena)
{

// 1: Producto
// 1: Reserva

 form = document.frm_carro;
 valor=cadena.split("-");
 document.getElementById('cant'+valor[1]).focus();
 if(valor[0]=="no") 
 {
  if(valor[4]=="0")	 
   alert(form.txt_masde.value + " "+document.getElementById('stock'+valor[1]).value+" " + " items");
  else
   alert(form.txt_masde.value + " "+valor[6]+" " + " items");
   
  form.reset();
 }
 else
 {
  
  if(valor[7]){
  
  	if(Trim(valor[4])=="0"){
		v=valor[3].replace('.',',');
   	 	document.getElementById("div_st1").innerHTML="<strong>"+v+" &euro;</strong>";
  	}else{
		v=valor[3].replace('.',',');
  	 	document.getElementById("div_st2").innerHTML="<strong>"+v+" &euro;</strong>";
  	}
  
  }
  
  v5=valor[5].replace('.',',');
  document.getElementById("div_stt").innerHTML="<strong>"+v5+" &euro;</strong>";
 }
}
function validar_fechaa(control){
	fecinicio=format_fecha(Trim(document.getElementById(fecinicio).value));
	fecfin=format_fecha(Trim(document.getElementById(fecfin).value));
	freserva=Trim(document.getElementById(txtFreserva).value);

	var msegActual = fecinicio.getTime();
	var msegFecha1 = fecinicio.getTime();
	
	alert(msegActual+msegFecha1);
}

function activaFormBeneficiario(obj){
	form = document.frm_carro;
	if(obj.checked==true){
		document.getElementById("infobene").style.display='';
		document.getElementById("idtext").innerHTML='Je souhaite offrir ma commande &agrave; un ami :';
	}else{
		form.txtMailAmi2.value="";
		document.getElementById("infobene").style.display='none';
		document.getElementById("idtext").innerHTML='Je souhaite offrir ma commande &agrave; un ami';
	}
}

function activaFormBeneficiario2(obj){
	form = document.frm_carro;
	if(obj.checked==true){
		
		document.getElementById("infobene2").style.display='';
		document.getElementById("idtext2").innerHTML='Je souhaite offrir ma commande &agrave; un ami :';
	}else{
		form.txtMailAmi.value="";
		document.getElementById("infobene2").style.display='none';
		document.getElementById("idtext2").innerHTML='Je souhaite offrir ma commande &agrave; un ami';
	}
}

function desactivarFecha(obj){
	form = document.frm_carro;
	if(obj.checked==true){
		document.frm_carro.txtFreserva.disabled="true";
	}
	if(obj.checked==false){
		document.frm_carro.txtFreserva.disabled="";	
	}
}

function chkFalse(obj){
	if(obj.checked==false){
		document.frm_carro.ckbox_mois.checked="false";
	}
}

function verificar_inscripcion(){
	form=document.frm_inscripcion;
	/*human = form.txt_human.value;
	if(human != 5) {
		alert("2 + 3 = 5");
		return(false);
	}*/
	partner = form.txt_codepartner.value;
	login = form.txt_login.value;
	mail = form.txt_email.value;
	cadena=partner+","+login+","+mail;
	jsrsExecute("./include/verificar_inscripcion.php",fin_verificar_inscripcion,"confirmar",cadena);
}

function fin_verificar_inscripcion(param){
	if(param==4){//todo ok
		form=document.frm_inscripcion;
		if(validar_inscripcion()) {
			form.submit();	
		}
	}else{
		if(param==1){//partner inexistente
			alert(document.frm_inscripcion.alert_codepartner.value);
		}else if(param==2){//login existente
			alert("login existe dejá");
		}else{//3.-email existente
			alert(document.frm_inscripcion.alert_exemail.value);
		}
	}
}