var array_tipo_paquete 	 = new Array(num_paquetes);
var array_id_paquete	 = new Array(num_paquetes);
var array_cantidad 		 = new Array(num_paquetes);
var array_idioma 		 = new Array(num_paquetes);
var num_paquetes 	  	 = 0;


function createCookie(name,value,days) 
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
	{ 
		var caducidad = new Date(1970, 1, 1, 0, 0, 0);
		var expires = -1;
		var expires = "";
	}
	
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

     
function agregar_paquete(tipo_paquete ,id_paquete ,idioma, nombre_paquete)
{
//	alert(tipo_paquete + " " + id_paquete + " " + idioma + " " + nombre_paquete);
	var count = 0;
	var x = '';
	var y = '';
	var mensaje_pop;
	if (readCookie("carrito_tempotours") != null)
	{
		restablecer_cookie('carrito_tempotours');
	}
    if (num_paquetes > 0)
    {
	    for (var i=0;i < num_paquetes ;i++) 
	    {
	    	x = array_id_paquete[i];
	    	y = array_idioma[i];
	   		if (x == id_paquete && y == idioma) 
	   		{
				array_cantidad[i] = parseInt(array_cantidad[i]) + 1;
				count = -1;
	   		}
	    }
	}
	
	if (count == 0) 
	{
		array_tipo_paquete[num_paquetes] = tipo_paquete;
    	array_id_paquete[num_paquetes] 	 = id_paquete;
    	array_cantidad[num_paquetes] 	 = 1;
    	array_idioma[num_paquetes]		 = idioma;
	    num_paquetes++;
	}

	imprimir_carrito();
	window.close();
}

function restablecer_cookie(name)
{
	if (num_paquetes == 0) 
	{
		var myCookie = readCookie(name);
		var linea 	 = myCookie.split(":");
		for( var k in linea ) 
		{
			var  hasta1 		= linea[k].indexOf(',');
			var  tipo_paquete  	= linea[k].substring(0,hasta1);
			var  medio  		= linea[k].substring(hasta1 + 1, linea[k].length);
			var  hasta2 		= medio.indexOf(',');
			var  id_paquete  	= medio.substring(0 ,hasta2);			
			var  interm 		= medio.substring(hasta2 + 1 , linea[k].length);
			var  hasta3 		= interm.lastIndexOf(',');
			var  idioma			= interm.substring(0 ,hasta3);
			var  cantidad		= interm.substring(hasta3 + 1, linea[k].length);
			
			array_tipo_paquete[num_paquetes] = tipo_paquete;
			array_id_paquete[num_paquetes] 	 = id_paquete;
			array_cantidad[num_paquetes] 	 = cantidad;
    		array_idioma[num_paquetes]		 = idioma;
    		num_paquetes++;
		}
	}
}


function imprimir_carrito()
{
	var msg = "";
	for (var i=0;i < num_paquetes;i++) 
	{
		if (array_cantidad[i] > 0)
		{	
			msg += array_tipo_paquete[i] + ',' + array_id_paquete[i] + ',' + array_idioma[i] + ',' + array_cantidad[i] + ':';
		}
	}

	msg = msg.substring(0, msg.length - 1);
	createCookie("carrito_tempotours" , msg);
}


function aumentar_cantidad(codigo, idioma, modalidad, page_id, actividad) 
{	
	restablecer_cookie('carrito_tempotours');

	for (i=0; i < num_paquetes; i++)
	{		
		if ( array_id_paquete[i] == codigo && array_idioma[i] == idioma)
		{
			valor_mod = parseInt(document.getElementById("valor_sube" + codigo + "-" + idioma).innerHTML);
			document.getElementById("valor_sube" + codigo  + "-" + idioma).innerHTML=valor_mod+1;			
			array_cantidad[i] = valor_mod + 1;
		}
	}

	imprimir_carrito();
	
	if (page_id == 1){ page = 'itinerario.php'; }
	if (page_id == 2){ page = 'descripcion.php'; }
	if (page_id == 3){ page = 'actividades_det.php'; }
	if (page_id == 4){ page = 'extensiones.php'; }
	if (page_id == 5){ page = 'viajepordestino.php'; }

}

