

var STATE_CODE_LEN = 2;
var NAIC_CODE_LEN = 5;
var PHONE_NUM_SPLITTER = '-';
var DATE_SPLITTER = '/';
var BASE_10 = 10;
var TEXT_AREA = "TEXTAREA";


String.prototype.trim = function() {
    a = this.replace(/^\s+/, '');
    return a.replace(/\s+$/, '');
};



// This function blocks any return key to be processed unless the "return key" was entered in text area.
// Pre-condition  : None.
// Post-condition: Function returns false if "return key" was pressed and the source control is not type of text box.   Otherwise return true.
function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  
   //If the location where enter key was pressed is not a text area then don't allowed the enter key to be processed.
   if(node.tagName != TEXT_AREA && evt.keyCode == 13) { return false;} 
 
}

// At loading time, link stop return key function to document itself.  So return key can be blocked from all controls except text box type.
document.onkeypress = stopRKey;


// This function check whether the value in the given field is a valid phone number or not.  
// Pre-condition  : None.
// Post-condition: If valid phone number then return true, otherwise return false value.
function IsPhoneNumber(field, required, message ){
	var oReg = /^\d{3}-\d{3}-\d{4}$/;
	var phoneNumberMatch;
	
	// If field is empty and  it is a required field then return false, otherwise return true.
	if(field.value == null || field.value.trim() == ""){
	
		if(required==true){
			alert(message);
			field.focus();
			return false;
		}
		else
			return true;
	}	

	// Check phone number.
	phoneNumberMatch = field.value.match(oReg) != null ? true : false;
  
	//If phone number is not matching then display a warning message and direct user to the field.
	if(!phoneNumberMatch){
		alert(message);
		field.focus();
	}
  
	return phoneNumberMatch;	
}

//	This function checks date value while users entering in.
//	Pre-condition  : None.
//	Post-condition : Function changes the field value with formatted version of date value. 
function ActiveCheckDate(field){
	var MONTH_JAN = 1;
	var MONTH_DEC = 12;
	var DAY_1 = 1;
	var DAY_31 = 31;
	var YEAR_START = 2008;
	var YEAR_END = 9999;
	var LEN_MONTH = 2;
	var LEN_DAY = 2;
	var LEN_YEAR = 4;
	
	var MODE_MONTH = 0;
	var MODE_DAY = 2;
	var MODE_YEAR = 4;
	var MODE_EXTRAPART;
	
	// Initialize month parts and other necessary variables.
	var monthPart = "";
	var dayPart = "";
	var yearPart = "";
	
	var partMode = MODE_MONTH;
	var modeCharCount = 0;
	var curChar = "";
	
	// Loop through each character in the field.
	for(var i=0; i<field.value.length;i++){
		
		//Get a character
		curChar = field.value.substring(i,i+1);
		
		// If mode is Month checking mode then treat current char as a part of month value.
		if(partMode == MODE_MONTH){
			
			//Check this is number or not.  If not just skip over.
			if(IsNumericValue(curChar)){				
				monthPart += curChar;
				modeCharCount++;
			}
		
			// Check whether current character count for month mode is over 2 characters or not.
			if(modeCharCount >= LEN_MONTH) {
				
				// Check whether the parsed month value is within month range or not.
				if(parseInt(monthPart,BASE_10) < MONTH_JAN || parseInt(monthPart,BASE_10) > MONTH_DEC){
					monthPart = "";
					modeCharCount = 0;			
				}
				else		
					partMode = MODE_DAY; modeCharCount =0;
			}
		
		}
		
		// if mode is Day checking mode then treat current char as a part of day value.
		else if(partMode == MODE_DAY){
			
			//Check whether current character is a numeric value or not.
			if(IsNumericValue(curChar)){				
				dayPart += curChar;
				modeCharCount++;
			}
		
			
			// Check whether current character count for month mode is over 2 characters or not.
			if(modeCharCount >= LEN_MONTH) {
				
				//Check whether the parsed day value is within day range or not.
				if(parseInt(dayPart,BASE_10) < DAY_1 || parseInt(dayPart,BASE_10) > DAY_31){
					dayPart = "";
					modeCharCount = 0;			
				}
				else		
					partMode = MODE_YEAR; modeCharCount =0;
			}		
		}
		
		// if mode is Year checking mode then treat current char as a part of year value.
		else if(partMode == MODE_YEAR){
			
			//Check whether current character is a numeric value or not.
			if(IsNumericValue(curChar)){				
				yearPart += curChar;
				modeCharCount++;
			}
		
			
			// Check whether the parsed year value is within the year range or not.
			if(modeCharCount >= LEN_YEAR) {
				
				if(parseInt(yearPart,BASE_10) < YEAR_START || parseInt(yearPart,BASE_10) > YEAR_END){
					yearPart = "";
					modeCharCount = 0;			
				}
				else		
					partMode = MODE_EXTRAPART; modeCharCount =0;
			}					
		}
		
		
	
	}
	
	
	//Combine parsed date parts together to form a complete formatted date value.
	if(partMode == MODE_MONTH)
		field.value = monthPart;
	else if(partMode == MODE_DAY)
		field.value = monthPart + (dayPart.length > 0 ? (DATE_SPLITTER + dayPart) : "");
	else 
		field.value = monthPart + DATE_SPLITTER + dayPart + (yearPart.length > 0 ? (DATE_SPLITTER + yearPart):"");
	
}


