// Main Form Validating Function
function Validator(frmname)
{
	this.formobj=document.forms[frmname];
	if(!this.formobj)
	{
		alert("Error retrieving Form: "+frmname);
		return;
	}
	if(this.formobj.onsubmit && this.formobj.submitted.value!='')
	{
		this.formobj.old_onsubmit = this.formobj.onsubmit;
		this.formobj.onsubmit=null;
	}
else
	{
		this.formobj.old_onsubmit = null;
	}
	this.formobj.onsubmit=form_submit_handler;

	this.addValidation = add_validation;
	this.setAddnlValidationFunction=set_addnl_vfunction;
	this.clearAllValidations = clear_all_validations;
}

function set_addnl_vfunction(functionname)
{
	this.formobj.addnlvalidation = functionname;
}

function clear_all_validations()
{
	for(var itr=0;itr < this.formobj.elements.length;itr++)
	{
		this.formobj.elements[itr].validationset = null;
	}
}

function form_submit_handler()
{
	for(var itr=0;itr < this.elements.length;itr++)
	{
		if(this.elements[itr].validationset &&
		!this.elements[itr].validationset.validate())
		{
			return false;
		}
	}
	if(this.addnlvalidation)
	{
		str =" var ret = "+this.addnlvalidation+"()";
		eval(str);
		if(!ret) return ret;
	}
	return true;
}

function add_validation(itemname,descriptor,errstr)
{
	if(!this.formobj)
	{
		alert("BUG: the form object is not set properly");
		return;
	}
	var itemobj = this.formobj[itemname];

	if(itemobj.length && isNaN(itemobj.selectedIndex) )
	{
		itemobj = itemobj[0];
	}
	if(!itemobj)
	{
		alert("BUG: Could not get the input object named: "+itemname);
		return;
	}

	if(!itemobj.validationset)
	{
		itemobj.validationset = new ValidationSet(itemobj);
	}
	itemobj.validationset.add(descriptor,errstr);
}

function ValidationDesc(inputitem,desc,error)
{
	this.desc=desc;
	this.error=error;
	this.itemobj = inputitem;
	this.validate=vdesc_validate;
}

function vdesc_validate()
{
	if(!validateInput(this.desc,this.itemobj,this.error))
	{
		this.itemobj.focus();
		return false;
	}
	return true;
}

function ValidationSet(inputitem)
{
	this.vSet=new Array();
	this.add= add_validationdesc;
	this.validate= vset_validate;
	this.itemobj = inputitem;
}

function add_validationdesc(desc,error)
{
	this.vSet[this.vSet.length]=
	new ValidationDesc(this.itemobj,desc,error);
}

function vset_validate()
{
	for(var itr=0;itr<this.vSet.length;itr++)
	{
		if(!this.vSet[itr].validate())
		{
			return false;
		}
	}
	return true;
}

function validateEmail(email)
{
	var splitted = email.match("^(.+)@(.+)$");
	if(splitted == null) return false;
	if(splitted[1] != null )
	{
		var regexp_user=/^\"?[\w-_\.]*\"?$/;
		if(splitted[1].match(regexp_user) == null) return false;
	}
	if(splitted[2] != null)
	{
		var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
		if(splitted[2].match(regexp_domain) == null)
		{
			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if(splitted[2].match(regexp_ip) == null) return false;
		}
		return true;
	}
	return false;
}

