////////////////////////////////////////////////////////////////////////////////
//
// Field Checking Javascript Functions(Spanish Version)
//
////////////////////////////////////////////////////////////////////////////////
//
// Copyright @ 2007 Casale Media
// All Rights Reserved.
//
// ---------------------------------------------------------------------------
//
// Legal Notice
//
// The information contained within this document is confidential,
// coyprighted and or trade secret. No part of this document may be
// reproduced or distributed in any form or by any means, in whole
// or in part, without the prior written permission of Casale Media.
//
////////////////////////////////////////////////////////////////////////////////

function fieldCheck(form, fields, error) {

	var IE4 = (document.all && !document.getElementById) ? true : false;
	var NS4 = (document.layers) ? true : false;
	var IE5 = (document.all && document.getElementById) ? true : false;
	var N6 = (document.getElementById && !document.all) ? true : false;

        // make fields data accessible
        var fieldAry    = fields.split(',');
        var formObj     = eval('document.' + form);
        var count       = 0;
        var output      = error;

        // check each field
        while(fieldAry[count]) {

                // give vars friendly names
                var field       = eval('document.' + form + '.' + fieldAry[count]);
                var fieldName	= fieldAry[count];
                count++;
                var type        = fieldAry[count];
                count++;
                var max         = fieldAry[count];
                count++;
                var name        = fieldAry[count];
                count++;
                var required    = fieldAry[count];
                count++;

                // based on the field definition, figure out if there is an error present
                var errString = "";

                errString = checkFieldItem( field, type, max, name, required );

                if ( errString != "" ) {
                	if (errString=="campo valor requerido\n") {
                		output = output + " - " + name + "\n";
                	} else {
                		if (errString=="por favor provea una direcci" + unescape("%F3") + "n de correo electr" + unescape("%F3") + "nico v" + unescape("%E1") + "lida\n" && error=="") {
                			output = output + "Por favor provea una direcci" + unescape("%F3") + "n de correo electr" + unescape("%F3") + "nico v" + unescape("%E1") + "lida.\n";
                			alert(output);
                		} else {
                        		output = output + " - " + name + ", " + errString;
                        	}
                        }
                        if (IE5 || N6) {
				document.getElementById(fieldName).style.color='red';
			}
		} else {
			if (IE5 || N6) {
				document.getElementById(fieldName).style.color='#404040';
			}
		}
        }

        // figure out whether there was an error or not && submit
        if(output != error) {
                alert(output);
                return false;
        } else {
                return true;
        }
}

function checkFieldItem(field, type, max, name, required) {

	var freeEmail = /hotmail\.com|aol\.com|yahoo\.com|msn\.com|cs\.com|earthlink\.net|yahoo\.co\.uk|juno\.com|attbi\.com|netscape\.net|bellsouth\.net|webtv\.net|rter\.net|cox\.net|excite\.com|rediffmail\.com|yahoo\.ca|sbcglobal\.net|sympatico\.ca|yahoo\.co\.in|adelphia\.net|ntlworld\.com|hot\.ee|verizon\.net|wmconnect\.com|yahoo\.fr|prodigy\.net|att\.net|optonline\.net|rogers\.com|worldnet\.att\.net|web\.de|yahoo\.com\.au|netzero\.net|lycos\.com|alltel\.net|mindspring\.com|caramail\.com|bigpond\.com|telus\.net|btopenworld\.com|shaw\.ca|mchsi\.com|yahoo\.com\.sg|eudoramail\.com|abv\.bg|interia\.pl|blueyonder\.co\.uk|mail\.ee|btinternet\.com|tampabay\.rr\.com|earthlink\.com|frontiernet\.net|tds\.net|ameritech\.net|bright\.net|netzero\.com|latinmail\.com|indiatimes\.com|cogeco\.ca|yahoo\.com\.br|cfl\.rr\.com|telusplanet\.net|yahoo\.com\.mx|pandora\.be|insightbb\.com|chartermi\.net|mail\.com|email\.com|jippii\.fi|optusnet\.com\.au|yahoo\.com\.hk|ig\.com\.br|libero\.it|centurytel\.net|webmail\.co\.za|yahoo\.de|blackplanet\.com|plasa\.com|freemail\.hu|yahoo\.es|pacbell\.net|wanadoo\.fr|rochester\.rr\.com|yandex\.ru|gaggle\.net|cableone\.net|citlink\.net|swbell\.net|ev1\.net|cox-internet\.com|iwon\.com|talk21\.com|one\.lt|t-online\.de|neo\.rr\.com|videotron\.ca|mynet\.com|ns\.sympatico\.ca|twcny\.rr\.com|lycos\.co\.uk/;
	
        // check required fields
        if ( required == "1" && field.value == "" ) {
                return "campo valor requerido\n";
        }

        // if this field is not required, && the value is "", then just return "", not need to do the field check
        if ( required == "0" && field.value== "" ) {
                return "";
        }

        // check text filed's length
        if ( (type  == "text" || type == "password" || type == "select" || type == "file" )
                && field.value.length > max && max != 0 ) {
                return "excede la longitud\n";
        }

        // check email field format
        if ( (type == "email" || type == "emailNotFree") && (!field.value.match(/^([a-zA-Z0-9_\.\-\+\&\=])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,9})$/))) {
                return "por favor provea una direcci" + unescape("%F3") + "n de correo electr" + unescape("%F3") + "nico v" + unescape("%E1") + "lida\n";
        }
	
	// check emailNotFree field format
	if ( type == "emailNotFree" && (freeEmail.test(field.value))) {
                return "por favor provea una direcci" + unescape("%F3") + "n de correo electr" + unescape("%F3") + "nico no-gratuita\n";
        }

        // check url field format
        if ( type == "url" && (!field.value.match(/^http:\/\/.*\.([a-zA-Z0-9]{2,9})([\/]{0,1}).*$/)) ) {
                return "url no v" + unescape("%E1") + "lido\n";
        }

        // check int field format
        if ( type == "int" ) {
                var intValue = field.value.replace(/,/g, "");
                if (!intValue.match(/^\d+$/) ) {
                        return "valor entero no v" + unescape("%E1") + "lido\n";
                }
                // check if the value of int field exceed max
                if ( parseInt(intValue) > max && max !=0 ) {
                        return "excede el m" + unescape("%E1") + "ximo\n";
                }
        }

        // all the integers must be positive integers
        if ( type == "int" && field.value< 0 ) {
                return "necesita un entero positivo\n";
        }

        // check rate(field type: text) format
        //if ( $field = "rate" && !(field.value =~ m/^\d+\.\d*$|^\d*\.\d+$|^\d+$/) ) {
        //      return "viper_fieldcheck_bad_rate";
        //}

        if ( type == "radio" ) {
                // handler for multiple radio boxes or check boxes
                var boxCount    = 0;
                var isError     = 1;
                while (boxCount < field.length) {
                        if (field[boxCount].checked == true) {
                                isError = 0;
                        }
                        boxCount++;
                }
                if ( isError == 1 ) return "seleccione al menos un valor\n";
        }

        if (type == "checkbox") {
                // handler for single check box
		var checkedCount = 0;
		for (var i=0; i<field.length; i++) {
			if (field[i].checked) checkedCount++;
		}
		if (!checkedCount) {
                        return "seleccione la casilla\n";
                }
        }

        return "";
}