// This function actively check phone number as user enters in.  It will automatically format the phone number for the user.
// Pre-condition  : None.
// Post-condition : Change field value to the formatted version of phone number while filtering out invalid values.
function ActiveCheckPhoneNumber(field){
	
	// Function Constants
	var LEN_AREA_CODE = 3;
	var LEN_PREFIX_CODE = 3;
	var LEN_SUFIX_CODE = 4;
	var MODE_AREA = 0;
	var MODE_PREFIX = 1;
	var MODE_SUFIX = 2;
	var MODE_EXTRA = 3;
	
	// Declare and initialize local variables for the phone number processing.
	var areaCodePart = "";
	var prefixCodePart = "";
	var sufixCodePart = "";
	var newPhoneNumber = "";
	
	var partMode = MODE_AREA;
	var modeCharCount = 0;
	
	// Loop through each character in the field.
	for(var i=0; i<field.value.length;i++){
		
		// Extract Area code from the value.
		if(partMode == MODE_AREA){
			
			//Check whether the value is numeric or not
			if(IsNumericValue(field.value.substring(i,i+1))){
				areaCodePart += field.value.substring(i,i+1);
				modeCharCount++;

			}
			
			// If Area code is extracted then move on to next step.
			if(modeCharCount >= LEN_AREA_CODE) {partMode = MODE_PREFIX; modeCharCount=0;}

		}
		
		// Extract Prefix from the field value.
		else if(partMode == MODE_PREFIX){
			//Check whether the value is numeric or not
			if(IsNumericValue(field.value.substring(i,i+1))){
				prefixCodePart += field.value.substring(i,i+1);
				modeCharCount++;
			}
			
			// If Prefix code is extracted then move on to next step.
			if(modeCharCount >= LEN_PREFIX_CODE) {partMode = MODE_SUFIX; modeCharCount=0;}			
		}
		
		// Extract Sufix from the field value.
		else if(partMode == MODE_SUFIX){
			//Check whether the value is numeric or not
			if(IsNumericValue(field.value.substring(i,i+1))){
				sufixCodePart += field.value.substring(i,i+1);
				modeCharCount++;
			}
			
			// If sufix code is extracted then move on to next step.
			if(modeCharCount >= LEN_SUFIX_CODE) {partMode = MODE_EXTRA; modeCharCount=0;}			

		}
	
	}
	
	// Combine all parts together to form a complete formatted phone number.
	if(partMode == MODE_AREA)
		field.value = areaCodePart;
	else if(partMode == MODE_PREFIX)
		field.value = areaCodePart + (prefixCodePart.length > 0 ? (PHONE_NUM_SPLITTER + prefixCodePart) : "");
	else 
		field.value  = areaCodePart + PHONE_NUM_SPLITTER + prefixCodePart + (sufixCodePart.length > 0 ? (PHONE_NUM_SPLITTER + sufixCodePart) : "");
	
}