function disminuir_cantidad(codigo, idioma, modalidad, page_id, actividad) 
{
	var total = 0;
	var totalxone = 0;
	
	if (page_id == 1){ page = 'itinerario.php'; }
	if (page_id == 2){ page = 'descripcion.php'; }
	if (page_id == 3){ page = 'actividades_det.php'; }
	if (page_id == 4){ page = 'extensiones.php'; }
	if (page_id == 5){ page = 'viajepordestino.php'; }

	restablecer_cookie('carrito_tempotours');
	
	for (var i=0; i < num_paquetes; i++)
	{
		if ( array_id_paquete[i] == codigo  && array_idioma[i] == idioma)
		{
			totalxone += parseInt(array_cantidad[i]);
			valor_mod = parseInt(document.getElementById("valor_sube" + codigo + "-" + idioma).innerHTML);
			array_cantidad[i] = valor_mod - 1;

			if (totalxone > 1 )
			{
				document.getElementById("valor_sube" + codigo  + "-" + idioma).innerHTML=valor_mod-1;
			}
		}
		total += parseInt(array_cantidad[i]);
	}

	if (total <= 0)
	{
		deleteCookie('carrito_tempotours','/');
		window.location.href='ver_carrito.php?page=' + page + '&codigo='+ codigo + '&idioma=' + idioma + '&modalidad=' + modalidad  + '&actividad=' + actividad;
	}
	
	if (totalxone <= 0)
	{
		imprimir_carrito();
		window.location.href='ver_carrito.php?page=' + page + '&codigo='+ codigo + '&idioma=' + idioma + '&modalidad=' + modalidad  + '&actividad=' + actividad;
	}

	imprimir_carrito();
}

function eliminar_paquete( codigo, idioma, modalidad, page_id, actividad)
{
	var total = 0;
	restablecer_cookie('carrito_tempotours');
	
	for (var i=0; i < num_paquetes; i++)
	{
		if ( array_id_paquete[i] == codigo && array_idioma[i] == idioma)
		{
			array_cantidad[i] = 0 ;
		}
		total += parseInt(array_cantidad[i]);
	}
	total > 0 ? imprimir_carrito() : deleteCookie('carrito_tempotours','/');

	page = '';
	
	if (page_id == 1){ page = 'itinerario.php'; }
	if (page_id == 2){ page = 'descripcion.php'; }
	if (page_id == 3){ page = 'actividades_det.php'; }
	if (page_id == 4){ page = 'extensiones.php'; }
	if (page_id == 5){ page = 'viajepordestino.php'; }

	window.location.href='ver_carrito.php?page=' + page + '&codigo='+ codigo + '&idioma=' + idioma + '&modalidad=' + modalidad + '&actividad=' + actividad;
}

function eliminar_paquetes()
{
	var myForm  = document.getElementById('myForm');
	var cuentaChecked = 0;

	for(var i=0;i<myForm.elements.length;i++) 
	{
		if(myForm.elements[i].checked && myForm.elements[i].type == 'checkbox')
		{
			cuentaChecked += 1;
		}
	}
	
	if(cuentaChecked == 0) 
	{
		window.alert("seleccione al menos un item a eliminar");
		return false;
	}
	
	for(var i=0;i<myForm.elements.length;i++)
	{
		if(myForm.elements[i].name.substring(0,10) == 'delete_id_' && myForm.elements[i].type == 'checkbox' && myForm.elements[i].checked )
		{
			// delete_id_43,1,1,3,43
			var pbloque	= myForm.elements[i].name.substring(10, myForm.elements[i].name.length);
			var pcoma	= pbloque.indexOf(',');
			
			var sbloque	= pbloque.substring(pbloque.indexOf(',') + 1, myForm.elements[i].name.length);
			var scoma	= sbloque.indexOf(',');
			
			var tbloque	= sbloque.substring(sbloque.indexOf(',') + 1, myForm.elements[i].name.length);
			var tcoma	= tbloque.indexOf(',');
			
			var cbloque	= tbloque.substring(tbloque.indexOf(',') + 1, myForm.elements[i].name.length);
			var ccoma	= cbloque.indexOf(',');
			
			var qbloque = cbloque.substring(cbloque.indexOf(',') + 1, myForm.elements[i].name.length);
			var qcoma	= qbloque.indexOf(',');

			var codigo  = pbloque.substring(0,pcoma);
			var idioma	= sbloque.substring(0,scoma);
			var cantid	= tbloque.substring(0,tcoma);
			var pageid	= cbloque.substring(0,ccoma);
			var activi	= qbloque;
					
			eliminar_paquete(codigo, idioma, 0, pageid, activi);
		}
	}
}