function TestComparison(objValue,strCompareElement,strvalidator,strError)
{
	var bRet=true;
	var objCompare=null;
	if(!objValue.form)
	{
		alert("BUG: No Form object!");
		return false
	}
	objCompare = objValue.form.elements[strCompareElement];
	if(!objCompare)
	{
		alert("BUG: Element with name"+strCompareElement+" not found !");
		return false;
	}
	if(strvalidator != "eqelmnt" &&
	strvalidator != "neelmnt")
	{
		if(isNaN(objValue.value))
		{
			alert(objValue.name+": Should be a number ");
			return false;
		}
		if(isNaN(objCompare.value))
		{
			alert(objCompare.name+": Should be a number ");
			return false;
		}
	}
	var cmpstr="";
	switch(strvalidator)
	{
		case "eqelmnt":
		{
			if(objValue.value != objCompare.value)
			{
				cmpstr = " should be equal to ";
				bRet = false;
			}
			break;
		}
		case "ltelmnt":
		{
			if(eval(objValue.value) >= eval(objCompare.value))
			{
				cmpstr =  " should be less than ";
				bRet = false;
			}
			break;
		}
		case "leelmnt":
		{
			if(eval(objValue.value) >  eval(objCompare.value))
			{
				cmpstr =  " should be less than or equal to";
				bRet = false;
			}
			break;
		}
		case "gtelmnt":
		{
			if(eval(objValue.value) <=  eval(objCompare.value))
			{
				cmpstr =  " should be greater than";
				bRet = false;
			}
			break;
		}
		case "geelmnt":
		{
			if(eval(objValue.value) < eval(objCompare.value))
			{
				cmpstr =  " should be greater than or equal to";
				bRet = false;
			}
			break;
		}
		case "neelmnt":
		{
			if(objValue.value.length > 0 &&
			objCompare.value.length > 0 &&
			objValue.value == objCompare.value)
			{
				cmpstr = " should be different from ";
				bRet = false;
			}
			break;
		}
	}
	if(bRet==false)
	{
		if(!strError || strError.length==0)
		{
			strError = objValue.name + cmpstr + objCompare.value;
		}
		alert(strError);
	}
	return bRet;
}

function TestSelMin(objValue,strMinSel,strError)
{
	var bret = true;
	var objcheck = objValue.form.elements[objValue.name];
	var chkcount =0;
	if(objcheck.length)
	{
		for(var c=0;c < objcheck.length;c++)
		{
			if(objcheck[c].checked == "1")
			{
				chkcount++;
			}
		}
	}
else
	{
		chkcount = (objcheck.checked == "1")?1:0;
	}
	var minsel = eval(strMinSel);
	if(chkcount < minsel)
	{
		if(!strError || strError.length ==0)
		{
			strError = "Please Select at least"+minsel+" check boxes for"+objValue.name;
		}
		alert(strError);
		bret = false;
	}
	return bret;
}

function TestDontSelectChk(objValue,chkValue,strError)
{
	var pass=true;
	var objcheck = objValue.form.elements[objValue.name];
	if(objcheck.length)
	{
		var idxchk=-1;
		for(var c=0;c < objcheck.length;c++)
		{
			if(objcheck[c].value == chkValue)
			{
				idxchk=c;
				break;
			}
		}
		if(idxchk>= 0)
		{
			if(objcheck[idxchk].checked=="1")
			{
				pass=false;
			}
		}
	}
else
	{
		if(objValue.checked == "1")
		{
			pass=false;
		}
	}
	if(pass==false)
	{
		if(!strError || strError.length ==0)
		{
			strError = "Can't Proceed as you selected "+objValue.name;
		}
		alert(strError);

	}
	return pass;
}

function TestRequiredInput(objValue,strError)
{
	var ret = true;
	if(eval(objValue.value.length) == 0)
	{
		if(!strError || strError.length ==0)
		{
			strError = objValue.name + " : Required Field";
		}
		alert(strError);
		ret=false;
	}
	return ret;
}

function TestMaxLen(objValue,strMaxLen,strError)
{
	var ret = true;
	if(eval(objValue.value.length) > eval(strMaxLen))
	{
		if(!strError || strError.length ==0)
		{
			strError = objValue.name + " : "+ strMaxLen +" characters maximum ";
		}
		alert(strError + "\n[Current length = " + objValue.value.length + " ]");
		ret = false;
	}
	return ret;
}

