// * Set parameters for form elements before running.                                     *
// *                                                                                      *
// *    KEY:                                                                              *
// *  e.longname = specifies a readable name for the element for messages                 *
// *   e.setsize = needs to be a certain [t\f] (element.setsize = true)                   *
// *   e.maxsize = (element.maxsize = "20"); "" = no max # of characters                  *
// *   e.minsize = (element.minsize = "1"); "" = no min # of characters                   *
// *       e.req = required field [t\f] (element.req = true)                              *
// *       e.val = validation type      (element.val = "?")                               *
// *                blank - Not Necessary                                                 *
// *                a - all characters allowed (not checked for validity)                 *
// *                t - textarea characters (extended characters + CR's etc)              *
// *                c - Only characters (a-z and A-Z)						              *
// *                x - extended characters                                               *
// *                e - email characters and validation                                   *
// *                l - limited characters                                                *
// *                p - password validation                                               *
// *                    need to specify an element.passchk for a confirm.                 *
// *                    ex element.passchk = document.form.passchk.value                  *
// *                w - whole numbers only (strips non-number characters)                 *
// *                    if a number field, need to define an element.minvalue             *
// *                      and element.maxvalue setting for value                          *
// *                n - numbers only, "." allowed                                         *
// *                    if a number field, need to define an element.min                  *
// *                      and element.max setting for value                               *
// *                z - Zip/Postal Code number only if                                    *
// *                    country = "US", "USA", or "United States"                         *
// *                    if a zipcode field, need to define an element.min                 *
// *                      and element.max setting for value                               *
// *                      also need to specify element.country                            *
// *                      (ex document.f.zip.country = document.f.country)                *
// *                                                                                      *
// * (note: e = form element)                                                             *
// * If e is a select list or dropdown, and there are spacer options that shouldn't be    *
// * selected, set the option's value to "invalid"                                        *
// *                                                                                      *
// *           loadForm() {                                                     *
// *             document.forms["email"].elements["emailaddr"].longname = "Email Address" *
// *             document.forms["email"].elements["emailaddr"].req = true                 *
// *             document.forms["email"].elements["emailaddr"].val = e                    *
// *           }                                                                          *
// ****************************************************************************************

var e = ""
var vLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var vNumbers = "0123456789"
var vExt1 = "-_."
var vExt2 = "@"
var vExt3 = "?:; #+$%^*=,!/&\'\\()<>[]{}\""
var vExt4 = "\f\n\r\t"
var validChars = ""
var focus_set = false
var focus_point = ""
var blank_msg = ""
var error_msg = ""
var alert_msg = ""

function blankMsg(fe) {
	if (focus_set != true) {
		focus_set = true
		focus_point = fe
	}
	blank_msg += "\n     " + fe.longname
	return true
}

function isblank(s) {  // Checks for a string containing only whitespace characters
	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false
	}
	return true
}

function checkSize(fe) {
	if (fe.setsize == true) {
		if (fe.minsize != "") {
			if (fe.value.length <= (fe.minsize - 1)){
				error_msg += "\n     Your " + fe.longname + " must be at least " + fe.minsize + " characters"
				if (focus_set != true) {
					focus_set = true
					focus_point = fe
				}
			}
		}
		if (fe.maxsize != "") {
			if (e.value.length >= (fe.maxsize)){
				error_msg += "\n     Your " + e.longname + " must be less than " + fe.maxsize + " characters"
				if (focus_set != true) {
					focus_set = true
					focus_point = fe
				}
			}
		}
	}
	
	return
}

function isValidChar(c) {
	for (var i=0; i < validChars.length; i++) {
		if (c == validChars.charAt(i)) {
			return true
		}
	}
	return false
}
 
function isValidString(s) {
	for (var i=0; i < s.length; i++) {
		if (isValidChar(s.charAt(i)) != true ) {
			return s.charAt(i)
		}
	}
	return true
}

function validEmail(s) {  // Checks string for valid email address
	validChars = vLetters + vNumbers + vExt1 + vExt2
	for (var i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (isValidChar(c) != true) {
			return false
		}
	}
	if ( (s.indexOf("@")<1) || (s.indexOf(".")<3) || (s.length <7) ||  (s.lastIndexOf(".") < s.indexOf("@")) || (s.indexOf("@") != s.lastIndexOf("@")) || (s.lastIndexOf(".")==(s.length-1))){
	return false
		}
	
	return true
}

function numberStrip(s) {
	var n = ""
	for (var i=0; i < s.length; i++) {
		if (isValidChar(s.charAt(i))) {
			n += s.charAt(i)
		}
	}
	return n
}
 
