/*Utils: general functions
**/


/**
 *  Unchecked all the options from the checkbox or the radio.
 *
 */
function clearChecks( object )
{
    if ( object && object.length > 0 )
    {
        for ( var i = 0; i < object.length; i++ )
        {
            object[i].checked = false;
        }
    }
    else
    {
        object.checked = false;
    }
}

function checkMail( x )
{
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	return filter.test(x);
}

function checkPhoneNumber( x )
{
	var filter  = /^[0-9\(\)\-\s]+$/;
	return filter.test(x);
}



/**
 * Count the number of checked objects ( checkbox & radio )
 *
 */
function countChecked( object )
{
    
    var selected = 0;
    for (var i=0; i < object.length; i++) {
        if( object[i].checked )
            selected ++;
    }

    return selected;

}

/**
 *  Verify if an array of radio buttons have one selected.
 *
 */
function isChecked(radio) {
   var i;
   
   if( !radio ){
       return false;
   }

   if( radio.checked ){
       return true;
   }

   for (i=0; i<radio.length; i++) {
      if (radio[i].checked) {
          return true;
      }
   }
   return false;
}

/**
 * This function return true if the string is empty.
 *
 */
function isblank(s)
{

    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

// This is the function that performs form verification. It will be invoked
// from the onSubmit() event handler. The handler should return whatever
// value this function returns.
function verify(f)
{

    var msg;
    var empty_fields = "";
    var errors = "";



    // Loop through the elements of the form, looking for all
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {

        var e = f.elements[i];

		//alert( e.name + " = " + e.type );

        if ((	(e.type == "hidden") || (e.type == "radio") || (e.type == "select-one")
			 || (e.type == "password") || (e.type == "text") || (e.type == "textarea")) ) {

            var value = e.value;
            if( e.type == "select-one")
                value = e.options[e.selectedIndex].value;


            if( e.type == "radio"){
                if( !isChecked( e ) )
                    value = ""
            }



            // first check if the field is empty
            if( !e.optional && e.optional != null ){


                if ( (value == null) || (value == "") || isblank(value) )
                {
                    empty_fields += "\n          " + e.displayName;
                    continue;
                }


            }

            // Now check for fields that are supposed to be numeric.
            // If the field isBlank then it will not be checked.
            if ( !isblank(value) && (e.numeric || (e.min != null) || (e.max != null)) ) {
				var v = parseFloat(e.value);

                if (isNaN(e.value) ||
                    ((e.min != null) && (v < e.min)) ||
                    ((e.max != null) && (v > e.max))) {
                    errors += "- The field " + e.displayName + " must be a number";
					if (e.min != null)
                        errors += " that is greater than " + e.min;
                    if (e.max != null && e.min != null)
                        errors += " and less than " + e.max;
                    else if (e.max != null)
                        errors += " that is less than " + e.max;
                    errors += ".\n";
                }
            }

            // Check if the field is a valid email
            if ( !isblank(value) && e.email ) {
                if ( !checkMail( value ) )
                    errors += "- The field " + e.displayName + " must be a valid email address.\n";
            }

            // Check if the field is equal than another field.
            if( e.equal != null && e.value != e.equal.value ){
                errors += "- The fields " + e.displayName + " and " + e.equal.displayName + " must be equals.\n";
			}

			// Check if the field is a valid phone number
            if ( !isblank(value) && e.phone ) {
                if ( !checkPhoneNumber( value ) )
                    errors += "- The field " + e.displayName + " must be a valid phone number.\n";
			}


        }

    }

    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted.
    // Otherwise return true.
    if (!empty_fields && !errors) return true;

    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following error(s).\n";
    msg += "Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- The following required field(s) are empty:"
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
}
