/* creation date: 01/09/00 */
/*************************************************
* Application Form Validation Functions
*
* - Check and Format dollar value
* - Check and Modify a phone number (North American format)
* - Check for required values
* - Check checkbox value and reset to unchecked
* - Check for Payment Option 5: PAC
* - Check for Confirmation checkbox value
* - Filter out commas for Email
* - Filter out alpha characters, dollar signs and commas
* - Submit Form to the URL provided
*
* Created for: Wayfarer Insurance Brokers Ltd.
* Created by: Pinnacle Communications Group Inc.
**************************************************/

// Check and Format dollar value
function checkValue(input) {
	s = input.value;

	// characters to be stripped out
	var comma = "() =+-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*_`~;:',/<>?[]\{}|";
	var decimal = ".";
	var i;
	var returnString = "";

	// search through string and append to unfiltered values to returnString
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		
		if (c == decimal && (i != (s.length-3))) {
			c = "";;
		}
		
		if (comma.indexOf(c) == -1) {
			returnString += c;
		}
	}

	// return input.value without commas
	input.value = returnString;

}




// Check and Modify a phone number (North American format)
function phoneFilter(input) {
	s = input.value;

	// characters to be stripped out
	filteredValues = "() =+-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*_`~;:',./<>?[]\{}|";
	var i;
	var returnString = "";

	// search through string and append to unfiltered values to returnString
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) == -1) returnString += c;
	}

	// check returnString for a blank value, 10-digit number or 1 before area code
	if (returnString == "") {
		input.value = returnString;
	} else if (returnString.length < 10) {
		alert("Please enter a Phone Number in the following format: (nnn) nnn-nnnn");
		input.focus();
	} else if (returnString.substring(0,1) == 1) {
		alert("Please do not add a 1 before your Area Code. Use the following format: (nnn) nnn-nnnn");
		input.focus();
	} else {

		// create substrings from returnString
		var phone1 = returnString.substring(0,3);
		var phone2 = returnString.substring(3,6);
		var phone3 = returnString.substring(6,10);

		// create returnString1 as the formatted string
		var returnString1 = "(" + phone1 + ") " + phone2 + "-" + phone3;

		input.value = returnString1;
	}
}

// Check for required values
function checkrequired(which) {
        var pass=true;
        if (document.images) {
                for (i=0;i<which.length;i++) {
                        var tempobj=which.elements[i];
                        if (tempobj.name.substring(0,9)=="required_") {
                                if (((tempobj.type=="text"||tempobj.type=="textarea")&&
                                tempobj.value=='')||(tempobj.type.toString().charAt(0)=="s"&&
                                tempobj.selectedIndex==0)) {
                                        pass=false;
                                        break;
                                }
                        }
                }
        }
        if (!pass) {
                shortFieldName=tempobj.name.substring(9,30).toUpperCase();
                alert("Please ensure that the "+shortFieldName+" field was properly completed.");
				tempobj.focus();
                return false;
        }
}

// Check checkbox value and reset to unchecked
function checkCheckBox(input) {
	if(input.checked == true) {
		input.checked = false;
	} else {
		return true;
	}
}

// Check for Payment Option 5: PAC
function checkOption5(url,page_link) {
	window.open(url, 'confirmation', 'width=400,height=200,');
	window.document.forms[0].action = "#" + page_link;
	submit.form();
}

// Check for Confirmation checkbox value
function checkConfirmation(url,page_link) {
	if(window.document.forms[0].Confirmation.checked) {
		return true;
	} else {
		window.open(url, 'confirmation', 'width=400,height=200,');
		window.document.forms[0].action = "#" + page_link;
		submit.form();
	}
}

// Filter out commas for Email
function emailFilter (input) {
	s = input.value;

	// characters to be stripped out
	filteredValues = ",";
	var i;
	var returnString = "";

	// search through string and append to unfiltered values to returnString
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) == -1) returnString += c;
	}

	// return input.value without commas
	input.value = returnString;

}

// Filter out alpha characters, dollar signs and commas
function commaFilter (input) {
	s = input.value;

	// characters to be stripped out
	filteredValues = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$,";
	var i;
	var returnString = "";

	// search through string and append to unfiltered values to returnString
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) == -1) returnString += c;
	}

	// return input.value without commas
	input.value = returnString;

}


// Submit Form to the URL provided
function submitForm(url) {
	document.forms[0].action = url;
}

