var ajax = new Array();
var imagen_hidden = new Image();
var imagen_show = new Image();

 imagen_hidden.src = "images/spacer.gif";
 imagen_show.src = "images/cargando_ajax.gif";

function misDatos(theForm) {
	var email = theForm.checaEmail.value;
	
	if(email != "") {
		if (document.images) {
			document.getElementById('maill').src = imagen_show.src;
		}
		
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'getDatos.php?e='+email;
		ajax[index].onCompletion = function(){ getDatos(index) };
		ajax[index].runAJAX();
				
	} 
	return (false);
}

function misCiudades(input) {
	var cp = input.value;
	document.getElementById('cCiudad').options.length = 0;
	document.getElementById('cEstado').selectedIndex = 0;
	document.getElementById('cCine').options.length = 0;
	if(cp.length == 5) {
		if (document.images) {
			document.getElementById('ciudadl').src = imagen_show.src;
		}
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'getCiudades.php?cp='+cp;
		ajax[index].onCompletion = function(){ createCities(index) };
		ajax[index].runAJAX();
				
	} else {
		document.getElementById('cEstado').disabled = false;
		showErrorCP('visible');
		document.getElementById('cCP').value = "";
		document.getElementById('cCine').options[document.getElementById('cCine').options.length] = new Option('Selecciona tu Cine Preferido','');
	}
}


function misCiudadesE(input) {
	var estado = input.options[input.selectedIndex].value;
	document.getElementById('cCiudad').options.length = 0;
	document.getElementById('cCine').options.length = 0;
	
	if(input.selectedIndex > 0 && document.getElementById('cCP').value.length < 5) {
		if (document.images) {
			document.getElementById('ciudadl').src = imagen_show.src;
		}
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'getCiudades.php?e='+estado;
		ajax[index].onCompletion = function(){ createCities(index) };
		ajax[index].runAJAX();
		
	}
}

function misCinesE(input) {
	var cadena = input.value;
	var estado = document.getElementById('cEstado').value;
	document.getElementById('cCine').options.length = 0;
	if(input.selectedIndex > 0) {

		if (document.images) {
			document.getElementById('cinel').src = imagen_show.src;
		}
		var index = ajax.length;
		ajax[index] = new sack();
		
		ajax[index].requestFile = 'getCines.php?c='+cadena+'&e='+estado;
		ajax[index].onCompletion = function(){ createCines(index) };
		ajax[index].runAJAX();
		
	} else {
		document.getElementById('cCine').options[document.getElementById('cCine').options.length] = new Option('Selecciona tu Cine Preferido','');
	}
}

function misCadenasE(input) {
	var estado = input.value;
	document.getElementById('cCine').options.length = 0;
	if(input.selectedIndex > 0) {
		if (document.images) {
			document.getElementById('cadenal').src = imagen_show.src;
		}
		var index = ajax.length;
		ajax[index] = new sack();
		ajax[index].requestFile = 'getOperadores.php?e='+estado;
		ajax[index].onCompletion = function(){ createCadenas(index) };
		ajax[index].runAJAX();
	} else {
		document.getElementById('cCine').options[document.getElementById('cCine').options.length] = new Option('Selecciona tu Cine Preferido','');
	}
}

function getDatos(index) {
	var theForm = document.getElementById('RegistroClave');
	var objSexo = "";
	var objEstadoCivil = "";
	var objHijos = "0";
	var objEstado = "";
	var objDir = "";
	showErrorMail('hidden');
	eval(ajax[index].response);
	if (document.images) {
			document.getElementById('maill').src = imagen_hidden.src;
	}
	if(theForm.cID.value == ""){
		showErrorMail('visible');
	} else {
		setCheckedValue(theForm.cSexo, objSexo)
		setCheckedValue(theForm.cECivil, objEstadoCivil)
		objDir = theForm.cDireccion.value.toString();
		theForm.cDireccion.value = objDir.replace("|","\r\n");
		if(theForm.cCP.value != ""){
			misCiudades(theForm.cCP);
		} else {
			misCiudadesE(theForm.cEstado);
		}
	}
	
}

function setSelectValue(selectObj, newValue) {
	if(!selectObj)
		return;
	var selectLength = selectObj.length;
	if(selectLength == undefined) {
		selectObj.selectedIndex = (selectObj.value == newValue.toString());
		return;
	}
	for(n=0;n<selectObj.options.length;n++){
		if(selectObj.options[n].value == newValue) {
			selectObj.selectedIndex = n;
			break;
		}
	}
}