// This function check whether a given value is a valid Date or not
// Pre-condition : None.
// Post-condition: It valud date then this furnction retunr true value, else returns false value.
function IsValidDatePattern(dateToCheck){
	var validDatePattern = /^((0[1-9])|(1[0-2])){1}\/(([(0|1|2)][0-9])|(3[0-1])){1}\/20([0-9][0-9]){1}$/
	var datePatternMatch;
	datePatternMatch = dateToCheck.match(validDatePattern) != null ? true : false;
	
	return datePatternMatch;
}


// This function checks a given field value is a valid or not and display warning message if it is not.
// Pre-condition : None.
// Post-condition: If date is valid, this function returns true value.
//			If date is invalid then a warning message will be displayed, the user will be directed to the field with error and function returns false value.
function IsValidDate(field, required, message ){
	
	// If this is a required value and no values specified then user will get warning message and function returns false value.
	if(field.value == null || field.value.trim() == ""){
		if(required==true){
			alert(message);
			field.focus();
			return false;
		}
		// No further processing required, return true.
		else
			return true;
	}
	
	if(!IsValidDatePattern(field.value)){
		alert(message);
		field.focus();
		return false;
	}
	else
		return true;	
	
}

// This function check whether a given value is numeric or not.
// Pre-conditioin : None.
// Post-condition : Function returns true if given value is numeric, otherwise return false.
function IsNumericValue(numberValue){
	var oReg = /^\d*$/;
	var numberMatch;
  
  
	numberMatch = numberValue.match(oReg) != null ? true : false;
	return numberMatch;
}

// This function check a value in given field is a numeric value or not.  If not it will dispaly a warning message, direct the user to the field, and returns false value.
// Pre-condition : None.
// Post-condition: Fucntion returns true if value is number, or no value and it is not required field.
//			If value is not a number, this function will display a warning message, direct the user to the field, and returns a false value.
function IsNumber(field, required, message ){
	var oReg = /^\d*$/;
	var numberMatch;
  
	// If this is a required field and no values are assigned then warn user and return false value.
	if(field.value == null || field.value.trim() == ""){
		if(required==true){
			alert(message);
			field.focus();
			return false;
		}
		
		// Not required and no values in the field, no further processing required.
		else
			return true;
	}
  
	// Check whether this is numeric field or not.
	if(!IsNumericValue(field.value)){
		alert(message);
		field.focus();
		return false;
	}
	else
		return true;
  
}

// This function check whether the given field has value when it is required.
// Pre-condition : None.
// Post-condition: Function returns true if there is value when it is required.
//			Function returns false when the field is required and there is not value in the field.
function checkField(field, required, message ){
	
	// No value detected.
	if(field.value == null || field.value.trim() == ""){
		
		// This is required field then display warning message, and focus the control
		if(required == true){
			alert(message);
			field.focus();
			return false;
		}
		
		// Not a required field, return true.
		else
			return true;
	}
	
	// There is value
	else
	  return true;
}


// Check whether the given value is a integer value.
function IntValue(stringVal){
	var convVal;
	
	convVal = parseInt(PureNumber(stringVal));

	return (isNaN(convVal) ? 0 : convVal);
}

// This function remove all non number characters from the string.
// Pre-condition : None.
// Post-condition: Function returns numeric only value of the string.
function PureNumber(stringVal){
	var pureNumberExtracted = "";
	
	for(i=0;i < String(stringVal).length;i++){
		var currChar;
		currChar = stringVal.substring(i,i+1);
		if(currChar >= '0' && currChar <= '9')
			pureNumberExtracted = pureNumberExtracted + stringVal.substring(i,i+1);
	}
	return pureNumberExtracted;
}