function TestMinLen(objValue,strMinLen,strError)
{
	var ret = true;
	if(eval(objValue.value.length) <  eval(strMinLen))
	{
		if(!strError || strError.length ==0)
		{
			strError = objValue.name + " : " + strMinLen + " characters minimum  ";
		}
		alert(strError + "\n[Current length = " + objValue.value.length + " ]");
		ret = false;
	}
	return ret;
}

function TestInputType(objValue,strRegExp,strError,strDefaultError)
{
	var ret = true;

	var charpos = objValue.value.search(strRegExp);
	if(objValue.value.length > 0 &&  charpos >= 0)
	{
		if(!strError || strError.length ==0)
		{
			strError = strDefaultError;
		}
		alert(strError + "\n [Error character position " + eval(charpos+1)+"]");
		ret = false;
	}
	return ret;
}

function TestEmail(objValue,strError)
{
	var ret = true;
	if(objValue.value.length > 0 && !validateEmail(objValue.value)	 )
	{
		if(!strError || strError.length ==0)
		{
			strError = objValue.name+": Enter a valid Email address ";
		}
		alert(strError);
		ret = false;
	}
	return ret;
}

function TestLessThan(objValue,strLessThan,strError)
{
	var ret = true;
	if(isNaN(objValue.value))
	{
		alert(objValue.name+": Should be a number ");
		ret = false;
	}
else
	if(eval(objValue.value) >=  eval(strLessThan))
	{
		if(!strError || strError.length ==0)
		{
			strError = objValue.name + " : value should be less than "+ strLessThan;
		}
		alert(strError);
		ret = false;
	}
	return ret;
}

function TestGreaterThan(objValue,strGreaterThan,strError)
{
	var ret = true;
	if(isNaN(objValue.value))
	{
		alert(objValue.name+": Should be a number ");
		ret = false;
	}
else
	if(eval(objValue.value) <=  eval(strGreaterThan))
	{
		if(!strError || strError.length ==0)
		{
			strError = objValue.name + " : value should be greater than "+ strGreaterThan;
		}
		alert(strError);
		ret = false;
	}
	return ret;
}

function TestRegExp(objValue,strRegExp,strError)
{
	var ret = true;
	if( objValue.value.length > 0 &&
	!objValue.value.match(strRegExp) )
	{
		if(!strError || strError.length ==0)
		{
			strError = objValue.name+": Invalid characters found ";
		}
		alert(strError);
		ret = false;
	}
	return ret;
}

function TestDontSelect(objValue,index,strError)
{
	var ret = true;
	if(objValue.selectedIndex == null)
	{
		alert("error in TestDontSelect");
		ret = false;
	}
else
	if(objValue.selectedIndex == eval(index))
	{
		if(!strError || strError.length ==0)
		{
			strError = objValue.name+": Please Select one option ";
		}
		alert(strError);
		ret =  false;
	}
	return ret;
}

function TestSelectOneRadio(objValue,strError)
{
	var objradio = objValue.form.elements[objValue.name];
	var one_selected=false;
	for(var r=0;r < objradio.length;r++)
	{
		if(objradio[r].checked == "1")
		{
			one_selected=true;
			break;
		}
	}
	if(false == one_selected)
	{
		if(!strError || strError.length ==0)
		{
			strError = "Please select one option from "+objValue.name;
		}
		alert(strError);
	}
	return one_selected;
}