function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function createCities(index)
{
	showErrorCP('hidden');
	var obj = document.getElementById('cCiudad');
	var objEstado = '';
	var objSelEstado = document.getElementById('cEstado');
	var objCadenas = document.getElementById('cCadena');
	var cp = document.getElementById('cCP');
	eval(ajax[index].response);	
	for(n=0;n<objSelEstado.options.length;n++){
		if(objSelEstado.options[n].value == objEstado) {
			objSelEstado.selectedIndex = n;
			break;
		}
	}
	if(obj.options.length == 2){
		obj.selectedIndex = 1;
	}
	obj.disabled = false;
	objSelEstado.disabled = false;
	if(objSelEstado.selectedIndex > 0){
		if(cp.value != "") {
			objSelEstado.disabled = true;
			//misCadenasE(objSelEstado);
		}
		misCadenasE(objSelEstado);
	} else {
		cp.value = "";
		showErrorCP('visible');
	}
	if(obj.selectedIndex > 0){
		obj.disabled = true;
	}
	if(obj.options.length == 1){
		obj.options.length = 0;
	}
	
	if (document.images) {
		document.getElementById('ciudadl').src = imagen_hidden.src;
	}
	if(document.getElementById('cCine').options.length == 0){
		document.getElementById('cCine').options[document.getElementById('cCine').options.length] = new Option('Selecciona tu Cine Preferido','');
	}
}

function createCadenas(index)
{
	var objCadenas = document.getElementById('cCadena');
	eval(ajax[index].response);	
	
	if (document.images) {
		document.getElementById('cadenal').src = imagen_hidden.src;
	}
	misCinesE(objCadenas);
}

function createCines(index)
{
	//showErrorCP('hidden');
	var obj = document.getElementById('cCine');
	//var objEstado = '';
	//var objSelEstado = document.getElementById('cEstado');
	//var cp = document.getElementById('cCP');
	eval(ajax[index].response);	
	
	if (document.images) {
		document.getElementById('cinel').src = imagen_hidden.src;
	}
	
	if(document.getElementById('cCine').options.length == 0){
		document.getElementById('cCine').options[document.getElementById('cCine').options.length] = new Option('Selecciona tu Cine Preferido','');
	}
}

function init(lHayUsuario) {
	if(!lHayUsuario) { showErrorMail('hidden'); }
	showErrorCP('hidden');
}

function showErrorCP(estado) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById('errorCP').style.visibility = estado;
	} else {
		if (document.layers) { // Netscape 4
			document.errorCP.visibility = estado;
		} else { // IE 4
			document.all.errorCP.style.visibility = estado;
		}
	} 
}

function showErrorMail(estado) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById('errorMail').style.visibility = estado;
	} else {
		if (document.layers) { // Netscape 4
			document.errorMail.visibility = estado;
		} else { // IE 4
			document.all.errorMail.style.visibility = estado;
		}
	} 
}