function wholeNumberCheck(fe){ // checks for valid numbers
	validChars = vNumbers
	var v = parseFloat(numberStrip(fe.value))
	fe.value = v
	if ((isNaN(v)) || (fe.value == null) || (fe.value == "") || (v < fe.minvalue) || (v > fe.maxvalue)) {
		return false
	}
	return true
}


function numberCheck(fe){ // checks for valid numbers
	var v = fe.value
	if ((isNaN(v)) || (fe.value == null) || (fe.value == "") || (v < fe.minvalue) || (v > fe.maxvalue)) {
		validChars = vNumbers + "."
		v = parseFloat(numberStrip(fe.value))
		fe.value = v
		return false
	}
	return true
}


function validForm(f,testing) {
	//reset variables
	validChars = ""
	focus_set = false
	focus_point = ""
	blank_msg = ""
	error_msg = ""
	alert_msg = ""	
	var has_radio=false
	for (var i=0; i < f.length; i++) {
		e = f.elements[i]
		if (e.type == "radio") {
			has_radio=true		
			if (e.checked == true) {
				var radiocheck=true
			}
			var radioname=e.name
			}
		if (e.type == "select-one") {
			if ((e.req == true) && (e.options[e.selectedIndex].value == "invalid")) {
				if (e.longname) {
					error_msg += "\n     Invalid Selection - " + e.longname + " has an invalid selection."
				}else{
					error_msg += "\n     Invalid Selection - " + e.name + " has an invalid selection."
				}
				if (focus_set != true) {
					focus_point = e
					focus_set = true
				}
			}
		}
		if (e.type == "select-multiple") {
			for (var j=0; j < e.length; j++) {
				if (e.options[j].selected == true) {
					if ((e.req == true) && (e.options[j].value == "invalid")) {
						if (e.longname) {
							error_msg += "\n     Invalid Selection - " + e.longname + " has an invalid selection."
						}else{
							error_msg += "\n     Invalid Selection - " + e.name + " has an invalid selection."
						}
						if (focus_set != true) {
							focus_point = e
							focus_set = true
						}
					}
				}
			}
		}
		
		if ((e.type == "password")||(e.type == "text")||(e.type == "hidden")||(e.type == "textarea")) {
			if (isblank(e.value) != true) {
				checkSize(e)
				if (e.val == "p") {
					validChars = vLetters + vNumbers + vExt1
					if ((isValidString(e.value) != true)) {
						if (focus_set != true) {
							focus_set = true
							focus_point = e
						}
						error_msg += "\n     Invalid Password Entry - " + isValidString(e.value) + " - in " + e.longname + " field."
					}
					if (e.value != f.passchk.value) {
						if (focus_set != true) {
							focus_set = true
							focus_point = e
						}
						error_msg += "\n     Passwords don't match!"
					}
				}

				if (e.val == "t") {
					validChars = vLetters + vNumbers + vExt1 + vExt2 + vExt3 + vExt4
					if (isValidString(e.value) != true) {
						if (focus_set != true) {
							focus_set = true
							focus_point = e
						}
						error_msg += "\n     Invalid Text Entry - " + isValidString(e.value) + " - in " + e.longname + " field."
					}
				}

				if (e.val == "x") {
					validChars = vLetters + vNumbers + vExt1 + vExt2 + vExt3
					if (isValidString(e.value) != true) {
						if (focus_set != true) {
							focus_set = true
							focus_point = e
						}
						error_msg += "\n     Invalid Text Entry - " + isValidString(e.value) + " - in " + e.longname + " field."
					}
				}

				if (e.val == "l") {
					validChars = vLetters + vNumbers + vExt1
					if (isValidString(e.value) != true) {
						if (focus_set != true) {
							focus_set = true
							focus_point = e
						}
						error_msg += "\n     Invalid Text Entry - " + isValidString(e.value) + " - in " + e.longname + " field."
					}
				}

				if (e.val == "c") {
					validChars = vLetters + vExt4
					if (isValidString(e.value) != true) {
						if (focus_set != true) {
							focus_set = true
							focus_point = e
						}
						error_msg += "\n     Invalid Text Entry - " + isValidString(e.value) + " - in " + e.longname + " field."
					}
				}

				if (e.val == "l2") {
					validChars = vLetters + vNumbers + vExt1 + " "
					if (isValidString(e.value) != true) {
						if (focus_set != true) {
							focus_set = true
							focus_point = e
						}
						error_msg += "\n     Invalid Text Entry - " + isValidString(e.value) + " - in " + e.longname + " field."
					}
				}

				if (e.val == "e") {
					if ((validEmail(e.value) != true)) {
						if (focus_set != true) {
							focus_set = true
							focus_point = e
						}
						error_msg += "\n     Invalid Email Address - " + e.longname
					}
				}

				if (e.val == "n") {
					if (numberCheck(e) != true) {
						if (focus_set != true) {
							focus_set = true
							focus_point = e
						}
						error_msg += "\n     Invalid Number Entry - " + e.longname
						error_msg += "\n          Must be a number between " + e.minvalue + " and " + e.maxvalue + "."
					}
				}

				if (e.val == "w") {
					if (wholeNumberCheck(e) != true) {
						if (focus_set != true) {
							focus_set = true
							focus_point = e
						}
						error_msg += "\n     Invalid Whole Number Entry - " + e.longname
						error_msg += "\n          Must be a whole number between " + e.minvalue + " and " + e.maxvalue + "."
					}
				}

				if (e.val == "z") {
					if (wholeNumberCheck(e) != true) {
						if (focus_set != true) {
							focus_set = true
							focus_point = e
						}
						error_msg += "\n     Invalid Whole Number Entry - " + e.longname
						error_msg += "\n          Must be a whole number between " + e.minvalue + " and " + e.maxvalue + "."
					}
				}
			} else {
				if (e.req == true) {
					blankMsg(e)
				}
			}
		}
	}
	if (has_radio==true && radiocheck!=true)
	{
		error_msg += "\n     Invalid Selection - " + radioname + " has an invalid selection."
	}
	if ((blank_msg == "")&&(error_msg == "")) {
		
			return true
		
	} else {
		alert_msg = "Your entries were not submitted, due to the following..."
		alert_msg += "\n______________________________________________________________"
		if (blank_msg != "") {
			alert_msg += "\n\nBlank Fields:\n"
			alert_msg += blank_msg
		}
		if (error_msg != "") {
			alert_msg += "\n\nErrors:\n"
			alert_msg += error_msg
		}
		alert_msg += "\n_______________________________________________________________"
		alert(alert_msg)
		//focus_point.focus()
		return false
	}
}
function submitTest(f) {
	var submitMsg = "<html>\n\t<head>\n\t\t<title>Form is valid.</title>\n\t</head>\n\t<body onblur='window.self.close()'>\n\t\t<table cellpadding=\"2\" border=\"1\">"
	for (var i=0; i < f.length; i++){
		var e = f.elements[i]
		alert(e.longname)
		if ((e.type == "text")||(e.type == "hidden")||(e.type == "textarea")||(e.type == "password")||(e.type == "button")||(e.type == "submit")) {
			submitMsg += "\n\t\t\t<tr>"
			submitMsg += "\n\t\t\t\t<td>"
			submitMsg += "\n\t\t\t\t\t" + e.name
			submitMsg += "\n\t\t\t\t</td>"
			submitMsg += "\n\t\t\t\t<td>"
			submitMsg += "\n\t\t\t\t\t&nbsp;&nbsp;&nbsp;" + e.value
			submitMsg += "\n\t\t\t\t</td>"
			submitMsg += "\n\t\t\t</tr>"
		}
		if (e.type == ("checkbox")) {
			if (e.checked = true) {
				submitMsg += "\n\t\t\t<tr>"
				submitMsg += "\n\t\t\t\t<td>"
				submitMsg += "\n\t\t\t\t\t" + e.name
				submitMsg += "\n\t\t\t\t</td>"
				submitMsg += "\n\t\t\t\t<td>"
				submitMsg += "\n\t\t\t\t\t&nbsp;&nbsp;&nbsp;" + e.value
				submitMsg += "\n\t\t\t\t</td>"
				submitMsg += "\n\t\t\t</tr>"
			}
		}
		if (e.type == ("radio")) {
			if (e.checked == true) {
				submitMsg += "\n\t\t\t<tr>"
				submitMsg += "\n\t\t\t\t<td>"
				submitMsg += "\n\t\t\t\t\t" + e.name
				submitMsg += "\n\t\t\t\t</td>"
				submitMsg += "\n\t\t\t\t<td>"
				submitMsg += "\n\t\t\t\t\t&nbsp;&nbsp;&nbsp;" + e.value
				submitMsg += "\n\t\t\t\t</td>"
				submitMsg += "\n\t\t\t</tr>"
			}
		}

		if (e.type == "select-one") {
			submitMsg += "\n\t\t\t<tr>"
			submitMsg += "\n\t\t\t\t<td>"
			submitMsg += "\n\t\t\t\t\t" + e.name
			submitMsg += "\n\t\t\t\t</td>"
			submitMsg += "\n\t\t\t\t<td>"
			submitMsg += "\n\t\t\t\t\t&nbsp;&nbsp;&nbsp;" + e.options[e.selectedIndex].value
			submitMsg += "\n\t\t\t\t</td>"
			submitMsg += "\n\t\t\t</tr>"
		}

		if (e.type == "select-multiple") {
			submitMsg += "\n\t\t\t<tr>"
			submitMsg += "\n\t\t\t\t<td>"
			submitMsg += "\n\t\t\t\t\t" + e.name
			submitMsg += "\n\t\t\t\t</td>"
			submitMsg += "\n\t\t\t\t<td>"
			for (var j=0; j < e.length; j++) {
				if (e.options[j].selected == true) {
					submitMsg += "\n\t\t\t\t\t&nbsp;&nbsp;&nbsp;" + e.options[j].value + "<br>"
				}
			}
			submitMsg += "\n\t\t\t\t</td>"
			submitMsg += "\n\t\t\t</tr>"
		}
	}
	submitMsg += "\n\t\t</table>\n\t<body>"
	var msgWindow = window.open("","test_submit","resizable,status,width=600,height=400,scroll=auto")
	var target = msgWindow.document
	target.write(submitMsg)
	target.close()
	msgWindow.focus()
}
function AddList(obj,val)
{
var text1;
var Bcheck;
Bcheck=true;
 eval("var maxz=document.form1." + obj + ".length");
	for(var z=0;z<maxz;z++)
	{
		text1=eval("document.form1." + obj + ".options(" + z + ").text");
		if(val.toUpperCase()==text1.toUpperCase())
		{
			Bcheck=false;
			z=maxz+1
		}
		else
		{
			Bcheck=true
		}
	}
	if(Bcheck==true)
	{			
			var oOption = document.createElement("OPTION");
			eval("document.form1." + obj + ".options.add(oOption)" );
			oOption.innerText = val;
			indx=oOption.index;
			var ds=obj.indexOf("_");
			if (ds>=1) {
			cap=obj.substring(0,1);
			div="div" + cap.toUpperCase() +(obj.substring(1,obj.indexOf("_")));
			}
			else
			{			
			div="div" + (obj.substring(3,obj.length));
			}
			eval("document.form1." + obj + ".options[" + indx + "].selected=true")
			eval("document.form1." + obj + ".focus()")
			eval("hide('" + div + "');");
	}
}
function show(object){
	if (document.layers && document.layers[object] != null)
	      document.layers[object].visibility = 'visible';
	else if (document.all)
	      document.all[object].style.visibility = 'visible';
}
function hide(object){
	for(var i=0;i<=5000;i++);
		if (document.layers && document.layers[object] != null)			
			document.layers[object].visibility = 'hidden';
		else if (document.all)
			  document.all[object].style.visibility = 'hidden';			  
}