function validateInput(strValidateStr,objValue,strError)
{
	var ret = true;
	var epos = strValidateStr.search("=");
	var  command  = "";
	var  cmdvalue = "";
	if(epos >= 0)
	{
		command  = strValidateStr.substring(0,epos);
		cmdvalue = strValidateStr.substr(epos+1);
	}
else
	{
		command = strValidateStr;
	}
	switch(command)
	{
		case "req":
		case "required":
		{
			ret = TestRequiredInput(objValue,strError)
			break;
		}
		case "maxlength":
		case "maxlen":
		{
			ret = TestMaxLen(objValue,cmdvalue,strError)
			break;
		}
		case "minlength":
		case "minlen":
		{
			ret = TestMinLen(objValue,cmdvalue,strError)
			break;
		}
		case "alnum":
		case "alphanumeric":
		{
			ret = TestInputType(objValue,"[^A-Za-z0-9]",strError,
			objValue.name+": Only alpha-numeric characters allowed ");
			break;
		}
		case "alnum_s":
		case "alphanumeric_space":
		{
			ret = TestInputType(objValue,"[^A-Za-z0-9\\s]",strError,
			objValue.name+": Only alpha-numeric characters and space allowed ");
			break;
		}
		case "num":
		case "numeric":
		{
			ret = TestInputType(objValue,"[^0-9]",strError,
			objValue.name+": Only digits allowed ");
			break;
		}
		case "alphabetic":
		case "alpha":
		{
			ret = TestInputType(objValue,"[^A-Za-z]",strError,
			objValue.name+": Only alphabetic characters allowed ");
			break;
		}
		case "alphabetic_space":
		case "alpha_s":
		{
			ret = TestInputType(objValue,"[^A-Za-z\\s]",strError,
			objValue.name+": Only alphabetic characters and space allowed ");
			break;
		}
		case "email":
		{
			ret = TestEmail(objValue,strError);
			break;
		}
		case "lt":
		case "lessthan":
		{
			ret = TestLessThan(objValue,cmdvalue,strError);
			break;
		}
		case "gt":
		case "greaterthan":
		{
			ret = TestGreaterThan(objValue,cmdvalue,strError);
			break;
		}
		case "regexp":
		{
			ret = TestRegExp(objValue,cmdvalue,strError);
			break;
		}
		case "dontselect":
		{
			ret = TestDontSelect(objValue,cmdvalue,strError)
			break;
		}
		case "dontselectchk":
		{
			ret = TestDontSelectChk(objValue,cmdvalue,strError)
			break;
		}
		case "selmin":
		{
			ret = TestSelMin(objValue,cmdvalue,strError);
			break;
		}
		case "selone":
		{
			ret = TestSelectOneRadio(objValue,strError);
			break;
		}
		case "eqelmnt":
		case "ltelmnt":
		case "leelmnt":
		case "gtelmnt":
		case "geelmnt":
		case "neelmnt":
		{
			return TestComparison(objValue,cmdvalue,command,strError);
			break;
		}
	}
	return ret;
}

// Advance Tab -- moves focus after max length of field is met
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e)
{
	var keyCode = (isNN) ? e.which : e.keyCode;
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];

	if(input.value.length >= len && !containsElement(filter,keyCode))
	{
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
	function containsElement(arr, ele)
	{
		var found = false, index = 0;
		while(!found && index < arr.length)
		if(arr[index] == ele)
		found = true;
	else
		index++;
		return found;
	}
	function getIndex(input)
	{
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
	else i++;
	return index;
}
return true;
}

// Show hide Layers
function showHide(layerName)
{
	if (document.getElementById)
	{
		var fred = document.getElementById(layerName);
		var display = fred.style.display ? '' : 'none';fred.style.display = display;return;
	}
}

// Validate Check Boxes
function validateThisBox(formName, field, state)
{
	if(state == "checked")
	{
		if(eval("document." + formName + "." + field + ".checked") == "false");
		{
			alert("You must click on \"I agree\", to continue processing your order.");
			return false;
		}
	}
	return true;
}

// Disable form elements on click
function init()
{
	if (!document.layers) return;
	var box = document.forms[0].elements;
	for (var i=0;i<box.length;i++)
	{
		box[i].disabled = false;
	}
}

function disableIt(obj)
{
	obj.disabled = !(obj.disabled);
	var z = (obj.disabled) ? 'disabled' : 'enabled';
	alert(obj.type + ' now ' + z);
}

function extracheck(obj)
{
	return !obj.disabled;
}

// Disable check box
var t = "locked";
var f = "unlocked";
function lockIt(p)
{
	var l = document.theForm.key.value;
	if(l==p)return;

	document.theForm.pooling.disabled=(document.theForm.key.value=(l==f)?t:f)==t;

}

