var campoError = '';
var urlPost = '';
function suscribir()
{
	document.getElementById("mensajeError").style.visibility = "hidden";
	if (campoError != "")
	{
		if (document.getElementById(campoError).type == "text" && document.getElementById(campoError).value == "CAMPO INCORRECTO")
			document.getElementById(campoError).value = "";

		if (document.getElementById(campoError).type == "select-one" && document.getElementById(campoError).options[0].text == "CAMPO INCORRECTO")
			document.getElementById(campoError).options[0].text = "Elija una opcion..";		
	}
				
	//Chequeo los campos obligatorios
	var controles = [
		[document.form1.elements['nombre'],'noEsVacio'],
		[document.form1.elements['custom_Apellido'],'noEsVacio'],
		[document.form1.elements['email'],'esEmail'],
		[document.form1.elements['custom_Empresa'],'noEsVacio'],
	        [document.form1.elements['custom_Sector'],'noEsVacio'],
                [document.form1.elements['custom_SitioWeb'],'noEsVacio'],
                [document.form1.elements['inf_field_City'],'noEsVacio'],
                [document.form1.elements['inf_field_Country'],'noEsVacio'],
                [document.form1.elements['custom_DondeNosConocio'],'noEsVacio'],
                [document.form1.elements['custom_Telefono'],'esNumero']
	     ];

	         		
    //Devuelve vacio si esta todo bien, caso contrario devuelve tipo de error en el formulario
    campoError = '';	
  	var error = controlFormularios(controles);		
		
	if(error != "")
	{
		if (document.getElementById(campoError).type == "text")
			document.getElementById(campoError).value = "CAMPO INCORRECTO";
	
		if (document.getElementById(campoError).type == "select-one")
			document.getElementById(campoError).options[document.getElementById(campoError).selectedIndex].text = "CAMPO INCORRECTO";
		document.getElementById("mensajeError").style.visibility = "visible";
		document.getElementById(campoError).focus();
	}
   	else
   	{   		
		document.form1.action = urlPost;   		
		document.form1.submit();		
   	}
}

function controlFormularios(controles)
{
	var error = "";
	var i = 0;
	while (i < controles.length && error == "" )
	{
		error = validarControl(controles[i][0].value, controles[i][1]);

		if (error != "")
			campoError = controles[i][0].name;		
		i++;
	}	
	return error;
}

function validarControl(elementoControl, tipoControl)
{	
	//Aca controlo control por control
	estadoActual = "";
	switch ( tipoControl )
	{ 
		case 'esNumero': 
			estadoActual = isNumeric(elementoControl);
			break;
		case 'noEsVacio': 
			estadoActual = isEmpty(elementoControl);
			break;
		case 'esFecha': 
			estadoActual = isDate(elementoControl);
			break;
		case 'esEmail': 
		   estadoActual = isValidEmail(elementoControl);
			break;
		case 'noCero': 
		   estadoActual = esDistintoDeCero(elementoControl);
			break;
		case 'noCero_0': 
		   estadoActual = esDistintoDeCero_0(elementoControl);
			break;
	}
    return estadoActual;
}

function isNumeric(sText)
{
	if( sText.length == 0)
   			IsNumber = 'esNumero';
   	else
   	{ 
		var ValidChars = "0123456789.-";
		var IsNumber = '';
		var Char;
		  
   		for (i = 0; i < sText.length && IsNumber == ""; i=i+1) 
      	{ 
       		Char = sText.charAt(i); 
      		if (ValidChars.indexOf(Char) == -1) 
       			IsNumber = 'esNumero';
        }
	}
   	return IsNumber;   
}

function isEmpty(aTextField) 
{
	if ((aTextField.length==0) || (aTextField==null)) 
		return 'noEsVacio';
	else 
		return ''; 
}	

function isValidEmail(str) 
{    
	var filter=/^[A-Za-z][A-Za-z0-9_.-]*@[A-Za-z0-9_.-]+\.[A-Za-z0-9_.]+[A-za-z]$/;
		
	if (filter.test(str))
		return '';
	else
		return 'esEmail';
} 

function esDistintoDeCero(str)
{
	if (str == 0)
   		return 'noCero';
   	else
		return '';
}

function esDistintoDeCero_0(str)
{
	if (str == "0/0")
   		return 'noCero_0';
   	else
		return '';
}

function isDate(dtStr)
{
	var dtCh= "/";
	var daysInMonth = DaysArray(12)
	var pos1 = dtStr.indexOf(dtCh)
	var pos2 = dtStr.indexOf(dtCh,pos1+1)
	var strDay = dtStr.substring(0,pos1)
	var strMonth = dtStr.substring(pos1+1,pos2)
	var strYear = dtStr.substring(pos2+1)
	strYr = strYear
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	
	if ( pos1 == -1 || pos2 == -1 || day <= 0 || day > 31 )
		return 'esFecha';
	
	if (strMonth.length < 1 || month < 1 || month > 12)
		return 'esFecha';
	
	if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear)
		return 'esFecha';
	
	if (dtStr.indexOf(dtCh, pos2+1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false)
		return 'esFecha';
	
	return '';
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
