// Name		toggle
//
// Description	Hides/Displays an input text, based on the previous button selection
//
// Parameters   field id		The field id of the displayed/hidden text
//		value		The value field will either be ""(for displaying 
//				text field) or will be none(for hiding the text field)
// Returns	true/false	Success of operation
//

function toggle(id, value) {
  	var displayHiddenText = document.getElementById(id);
  	  if (displayHiddenText==null) {
  		return;
  	}
  	  displayHiddenText.style.display = value;
  }
  
// Name		initialize
//
// Description	Sets the initial value of a field
//
// Parameters   field id	The field id for which the initial value is being set
//		value		The initial value to be set for the field id
// Returns	true/false	Success of operation
//  
  

  function initialize(){
  	toggle('showWorkExperienceText','none');
  	toggle('describeCarMakeModel','none');  	
  	
  	var mydate=new Date();
	var theyear=mydate.getYear();
	if (theyear < 1000){
		theyear+=1900;
	}
	var themonth=mydate.getMonth()+1;
	if (themonth<10){
		themonth="0"+themonth;
	}
	var theday=mydate.getDate();
	if (theday<10){
		theday="0"+theday;
	}
	//////EDIT below three variable to customize the format of the date/////
	
	var displayfirst=theyear;
	var displaysecond=themonth;
	var displaythird=theday;
	
	document.getElementById('prospect_today').value=displayfirst+"-"+displaysecond+"-"+displaythird;
  
  }
  	

  

// Name		validate_select_blank
//
// Description	Checks if a mandatory form select field is blank.  If so,
//		displays error and resumes to the field.  
//
// Parameters	field           The field to check for blank
//              description     The field description to display in the
//                              error message
//              submit          Flag if function should attempt to reset focus
//                              on the field (workaround for JavaScript bug)
//
// Returns	true/false      Success of operation
//

function validate_select_blank(field, description, submit)
{
	
	if (field.options[field.selectedIndex].value == '' ){           
    	window.alert("Please select " + description);
            
    	if(submit) {
        	field.focus();
        }
            
        return false;
    }
    return true;
}


// Name		validate_value_blank
//
// Description	Checks if a mandatory form field is blank.  If so,
//		displays error and resumes to the field.  Strips trailing
//              blanks too.
//
// Parameters	field           The field to check for blank
//              description     The field description to display in the
//                              error message
//              submit          Flag if function should attempt to reset focus
//                              on the field (workaround for JavaScript bug)
//
// Returns	true/false      Success of operation
//

function validate_value_blank (field, description, submit)
{
   
    // Clear any previous status message
    //
    window.defaultStatus = "";

    // Nav 4 does not support field.value for select fields, (IE does)
    // so get it the "official" way
    //
    
    

        value = field.value;

    // Strip trailing blanks from field value
    //
    value = strip(value);

    // Look for any non-blank character to be ok
    //
    for (var i = 0; i < value.length; i++) {

        // If a non-blank found, assign striped value to field
        // and return successful
        //
        if (value.charAt(i) != " ") {
            field.value = value;
            return true;
        }
    }

    // if a text field and only blanks where found, make it empty
    //
	if (field.type == "text")
	    field.value = "";

    // Setup the error message
    //
	var message = "Please enter '" + description + "'.";
	
    // Only refocus on the field if trigger during onSumit.  Due to a bug in
    // JavaScript in IE and Nav, this can cause endless loops otherwise.
    //
     if ( field.value == "0") submit = false;
           if (submit) {
       
               // Display message, set focus to the field
               //
           	window.alert(message);
       
               // If the field is a text field, select the current input
               //
           	if (field.type == "text")
               	field.select();
       
               field.focus();
       
           // Else just put a message in the status bar
           //
           } else
               window.defaultStatus = message;
       
   	return false;
    
} // end function validate_value_blank