function isDis()
{
	return (document.theForm.key.value==t);
}

// Check  & un-check checkboxes
function checkBox(fieldName, state)
{
	if(state == "c")
	{
		var object = eval("document.theForm."+ fieldName);
		object.checked = true;
	}
else
	{
		var object = eval("document.theForm."+ fieldName);
		object.checked = false;
	}
}

// Validate Agreement
function checkAgreement()
{
	var agreement;
	for(var i=0; i<document.theForm.finePrint.length; i++)
	{

		if(document.theForm.finePrint[i].checked)
		{
			agreement =  document.theForm.finePrint[i].value;
		}
	}
	if(agreement == 'n')
	{
		alert("You must click on \"I agree\", to continue processing your order.");
		return false;
	}
	return true;
}

// Validate Card Form
var selectedCard       = null;
var selectedCardPlan  = null;
function validateAirCardForm()
{
	for(var i=0; i<document.theForm.airCard.length; i++)
	{
		if(document.theForm.airCard[i].checked)
		{
			selectedCard = document.theForm.airCard[i].value.substring(0,1);
		}
	}
	if(selectedCard == "" || selectedCard == null)
	{
		alert("Please select a Connection Card before submitting this form.");
		return false;
	}

	for(var i=0; i<document.theForm.selCardPlan.length; i++)
	{
		if(document.theForm.selCardPlan[i].checked)
		{
			selectedCardPlan = document.theForm.selCardPlan[i].value;
		}
	}
	if(selectedCardPlan == "" || selectedCardPlan == null)
	{
		alert("Please select a Plan for the selected Connection Card.");
		return false;
	}
	return true;
}

// Validate Card Java Form
var selectedCard       = null;
var selectedCardPlan  = null;
function validateAirCardFormJava()
{
	for(var i=0; i<document.theForm.card.length; i++)
	{
		if(document.theForm.card[i].checked)
		{
			selectedCard = document.theForm.card[i].value.substring(0,1);
		}
	}
	if(selectedCard == "" || selectedCard == null)
	{
		alert("Please select a Connection Card before submitting this form.");
		return false;
	}

	for(var i=0; i<document.theForm.selCardPlan.length; i++)
	{
		if(document.theForm.selCardPlan[i].checked)
		{
			selectedCardPlan = document.theForm.selCardPlan[i].value;
		}
	}
	if(selectedCardPlan == "" || selectedCardPlan == null)
	{
		alert("Please select a Plan for the selected Connection Card.");
		return false;
	}
	return true;
}

// Site-O-Matic Field Validation
function validateDiscountField()
{
	for(var i=0; i<document.theForm.siteType.length; i++)
	{
		if(document.theForm.siteType[i].checked)
		{
			siteType = document.theForm.siteType[i].value;
		}
	}
	if(siteType == "std" && document.theForm.stdDisc.value == "")
	{
		alert("Please enter the Employee Discount before submitting this form.");
		return false;
	}
else if(siteType == "corp" && document.theForm.corpDisc.value == "")
{
	alert("Please enter the Corporate Discount before submitting this form.");
	return false;
}
else if(siteType == "split")
{
	if(document.theForm.corpDisc.value == "")
	{
		alert("Please enter the Corporate Discount before submitting this form.");
		return false;
	}
else if(document.theForm.stdDisc.value == "")
{
	alert("Please enter the Employee Discount before submitting this form.");
	return false;
}
}
else if(siteType == "s" && (document.theForm.stdDisc.value == "" && document.theForm.corpDisc.value == ""))
{
	alert("Please enter the Discount before submitting this form.");
	return false;
}
return true;
}