// This function removes any non number character from a given field.
// Pre-condition : None.
// Post-condition: Function replace field value with the value that contains only numeric characters.
function MakeNumberOnly(field){
	var regPattern = /^\d*/;
	var numberPart;
	numberPart = field.value.match(regPattern);	//Extract number characters only.
	if(numberPart != null)
		field.value = numberPart;
	else
		filed.value = "";
}


// This function add commas to the number value in a field.
// Pre-condition : None.
// Post-condition: Function replace current value in the given field with formatted version of the value.
function MakeFormattedNumberOnly(field){
	var regPattern = /^\d*/;
	var numberPart;
	numberPart = PureNumber(field.value);//.match(regPattern);	//Extract number characters only.
	if(numberPart != null)
		field.value = FormatNumber(numberPart);
	else
		filed.value = "";
}


// This function add commas to the number so it can be easily read.
// Pre-condition  : None.
// Post-condition : Function returns formatted number string.
function FormatNumber(valueString){
	valueString = String(valueString);  // Force this object to be a string value.
	var commaPlacement = 3;
	var len = valueString.length;
	var formattedNumber = "";
	var count = 1;
	
	for(var i= valueString.length -1; i >= 0;i--){
	
		if(count % commaPlacement == 0){

			if( i == 0 || i == valueString.length -1)
				formattedNumber = valueString.substring(i,i+1) + String(formattedNumber);
			else
				formattedNumber = "," + valueString.substring(i,i+1) + String(formattedNumber);
		}
		else
			formattedNumber = valueString.substring(i,i+1) + String(formattedNumber);
	
		count++;		

	}
	return formattedNumber;
}

// This function make a field value to match NAIC characters.
// Pre-condition : None.
// Post-condition: Function replace the given field value with only 5 digit numeric value that matches NAIC number.
function MakeNAICNumberOnly(field){
		
	if(field.value.length > NAIC_CODE_LEN){
		field.value = field.value.substring(0, NAIC_CODE_LEN);
	}
	
	MakeNumberOnly(field);
}

