/* Naming conventions for html form variables apply to first three letters in form element name.
 Reset and Submit elements are excluded.  Name conventions and descriptions set as follows:
FIRST LETTER
Prefix		Description
r			Required Field
x			Not Required Field
SECOND LETTER
Prefix		Description
s			Select Object- Selection(with value) required
o			Any Other Element type
THIRD LETTER
Prefix		Description
a			Phone-no Area Code (1231234/123-1234/123.1234)
b			Phone-Area Code (1231231234/123-123-1234/123.123.1234)
c			No Empty values - generic
d			Letters only - including spaces
e			Email
f			strPostal Code / Zip Code - use c for no empty values(too many countries!)
g			Numbers Only
p			Password 10 digits max
*/
function getfields(objForm) {
// Gather all form arrFields
var intAllFields = 0;
var arrAllField = new Array();
var intIndex = 0;
intAllFields = objForm.length;
for (intLoop=0; intLoop<intAllFields; intLoop++) {
	if ((objForm.elements[intLoop].type != "hidden")&&(objForm.elements[intLoop].type != "reset")&&(objForm.elements[intLoop].type != "submit")) {
		arrAllField[intLoop] = objForm.elements[intLoop];
		arrFields[intIndex] = arrAllField[intLoop];
		intIndex++;
		}
	}
return arrFields;
}


function getnumfields(arrFields) {
// Get number of useable arrFields
intFields = arrFields.length;
return intFields;
}

	function changeCasetwo(frmObj) {
		var index;
		var tmpStr;
		var tmpChar;
		var preString;
		var postString;
		var strlen;
		tmpStr = frmObj.value.toLowerCase();
		strLen = tmpStr.length;
		if (strLen > 0)  {
			for (index = 0; index < strLen; index++)  {
				if (index == 0)  {
					tmpChar = tmpStr.substring(0,1).toUpperCase();
					postString = tmpStr.substring(1,strLen);
					tmpStr = tmpChar + postString;
								 }
				else {
					tmpChar = tmpStr.substring(index, index+1);
					if (tmpChar == " " && index < (strLen-1))  {
						tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
						preString = tmpStr.substring(0, index+1);
						postString = tmpStr.substring(index+2,strLen);
						tmpStr = preString + tmpChar + postString;
																}
					 }
														}
						 }
		frmObj.value = tmpStr;
								}
								
function isdata(intCount,arrFields) {
if (arrFields[intCount].value != "") return true;
else return false;
}

function stripspace(strLetter) {
var strPattern
var strCompare
strPattern	=	/[\x20\s]/g;
strCompare	=	strLetter.replace(strPattern, "");
return strCompare;
}

function isletters(intCount,arrFields) {
var strPattern;
var strCompare;
var strLetter =	arrFields[intCount].value;
var strLetterStrip = stripspace(strLetter);
if (strLetterStrip != "") {
	strLetter	=	/[-a-z]/gi;
	strCompare		=	strLetterStrip.replace(strLetter, "");
	if (strCompare == "") return true;
	else return false;
	}
else return false;
}

function isnumbers(intCount,arrFields) {
var strPattern;
var strCompare;
var strNumber;
strNumber		=	arrFields[intCount].value;
if (isdata(intCount,arrFields)) {
	strPattern	=	/\d/gi;
	strCompare		=	strNumber.replace(strPattern, "");
	if (strCompare == "") return true;
	else return false;
	}
else return false;
}

function ispostal(intCount,arrFields) {
var strPattern;
var strCompare;
var strPostal;
strPostal		=	arrFields[intCount].value;
if (isdata(intCount,arrFields)) {
	strPattern	=	/[a-ceg-hj-npr-tvx-y]\d[a-ceg-hj-npr-tv-z](\x20)?\d[a-ceg-hj-npr-tv-z]\d/i;
	strPatternAlt	=	/\d{5}-?(\d{4})?/;
	strCompare		=	strPostal.replace(strPattern, "");
	strCompareAlt		=	strPostal.replace(strPatternAlt, "");
	if ((strCompare == "") || (strCompareAlt == "")) return true;
	else return false;
	}
else return false;
}

function IsEmailValid(intCount,arrFields)
{
var EmailOk  = true
var AtSym    = arrFields[intCount].value.indexOf('@')
var Period   = arrFields[intCount].value.lastIndexOf('.')
var Space    = arrFields[intCount].value.indexOf(' ')
var Length   = arrFields[intCount].value.length - 1   // Array is from 0 to length-1
if ((AtSym < 1) ||                     // '@' cannot be in first position
    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
    (Period == Length ) ||             // Must be atleast one valid char after '.'
    (Space  != -1))                    // No empty spaces permitted
   {  
      return false
   }
  else return true
}

function ispassten(intCount,arrFields){
if (arrFields[intCount].value.length > 9) return false;
else return true;
}