function chklist(val,div){
		if ("other" == val){
			eval("show('" + div + "');");
			//txtbox=div.substring(3).toLowerCase()+"1"
			//eval("document.all." + txtbox + ".focus()")
			
		}
		else
		{
			eval("hide('" + div + "');");
		}
}

function SelCompany(x,CompArr,ptop,pleft)
{
	var Xlen=x.length;
	var SelVal,Let1;
	var tempArr=new Array()
	var j=0
	if (CompArr.length>0)
	{
	for (var i=0;i<CompArr.length;i++)
	{
		SelVal=CompArr[i];
		Let1=SelVal.substring(0,Xlen);
		if (x.toUpperCase()==Let1.toUpperCase())
		{
			tempArr[j]=SelVal;
			j++; 
		}
	}
	
		if(document.layers)
		{
				FinalStr="<table border=1 cellspacing=0 bordercolor=black>"
		}
		else
		{
				FinalStr="<table border=0 cellspacing=1 bgcolor=black>"
		}
		var showmap=""

		for(var a=0;a<tempArr.length;a++)
		{
				if(a==0)
				{
					showmap=showmap+ "<tr><td bgcolor='#dddddd'><font color=red FACE=VERDANA size=2><b>Following exists in our Database...</b></font></td></tr><tr><td bgcolor='#6495ed'><font color=black FACE=VERDANA size=2>"+tempArr[a]	+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td></tr>"
				}
				else
				{
						if(tempArr[a].toLowerCase()=="undefined")
						{
						}
						else
						{
								showmap=showmap+"<tr><td bgcolor=#6495ed>"+"<font color=#000000 FACE=VERDANA size=2>"+tempArr[a]+"</font></td></tr>"
						}
				}

		}
			
			showmap=showmap+"</table>"
			FinalStr=FinalStr+showmap
		
		if (x.length>=1)
		{
			VisIE="visible";
			VisNet="show";		}		else		{
			VisIE="hidden";
			VisNet="hide";		}
			if(navigator.appName=="Microsoft Internet Explorer")
			{
					document.all.divCompany.style.left=ptop
					document.all.divCompany.style.top=pleft
					divCompany.innerHTML=FinalStr
					divCompany.style.height = 0;
					document.all.divCompany.style.visibility=VisIE;
			}
			else
			{
						document.divCompany.visibility=VisNet;
						document.divCompany.Top=100
						document.divCompany.left=0
						layername=document.divCompany
						layername.document.write(FinalStr)
						layername.document.close()
			}
	}
}