// Name		    validate_value_rad
//
// Description	Checks if a mandatory radiofield is blank.  If so,
//		        displays error and resumes to the field.
//
// Parameters	field           The radiofield to check a choice is selected
//              description     The field description to display in the
//                              error message
//              submit          Flag if function should attempt to reset focus
//                              on the field (workaround for JavaScript bug)
//
// Returns	    true/false      Success of operation
//
function validate_value_rad (field, description, submit)
{
    // Clear any previous status message
    //
    window.defaultStatus = "";

    // Loop thru the array of buttons checking if one has been chosen
    var pension_chosen = false;
    for (var i = 0; i < field.length; i++) {
        if (field[i].checked=="1") {
            pension_chosen = true;
        }
    }
    if (pension_chosen=="1") {
        return true;
    }

    // Setup the error message
    //
	var message = "Please select '" + description + "'.";
	
    // Only refocus on the field if trigger during onSumit.  Due to a bug in
    // JavaScript in IE and Nav, this can cause endless loops otherwise.
    //
    if (submit) {

        // Display message, set focus to the field
        //
    	window.alert(message);

        field[0].focus();

    // Else just put a message in the status bar
    //
    } else
        window.defaultStatus = message;

	return false;

} // end function validate_value_rad



 
// Name		validate_value_date
//
// Description	Checks if a form field is a valid date in mm/dd/yy[yy] format.  If not,
//              displays error in status bar optionally resumes to the field.
//
// Parameters	field           The field to check for blank
//              description     The field description to display in the
//                              error message
//              submit          Flag if function should attempt to reset focus
//                              on the field (workaround for JavaScript bug)
//
// Returns	    true/false      Success of operation
//
// Modification History
// 22/10/99 Drew Cox    Add extra year check for Ingres 6.4 date range 1582-2300
//
function validate_value_date (field, description, submit)
{
    // Clear any previous status message
    //
    window.defaultStatus = "";

    // Get character value of field
    //
    var value = ""+field.value;

    // If the field is blank, then it is ok
    //
    if (value == "")
        return true;

    // Reset error flag
    //
    var error      = false;
    var error_text = "";

    // Dummy loop to allow break if invalid form detected (almost a goto !)
    //
    for (var dummy = 1; dummy < 2; dummy ++) {
    
        // Split the date into 3 numbers on the "/" char
        //
        var date_array = value.split("/");

        // If there wasn't 3 parts to the date, it's invalid
        //
        if (date_array.length != 3) {
            error_text = "Date not in mm/dd/yyyy format";
            error      = true;
            break;
        }

        // Extract the day
        //
        var day = parseInt(date_array[1], 10);

        // If day is not a number, it is invalid
        //
        if (isNaN(day)) {
            error_text = "Day is not a number";
            error      = true;
            break;
        }        

        // Validate day
        //
        if ((day < 1) || (day > 31)) {
            error_text = day + " is an invalid day of the month";
            error      = true;
            break;
        }    

        // Extract the month
        //
        var month = parseInt(date_array[0], 10);

        // If month is not a number, it is invalud 
        //
        if (isNaN(month)) {
            error_text = "Month is not a number";
            error      = true;
            break;
        }

        // Validate month
        //
        if ((month < 1) || (month > 12)) {
            error_text = month + " is an invalid month";
            error      = true;
            break;
        }

        // Extract the year
        //
        var year = parseInt(date_array[2], 10);

        // If year is not a number, it is invalud 
        //
        if (isNaN(year)) {
            error_text = "Year is not a number";
            error      = true;
            break;
        }

        // Validate input year
        //
        if ((year < 0) || (year > 3000)) {
            error_text = year + " is an invalid year";
            error      = true;
            break;
        }

        // Convert year to 4 digits if it isn't
        //
        if (year < 100) {

            // Get current year 
            //
            var century = new Date().getYear();

            // Handle stupid JS thing with 19 or 2001 here
            //
            if (century < 100)
                century += 1900;

            // Extract century from current year
            //
            century = (century - (century % 100));

            // Add current century on to year
            //
            year += century;

        } // end if year < 100


        // Validate output year (ingres 6.4 date range)
        //
        if ((year < 1582) || (year > 2300)) {
            error_text = year + " is an invalid year";
            error      = true;
            break;
        }

        //
        // Do cross validation of day-month-year
        //
        
        // Validate 30 day months
        //
        if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {

            if ((day < 1) || (day > 30)) {
                error_text = day + " is invalid day for month " + month;
                error      = true;
                break;
            }
        }

        // Validate February and take into account leap years
        //
        else if (month == 2) {

            // If leap year (remember not on centries, but is on milleniums)
            //
            if ((year%4 == 0) && ((year%100 != 0) || (year%1000 == 0))) {
                
                if ((day < 1) || (day > 29)) {
                    error_text = day + " is invalid day for month " + month;
                    error      = true;
                    break;
                }
            } else {
                if ((day < 1) || (day > 28)) {
                    error_text = day + " is invalid day for month " + month;
                    error      = true;
                    break;
                }                
            } // else not leap year

        } // if february

        //
        // Reformat date into mm/dd/yyyy
        //


        // Replace the field value with the correct format
        //
        field.value = right("00"+month, 2) + "/" + 
                      right("00"+day, 2) + "/" + 
                      right("0000"+year, 4);

    } // dummy loop for validation
    
    // If the field is invalid display message and set focus to the field
    //
	if (error) {
	    
		var message = "The " + description + " is invalid. " +
		              error_text + ".";

        // Only submit on the field is flag is passed.  Due to a bug in
        // JavaScript in IE and Nav, this should onyl be done in the OnSubmit event
        //
        if (submit) {
    		window.alert(message);
    		field.select();
    		field.focus();

        // Else just put a message in the status bar
        //
        } else 
            window.defaultStatus = message;
		
		return (false);
		
	} // if error
	
	return (true);
	
} // end function validate_value_date




