function exist_num(name)
{
	var len = document.getElementById(name).value.length;
	var swf = false;
	
	for(i=0; i<len; i++)
	{
		var let = document.getElementById(name).value.substring(i,i+1);
	
		if (!isNaN(let))
		{
			swf=true;
			break;
		}
	}
	
	return swf;
}

function submitForm(lang)
 { 	
 	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 páis" : "Select country";
 	var Msg_City 				= lang == 1 ? "Ingrese la ciudad" : "Write city";	
 	var Msg_Phone 				= lang == 1 ? "Ingrese su telefono" : "Write phone";
 	var Msg_Email	  			= lang == 1 ? "Ingrese su E-mail" : "Write 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_InvalidEmail		= lang == 1 ? "E-mail invalido" : "Invalid E-mail";
 	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 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 su cometario" : "Write comentary";
 	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('or_nombre').value)=='')
	{
		window.alert(Msg_FirstName);
		document.getElementById('or_nombre').focus();
		return;
	}

 	if (exist_num('or_nombre'))
 	{
		document.getElementById('or_nombre').focus();
 		window.alert('No ingrese numeros en el nombre');
		return false;
 	}
	 		
	if(trim(document.getElementById('or_apellido').value)=='')
	{
		window.alert(Msg_LastName);
		document.getElementById('or_apellido').focus();
		return;
	}

	
 	if (exist_num('or_apellido'))
 	{
		window.alert('No ingrese numeros en el apellido');
		document.getElementById('or_apellido').focus();
		return false;
 	}
 		
	if(trim(document.getElementById('or_pais').value)=='')
	{
		window.alert(Msg_Country);
		document.getElementById('or_pais').focus();
		return;
	}

	if(trim(document.getElementById('or_ciudad').value)=='')
	{
		window.alert(Msg_City);
		document.getElementById('or_ciudad').focus();
		return;
	}
	
 	if (exist_num('or_ciudad'))
 	{
		window.alert('No ingrese numeros en la ciudad');
		document.getElementById('or_ciudad').focus();
		return false;
 	}	
	
 	if(trim(document.getElementById('or_telefono').value)=='')
	{
		window.alert(Msg_Phone);
		document.getElementById('or_telefono').focus();
		return;
	}

	if(trim(document.getElementById('or_email').value)=='')
	{
		window.alert(Msg_Email);
		document.getElementById('or_email').focus();
		return;
	}
	
	if ( !checkemail(trim(document.getElementById('or_email').value )))
	{
		window.alert(Msg_InvalidEmail);
		document.getElementById('or_email').focus();
		return;
	}

	if(trim(document.getElementById('or_email').value)=='')
	{
		window.alert(Msg_Email);
		document.getElementById('or_email').focus();
		return;
	}
	
	if(document.getElementById('or_conocistes').selectedIndex == 0)
	{
		window.alert(Msg_HowDidYouFindUs);
		document.getElementById('or_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('or_tipoalojamiento').selectedIndex == 0)
	{
		window.alert(Msg_Typeofaccommodation);
		document.getElementById('or_tipoalojamiento').focus();
		return;
	}
	
	if(document.getElementById('or_tiposervicio').selectedIndex == 0)
	{
		window.alert(Msg_Tourguide);
		document.getElementById('or_tiposervicio').focus();
		return;
	}
	
	if(document.getElementById('or_mes').selectedIndex == 0)
	{
		window.alert(Msg_Month);
		document.getElementById('or_mes').focus();
		return;
	}

 	if(document.getElementById('or_estadia').selectedIndex == 0)
	{
		window.alert(Msg_Staying);
		document.getElementById('or_estadia').focus();
		return;
	}
	
 	if(document.getElementById('or_ano').selectedIndex == 0)
	{
		window.alert(Msg_Year);
		document.getElementById('or_ano').focus();
		return;
	}

 	if(document.getElementById('or_adultos').selectedIndex==0 && document.getElementById('or_ninos').selectedIndex==0)
	{
		window.alert(Msg_npasajeros);
		document.getElementById('or_adultos').focus();
		return;
	}
	
	var myForm = document.getElementById('myForm');
	myForm.action = 'graba_organice.php';
	myForm.submit();
 }
 
function clearForm()
{ 	
	document.getElementById('or_nombre').value='';
	document.getElementById('or_apellido').value='';
	document.getElementById('or_pais').value='';
	document.getElementById('or_ciudad').value='';	 	
	document.getElementById('or_telefono').value='';
	document.getElementById('or_email').value='';
	document.getElementById('or_conocistes').selectedIndex = 0;

	var contenedor = document.getElementById("inputOtros");
	if (contenedor.firstChild != null ) {
		contenedor.removeChild(contenedor.firstChild);
	}
	
	for (i=0;i<document.forms[0].elements.length;i++) {
		if (document.forms[0].elements[i].name.substr(0,7)=='tipotur' && document.forms[0].elements[i].type == 'checkbox') {
			document.forms[0].elements[i].checked = 0;
		}
	}
	
	document.getElementById('or_tipoalojamiento').selectedIndex = 0;
	document.getElementById('or_tiposervicio').selectedIndex = 0;	
	document.getElementById('or_mes').selectedIndex = 0;
	document.getElementById('or_estadia').selectedIndex = 0;
	document.getElementById('or_ano').selectedIndex = 0
	document.getElementById('or_adultos').value='';
	document.getElementById('or_ninos').value='';
	document.getElementById('or_comentario').value='';
	document.getElementById('or_nombre').focus();

	var varTrs = document.getElementsByTagName('tr');
	for (var j=0; j<varTrs.length;j++) {
		if (varTrs[j].id.substr(0,11)=="row_ciudad_") {
			var obj = varTrs[j];
			if (obj.id != "row_ciudad_1")
			{
				obj.parentNode.removeChild(obj);
				j--;
			}
		}
	}
	
	document.getElementById("ciudad1").selectedIndex=0;
}
  
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 addDynamicComboCiudad(source, obj, idioma)
{	
	var total1			= dameNrSelects();
	var numRow1 		= total1+1;
	var varTbody 		= document.createElement('tbody');
	var varTr 			= document.createElement('tr');	
	varTr.id 			= 'row_ciudad'+'_'+numRow1;
	var varTdCaption	= document.createElement('td');
	var captionLeft		= idioma == 1 ? "Destino " : "Destination ";
	var nodoTextoOption = document.createTextNode(captionLeft);
	varTdCaption.appendChild(nodoTextoOption);
	varTdCaption.className = 'arial10';
	var varTdCiudad 	= document.createElement('td');
	varTdCiudad.align   = "center"; 
    var varTdDelete 	= document.createElement('td');    
    var varImg 			= document.createElement('img');
    varImg.src 			= 'images/publish_x.png';
    varImg.border 		= 0;
    varImg.title 		= 'Eliminar esta Fila';
	var varHref 		= document.createElement('a');
	varHref.href		= 'javascript:;';
	varHref.onclick 	= function(){delURL(varTr, idioma);};
	varHref.appendChild(varImg);
	varTdDelete.appendChild(varHref);	
	var varSelect 		= document.createElement("select");
	var selectName  	= 'ciudad' + numRow1;
	varSelect.id 		= selectName;
	varSelect.name 		= selectName;
	varSelect.onchange 	= function(){addDynamicComboCiudad('ciudades',this, idioma)};
	var varOption   	= document.createElement("option");
	var nodoTextoOption = idioma == 1 ? document.createTextNode("[ Selecione ]") : document.createTextNode("[ Choose ]");

	varOption.appendChild(nodoTextoOption);
 	varSelect.appendChild(varOption);
    source = eval(source);
    
    for (x in source) {
		var varOption = document.createElement("option");
  	    var nodoTextoOption = document.createTextNode(source[x][2]) ;
  	    varOption.appendChild(nodoTextoOption);
	    varSelect.appendChild(varOption);
    }
    
	varTdCiudad.appendChild(varSelect);	
	varTr.appendChild(varTdCaption);
	varTr.appendChild(varTdCiudad);
	varTr.appendChild(varTdDelete);

	if(total1 == 0) {
		document.getElementById("ciudad").appendChild(varTr);
	} else {
		var id =obj.id.substr(obj.id.lastIndexOf("d")+1, obj.id.length);
		if (total1 == id) {
			document.getElementById("ciudad").appendChild(varTr);
		}
	}
}

function delURL(obj, idioma)
{
	var id = parseInt(obj.id.substr(obj.id.lastIndexOf("_")+1, obj.id.length));
	var total1 = dameNrSelects();

	if (total1 == 1) {
		if (idioma == 1) {
			alert('No puede eliminar todos las destinos');
			return;
		} else {
			alert('you cant not delete all the destinies');
			return;
		}
		addDynamicComboCiudad('ciudades', '', idioma)
	}
	
	obj.parentNode.removeChild(obj);

	while (id < total1) {
		var id = id+1;
		var varIdReplace = id-1;
 		document.getElementById("ciudad" + id).name =  "ciudad" + varIdReplace;
 		document.getElementById("ciudad" + id).id = "ciudad" + varIdReplace;
 		document.getElementById('row_ciudad_'+id).id = "row_ciudad_" + varIdReplace;
	}
}

function dameNrSelects()
{
	var total1=0;
	var varSelects = document.getElementsByTagName('select');
	for (var i=0; i<varSelects.length;i++) {
		var end = varSelects[i].id.lastIndexOf("d");
		if (varSelects[i].id.substr(0, end + 1)=="ciudad") {
			total1 += 1;
		}
	}
	return total1;
}

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","31");
		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 = '';
}


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);
}