function deleteCookie(name, path) 
{
	var c = "";
	c += name + "=";
	c += "; path=" + path;
	var caducidad = new Date(1970, 1, 1, 0, 0, 0);
	c += "; expires=" + caducidad.toGMTString();
	document.cookie = c;
}


 function submitForm(lang, var_cookie)
 {
 	if (var_cookie == 1)
 	{
	 	var myForm = document.getElementById('myForm');	 	
	 	var Msg_FirstName  			= lang == 1 ? "Ingrese su nombre" : "Write Name"
	 	var Msg_LastName 			= lang == 1 ? "Ingrese su apellido" : "Write LastName";
	 	var Msg_Country 			= lang == 1 ? "Elija su pais" : "Choose yuor Country";
	 	var Msg_City 				= lang == 1 ? "ingrese la ciudad" : "Write ciudad";
	 	var Msg_Phone 				= lang == 1 ? "ingrese su telefono" : "Write Your Phone";
	 	var Msg_Email	  			= lang == 1 ? "ingrese su E-mail" : "Write your E-mail";	 	
		var Msg_InvalidEmail		= lang == 1 ? "E-mail invalido" : "Invalid E-mail";
	 	var Msg_HowDidYouFindUs 	= lang == 1 ? "Escoja opción en Cómo nos encontró" : "Choose option in How Did You Find Us?";
	 	var Msg_HowDidYouFindUs2	= lang == 1 ? "Cómo nos encontró" : "How Did You Find Us?";
	 	var Msg_Typeofaccommodation = lang == 1 ? "Elija el tipo de Alojamiento" : "Choose Type of accommodation";
	 	var Msg_Tourguide	  		= lang == 1 ? "Elija el tipo de Servicio" : "Choose Tourguide:";
	 	var Msg_Month  				= lang == 1 ? "Elija el mes" : "Choose Month"; 
	 	var Msg_Staying				= lang == 1 ? "Elija el Numero de dias" : "Choose number of days";
	 	var Msg_Year  				= lang == 1 ? "Elija el año" : "Choose Year";	 	
	 	var Msg_Adults			 	= lang == 1 ? "Ingrese el numero de adultos" : "Write the number of Adults";
	 	var Msg_Children 			= lang == 1 ? "Ingrese el numero de niños" : "Write the number of Children";
	 	var Msg_Comentary 			= lang == 1 ? "Ingrese el comentario" : "Write commentary";
	 	var Msg_npasajeros 			= lang == 1 ? "Ingrese el numero de pasajeros en adultos o niños" : "Write number of passengers in adults or children";

		if(trim(document.getElementById('pr_nombre').value)=='')
		{
			window.alert(Msg_FirstName);
			document.getElementById('pr_nombre').focus();
			return;
		}
	
		if(trim(document.getElementById('pr_apellido').value)=='')
		{
			window.alert(Msg_LastName);
			document.getElementById('pr_apellido').focus();
			return;
		}
	
		if(trim(document.getElementById('pr_pais').value)=='')
		{
			window.alert(Msg_Country);
			document.getElementById('pr_pais').focus();
			return;
		}
	
		if(trim(document.getElementById('pr_ciudad').value)=='')
		{
			window.alert(Msg_City);
			document.getElementById('pr_ciudad').focus();
			return;
		}
			
	 	if(trim(document.getElementById('pr_telefono').value)=='')
		{
			window.alert(Msg_Phone);
			document.getElementById('pr_telefono').focus();
			return;
		}
		
		if(trim(document.getElementById('pr_correo').value)=='')
		{
			window.alert(Msg_Email);
			document.getElementById('pr_correo').focus();
			return;
		}
	
		if ( !checkemail(trim(document.getElementById('pr_correo').value )))
		{
			window.alert(Msg_InvalidEmail);		
			document.getElementById('pr_correo').focus();
			return;
		}
		
	 	if(document.getElementById('pr_conocistes').selectedIndex == 0)
		{
			window.alert(Msg_HowDidYouFindUs);
			document.getElementById('pr_conocistes').focus();
			return;
		}
		
		if (document.getElementById("Otros") != null )
		{
			var valOtros = document.getElementById("Otros").value;
			if (valOtros == ' Escriba aqui ...' || valOtros == ' Write here ...' || valOtros == '')
			{
				window.alert(Msg_HowDidYouFindUs2);
				document.getElementById('Otros').focus();
				return;
			}
		}
		
		if(document.getElementById('pr_tipoalojamiento').selectedIndex == 0)
		{
			window.alert(Msg_Typeofaccommodation);
			document.getElementById('pr_tipoalojamiento').focus();
			return;
		}
		 	
	 	if(document.getElementById('pr_tiposervicio').selectedIndex == 0)
		{
			window.alert(Msg_Tourguide);
			document.getElementById('pr_tiposervicio').focus();
			return;
		}
		
	 	if(document.getElementById('pr_mes').selectedIndex == 0)
		{
			window.alert(Msg_Month);
			document.getElementById('pr_mes').focus();
			return;
		}
		
		if(document.getElementById('pr_estadia').selectedIndex == 0)
		{
			window.alert(Msg_Staying);
			document.getElementById('pr_estadia').focus();
			return;
		}
			
	 	if(document.getElementById('pr_ano').selectedIndex == 0)
		{
			window.alert(Msg_Year);
			document.getElementById('pr_ano').focus();
			return;
		}
	
	 	if
	 	(
	 		document.getElementById('pr_adultos').selectedIndex==0 &&
	 		document.getElementById('pr_ninos').selectedIndex==0
	 	)
		{
			window.alert(Msg_npasajeros);
			document.getElementById('pr_adultos').focus();
			return;
		}
		
		myForm.action = 'graba_cotizador.php';
		myForm.submit();
 	}
 	else
 	{
	 	var Msg_noitems = lang == 1 ? "tienes que agregar al menos un producto al carrito" : "you must add at least a product to the cart";
 		window.alert(Msg_noitems);
 	}
 }
 