// Name		    validate_value_integer
//
// Description	Checks if a form field is a valid integer.  If not,
//		        displays error and resumes to the field.
//
// Parameters	field           The field to check for integer
//              description     The field description to display in the
//                              error message
//              submit          Flag if function should attempt to reset focus
//                              on the field (workaround for JavaScript bug)
//
// Returns	    true/false      Success of operation
//
function validate_value_integer (field, description, submit)
{
    // Clear any previous status message
    //
    window.defaultStatus = "";

    var value  = field.value;
    var error  = false;
    var found  = false;
    var skipto = 0;

    // Check for anything other than numerals 0-9, skipping leading blanks
    //
    for (var j = 0; j < value.length; j++) {
    
        // Skip LEADING blanks, but once we've found a valid char
        // another blank becomes an error
        //
        if (value.charAt(j) == " ")
        
            if (found) {
                error = true;
                break;
            } else
                skipto = j + 1;

        // If a valid char is found, set flag to make any further blanks
        // an error.
        //
        else if ((value.charAt(j) >= "0") && (value.charAt(j) <= "9"))
            found = true;

        // Else the char is invalid
        else {
            error = true;
            break;
        }
            
    } // end for

    // If the field is invalid, display message, select field value,
    // set focus to the field and return unsuccessful.
    //
	if (error)	{
	    
		var message = "The field " + description + " is not a valid number.";

	    // Only submit on the field is flag is passed.  Due to a bug in
        // JavaScript in IE and Nav, this should onyl be done in the OnSubmit event
        //
        if (submit) {
    		window.alert(message);            
    		field.select();
    		field.focus();
		} else 
            window.defaultStatus = message;
		
		return (false);
		
	} // if error

	// Remove any leading spaces
	//
	if (skipto >= value.length)
	    value = "";
    else if (skipto > 0)
    	value = value.substring(skipto);

    // Put modified value back on the form
    //
    field.value = value;

    // Return successful
	return (true);
	
} // end function validate_value_integer



// Name		    validate_value_money
//
// Description	Checks if a form field is a valid money amount.  If not,
//		        displays error and resumes to the field.    *** Needs work
//
// Parameters	field           The field to check for money
//              description     The field description to display in the
//                              error message
//              submit         Flag if function should attempt to reset focus
//                              on the field (workaround for JavaScript bug)
//
// Returns	    true/false      Success of operation
//
function validate_value_money (field, description, submit)
{
    // Clear any previous status message
    //
    window.defaultStatus = "";

    var value       = field.value;
    var error       = false;
    var found_point = false;    //

    // Check for anything other than numerals 0-9 and '.'
    //
    for (var j = 0; j < value.length; j++) {

        if (value.charAt(j) == ".")        
            if (found_point)
                error = true;
            else
                found_point = true;
        else if ((value.charAt(j) < "0") || (value.charAt(j) > "9"))
            error = true;
          
    } // end for

    // If the field is blank, display message and set focus to the field
    //
	if (error)	{
	    
		var message = "The " + description + " is not a valid money amount.";

        // Only submit on the field is flag is passed.  Due to a bug in
        // JavaScript in IE and Nav, this should onyl be done in the OnSubmit event
        //
        if (submit) {
    		window.alert(message);
    		field.select();
    		field.focus();
		} else 
            window.defaultStatus = message;
		
		return (false);
		
	} // if error
	
	return (true);
	
} // end function validate_value_money



