var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;

//function checkSelect (theField,msg)
//{
//	if( theField.selectedIndex <= 0 )
//	{
//		alert(msg);
//		theField.focus();
//		return false;
//	}
//	return true;	
//}

function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}

function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}


function isDate (year, month, day)
{   

    var intYear = parseInt(year, 10);
    var intMonth = parseInt(month, 10);
    var intDay = parseInt(day, 10);
	
	if (isNaN(intYear)) return false; 
    if (isNaN(intMonth)) return false;    
    if (isNaN(intDay)) return false;  
    if (!(year.length == 4)) return false;  
	  
    if ((intDay < 1) || (intDay > 31)) return false;
    if ((intMonth < 1) || (intMonth > 12)) return false;
		
    // catch invalid days, except for February
    if (intDay > daysInMonth[intMonth]) return false; 

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    return true;
}

function check_rut(rut,dv){
	dig_ver = dv;
	
	if ( isNaN(rut) ){
		//alert("Sólo deben ingresarse números en el RUT");
		return(false)
	}

	if ( rut.length < 7 || dig_ver == ""){
		//alert("El RUT debe contener 7 u 8 dígitos y 1 dígito verificador");
		return(false)
	} 

	if ( rut.length < 8 )
		numero_rut = "0" + rut
	else
		numero_rut = rut;


        v8 = numero_rut.substring(7,8) * 2;
        v7 = numero_rut.substring(6,7) * 3;
        v6 = numero_rut.substring(5,6) * 4;
        v5 = numero_rut.substring(4,5) * 5;
        v4 = numero_rut.substring(3,4) * 6;
        v3 = numero_rut.substring(2,3) * 7;
        v2 = numero_rut.substring(1,2) * 2;
        v1 = numero_rut.substring(0,1) * 3;

	suma_rut = v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8;
	resto_rut = suma_rut % 11;

	digito_verificador = 11 - resto_rut;

	if ( digito_verificador == 10 )
		digito_verificador = "K";
	if ( digito_verificador == 11 )
		digito_verificador = 0;

        if ( digito_verificador == "K" ){
                if ( digito_verificador != dig_ver.toUpperCase() ){
                        //alert("El número de RUT esta incorrecto");
                        return (false)
                }
                else
                        return (true);
        } else {
                if ( digito_verificador != dig_ver ){
                        //alert("El número de RUT esta incorrecto");
                        return (false)
                }
                else
                        return (true);
        }

}

function verify_ccard(type,inNumber)
{

	if(type == "0")
	{	
		message="No ha seleccionado el tipo de tarjeta de crédito";
		//alert(message);
		return (0);
	}

        total = 1*0;
        tmp = 1*0;

        number = "";
        

        // make sure there are only numbers in the string...
        for(i = 0; i < inNumber.length; i++)
        {
		if(inNumber.charAt(i) >= "0" && inNumber.charAt(i) <= "9")
                {
                        number = number + inNumber.charAt(i);
                }
        }

        if(number.length < 13) return 10; // too short for anything

        first = "" + number.charAt(0);
        second = "" + number.charAt(1);
        third = "" + number.charAt(2);
        firstTwo = first + second;
        firstFour = firstTwo + third + number.charAt(3);

        if(type == "Master" ) // "MASTERCARD"
        {
                if(first != "5" || second < "1" || second > "5")
                        return 11;// invalid Mastercard prefix
                if(number.length != 16)
                        return 21;
        }
        else if(type == "Visa" ) // "VISA"
        {
                if(first != "4")
                        return 12;// invalid Visa prefix
                if(number.length != 13 && number.length != 16)
                        return 22;
        }
        else if(type == "4") // "AMERICAN EXPRESS"
        {
                if(first != "3" || (second != "4" && second != "7"))
                        return 13;// invalid American Express Prefix
                if(number.length != 15) 
                        return 23;
        }
        else if(type == "DISC")
        {
                        if(firstFour != "6011")
                                return 14;// invalid prefix.
                        if(number.length != 16)
                                return 24;
        }
        else if(type == "Dinners") // "DINNERS"
        {
                if(firstTwo != "36"
                        && firstTwo != "38"
                        && (firstTwo != "30" ||
                                (third < "0" || third > "5")))
                {
                        return 15;
                }
                if(number.length != 14)
                        return 25;
        }
        else if(type == "enRoute")
        {
                if(firstFour != "2014"
                        && firstFour != "2149")
                        return 16;// invalid enRoute card
                if(number.length != 15)
                        return 26;
		return 0; // no check sum calculation needed
        }
        else if(type == "JCB")
        {
                if(firstFour != "2131"
                        && firstFour != "1800"
                        && (first != "3") )
                        return 17;
                if(number.length != 16 && first =="3")
                        return 27;
                if(number.length != 15 && first != "3")
                        return 28;
        }
        // now check the credit card suffix and length vs. the type
         // do the check sum
        for(loc = number.length - 2; loc >= 0; loc -= 2)
        {
                total += 1 * number.charAt(loc +1);
                tmp = number.charAt(loc) * 2;
		if(tmp > 9) total += 1;
		total += tmp%10;
        }
	if(number.length % 2 > 0)
	total += 1 * number.charAt(0);

        return (total % 10);
}

