// JavaScript Document

function IsNumeric(sText)
{
   var ValidChars = "0123456789.-+";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

function cambiar_pais () {
	var d=document.titulares;
	var pais=d.pais.value;
	var prov_array=new Array("Álava","Albacete","Alicante","Almería","Asturias","Ávila","Badajoz","Barcelona","Burgos","Cáceres","Cádiz","Cantabria","Castellón","Ceuta","Ciudad Real","Córdoba","Cuenca","Girona","Granada","Guadalajara","Guipúzcoa","Huelva","Huesca","Islas Baleares","Jaén","La Coruña","La Rioja","Las Palmas","León","Lleida","Lugo","Madrid","Málaga","Melilla","Murcia","Navarra","Orense","Palencia","Pontevedra","Salamanca","Santa Cruz de Tenerife","Segovia","Sevilla","Soria","Tarragona","Teruel","Toledo","Valencia","Valladolid","Vizcaya","Zamora","Zaragoza");

	if (pais=="no select") {
		document.getElementById("provincias").innerHTML = '<input maxlength="45" size="50" name="provincia" value="Foreigner" readonly="readonly" />';
	} else {
		if (pais=="Spain") {
			// load select de provincias
			var provincias='<select name="provincia">';
			for (i=0;i<prov_array.length;i++) {
				provincias=provincias+'<option value="'+prov_array[i]+'">'+prov_array[i]+'</option>';
			}
			document.getElementById("provincias").innerHTML = provincias;
		} else {
			document.getElementById("provincias").innerHTML = '<input maxlength="45" size="50" name="provincia" value="Foreigner" readonly="readonly" />';
		}
	}
}


function checkscript() {
		var d=document.titulares;

		var sector_array=new Array("PELUQUERÍA","ESTÉTICA","SPA","FITNESS","MEDICINA ESTÉTICA","OTROS");

		var principal_array=new Array("SALÓN PELUQUERÍA","SALÓN DE BELLEZA","SPA","CENTRO MÉDICO","FITNESS","HOTEL","FABRICANTE","DISTRIBUIDOR","MAYORISTA","OTROS");
		var area_array=new Array("PROPIETARIO-SOCIO","DIRECTOR/GERENTE","EMPLEADO","AUTÓNOMO","ESTUDIANTE","OTROS");
		d.sectortodos.value="";
		d.principaltodos.value="";
		d.areatodos.value="6"; // OTROS

		var indice=0;
		cs="t";

		if (d.nombre.value=="") {cs = "f"; d.nombre.focus();}
	    if (d.apellido.value=="") {cs = "f"; d.apellido.focus();}
	    if (d.apellido.value==d.nombre.value) {cs = "f"; d.nombre.focus();}
        if (cs=="f") {
			alert ('Please, complete all the required fields');
			return false;
		} else {		
   		   		if (!Validate_Email_Address(d.email.value)) {
					alert ('Please, check the format of the e-mail');
					d.email.focus();
					return false;
		   		} else {
					if (d.email2.value!=d.email.value) {
						alert ('Please, the e-mail do not match');
						d.email2.focus();
						return false;
					} else {
						indice = d.pais.selectedIndex;
						//alert (indice);
						if (d.pais.options[indice].value=="no select") {
							alert ('Please, complete all the required fields');
							d.pais.focus();
							return false;
						}
						if (d.direccion.value=="") {
							alert ('Please, complete all the required fields');
							d.direccion.focus();
							return false;
						}
						if (d.cpostal.value=="") {
							alert ('Please, complete all the required fields');
							d.cpostal.focus();
							return false;
						} else {
							/*if (!IsNumeric(d.cpostal.value)) {
								alert ('Please, the field of postal code must be numerical and without spaces in target');
								d.cpostal.focus();
								return false;
							}*/
						}
						if (d.provincia.value=="") {
							alert ('Please, complete all the required fields');
							d.provincia.focus();
							return false;
						}
						if (d.poblacion.value=="") {
							alert ('Please, complete all the required fields');
							d.poblacion.focus();
							return false;
						}
						if (d.telparticular.value!="") {
							if (!IsNumeric(d.telparticular.value)) {
								alert ('Please, the field of phone must be numerical and without spaces in target');
								d.telparticular.focus();
								return false;
							}
						}
						if (d.movil.value!="") {
							if (!IsNumeric(d.movil.value)) {
								alert ('Please, the field of mobile phone must be numerical and without spaces in target');
								d.movil.focus();
								return false;
							}
						}
						if (d.fax.value!="") {
							if (!IsNumeric(d.fax.value)) {
								alert ('Please, the field of fax must be numerical and without spaces in target');
								d.fax.focus();
								return false;
							}
						}

						// miramos sector (+txtsector), principal(+txtprincipal) y area
						
						for (i=0;i<sector_array.length;i++) {
							if (d.sector[i].checked) {
								//PELUQUERÍA,ESTÉTICA,SPA,FITNESS,MEDICINA ESTÉTICA,OTROS
								d.sectortodos.value=d.sectortodos.value+sector_array[i]+",";
							}
						}
						if (d.sectortodos.value=="") {
							alert ('Please, fill at least one option of the field Sector of activity');
							return false;
						} else {
							if (d.sectortodos.value.indexOf("OTROS")>=0) {
								if (d.txtsector.value=="") {
									alert ('Please, you must fill up the text field Others of Sector of activity');
									d.txtsector.focus();
									return false;
								}
							}
						}
						
						for (i=0;i<principal_array.length;i++) {
							if (d.principal[i].checked) {
								//SALÓN PELUQUERÍA,SALÓN DE BELLEZA,SPA,CENTRO MÉDICO,FITNESS,HOTEL,FABRICANTE,DISTRIBUIDOR,MAYORISTA,OTROS
								d.principaltodos.value=d.principaltodos.value+principal_array[i]+",";
							}
						}
						if (d.principaltodos.value=="") {
							alert ('Please, fill at least one option of the field Main activity');
							return false;
						} else {
							if (d.principaltodos.value.indexOf("OTROS")>=0) {
								if (d.txtprincipal.value=="") {
									alert ('Please, you must fill up the text field Others of Main activity');
									d.txtprincipal.focus();
									return false;
								}
							}
						}
						
						d.areatodos.value=area_array[getRadioButtonSelectedValue(d.area)-1];
						if (d.areatodos.value=="") {
							alert ('Please, fill an option in the Responsability Area');
							return false;
						} 
						// --------------------------------
						if  (d.recibir1txt.value=="1") {
							return true;
						} else {
							alert ('Please, you must select the option to receive tickets');
							d.recibir1.focus();
							return false;
						}
					}
           		}
			
		}
}

function getRadioButtonSelectedValue(ctrl)
{
    for(i=0;i<ctrl.length;i++) {
        if(ctrl[i].checked) { 
			return ctrl[i].value;
		}
	}
}


function Validate_String(string, return_invalid_chars)
         {
         valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
         invalid_chars = '';
         
         if(string == null || string == '')
            return(true);
         
         //For every character on the string.   
         for(index = 0; index < string.length; index++)
            {
            car= string.substr(index, 1);                        
            
            //Is it a valid character?
            if(valid_chars.indexOf(car) == -1)
              {
              //If not, is it already on the list of invalid characters?
              if(invalid_chars.indexOf(car) == -1)
                {
                //If it's not, add it.
                if(invalid_chars == '')
                   invalid_chars += car;
                else
                   invalid_chars += ', ' + car;
                }
              }
            }                     
            
         //If the string does not contain invalid characters, the function will return true.
         //If it does, it will either return false or a list of the invalid characters used
         //in the string, depending on the value of the second parameter.
         if(return_invalid_chars == true && invalid_chars != '')
           {
           last_comma = invalid_chars.lastIndexOf(',');
           
           if(last_comma != -1)
              invalid_chars = invalid_chars.substr(0, $last_comma) + 
              ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
                      
           return(invalid_chars);
           }
         else
           return(invalid_chars == ''); 
         }

function Validate_Email_Address(email_address)
         {
         //Assumes that valid email addresses consist of user_name@domain.tld
         at = email_address.indexOf('@');
         dot = email_address.indexOf('.');
         
         if(at == -1 || 
            dot == -1 || 
            
            dot == 0 || 
            dot == email_address.length - 1)
            return(false);
            
         user_name = email_address.substr(0, at);
         domain_name = email_address.substr(at + 1, email_address.length);                  
         
         if(Validate_String(user_name) == false || 
            Validate_String(domain_name) == false)
            return(false);                     
         
         return(true);
         }
		 
function checkeando (cual) {
	var d=document.titulares;
	var elemento=cual.name;
	var valortxt="document.titulares."+elemento+".checked";
	var valortxt2="document.titulares."+elemento+"txt.value";
	var valor=eval(valortxt);
		if (valor) {
			eval (valortxt2+"=1");
		} else {
			eval (valortxt2+"=0");
		}
	//alert (document.titulares.sector.checked);
}

