function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	verify password is at least 6 chars long
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function chkPassminLength(form)	{
	if (form.r_txtPassword.value.length<6)		{
		alert("Password must be at least 6 characters in length.")
		form.r_txtPassword.select()
		return false;
	}
	return true;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	verify PC prods password does not exceed 15 chars 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function chkPCmaxLength(form)	{
	var sProdSelected
	for (var i=0; i < form.r_selProduct.length; i++)		{
		if (form.r_selProduct.options[i].selected)		{
			sProdSelected=form.r_selProduct.options[i].value
		}
	}
	switch (eval(sProdSelected))	{
		// omitting break statement causes last case to execute for every one above
		case 1:		//	InfoAnalysis
		case 6:		//	PortfolioEvaluation
		case 7:		//	Hamlet
		case 9:		//	LeaseEnterprise
		case 60:	//	Rapport
			if (form.r_txtPassword.value.length>15)		{
				alert("Password may not exceed 15 characters.")
				form.r_txtPassword.select()
				return false;
			}
	}
	return true;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	validate login form before submitting
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function validate(form) {
	//	verify required fields are filled out
	var chkfield=checkRequiredTextFields(form)
	if (chkfield == true)		{
		return true
	} else {
		var msg
		switch (chkfield.name) {
			case "r_txtLogin":
				msg="Please enter your username."
				break
			case "r_txtPassword":
				msg="Please enter password."
				break
			default:
				msg="Please fill out all required fields."
		}
		alert(msg);
		chkfield.select()
		return false
	}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	submit login form
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function submitForm(form) {
	if (form)	{			//	prevents errors in case lookup error hides login form but not button 
		if (validate(form))		{
			if (chkPassminLength(form))		{
				if (chkPCmaxLength(form))		{
					if(form.chkRememberMe.value == 'true')	{
						//check remember me and set cookie
						var cookieVal = form.r_txtLogin.value;
						var expires = new Date();
						expires.setFullYear(expires.getFullYear() + 1);
						setCookie("IDSLogin",cookieVal,expires,"/");
					}
					//submit
					form.submit()
				}
			}
		}
	}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// form submit when password field is selected and user presses Enter
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function submitenter(field,e)	{
	var keycode;
	if (window.event) keycode=window.event.keyCode;
	else if (e) keycode=e.which;
	else return true;

	if (keycode==13)	{
		submitForm(field.form)
		return false;
	}
	else
	 return true;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	analogous to VBScript Trim() function;
//	removes leading and trailing spaces from passed string
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function trim(str) {
	str = str.replace(/\B\s+/,"").replace(/\s+$/,"")
	return str
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	required text and pasword fields should be named with the 
//	prefix 'r_' 
// returns field name if validation fails
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function checkRequiredTextFields(form) {
	for (var i = 0; i < form.elements.length; i++)		{
		var elm = form.elements[i]
		//	text (and password) fields whose name begin with 'r_' and have no input
		if (elm.type == "textarea" || elm.type == "text" || elm.type == "password")		{
			if (elm.name.substr(0,2) == "r_" && trim(elm.value) == "")		{
				return elm
			}
		}
	}
	return true
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	confirm valid email address
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function validEmail(str) {
	var re = /\b@.+\.\b/					//	@ not the first character in a word followed by at least one character, and a dot that's not the end of a word
	if (str.search(re) == -1)		{
		return false
	}
	return true
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Variable Forbidden Character validation
//	confirm string doesn't contain any of the characters passed as parameter
//	returns true if the string passes, ie. does NOT contain the forbidden chars
//	1. str - string to be tested
//	2. chrList - a string of the forbidden chars WITHOUT additional spaces or delimiters, enclosed within single or double quotes (ex: ";':-")
//	   note: the following special chars must be escaped with a backslash (\) to include them as forbidden chars: \ ' " 
//	   the quotes only when enclosed by similar quotes (ex: "\"'" OR '"\'')
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function doesnotcontain(str,chrList) {
	chrList=chrList.replace(/([\\\^])/g,"\\$1")		//	escape special chars: \ ^
	var re=new RegExp("["+ chrList +"]","g") 
	if (re.test(str))		{
		return false
	}
	return true
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	Standard Text field validation (variable maxlength:)
//	1. allow alpha-numeric plus space, single hyphen, parens, comma, period, single quote
//	2. requires maxlength as a parameter (null means no maxlength)
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function standardTextValidat(str, maxlength) {
	if (maxlength != null)		{
		if (str.length > maxlength)		{
			return false;
		}
	}
	var re = /[^A-Za-z0-9 \-(),\.'\?]|(--)/
	if (!re.test(str))		{
		return true;
	}
	return false;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	object creation, stores radio button properties
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function radioButton(checked,value)	{
		this.checked=checked
		this.value=value
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//	dump radio button values into arrays
//	as workaround for the fact that if there's only one radio button, button group length is undefined
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function buildRadioArray(objRadioGroup,objArray) {
	var objRadioGroupLen=objRadioGroup.length
	if (!objRadioGroupLen)		{		// if there's only one radio button, button group length is undefined
		objArray[0]=new radioButton(objRadioGroup.checked,objRadioGroup.value)
	} else	{
		for (var i=0; i < objRadioGroupLen; i++)		{
			objArray[i]=new radioButton(objRadioGroup[i].checked,objRadioGroup[i].value)
		}
	}
}

function search()		{
		var form=document.frmSearch
		if (trim(form.query.value)!="")		{
			form.submit()
		}
	}