// Name		    validate_value_float
//
// Description	Checks if a form field is a valid float amount.  If not,
//		        displays error and resumes to the field.    *** Needs work
//
// Parameters	field           The field to check for money
//              description     The field description to display in the
//                              error message
//              submit         Flag if function should attempt to reset focus
//                              on the field (workaround for JavaScript bug)
//
// Returns	    true/false      Success of operation
//
function validate_value_float (field, description, submit)
{
    // Clear any previous status message
    //
    window.defaultStatus = "";

    var value       = field.value;
    var error       = false;
    var found_point = false;    //

    // Check for anything other than numerals 0-9 and '.'
    //
    for (var j = 0; j < value.length; j++) {

        if (value.charAt(j) == ".")        
            if (found_point)
                error = true;
            else
                found_point = true;
        else if ((value.charAt(j) < "0") || (value.charAt(j) > "9"))
            error = true;
          
    } // end for

    // If the field is blank, display message and set focus to the field
    //
	if (error)	{
	    
		var message = "The " + description + " is not a valid decimal amount.";

        // Only submit on the field is flag is passed.  Due to a bug in
        // JavaScript in IE and Nav, this should onyl be done in the OnSubmit event
        //
        if (submit) {
    		window.alert(message);
    		field.select();
    		field.focus();
		} else 
            window.defaultStatus = message;
		
		return (false);
		
	} // if error
	
	return (true);
	
} // end function validate_value_float




// Name		    check_field_length
//
// Description	Function to validate the maximum length of text in a textarea.
//
// Parameters	field     		The field to validate the length of
//              maxLength   	The maximum length of text
//              description 	The displayed name of the field
//
// Returns	    true/false      Success of operation
//
function check_field_length(field, maxLength, description) {

    if (field.value.length > maxLength) {

      	window.alert(
		"The text in " + description + " has exceeded the maximum number of "
       		+ maxLength + " characters.");

	field.focus();
	return false;
	
    } else {
    
    	return true;
    	
    } // if
    
} // check_field_length




// Name		    check_field_min_length
//
// Description	Function to validate the minimum length of text in a textarea.
//
// Parameters	field     		The field to validate the length of
//              minLength   	The minimum length of text
//              description 	The displayed name of the field
//
// Returns	    true/false      Success of operation
//
function check_field_min_length(field, minLength, description) {

    if (field.value.length < minLength) {

      	window.alert(
		"The text in " + description + " must be at least " + minLength + " characters.");

	field.focus();
	return false;    
	
    } else {
    
    	return true;
    	
    } // if
    
} // check_field_min_length



// Function to strip trailing blanks from a string
//
function strip(string)
{
    var tmp_string = "";
    var out_string = "";

    // Loop through input string
    //
    for (var i = 0; i < string.length; i ++) {

        // If a blank char, copy to holding string, pending further non-blank
        // chars
        //
        if (string.charAt(i) == " ")
            tmp_string += string.charAt(i)

        // Else non-blank char, copy it to the output string
        //
        else {

            // If there are some blanks cached in the temp string, append
            // them first and clear the cache
            //
            if (tmp_string != "") {
                out_string += tmp_string;
                tmp_string = "";
            }

            // Copy character to out string
            //
            out_string += string.charAt(i)
            
        } // else
        
    } // for 

    // Return the output string
    //
    return out_string;

} // end function strip





// Function to get right-most chars of string
//
function right(string, num_chars)
{
    if (num_chars >= string.length)
        return new String(string);
        
    return string.substr((string.length - num_chars), num_chars);
}


/*
* Calling samples
*
<script src="/scripts/validate.js" type="text/javascript" language="javascript"></script>

<script type="text/javascript" language="JavaScript"> 

    // Validate form fields
    //
    function CheckForm(thisForm)
    {
        // Check for blank mandatory fields and valid data types    
        //
        if (validate_value_blank(thisForm.fieldname, 'field description', true) != true ||
            validate_value_float(thisForm.fieldname, 'field description', true) != true 
        ) return false;
    }

</SCRIPT>

    <form method="post" action="" onsubmit="return CheckForm(this);">
    
    <input type="submit" name="action" value="CONTINUE &gt;&gt;" onclick="return CheckForm(thisform);">
    
    <a href="" onclick='openWindow("url/for/window/source,"windowNew");'>click here</a>     
    
*
*/


