// VARIABLE DECLARATIONS
var digits = ".0123456789";
var lowercaseLetters = "abcdefghijklmnopqrstuvwxyz"
var uppercaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// Alphabetic check
function alphaCheck(theField){
	var val=theField.value;
	var chk = /[a-zA-Z_ ']+/;
	if (chk.test(val)){return 1;}
	else{
		alert('This field only accepts valid alphabetic characters (A-Za-z_ \').');
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Alphanumeric check script-
function alphanumericCheck (theField){
	var val=theField.value;
	var chk = /[0-9a-zA-Z_ ']+/;
	if (chk.test(val)){return 1;}
	else{
		alert("Please input a alphanumeric string. A-Za-z0-9_");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// currency check script-
function currencyCheck (theField){
	var val=theField.value;
	var anum=/(^\d+\.{0,1}\d{0,2}$)/;
	if (anum.test(val)){}
	else{
		alert("Please input a valid US currency amount.");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Date Check
function dateCheck (theField){
	var val=theField.value;
	var formats = 'Accepted Formats MM-DD-YY, MM/DD/YY, MM-DD-YYYY, MM/DD/YYYY';
	//var pattern = /^(1[0-2]|0?[1-9])[/-](0?[1-9]|[12][0-9]|3[01])[-/]([0-9]{2}|[0-9]{4})$/;
	var pattern = /\d+/;
	if (pattern.test(val)){}
	else{
		alert("Please input a valid date.\n" + formats);
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Get the number of day in a given month and year.
function getLastDay(y, m) {
	var d = 0;
	if(m == 2) {
		if((y % 4) != 0) {d = 28;} 
		else if((y % 400) == 0){d = 29;} 
		else if((y % 100) == 0) {d = 28;} 
		else {d = 29;}
		} 
	else if((m == 1) || (m == 3) || (m == 5) || (m == 7) || (m == 8) || (m ==10) || (m == 12)){d = 31;}
	else {d = 30;}
	return d;
	}
// Email Check
function emailCheck (theField){
	var val=theField.value;
	var filter=/^.+@.+\..{2,3}$/;
	if (filter.test(val)){}
	else{
		alert("Please input a valid email address.");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Returns the Date - returns Wednesday January 31, 2001
function getDate (){
	var mydate=new Date();		
	var year=mydate.getYear();		
 	if (year<2000){year=1900+year;}		
	var day=mydate.getDay();		
	var month=mydate.getMonth();		
	var daym=mydate.getDate();		
	if (daym<10){daym="0"+daym}		
	var dayarray=new Array("Sun","Mon","Tues","Wed","Thur","Fri","Sat");		
	var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var result=""+dayarray[day]+", "+montharray[month]+" "+daym+", "+year+"";
	return result;
	}
// Integer Check
function integerCheck (theField){
	var val=theField.value;
	var anum=/(^\d+$)/;
	if (anum.test(val)){}
	else{
		alert("This field only accepts valid positive integers.");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Real Number check script-
function realnumberCheck (theField){
	var val=theField.value;
	var anum=/(^\d+$)|(^\d+\.\d+$)/;
	if (anum.test(val)){}
	else{
		alert("Please input a valid real number.");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
//Reformat
function reformat (s){
	var arg;
	var sPos = 0;
	var resultString = "";
	for (var i = 1; i < reformat.arguments.length; i++) {
		arg = reformat.arguments[i];
		if (i % 2 == 1){ resultString += arg;}
		else {
			resultString += s.substring(sPos, sPos + arg);
			sPos += arg;
			}
		}
	return resultString;
	}
// Removes all characters which appear in string bag from string s.
function stripChar(s){
	var i;
	var returnString = '';
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (digits.indexOf(c) > 0){returnString += c;}
		}
	return returnString;
	}
// Set focus on a field in a form
function setFocus(theField) {theField.focus();}
// Time Check
function timeCheck (theField){
	var val=theField.value;
	var anum=/(^\d{1,2}\.\d{2}$)/;
	if (anum.test(val)){}
	else{
		alert("Please input a valid time - military format. HH:MM");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Zip Code Check -
function zipCheck(val,theField){
	var sval=stripChar(val);
	if((sval.length == 9)||(sval.length == 5)){
		if(sval.length == 9){
			var x=reformat(sval,"",5,"-",4,"",0);
			theField.value=x;
			}
		}
	else{
		alert("Please input a valid US Zip Code!");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// SSN Check -
function ssnCheck(theField){
	var val=theField.value;
	var sval=stripChar(val);
	var x=reformat(sval,"",3,"-",2,"-",4);
	var anum=/\d\d\d\-\d\d\-\d\d\d\d/;
	if (anum.test(x)){theField.value=x;}
	else{
		alert("Please input a valid SSN number.\n\nFormat: 999-99-9999");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Format Phone - sets the value of the field to a phone number format.
function phoneCheck(theField){
	var val=theField.value;
	var sval=stripChar(val);
	if(sval.length == 10){
		var x=reformat(sval, "(", 3, ") ", 3, "-", 4);
		theField.value=x;
		}
	else{
		alert("Please input a valid 10 digit phone number.\n\nFormat: 999 999-9999");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Custom1 - custom1Check
function custom1Check(theField){
	var val=theField.value;
	var anum=/N(\d{9,9}$)/;
	if (anum.test(val)){return 1;}
	else{
		alert("Please input a valid Account number.\n\nFormat: N999999999");
		theField.focus();
		theField.select();
		return 0;
		}
	return 1;
	}
// Custom2 - custom2Check
function custom2Check(theField){
	var val=theField[theField.options.selectedIndex].value;
	alert('c2 val='+val);
	if(val == 1 && document.addedit.scheduled_date.length == 0){
		alert('Schedule Date is required when Customer Status=Scheduled');
		document.addedit.scheduled_date.focus();
		return 0;
		}
	if(val == 2 && document.addedit.install_date.length == 0){
		alert('Install Date is required when Customer Status=Installed');
		document.addedit.install_date.focus();
		return 0;
		}
	if(val == 3 && document.addedit.bundled_date.length == 0){
		alert('Bundled Date is required when Customer Status=Bundled');
		document.addedit.bundled_date.focus();
		return 0;
		}
	return 1;
	}
// Custom3 - custom3Check
function custom3Check(theField){return 1;}
// Custom4 - custom4Check
function custom4Check(theField){return 1;}
// Custom5 - custom5Check
function custom5Check(theField){return 1;}
// The following functions are used in the List Results call
function Check(){
   if (!document.listdata.checkall) {return;}
   var t = document.listdata.elements.length;
   var c = 0;
   if (document.listdata.checkall.checked) {c = 1;}
   for (var i = 0; i < t; i++) {
	if (document.listdata.elements[i].name == "i_selected") {
    	document.listdata.elements[i].checked = c;
		}
   	}
}
function buildmail(){
var t = document.listdata.elements.length;
var chek = Array(50);
var x=0;
for (var i = 0; i < t; i++) {
	if (document.listdata.elements[i].name == "i_selected" && document.listdata.elements[i].checked) {
		chek[x]=document.listdata.elements[i].value;
		x=x+1;
		}
	}
var list = "";
for (var i = 0; i < t; i++) {
	for (var y = 0; y<x; y++){
		var em = "i_email_"+chek[y];
		if (document.listdata.elements[i].name == em) {
			list = list + document.listdata.elements[i].value + ",";
			}
		}
	}
parent.location.href='mailto:'+list;
}
// End

	