function SelCountry(x)
{
	var SelVal,Let1;
	for (var i=0;i<=maxIndx;i++)
	{
		SelVal=StateArr[i]
		if (x==SelVal)
		{
		var country_id=StateArr1[i];
		i=maxIndx+1
		}
	}
	for (i=0;i<document.form1.country_id.length;i++)
	{
		SelVal=document.form1.country_id.options(i).value
		if (country_id==SelVal)
		{
		document.form1.country_id.options[i].selected=true
		i=document.form1.country_id.length+1
		}
	}
}
function SelState(x)
{
	var SelVal,z=2;
	for (var i=0;i<=maxIndx;i++)
	{
		SelVal=StateArr1[i]
		document.form1.state_id.options(i+2).value="invalid";
		document.form1.state_id.options(i+2).text="";
		if (x==SelVal)
		{
		document.form1.state_id.options(z).value=StateArr[i];
		document.form1.state_id.options(z).text=StateArr2[i];
		z=z+1
		}
		if (x=="invalid")
		{
		document.form1.state_id.options(i+2).value=StateArr[i];
		document.form1.state_id.options(i+2).text=StateArr2[i];
		}
	}
}


function CompanyList(initval,CompArr,ptop,pleft)
{	var x;
	num=initval.search(" ")
	if(num>-1)
	{arr=initval.split(" ")
	 x=arr[0]
	}
	else
	{x=initval
	}
	
	var Xlen=x.length;
	var SelVal,Let1;
	var tempArr=new Array()
	var j=0
	if (CompArr.length>0)
	{
	for (var i=0;i<CompArr.length;i++)
	{
		SelVal=CompArr[i];
		Let1=SelVal.substring(0,Xlen);
		if (x.toUpperCase()==Let1.toUpperCase())
		{
			tempArr[j]=SelVal;
			j++; 
		}
	}
	
		if(document.layers)
		{
				FinalStr="<table border=1 cellspacing=0 bordercolor=black>"
		}
		else
		{
				FinalStr="<table border=0 cellspacing=1 bgcolor=black>"
		}
		var showmap=""

		for(var a=0;a<tempArr.length;a++)
		{
				if(a==0)
				{
					showmap=showmap+ "<tr><td bgcolor='#dddddd'><font color=red FACE=VERDANA size=2><b>Following exists in our Database...</b></font></td></tr><tr><td bgcolor='#6495ed'><font color=black FACE=VERDANA size=2>"+tempArr[a]	+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td></tr>"
				}
				else
				{
						if(tempArr[a].toLowerCase()=="undefined")
						{
						}
						else
						{
								showmap=showmap+"<tr><td bgcolor=#6495ed>"+"<font color=#000000 FACE=VERDANA size=2>"+tempArr[a]+"</font></td></tr>"
						}
				}

		}
			
			showmap=showmap+"</table>"
			FinalStr=FinalStr+showmap
		
		if (x.length>=1)
		{
			VisIE="visible";
			VisNet="show";		}		else		{
			VisIE="hidden";
			VisNet="hide";		}
			if(navigator.appName=="Microsoft Internet Explorer")
			{
					document.all.divCompany.style.left=ptop
					document.all.divCompany.style.top=pleft
					divCompany.innerHTML=FinalStr
					divCompany.style.height = 0;
					document.all.divCompany.style.visibility=VisIE;
			}
			else
			{
						document.divCompany.visibility=VisNet;
						document.divCompany.Top=100
						document.divCompany.left=0
						layername=document.divCompany
						layername.document.write(FinalStr)
						layername.document.close()
			}
	}
}
