//--------------------------------------------------
// populate script for the Birthdate dropdown menus

// update the day options
var monthLength = new Array(31,29,31,30,31,30,31,31,30,31,30,31);
function populateDays(dropdown, drivenby) {
// reset options to default
	     dropdown.options[30].text = 31;dropdown.options[29].text = 30;dropdown.options[28].text = 29;
// find the selected month
	     for (var i = 0; i < drivenby.options.length; i++) {
	          if (drivenby.options[i].selected == 1) {
	                lastDay = monthLength[i];
	           }
	     }
// set options to days in selected month
		 for (var j = lastDay ; j < dropdown.options.length; j++) {
	           dropdown.options[j].text = lastDay;
	     }
}
//--------------------------------------------------
// build year options for document write
var date = new Date();
var year = date.getYear();
	if (year < 1000) {
	    year += 1900;		
	}
var start_year = parseInt(year) - 112;
var start_option = '';
	
//------------------------------------------------------------------
// Since there is no way to set class properties dynamically 
// this is a special function to do that
// so we can set the style of the active menu easily

// set the var for getElementByClass function
var currentMenu = '';
// to set properties of class object, just call getElementByClass(d) with onLoad function


/* getElementByClass */
var allHTMLTags = new Array();    
function getElementByClass(theClass) {

    //Create Array of All HTML Tags
    var allHTMLTags=document.getElementsByTagName('*');

    //Loop through all tags using a for loop
    for (i=0; i<allHTMLTags.length; i++) {
    	//Get all tags with the specified class name.
    	if (allHTMLTags[i].className==theClass) {
    		allHTMLTags[i].style.background='#f3d4a1';
    		allHTMLTags[i].style.color='#555753';
    		currentMenu = allHTMLTags[i];
    	}    
    }    
}
//----------------------------------------------------
// Validate Product Form

// A utility function that returns true if a string contains only 
// whitespace characters.
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,more)
{

    var msg;
    var empty_fields = "";
    var errors = "";
	var bad_email = "";

    // 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];
       

		if (((e.type == "text") || (e.type == "textarea")) && !e.optional) {
            // first check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + e.name;
                continue;
            }
        }
    }//for

	// special handling on the email address(es)
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(f.Send_To_Email.value) == false){
		bad_email += "\n          Correspondence Email";		
	}
	
	// Now, if there were any errors, then display the messages, and
    // return false to prevent the form from being submitted.  Otherwise return true
    if (!empty_fields && !errors && !bad_email) {
	// set hidden fields with real data and return 
	
	// Get dates
		var myDay  = f.my_day.options[f.my_day.selectedIndex].text;
		var myMonth  = f.my_month.options[f.my_month.selectedIndex].text;
		var myYear  = f.my_year.options[f.my_year.selectedIndex].text;
		f.custom2.value = 'Name: ' + f.Name.value;
		f.custom4.value = 'Birthdate: ' + myMonth + '-' + myDay + '-' + myYear;
		if(more==1) {
			var otherDay  = f.other_day.options[f.other_day.selectedIndex].text;
			var otherMonth  = f.other_month.options[f.other_month.selectedIndex].text;
			var otherYear  = f.other_year.options[f.other_year.selectedIndex].text;
			f.custom6.value = 'Other Name: ' + f.Other_Name.value;
			f.custom8.value = 'Other Birthdate: ' + otherMonth + '-' + otherDay + '-' + otherYear;
		}
		f.custom10.value = 'Send Report To: ' + f.Send_To_Name.value;
		f.custom12.value = 'Send Report Email: ' + f.Send_To_Email.value;		
	
		return confirm("Is all the information correct?\n Hit OK to submit or CANCEL to make changes.");
	}

    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";
    }
	    if (bad_email) {
        msg += "- The following email is invaild:" 
                + bad_email + "\n";
        //if (errors) msg += "\n";
    }
	
    msg += errors;
    alert(msg);
    return false;	
}
//--------------------------------------------------------------------------
// Validate Readings Payment Form
function validate_form_payment(form) {
	if(form.os0.value == '' || form.os0.value.length < 7 || form.os0.value.length > 7) {
		alert('Please include a valid Confirmation Number.');
		return false;
	}
		return confirm("Is all the information correct?\n Hit OK to submit or CANCEL to make changes.");

}
//--------------------------------------------------------------------------