function clearForm()
{ 	
	document.getElementById('pr_nombre').value='';
	document.getElementById('pr_apellido').value='';
	document.getElementById('pr_pais').value='';
	document.getElementById('pr_ciudad').value='';	
 	document.getElementById('pr_telefono').value='';	
	document.getElementById('pr_correo').value='';
 	document.getElementById('pr_conocistes').selectedIndex = 0;
	document.getElementById('pr_tipoalojamiento').selectedIndex = 0;	 	
	document.getElementById('pr_tiposervicio').selectedIndex = 0
	document.getElementById('pr_mes').selectedIndex = 0
	document.getElementById('pr_estadia').value=''
	document.getElementById('pr_ano').selectedIndex = 0
 	document.getElementById('pr_adultos').value='';
	document.getElementById('pr_ninos').value='';
	document.getElementById('pr_comentario').value='';
	var contenedor = document.getElementById("inputOtros");
	if (contenedor.firstChild != null ) {
		contenedor.removeChild(contenedor.firstChild);
	}
	document.getElementById('pr_tipoalojamiento').focus();
}

function ltrim(str)
{
	while(""+str.charAt(0)==" "){
		str=str.substring(1,str.length);}
	return str; 
}

function reverse(str)
	{
	var reversedstr = "";
	var strArray;
	strArray = str.split("");
	for(var i = str.length -1 ; i >= 0 ; i--)
	{
		reversedstr += strArray[i];
	}
	return reversedstr;
}

function trim(str)
{
	str = ltrim(str);
	str = reverse(str);
	str = ltrim(str);
	str = reverse(str);
	return str; 
}

function checkemail(str)
{
	var filter=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/
	return (filter.test(str))
}

function select_all()
{
	var myForm  = document.getElementById('myForm');

	for (i=0;i<myForm.elements.length;i++) 
	{
		if ( myForm.elements[i].type == 'checkbox' && myForm.elements[i].name != 'chk_select_all')
		{
			if (myForm.chk_select_all.checked == 1)
			{
				myForm.elements[i].checked = 1;
				myForm.btn_eliminar.disabled = 0;
			}
			else
			{
				myForm.elements[i].checked = 0;
				myForm.btn_eliminar.disabled = 1;
			}
		}
	}
}

function checked_first()
{
	var myForm  = document.getElementById('myForm');
	var chkForm = 0;
	var chkMark = 0;

	for(var i=0;i<myForm.elements.length;i++){
		if( myForm.elements[i].name.substr(0,10)=='delete_id_' && myForm.elements[i].type=='checkbox')
		{
			if(myForm.elements[i].checked)
			{
				chkMark += 1;
			}
			
			chkForm += 1;
		}
	}

	myForm.btn_eliminar.disabled  = chkMark >= 1 ? 0 : 1;
}

