
var esc = "\\\\";
var space = "\\s";
var openBR = "\\[";
var closeBR = "\\]";
var nonASCII = "\\x80-\\xff";
var ctrl = "\\000-\\037";

var illegal = space + "\\(\\)<>\\@,;:\\\"" + esc + openBR + closeBR + nonASCII + ctrl;
var atom = "[^" + "\\." + illegal + "]";

var localPart = atom + "+(\\.{1}" + atom + "+)*";
var domain = atom + "+(\\.{1}" + atom + "+)+";
var email = "^" + localPart + "\@" + domain + "$";
var reEmail = new RegExp( email);

var reWhitespace = /^\s+$/
var reInteger = /^\d+$/

var mP = "No ingresó ningún valor en el campo "
var mPMax = "El campo '"
var mSuffix = " . Es un campo requerido. Por favor ingrese un valor."
var mSuffixMax = "' debe tener un máximo de ";
var mSuffixMax2 = " caracteres.";
var mRadio = "Debe especificar una opción.";

var defEOK = false

var invalidF = null;

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s) {
    return ( isEmpty(s) || reWhitespace.test(s));
}

function isInteger (s){

	var i;

    if (isEmpty(s))
       if (isInteger.arguments.length == 1) return defEOK;
       else return (isInteger.arguments[1] == true);

    return reInteger.test(s)
}


function getFrmFldType( f) {
	var t;

	t = f.type;
	if(( t == null) && ( f[ 0] != null))
		t = f[ 0].type;

	return t;

}

function setFocus( f) {

	var t = getFrmFldType( f);

	if(	( t == "radio") && f.length) {
		f[ 0].focus();
	} else {
		f.focus();

		if( f.select != null)
		    f.select();

	}

}

var lastMarked;
var lastColor;

function anyColorInput( f, c) {
    if( f.label)
		f.label.style.color = c;
	else if( f.parentElement != null)
		f.parentElement.style.color = c;
	else if( f.length && ( f[ 0] != null)) {
		if( f[ 0].parentElement != null)
			for( var i = 0; i < f.length; i++)
				f[ i].parentElement.style.color = c;
	}

	lastMarked = f;
}

function colorInput( f) {

	if( lastMarked != null) {
		anyColorInput( lastMarked, "#000000");
		lastMarked = null;
	}

	anyColorInput( f, "#FF0000");
	lastMarked = f;
}

function warnEmpty (f, s, uM, cI)
{
	var result;
	var fieldType = getFrmFldType( f);

	if( f.uM != null)
		uM = f.uM;
	else if( fieldType == "radio") {
		if( uM == null)
			uM = mRadio;
	}

	setFocus( f);
	result = warnEmptyNoFocus( s, uM);
	setFocus( f);

    if( cI)
        colorInput( f);

    invalidF = f;
    setTimeout( "setFocus( invalidF);", 1);

	return result;
}

function warnEmptyNoFocus( s, uM) {
	if( uM == null)
    	alert( mP + s + mSuffix);
    else
    	alert( uM);

    return false
}


function checkString (f, s, eOK, mL)
{
    if (checkString.arguments.length == 2) eOK = defEOK;
    if (checkString.arguments.length == 3) mL = 0;

    if ((eOK == true) && (isEmpty(f.value))) return true;
    if (isWhitespace(f.value))
       return warnEmpty (f, s);

    if( ( mL > 0) && ( f.value.length > mL))
    	return warnInvalid( f, mPMax + s + mSuffixMax + mL + mSuffixMax2);
    else return true;
}

function checkSelectValue (f, s, eOK)
{
	var lV;

	with( f.options[ f.selectedIndex]) {
		lV = value;
		if( ( lV == null) || ( lV.length == 0))
			lV = text;
	}

    if (checkSelectValue.arguments.length == 2) eOK = defEOK;
    if ((eOK == true) && (isEmpty( lV))) return true;
    if (isWhitespace( lV))
       return warnEmpty (f, s);
    else return true;
}


function checkPositiveInteger (f, s, eOK) {
    var v = f.value;

    if (checkPositiveInteger.arguments.length == 2) eOK = defEOK;
    if ((eOK == true) && (isEmpty( v))) return true;
    if (!isInteger( v))
       return warnInvalid (f, s);
    else if ( !isPositiveInteger( v))
       return warnInvalid (f, s);
    else return true;
}

function checkInteger (f, s, eOK) {
    var v = f.value;

    if (checkInteger.arguments.length == 2) eOK = defEOK;
    if ((eOK == true) && (isEmpty( v))) return true;
    if (!isInteger( v))
       return warnInvalid (f, s);
    else return true;
}

function checkEmail (f, eOK)
{
    var v = f.value;

    if (checkEmail.arguments.length == 1) eOK = defEOK;
    if ((eOK == true) && (isEmpty( v))) return true;
    else if( !isEmail( v, false)) return warnInvalid (f, iEmail);
    else return true;
}

function warnInvalid (f, s, cI)
{
	var result;
	var fieldType = getFrmFldType( f);

	setFocus( f);
	alert( s);
	setFocus( f);

    if( cI)
    	colorInput( f);

    invalidF = f;
    setTimeout( "setFocus( invalidF);", 1);

	return false;
}

function isPositiveInteger (s)
{   var secondArg = defEOK;

    if (isPositiveInteger.arguments.length > 1)
        secondArg = isPositiveInteger.arguments[1];

    return (isInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}

function getRadioBtnValue( f) {
	if( f.length) {
		for( var i = 0; i < f.length; i++) {
			if( f[ i].checked) {
				return f[ i].value;
			}
	    }
	} else
		if( f.checked) {
			return f.value;
		}

	return -1;

}


function checkRadioBtn( f, eOK, uM) {
	var isRE;

	if ( checkRadioBtn.arguments.length == 1) {
		eOK = defEOK;
		uM = "";
	}
	isRE = ( getRadioBtnValue( f) == -1);

	if( isRE)
		if ( eOK == true)
			return true;
		else
	    	return warnEmpty( f, null, uM);
	else return true;
}

function enableFrmFld( f, enable) {

	if( f.name == null) {
		if( f[ 0].name != null) {
			if( f[ 0].disabled != null) {
				for( var i = 0; i < f.length; i++)
					f[ i].disabled = !enable;
			}
		}
	} else if( f.disabled != null)
		f.disabled = !enable;
}

function checkFieldLength( f, mL, ccf) {
	var s = f.value;
	var l;
	var r;

	r = ( s == null);
	if( !r) {
        l = s.length;
		r = ( l < mL);
		if( !r) {
			f.value = s.substring( 0, mL);
		}

        if( ccf != null)
            ccf.value = l;
	}

	return r;
}

var FormCheck = true;