function whatrequired(intCount,arrFields) {
intCase	=	0;
if ((arrFields[intCount].name.substring(0,3) == "roc")||((arrFields[intCount].name.substring(0,3) == "xoc")&&(isdata(intCount,arrFields)))) intCase = 1;
if ((arrFields[intCount].name.substring(0,3) == "rod")||((arrFields[intCount].name.substring(0,3) == "xod")&&(isdata(intCount,arrFields)))) intCase = 2;
if ((arrFields[intCount].name.substring(0,3) == "roe")||((arrFields[intCount].name.substring(0,3) == "xoe")&&(isdata(intCount,arrFields)))) intCase = 3;
if ((arrFields[intCount].name.substring(0,3) == "rof")||((arrFields[intCount].name.substring(0,3) == "xof")&&(isdata(intCount,arrFields)))) intCase = 1;
if ((arrFields[intCount].name.substring(0,3) == "rog")||((arrFields[intCount].name.substring(0,3) == "xog")&&(isdata(intCount,arrFields)))) intCase = 5;
if ((arrFields[intCount].name.substring(0,3) == "rop")||((arrFields[intCount].name.substring(0,3) == "xop")&&(isdata(intCount,arrFields)))) intCase = 6;
if ((arrFields[intCount].name.substring(0,3) == "rsc")||((arrFields[intCount].name.substring(0,3) == "xoc")&&(isdata(intCount,arrFields)))) intCase = 7;
return intCase;
}


function winopen(strMsg) {
	warn = window.open('','Warning','dependent=0,directories=0,height=200,width=250,hotkeys=0,menubar=0,resizable=0,screenx=250,screeny=250,scrollbars=0,status=0,toolbar=0');
	toclose = "<div style='font-style: arial; font-size=10pt; text-align: right'><a href='javascript: this.close()'>Close</a></div>"
	bckgrd = "<html><head><title>Validation Message</title></head><body style='background-color: #ffffff'>";
	bckgrdend = "</body></html>";
	warn.document.open();
	warn.document.write(bckgrd);
	warn.document.write(strMsg);
	warn.document.write(toclose);
	warn.document.write(bckgrdend);
	warn.document.close();
				
}


function validate(objForm) {
arrFields	=	new Array();
intFields	=	0;
intLoop		=	0;
intCount	=	0;
intCase		=	0;
strField	=	"";
strMsg		=	"";
strCheck	=	"no";
getfields(objForm);
getnumfields(arrFields);
if (intFields > 0) {
	for (intCount=0; intCount<intFields; intCount++) {
		switch (whatrequired(intCount,arrFields)) {
		case 1:
			if (isdata(intCount,arrFields)) strCheck = "yes";
			else {
				strField	=	arrFields[intCount].name.substring(3);
				arrFields[intCount].focus();
				strMsg	=	"<div style='font-style: arial; font-size=10pt'><h1 align=center>Warning</h1>The <b>"+strField+"</b> field is required to process this form.</div>";
				strCheck = "no";
				winopen(strMsg);
				}
			break;
		case 2:
			if (isletters(intCount,arrFields)) strCheck = "yes";
			else {
				strField	=	arrFields[intCount].name.substring(3);
				arrFields[intCount].focus();
				strMsg	=	"<div style='font-style: arial; font-size=10pt'><h1 align=center>Warning</h1>The <b>"+strField+"</b> field will accept letters only.</div>";
				strCheck = "no";
				winopen(strMsg);
				}
			break;
		case 3:
			if (IsEmailValid(intCount,arrFields)) strCheck = "yes";
			else {
				strField	=	arrFields[intCount].name.substring(3);
				arrFields[intCount].focus();
				strMsg	=	"<div style='font-style: arial; font-size=10pt'><h1 align=center>Warning</h1>The <b>"+strField+"</b> field is required to process this form (eg. name@domain.com).</div>";
				strCheck = "no";
				winopen(strMsg);
				}
			break;
		case 4:
			if (ispostal(intCount,arrFields)) strCheck = "yes";
			else {
				strField	=	arrFields[intCount].name.substring(3);
				arrFields[intCount].focus();
				strMsg	=	"<div style='font-style: arial; font-size=10pt'><h1 align=center>Warning</h1>The <b>"+strField+"</b> field will accept a valid Postal or Zip code only.</div>";
				strCheck = "no";
				winopen(strMsg);
				}
			break;
		case 5:
			if (isnumbers(intCount,arrFields)) strCheck = "yes";
			else {
				strField	=	arrFields[intCount].name.substring(3);
				arrFields[intCount].focus();
				strMsg	=	"<div style='font-style: arial; font-size=10pt'><h1 align=center>Warning</h1>The <b>"+strField+"</b> field will accept numbers (0-9) only.</div>";
				strCheck = "no";
				winopen(strMsg);
				}
			break;
		case 6:
			if (ispassten(intCount,arrFields)) strCheck = "yes";
			else {
				strField	=	arrFields[intCount].name.substring(3);
				arrFields[intCount].focus();
				strMsg	=	"<div style='font-style: arial; font-size=10pt'><h1 align=center>Warning</h1>The <b>"+strField+"</b> field will accept total of 10 characters.</div>";
				strCheck = "no";
				winopen(strMsg);
				}
			break;
		case 7:
			if (isdata(intCount,arrFields)) strCheck="yes";
			else {
				strField	=	arrFields[intCount].name.substring(3);
				arrFields[intCount].focus();
				strMsg	=	"<div style='font-style: arial; font-size=10pt'><h1 align=center>Warning</h1>The <b>"+strField+"</b> fields is required.</div>";
				strCheck = "no";
				winopen(strMsg);
				}
			break;
		default:
			strCheck = "yes";
		}
		if (strCheck == "no") break;	
		}
	if (strCheck == "yes") return true;
	else return false;
	}
else {
	strMsg	=	"<div style='font-style: arial; font-size=10pt'><h1 align=center>Warning</h1>There are no input arrFields in this form</div>";
	winopen(strMsg);
	return false;
	}
}