function ContinueBuying(page, codigo, idioma, modalidad, actividad)
{
	if(page=='descripcion.php'){
		var URLFRIENDLY = 'descripcion-codigo-'+codigo+'-idioma-'+idioma+'-modalidad-'+modalidad+'.htm';
	}
	else if (page=='itinerario.php'){
		var URLFRIENDLY = 'itinerario-codigo-'+codigo+'-idioma-'+idioma+'-modalidad-'+modalidad+'.htm';
	}
	else if (page=='actividades_lista.php'){
		var URLFRIENDLY = 'actividades_lista-codigo-'+codigo+'-idioma-'+idioma+'-modalidad-'+modalidad+'.htm';
	}
	else if (page=='extensiones.php'){
		
/* 
   RewriteRule extensiones-actividad-(.*)-modalidad-(.*)-idioma-(.*)-codigo-(.*)\.htm$ extensiones.php?actividad=$1&modalidad=$2&idioma=$3&codigo=$4

   */
		var URLFRIENDLY = 'extensiones-actividad-'+actividad+'-modalidad-'+modalidad+'-idioma-'+idioma+'-codigo-'+codigo+'.htm';
	}else{
		var URLFRIENDLY = page + '?codigo='+ codigo + '&idioma=' + idioma + '&modalidad=' + modalidad + '&actividad=' + actividad;
	}
	window.location.href = URLFRIENDLY;
}

function crearInputByOtros(obj, idioma)
{
	var contenedor = document.getElementById("inputOtros");
	if (contenedor.firstChild != null ) {
		contenedor.removeChild(contenedor.firstChild);
	}

	if (obj.selectedIndex == 6){
		var nuevoInput = document.createElement("input");
		var nombreInput= "Otros";
		nuevoInput.setAttribute("type","text");
		nuevoInput.setAttribute("name", nombreInput);
		nuevoInput.setAttribute("id",nombreInput);
		nuevoInput.setAttribute("size","41");
		var msgText = idioma==1 ? ' Escriba aqui ...' : ' Write here ...';
		nuevoInput.setAttribute("value",msgText);
		nuevoInput.onfocus = function() {clearValue(nuevoInput)};
		contenedor.appendChild(nuevoInput);
	}
}

function clearValue(obj)
{
	obj.value = '';
}

function popupWindow(mypage, myname, w, h, scroll,  idioma, mensaje) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	var mensaje = escape(mensaje);
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',menubar=no,status=no,resizable=no,modal=yes';
	win = window.open(mypage + '?idioma=' + idioma + '&mensaje=' + mensaje, myname, winprops);
	if (!win.opener) {
		win.opener = self;
	}
	
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function popupWindow_car(tipo, codigo, idioma, nombre) {
	//alert(tipo+", "+codigo+", "+idioma+", "+nombre);
	var mypage = 'pop_carrito.php';
	var myname = 'mensaje';
	var w=380;
	var h=115;
	var scroll = 'no';
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	var url = mypage + '?tipo=' + tipo + '&codigo=' + codigo + '&idioma=' + idioma + '&nombre=' + escape(nombre);
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',menubar=no,status=no,resizable=no,modal=yes';
	win = window.open(url, myname, winprops);
	if (!win.opener) {
		win.opener = self;
	}
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

var nav4 = window.Event ? true : false;
function acceptNumTelefono(evt)
{
	// NOTE: Backspace = 8, Enter = 13, '0' = 48, '9' = 57, '.' = 46
	var key = nav4 ? evt.which : evt.keyCode;
	return (key <= 13 || (key >= 48 && key <= 57) || key == 91 || key == 93 || key == 40 || key == 41);
}

function verf_ie()
{
	this.ver=navigator.appVersion;
	this.agent=navigator.userAgent;
	this.dom=document.getElementById?1:0;
	this.opera=window.opera?1:0;
	this.ie55=(this.ver.indexOf("MSIE 5.5")>-1&&this.dom&&!this.opera)?1:0;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1&&this.dom&&!this.ie55&&!this.opera)?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1&&this.dom&&!this.opera)?1:0;
	this.ie4=(document.all&&!this.dom&&!this.opera)?1:0;
	this.ie=this.ie4||this.ie5||this.ie6;
	return this.ie;
}

function resizeDialogToContent()
{
	if (verf_ie() == 1) {
	  	// resize window so there are no scrollbars visible
	    var dw = window.dialogWidth;
	    while (isNaN(dw)) {
	      dw = dw.substr(0,dw.length-1);
	    }
	    difw = dw - this.document.body.clientWidth;
	    window.dialogWidth = this.document.body.scrollWidth+difw+'px';	
	    var dh = window.dialogHeight;
	    while (isNaN(dh)) {
	      dh = dh.substr(0,dh.length-1);
	    }
	    difh = dh - this.document.body.clientHeight;
	    window.dialogHeight = this.document.body.scrollHeight+difh+'px';
	}
}
	
function Init()
{
  resizeDialogToContent();
}