function Form_Validator(theForm)
{

  if (theForm.acepto.checked == false)
  {
    alert("Para poder participar debes Aceptar las condiciones de la Promoción.");
    theForm.acepto.focus();
    return (false);
  }
  if (theForm.cNombre.value == "")
  {
    alert("Por favor ingresa un valor para el campo \"Nombre\".");
    theForm.cNombre.focus();
    return (false);
  }

  if (theForm.cApellidos.value == "")
  {
    alert("Por favor ingresa un valor para el campo \"Apellidos\".");
    theForm.cApellidos.focus();
    return (false);
  }

  if (theForm.cDia.value == "")
  {
    alert("Por favor ingresa un valor para el campo \"Día\".");
    theForm.cDia.focus();
    return (false);
  }

  if (theForm.cDia.value.length < 1)
  {
    alert("Por favor ingresa al menos un caracter en el campo \"Día\".");
    theForm.cDia.focus();
    return (false);
  }

  if (theForm.cDia.value.length > 2)
  {
    alert("Por favor ingresa un máximo de 2 caracteres en el campo \"Día\".");
    theForm.cDia.focus();
    return (false);
  }

  var checkOK = "0123456789";
  var checkStr = theForm.cDia.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Por favor ingresa sólo caracteres númericos en el campo \"Día\".");
    theForm.cDia.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseInt(allNum);
  if (chkVal != "" && !(prsVal >= 1 && prsVal <= 31))
  {
    alert("Por favor ingresa un valor entre \"1\" y \"31\" en el campo \"Día\".");
    theForm.cDia.focus();
    return (false);
  }

  if (theForm.cMes.value == "")
  {
    alert("Por favor ingresa un valor para el campo \"Mes\".");
    theForm.cMes.focus();
    return (false);
  }

  if (theForm.cMes.value.length < 1)
  {
    alert("Por favor ingresa al menos un caracter en el campo \"Mes\".");
    theForm.cMes.focus();
    return (false);
  }

  if (theForm.cMes.value.length > 2)
  {
    alert("Por favor ingresa un máximo de 2 caracteres en el campo \"Mes\".");
    theForm.cMes.focus();
    return (false);
  }


  var checkOK = "0123456789";
  var checkStr = theForm.cMes.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Por favor ingresa sólo caracteres númericos en el campo \"Mes\".");
    theForm.cMes.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseInt(allNum);
  if (chkVal != "" && !(prsVal >= 1 && prsVal <= 12))
  {
    alert("Por favor ingresa un valor entre \"1\" y \"12\" en el campo \"Mes\".");
    theForm.cMes.focus();
    return (false);
  }

  if (theForm.cAnio.value == "")
  {
    alert("Por favor ingresa un valor para el campo \"Año\".");
    theForm.cAnio.focus();
    return (false);
  }

  if (theForm.cAnio.value.length < 4)
  {
    alert("Por favor ingresa 4 caracteres en el campo \"Año\".");
    theForm.cAnio.focus();
    return (false);
  }

  if (theForm.cAnio.value.length > 4)
  {
    alert("Por favor ingresa un máximo de 4 caracteres en el campo \"Año\".");
    theForm.cAnio.focus();
    return (false);
  }

  var checkOK = "0123456789";
  var checkStr = theForm.cAnio.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Por favor ingresa sólo caracteres númericos en el campo \"Año\".");
    theForm.cAnio.focus();
    return (false);
  }

  var chkVal = allNum;
  var prsVal = parseInt(allNum);
  if (chkVal != "" && !(prsVal >= 1900 && prsVal <= 1990))
  {
    alert("Por favor ingresa un valor entre \"1900\" y \"1990\" en el campo \"Año\".");
    theForm.cAnio.focus();
    return (false);
  }

  if (theForm.cEmail.value == "")
  {
    alert("Por favor ingresa un valor para el campo \"e-Mail\".");
    theForm.cEmail.focus();
    return (false);
  }

  if (theForm.cEmail.value.length < 6)
  {
    alert("Por favor revisa el valor para el campo \"e-Mail\".");
    theForm.cEmail.focus();
    return (false);
  }

  if (theForm.cEmail.value.length > 80)
  {
    alert("Por favor ingresa un máximo de 80 caracteres en el campo \"e-Mail\".");
    theForm.cEmail.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.@";
  var checkStr = theForm.cEmail.value;
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Por favor ingresa sólo letras, números o los caracteres \".-_@\" en el campo \"e-Mail\".");
    theForm.cEmail.focus();
    return (false);
  }

    if (theForm.cCP.value == "" && (theForm.cEstado.selectedIndex == 0 || theForm.cCiudad.selectedIndex == 0))
  {
    alert("Por favor ingresa un valor para el campo \"Código Postal\".");
    theForm.cCP.focus();
    return (false);
  }
  
    if (theForm.cCP.value.length < 5 && (theForm.cEstado.selectedIndex == 0 || theForm.cCiudad.selectedIndex == 0))
  {
    alert("Por favor revisa el valor para el campo \"Código Postal\" debería tener 5 dígitos.");
    theForm.cCP.focus();
    return (false);
  }
  
  var checkOK = "0123456789";
  var checkStr = theForm.cCP.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Por favor ingresa sólo caracteres númericos en el campo \"C.P.\".");
    theForm.cCP.focus();
    return (false);
  }

  if (theForm.cEstado.selectedIndex == 0) {
	alert("Por favor selecciona en que estado vives.");
	return (false);
  }
  
  if (theForm.cCiudad.selectedIndex == 0) {
	alert("Por favor selecciona en que ciudad vives.");
	return (false);
  }
  
  var checkOK = "0123456789 ()-.";
  var checkStr = theForm.cTelefono.value;
  var allValid = true;
  var validGroups = true;
  var decPoints = 0;
  var allNum = "";
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (!allValid)
  {
    alert("Por favor ingresa sólo caracteres númericos en el campo \"Teléfono\".");
    theForm.cTelefono.focus();
    return (false);
  }
  theForm.cEmail.disabled = false;
  return (true);
}