// This function checks whether a given value is valid e-mail address or not.
// Pre-condition  : None.
// Post-condition : Function returns true if given value is a valid e-mail address, otherwise return false value.
function IsValidEMailAddress(checkValue){
	var eMailPattern = /^([\w|!|#|\$|%|&|'|\*|\+|-|_|=|\^|`|~|\/|\?|\{|\||\}])+(.([\w|!|#|\$|%|&|'|\*|\+|-|_|=|\^|`|~|\/|\?|\{|\||\}])+)*@([\w|-])+(.([\w|-])+)*.[a-zA-Z|-]{2,9}$/;
	
	return checkValue.match(eMailPattern) != null ? true : false;
		
}

// This function check whether a value in a given field is a valid e-mail message or not.  If the valud is invalid e-mail address then it let user know.
//  Also this function check whether this is required field or not too.
// Pre-condition : None.
// Post-condition: Function returns true if the given field contains valid e-mail address.
//			Function disaply warning message, direct user to the field, and returns false if e-mail address is invalid.
// 
function IsValidEMail(field, required, message ){
	if(field.value == null || field.value.trim() == ""){
		if(required==true){
			alert(message);
			field.focus();
			return false;
		}
		else
			return true;
	}

	if(IsValidEMailAddress(field.value))
		return true;
	else{
			alert(message);
			field.focus();
			return false;	
	}

}

// This function calculate totals for the WSHIP form.
// Pre-conditioin : None.
// Post-condition : All necessary fields are calculated and populated with accurate date based on the user input.
function calculateTotals(field){
	var form;
	form = document.forms["WSHIPFORM"];

	//MakeNumberOnly(field);
	MakeFormattedNumberOnly(field);

	form.TotalJan.value = FormatNumber(IntValue(form.GHJan.value) + IntValue(form.IHJan.value) + IntValue(form.BHJan.value) + IntValue(form.HOJan.value) + IntValue(form.MSJan.value));
	form.TotalFeb.value = FormatNumber(IntValue(form.GHFeb.value) + IntValue(form.IHFeb.value) + IntValue(form.BHFeb.value) + IntValue(form.HOFeb.value) + IntValue(form.MSFeb.value));
	form.TotalMar.value = FormatNumber(IntValue(form.GHMar.value) + IntValue(form.IHMar.value) + IntValue(form.BHMar.value) + IntValue(form.HOMar.value) + IntValue(form.MSMar.value));
	form.TotalApr.value = FormatNumber(IntValue(form.GHApr.value) + IntValue(form.IHApr.value) + IntValue(form.BHApr.value) + IntValue(form.HOApr.value) + IntValue(form.MSApr.value));
	form.TotalMay.value = FormatNumber(IntValue(form.GHMay.value) + IntValue(form.IHMay.value) + IntValue(form.BHMay.value) + IntValue(form.HOMay.value) + IntValue(form.MSMay.value));
	form.TotalJun.value = FormatNumber(IntValue(form.GHJun.value) + IntValue(form.IHJun.value) + IntValue(form.BHJun.value) + IntValue(form.HOJun.value) + IntValue(form.MSJun.value));
	form.TotalJul.value = FormatNumber(IntValue(form.GHJul.value) + IntValue(form.IHJul.value) + IntValue(form.BHJul.value) + IntValue(form.HOJul.value) + IntValue(form.MSJul.value));
	form.TotalAug.value = FormatNumber(IntValue(form.GHAug.value) + IntValue(form.IHAug.value) + IntValue(form.BHAug.value) + IntValue(form.HOAug.value) + IntValue(form.MSAug.value));
	form.TotalSep.value = FormatNumber(IntValue(form.GHSep.value) + IntValue(form.IHSep.value) + IntValue(form.BHSep.value) + IntValue(form.HOSep.value) + IntValue(form.MSSep.value));
	form.TotalOct.value = FormatNumber(IntValue(form.GHOct.value) + IntValue(form.IHOct.value) + IntValue(form.BHOct.value) + IntValue(form.HOOct.value) + IntValue(form.MSOct.value));
	form.TotalNov.value = FormatNumber(IntValue(form.GHNov.value) + IntValue(form.IHNov.value) + IntValue(form.BHNov.value) + IntValue(form.HONov.value) + IntValue(form.MSNov.value));
	form.TotalDec.value = FormatNumber(IntValue(form.GHDec.value) + IntValue(form.IHDec.value) + IntValue(form.BHDec.value) + IntValue(form.HODec.value) + IntValue(form.MSDec.value));

	form.GHTotal.value = FormatNumber(IntValue(form.GHJan.value) + IntValue(form.GHFeb.value)+ IntValue(form.GHMar.value)+ IntValue(form.GHApr.value)+ IntValue(form.GHMay.value)+ IntValue(form.GHJun.value)+ IntValue(form.GHJul.value)+ IntValue(form.GHAug.value)+ IntValue(form.GHSep.value)+ IntValue(form.GHOct.value)+ IntValue(form.GHNov.value)+ IntValue(form.GHDec.value));
	form.IHTotal.value = FormatNumber(IntValue(form.IHJan.value) + IntValue(form.IHFeb.value)+ IntValue(form.IHMar.value)+ IntValue(form.IHApr.value)+ IntValue(form.IHMay.value)+ IntValue(form.IHJun.value)+ IntValue(form.IHJul.value)+ IntValue(form.IHAug.value)+ IntValue(form.IHSep.value)+ IntValue(form.IHOct.value)+ IntValue(form.IHNov.value)+ IntValue(form.IHDec.value));
	form.BHTotal.value = FormatNumber(IntValue(form.BHJan.value) + IntValue(form.BHFeb.value)+ IntValue(form.BHMar.value)+ IntValue(form.BHApr.value)+ IntValue(form.BHMay.value)+ IntValue(form.BHJun.value)+ IntValue(form.BHJul.value)+ IntValue(form.BHAug.value)+ IntValue(form.BHSep.value)+ IntValue(form.BHOct.value)+ IntValue(form.BHNov.value)+ IntValue(form.BHDec.value));
	form.HOTotal.value = FormatNumber(IntValue(form.HOJan.value) + IntValue(form.HOFeb.value)+ IntValue(form.HOMar.value)+ IntValue(form.HOApr.value)+ IntValue(form.HOMay.value)+ IntValue(form.HOJun.value)+ IntValue(form.HOJul.value)+ IntValue(form.HOAug.value)+ IntValue(form.HOSep.value)+ IntValue(form.HOOct.value)+ IntValue(form.HONov.value)+ IntValue(form.HODec.value));
	form.MSTotal.value = FormatNumber(IntValue(form.MSJan.value) + IntValue(form.MSFeb.value)+ IntValue(form.MSMar.value)+ IntValue(form.MSApr.value)+ IntValue(form.MSMay.value)+ IntValue(form.MSJun.value)+ IntValue(form.MSJul.value)+ IntValue(form.MSAug.value)+ IntValue(form.MSSep.value)+ IntValue(form.MSOct.value)+ IntValue(form.MSNov.value)+ IntValue(form.MSDec.value));
	
	form.TotalTotal.value = FormatNumber(IntValue(form.TotalJan.value) + IntValue(form.TotalFeb.value)+ IntValue(form.TotalMar.value)+ IntValue(form.TotalApr.value)+ IntValue(form.TotalMay.value)+ IntValue(form.TotalJun.value)+ IntValue(form.TotalJul.value)+ IntValue(form.TotalAug.value)+ IntValue(form.TotalSep.value)+ IntValue(form.TotalOct.value)+ IntValue(form.TotalNov.value)+ IntValue(form.TotalDec.value));
	
	
	form.StopLossTotal.value = FormatNumber(IntValue(form.StopLossJan.value) + IntValue(form.StopLossFeb.value)+ IntValue(form.StopLossMar.value)+ IntValue(form.StopLossApr.value)+ IntValue(form.StopLossMay.value)+ IntValue(form.StopLossJun.value)+ IntValue(form.StopLossJul.value)+ IntValue(form.StopLossAug.value)+ IntValue(form.StopLossSep.value)+ IntValue(form.StopLossOct.value)+ IntValue(form.StopLossNov.value)+ IntValue(form.StopLossDec.value));
}

// This function checks whether the given field value is valid state or not.  This function will be used during users input as a dynamic check.
// Pre-condition  : None.
// Post-condition : Function replace field value with only valid part of the state.
function IsValidStateActiveCheck(field){

	var stateFirstCharPattern = /^[ACDFGHIKLMNOPRSTUVW]$/;
	var stateCodePattern = /^(AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY){1}$/;

	var fieldValue;
	var matchedPattern;

	// If field is empty then don't bother to check.
	if(field.value != null && fieldValue != ""){
	
		//Trim the value and make upper for comparison.
		fieldValue = field.value.trim().toUpperCase();
		
		//If state code length is more than 2 characters ignore anything extra.
		if(fieldValue.length > STATE_CODE_LEN)
			fieldValue = fieldValue.substring(0,STATE_CODE_LEN);
		
		//If user entered the first code for state, check whether the letter is allowed.
		if(fieldValue.length == 1){
			matchedPattern = fieldValue.match(stateFirstCharPattern);
			
			//if it is allowed, then replace with upper cased letter.
			if(matchedPattern != null)
				field.value = matchedPattern;
				
			//if it is not allowed, replace with blank, so user know they entered wrong one.
			else
				field.value = "";
		}
		
		//if user entered 2 or more characters, check the entered code with valid state code.
		else{
			
			//check the entered state is valie
			matchedPattern = fieldValue.match(stateCodePattern);
			
			//if it is valid, replace with upper cased one.
			if(matchedPattern != null){
				field.value = matchedPattern[0];
			}
			
			//if not valid, remove 2nd character from the field, so user know he/she entered wrong code.
			else{
				field.value = fieldValue.substring(0,1);
			}
			
		}	

	}
		
}


// This function checks whether the given state code is valid or not.
// Pre-condition : None.
// Post-condition: Function returns true if state code is valid, otherwise returns false.
function IsValidStateCode(field){
	var sourceState;
	var state = new Array("AL","AK","AS","AZ","AR","CA","CO","CT","DE","DC","FM","FL","GA","GU","HI","ID","IL","IN","IA","KS","KY","LA","ME","MH","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ","NM","NY","NC","ND","MP","OH","OK","OR","PW","PA","PR","RI","SC","SD","TN","TX","UT","VT","VI","VA","WA","WV","WI","WY");

	var stateFound = false;
	
	sourceState = field.value;
	sourceState = sourceState.trim();
	sourceState = sourceState.toUpperCase();
	var i = 0;
	while(!stateFound && i < state.length){
		if(state[i] == sourceState) stateFound = true; else i++;
	}
	
	return stateFound;
}

// This function checks whether a given field value is valid or not and warn user if the state is invalid.
// Pre-conditiion : None.
// Post-condition : return true if state code is valid.
//			If state code is invalid, it will display warning message, direct user to the field, and return false value.
function IsValidState(field, required, message){

	var validState = true;

	// Check whether this is a required field or not.
	if(field.value == null || field.value.trim() == ""){

		if(required == true){
			alert(message);
			field.focus();	
			validState = false;
		}
		else
			return true;  // Don't let null object go through this.  Instead return it to calling routine.

	}

	// Check whether the state code exceed the state code length.
	if(validState && field.value.length != STATE_CODE_LEN){
		alert(message);
		field.focus();
		validState = false;
	}

	// Check the code is valid or not.
	if(validState && !IsValidStateCode(field)){
		alert(message);
		field.focus();
		validState = false;
	}



	return validState;
}

// This function check whether the required radio button is selected or not
// Pre-condition : None.
// Post-condition: If radio button is selected then function returns true.
//			If radio button is not selected even it is required then function dispaly warning message, direct the user the control and returns false value.
function IsRadioButtonSelected(field, required, message)
{
	
	var radioLen = field.length;
	var radioSelected = false;
	
	// Check whether the given radio button is selected.
	if(radioLen != undefined){
		for(var i=0;i<radioLen; i++){
			if(field[i].checked == true){
				radioSelected = true;
			}
		}
	}
	
	// If radio button is selected return true.
	if(radioSelected) {
		return true;
	}
	
	// If radio button is not selected.
	else{
		// If not required, then return false.
		if(!required)
			return true;
		
		// It is required and not selected, dispaly message and direct the user to the control.
		else{
			alert(message);
			field[0].focus();	//Radio field itself cannot be focused.  A button in the group need a focus.
			return false;
		}
	}
		
}



function ValidateForm(form){
    var noError = true;



	if(noError) noError = checkField(form.MyselfName,true, "Step 1. Name is a required field.");
	if(noError) noError = checkField(form.MyselfAddr,true, "Step 1. Address is a required field.");
	if(noError) noError = checkField(form.MyselfCity,true, "Step 1. City is a required field.");
	if(noError) noError = IsValidState(form.MyselfSt,true, "Step 1. State is a required field." );
	
	if(noError) noError = checkField(form.MyselfZip,true, "Step 1. Zip Code is a required 5 digit numeric field.");


	if(noError) noError = checkField(form.Email,true, "Step 1. Email is a required field.");

	if(noError) noError = IsPhoneNumber(form.Phone,true,"Phone number is a required field using this format: 555-555-5555");	


	
	if(noError) noError = checkField(form.InsNameInvolved,true, "Step 3. Insurance company name is a required field.");
	


		if(noError) noError = checkField(form.AgentName,true, "Step 4. Agent or Broker name is a required field.");
	if(noError) noError = checkField(form.Comments,true, "Step 5. Explanation of the problem is a required field.");
	

	
	if(noError) noError = checkField(form.DeclareBy,true, "Step 6. Name is a required field.");
	if(noError) noError = IsValidDate(form.DeclareDate,true,"Step 6. Date is a required field using this format: mm/dd/yyyy.");	
	

		


	
if(noError) noError = confirm("You are about to submit a request for investigation form to The Washington State Office of the Insurance Commissioner.  Do you want to continue?");

	return noError;
}