// Jump Function
function jumpURL()
{
	var i, args = jumpURL.arguments; document.jumpReturnValue = false;
	for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

// Pop Up function
function popUp(page, name, w, h, scroll)
{
	name = "foo";
	page = page.split(" ").join("%20");
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;

	winprop  = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=yes';
	bar = window.open(page, name, winprop);
	bar.focus();
}

// Print Window Function
function printThisPage()
{
	var ua = navigator.userAgent.toLowerCase();

	var is_mac = ua.indexOf('mac') > 0;

	if (is_mac)
	{
		alert('To print:\n\nUse Command + P. on your keyboard\n')
	}
else
	{
		print();
	}
}

// resize window on launch
function resizeWin(popUpSize)
{
	if(opener)
	{
		if(document.body.id=="pop")
		{
			// redo sizes
			if (popUpSize == "short")
			{
				window.resizeTo(440, 220);
			}
		else if (popUpSize == "mid")
		{
			window.resizeTo(440, 330);
		}
	else if (popUpSize == "wide")
	{
		window.resizeTo(630, 440);
	}
else
	{
		window.resizeTo(440, 440);
	}
}
else
	{
		// pre-redo sizes
		if (popUpSize == "short")
		{
			window.resizeTo(570, 310);
		}
	else if (popUpSize == "wide")
	{
		window.resizeTo(670, 440);
	}
else
	{
		window.resizeTo(570, 440);
	}
}
}
}

// Stay on top Function
function stayOnTop(url,mwidth,mheight)
{
	mwidth = mwidth + 2;
	mheight = mheight + 2;
	w = window.open(url,"popup","width="+mwidth+"px,height="+mheight+"px,resizable=0,scrollbars=1");
	w.focus();
	w.resizeTo(mwidth, mheight);
}

// Show and Hide HTML
function hideIt(divToHide)
{
	showIt(divToHide,"none")
}

function showIt(divToHide,showOrHide)
{
	var fieldStyle = getStyleObject(divToHide);
	if(fieldStyle != false)
	{
		fieldStyle.display = showOrHide;
	}
}

function getStyleObject(objectId)
{
	if (document.getElementById && document.getElementById(objectId))
	{
		return document.getElementById(objectId).style;
	}
else if (document.all && document.all(objectId))
{
	return document.all(objectId).style;
}
else
	{
		return false;
	}
}

// Hide Status Bar messages.
var msg="";

function hideStatusMsg()
{
	window.status=msg;
	return true;
}

// Change Text
function changeTextValue()
{
	document.theForm.newUrl.value = "http://www."+document.theForm.incFileName.value+".callsprint.com";
}

// Change Text 2
function changeTextValueII(field1, field2)
{
	if(field1 == "shipAddr1")
	{
		document.theForm.cardAddr1.value = document.theForm.shipAddr1.value;
	}
else  if(field1 == "shipAddr2")
{
	document.theForm.cardAddr2.value = document.theForm.shipAddr2.value;
}
else  if(field1 == "shipCity")
{
	document.theForm.cardCity.value = document.theForm.shipCity.value;
}
else  if(field1 == "shipState")
{
	document.theForm.cardState.value = document.theForm.shipState.value;
}
else  if(field1 == "shipZip1")
{
	document.theForm.cardZip1.value = document.theForm.shipZip1.value;
}
else  if(field1 == "shipZip2")
{
	document.theForm.cardZip2.value = document.theForm.shipZip2.value;
}
}

// Site-O-Matic color picker
function returnColor(color)
{
	ForB         = 0;
	hexStr       = "0123456789ABCDEF";
	defaultValue = "#FFFFFF";

	if(ForB == 0)
	{
		document.theForm.styleColor.value = color;
		document.theForm.oTextArea.style.scrollbarFaceColor = color;
		document.theForm.oTextArea.style.backgroundColor = color;
	}
else
	{
		document.theForm.styleColor.value = defaultValue;
	}
}
// light up text fields on focus
color = new Array('D87C7C','white','silver');
function highlight(state)
{
	element = event.srcElement;
	if (element.tagName=='INPUT')
	{
		etype = element.type;

		if ((etype=='submit' || etype=='reset') && state==1)
		state=2;
		element.style.backgroundColor=color[state];
		element.focus();
	}
}
