/* bld_mail_link creates a mailto: link for the name given.  It builds
all links to quipugroup.com. */
function bld_mail_link(name) {
	document.write('<a href=mailto:' + name + '@quipugroup.com>' + name +  '@quipugroup.com' + '</a>');
}

/* bld_mail_link creates a mailto: link for the name given.  It builds
all links to quipugroup.com. */
function bld_mail_link(name) {
	document.write('<a href=mailto:' + name + '@quipugroup.com>' + name +  '@quipugroup.com' + '</a>');
}


/* function make_addr builds an email link without having the link exposed to spiders, crawlers, and other nasty things.
Usage:  when calling, pass form name/id and a comma-sep list of valtype_field.

Validation Types:	nb		Not Blank
					uspn	US Phone Number (really numbers, parens, dashes)
					em		Email Address
					sel		Something has been selected from a select list
							(requires an option with value="not_selected")
					ln#		field contains # characters
					eq		two field match (ex: password and confirm password)

Example:  onClick="return checkForm('testform','nb_myname,uspn_phone,em_email,nb_comments,sel_picklist');"

*/
function make_addr(domain,name) {
	var theDomain;
	theDomain = domain;
	document.write('<a href=mailto:' + name + '@' + theDomain + '>' + name + '@' + theDomain + '</a>');
}
/* end of make_addr */

/* bld_mail_link creates a mailto: link for the name given.  It builds
all links to quipugroup.com. */
function bld_mail_link(name) {
	document.write('<a href=mailto:' + name + '@quipugroup.com>' + name +  '@quipugroup.com' + '</a>');
}

/* checkForm does basic form field validation for a form; validation type is 
based on string attached to beginning of field name.  All fields to be  
validated should be passed to checkForm, in a comma-separated list.

Example: checkForm('nb_Name, zip_Zip, uspn_Phone, ln_6/username, eq_email/confirmEmail)

Supported validation types...
	-nb			Not Blank
	-uspn		US Phone Number
	-sel		Not "not_selected" (for selects where def item is 
				"Select...")
	-url		Begins with http:// or https:// and has more text
	-date		A date (mm/dd/yyyy is only acceptable format for clic)
	-zip		A US zip code, 5 or 5+4
	-ssn		A US Social Security Number
	-issn		ISSN
	-isbn		ISBN (10 or 13, with dashes or spaces as delim)
	-nojs		Not blank, but no <script> tags
	-ln			field is x characters long (ex: ln_##/username)
	-eq			two fields match (eq_password/verifyPassword)
	-either		one of two fields required.  With field type.
				(either_sel_FieldName/nb_FieldName)
	*/
function checkForm (flist) {
	var keepGoing = 'Y';
	var farray = flist.split(",");
	for (var Count = 0; Count < farray.length; Count++) {
		var splitAt = farray[Count].indexOf("_");
		var thisArg = farray[Count];
		var valType = thisArg.substring(0,splitAt);
		var valField = thisArg.substring(splitAt+1,farray[Count].length);

		if (valType != "ln" && valType != "eq") {
			var thisField = document.getElementById(valField);
		}

//Spambot check
		if (valType == 'sp' && keepGoing == 'Y') {
			if (thisField.value == null || thisField.value == '') {
				keepGoing = 'Y';
			} else {
				keepGoing = 'N';
			}
		}

// Not Blank (nb)		
		if (valType == 'nb' && keepGoing == 'Y') {
			var inString = thisField.value;
			var Ret = isBlank(inString);
			if (Ret) {
				thisField.style.backgroundColor = "#ffcccc";
				thisField.focus();
				var FMsg = "Please enter a value for the indicated field.\n";
				alert (FMsg);
				keepGoing = 'N';
			}
			else {
				thisField.style.backgroundColor = "";
				keepGoing = 'Y';
			}
		}
// US Phone Number (uspn)		
		if (valType == 'uspn' && keepGoing == 'Y') {
			var inString = thisField.value;
			var uspn_regexp = new RegExp(/^[0-9]{3}\s[0-9]{3}-[0-9]{4}$|^\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}$|^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^[0-9]{3}\.[0-9]{3}\.[0-9]{4}$/i);
			var validEntry = "\t(###) ###-####\n\t### ###-####\n\t###-###-####\n\t###.###.####";
			var fieldStatus = chkFieldRE(inString,uspn_regexp);
			if (fieldStatus == 'blank') {
				handle_blank(thisField,validEntry);
 				keepGoing = 'N';
			}
			if (fieldStatus == 'nomatch') {
				handle_bad(thisField,validEntry);
 				keepGoing = 'N';
			}
			if (fieldStatus == 'match') {
				thisField.style.backgroundColor = "";
				keepGoing = 'Y';
			}
		} // End of if (valType == 'uspn' && keepGoing == 'Y')
// Email Address
		if (valType == 'em' && keepGoing == 'Y') {
			var inString = thisField.value;
			var em_regexp = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/i);
			var validEntry = "\tAny Valid Email Address\n\tExample:\n\tsomeone@somenet.net";			
			var fieldStatus = chkFieldRE(inString,em_regexp);
			if (fieldStatus == 'blank') {
				handle_blank(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'nomatch') {
				handle_bad(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'match') {
				thisField.style.backgroundColor = "";
				keepGoing = 'Y';
			}
		} // End of if (valType == 'uspn' && keepGoing == 'Y')
// Select Box
		if (valType == 'sel' && keepGoing == 'Y') {
			for (var i = 0; i < thisField.length; i++) {
				if (thisField.options[i].selected) {
					var tmpValue = thisField.options[i].value;
					if (tmpValue == 'not_selected') {
						thisField.style.backgroundColor = "#ffcccc";
						thisField.focus();
						var FMsg = "Please select a value for the indicated field.\n";
						alert(FMsg);
						keepGoing = 'N';
					}
					else {
						thisField.style.backgroundColor = "";
						keepGoing = 'Y';
					}
				}
			}
		} // End of if (valType == 'em' && keepGoing == 'Y')
// URL		
		if (valType == 'url' && keepGoing == 'Y') {
			var inString = thisField.value;
			var url_regexp = new RegExp(/^http:\/\/[-A-z0-9]+\.[-A-z0-9]+\.[-A-z0-9]+|^https:\/\/[-A-z0-9]+\.[-A-z0-9]+\.[-A-z0-9]+/i);
			var validEntry = "Any valid URL, e.g.:\n\thttp://www.quipugroup.com\n--OR--\n\thttps://www.somewhere.org";
			var fieldStatus = chkFieldRE(inString,url_regexp);
			if (fieldStatus == 'blank') {
				handle_blank(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'nomatch') {
				handle_bad(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'match') {
				thisField.style.backgroundColor = "";
				keepGoing = 'Y';
			}
		} // End of if (valType == 'url' && keepGoing == 'Y')
// US Zip Code (5 or 5+4)		
		if (valType == 'zip' && keepGoing == 'Y') {
			var inString = thisField.value;
			var uszip_regexp = new RegExp(/^[0-9]{5}$|^[0-9]{5}-[0-9]{4}$/i);
			var validEntry = "\t#####\n\t#####-####";
			var fieldStatus = chkFieldRE(inString,uszip_regexp);
			if (fieldStatus == 'blank') {
				handle_blank(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'nomatch') {
				handle_bad(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'match') {
				thisField.style.backgroundColor = "";
				keepGoing = 'Y';
			}
		} // End of if (valType == 'zip' && keepGoing == 'Y')
// Date		
		if (valType == 'date' && keepGoing == 'Y') {
			var inString = thisField.value;
//			alert (thisField.value);
			var uszip_regexp = new RegExp(/^([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4})$/i);
			var validEntry = "\tmm/dd/yyyy";
			var fieldStatus = chkFieldRE(inString,uszip_regexp);
			if (fieldStatus == 'blank') {
				handle_blank(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'nomatch') {
				handle_bad(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'match') {
				thisField.style.backgroundColor = "";
				keepGoing = 'Y';
			}
		} // End of if (valType == 'date' && keepGoing == 'Y')
// Social Security Number
		if (valType == 'ssn' && keepGoing == 'Y') {
			var inString = thisField.value;
			var uszip_regexp = new RegExp(/^[0-9]{3}-[0-9]{2}-[0-9]{4}$|^[0-9]{9}$/i);
			var validEntry = "\t###-##-####\n\t#########";
			var fieldStatus = chkFieldRE(inString,uszip_regexp);
			if (fieldStatus == 'blank') {
				handle_blank(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'nomatch') {
				handle_bad(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'match') {
				thisField.style.backgroundColor = "";
				keepGoing = 'Y';
			}
		} // End of if (valType == 'ssn' && keepGoing == 'Y')
// ISSN
		if (valType == 'issn' && keepGoing == 'Y') {
			var inString = thisField.value;
			var uszip_regexp = new RegExp(/^[0-9]{4}-[0-9X]{4}$/i);
			var validEntry = "\t####-####";
			var fieldStatus = chkFieldRE(inString,uszip_regexp);
			if (fieldStatus == 'blank') {
				handle_blank(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'nomatch') {
				handle_bad(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'match') {
				thisField.style.backgroundColor = "";
				keepGoing = 'Y';
			}
		} // End of if (valType == 'issn' && keepGoing == 'Y')
// ISBN
	if (valType == 'isbn' && keepGoing == 'Y') {
			var inString = thisField.value;
			var uszip_regexp = new RegExp(/^[0-9]{3}-[0-9]{1,5}-[0-9]{1,7}-[0-9]{1,6}-[0-9]{1}$|^[0-9]{3}\s[0-9]{1,5}\s[0-9]{1,7}\s[0-9]{1,6}\s[0-9]{1}$|^[0-9]{1,5}-[0-9]{1,7}-[0-9]{1,6}-[0-9]{1}$|^[0-9]{1,5}\s[0-9]{1,7}\s[0-9]{1,6}\s[0-9]{1}$/i);
			var validEntry = "\tISBN (with dashes or spaces)\n\tExamples:\n\t\t0-7654-3188-3\n\t\t1 55591 575 2\n\t\t978-0-901690-54-8";
			var fieldStatus = chkFieldRE(inString,uszip_regexp);
			if (fieldStatus == 'blank') {
				handle_blank(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'nomatch') {
				handle_bad(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'match') {
				thisField.style.backgroundColor = "";
				keepGoing = 'Y';
			}
		} // End of if (valType == 'isbn' && keepGoing == 'Y')
// No Javascript
	if (valType == 'nojs' && keepGoing == 'Y') {
			var inString = thisField.value;
			var nojs_regexp = new RegExp(/<script\s+|<script>|<\/script>/i);
			var validEntry = "\tAny text, including HTML.\n\tPlease!  No Scripting!";
			var fieldStatus = chkFieldRE(inString,nojs_regexp);
			if (fieldStatus == 'blank') {
				handle_blank(thisField,validEntry);
				keepGoing = 'N';
			}
			if (fieldStatus == 'nomatch') {
				thisField.style.backgroundColor = "";
				keepGoing = 'Y';
			}
			if (fieldStatus == 'match') {
				handle_bad(thisField,validEntry);
				keepGoing = 'N';
			}
		} // End of if (valType == 'nojs' && keepGoing == 'Y')

//length (ex: ln_6/username)
	if (valType == 'ln' && keepGoing == 'Y') {
		//split length from field name
		var ln_array = valField.split("/",2);
		var field_len = ln_array[0];
		var thisField = document.getElementById(ln_array[1]);

		var inString = thisField.value;
		var lenString = inString.length;
		var Ret = isBlank(inString);
		if (Ret || (lenString < ln_array[0])) {
			thisField.style.backgroundColor = "#ffcccc";
			thisField.focus();
			var FMsg = "Please enter a value at least " + ln_array[0] + " characters long, for the indicated field.\n";
			alert (FMsg);
			keepGoing = 'N';
		}
		else {
			thisField.style.backgroundColor = "";
			keepGoing = 'Y';
		}
	}

//equal (ex: eq_password/verifyPassword)
	if (valType == 'eq' && keepGoing == 'Y') {
		//split field names
		var field_array = valField.split("/",2);
		var field1id = document.getElementById(field_array[0]);
		var field2id = document.getElementById(field_array[1]);

		if (field1id.value != field2id.value) {
			field1id.style.backgroundColor = "#ffcccc";
			field2id.style.backgroundColor = "#ffcccc";

			field1id.focus();
			var FMsg = field_array[0] + " and " + field_array[1] + " must match.\n";
			alert (FMsg);
			keepGoing = 'N';
		}
		else {
			field1id.style.backgroundColor = "";
			field2id.style.backgroundColor = "";

			keepGoing = 'Y';
		}
	}

//either (ex: either_sel_FieldName/nb_FieldName)
	if (valType == 'either' && keepGoing == 'Y') {
		//split field names
		var field_array = valField.split("/",2);
		
		//field1
		var splitpos = field_array[0].indexOf("_");
		var field1valType = field_array[0].substring(0,splitpos);
		var field1name = field_array[0].substring(splitpos+1,field_array[0].length);
		var field1id = document.getElementById(field1name);

		alert ("field1valType: " + field1valType + ", field1name: " + field1name + ", field1id: " + field1id);

		//field2
		var splitpos = field_array[1].indexOf("_");
		var field2valType = field_array[1].substring(0,splitpos);
		var field2name = field_array[1].substring(splitpos+1,field_array[1].length);
		var field2id = document.getElementById(field2name);

		alert ("field2valType: " + field2valType + ", field2name: " + field2name + ", field2id: " + field2id);
		
		
		switch (field1valType) {
			case "sel":
				if (field1id.value == 'not_selected') {
					field1id.style.backgroundColor = "#ffcccc";
					field1id.focus();
					var FMsg = "Please select a value for the indicated field.\n";
					alert(FMsg);
					keepGoing = 'N';
				}
			break;
			
			case "nb":
				var inString = field1id.value;
				var Ret = isBlank(inString);
				if (Ret) {
					field1id.style.backgroundColor = "#ffcccc";
					field1id.focus();
					var FMsg = "Please enter a value for the indicated field.\n";
					alert (FMsg);
					keepGoing = 'N';
				}
			break;
		}

		if (keepGoing == "Y") {
			switch (field2valType) {
				case "sel":
					if (field2id.value == 'not_selected') {
						field2id.style.backgroundColor = "#ffcccc";
						field2id.focus();
						var FMsg = "Please select a value for the indicated field.\n";
						alert(FMsg);
						keepGoing = 'N';
					}
				break;
				
				case "nb":
					var inString = field2id.value;
					var Ret = isBlank(inString);
					if (Ret) {
						field2id.style.backgroundColor = "#ffcccc";
						field2id.focus();
						var FMsg = "Please enter a value for the indicated field.\n";
						alert (FMsg);
						keepGoing = 'N';
					}
				break;
			}
		}



	}



		
	} // End of for (var Count = 0; Count < farray.length; Count++)
	if (keepGoing == 'Y') {
		return (true);
	}
	else {
		return (false);
	}
} // End of function checkForm (flist)

// isBlank 
function isBlank (InString) {
	if (InString==null) return (!false)
	if (InString.length!=0)
		return (!true);
	else
		return (!false);
} // End of function isBlank (InString)

function chkFieldRE (inString,matchRE) {
	if (inString.length == 0)  {
		var Ret = 'blank';
		return (Ret);
	}
	else {
		if (inString.match(matchRE)) {
			var Ret = 'good';
			return (Ret);
		}
		else {
			var Ret = 'nomatch';
			return (Ret);
		}
	}
} // End of function chkFieldRE

function handle_blank (thisField,validEntry) {
	thisField.style.backgroundColor = "#ffcccc";
	thisField.focus();
	var FMsg = "Please enter a value for the indicated field.\n\n Valid entries are:\n" + validEntry;
	alert(FMsg);
	keepGoing = 'N';
}

function handle_bad (thisField,validEntry) {
	thisField.style.backgroundColor = "#ffcccc";
	thisField.focus();
	var FMsg = "Please enter a valid value for the indicated field.\n\n Valid entries are:\n" + validEntry;
	alert(FMsg);
	keepGoing = 'N';
}

function set_text_value(fieldTextId,fieldTextValue) {
	var fieldTextObj = eval("document.getElementById(fieldTextId)");		
	fieldTextObj.value = fieldTextValue;
}

function set_from_select_value(OrgTextID,BuildingTextID,BuildingHiddenID,OrgName,BuildingName,BuildingID) {
	var OrgTextObj = eval("document.getElementById(OrgTextID)");		
	var BuildingTextObj = eval("document.getElementById(BuildingTextID)");		
	var BuildingHiddenObj = eval("document.getElementById(BuildingHiddenID)");		

	OrgTextObj.value = OrgName;
	if (BuildingName != "none") {
		BuildingTextObj.style.visibility = "visible";
		BuildingTextObj.value = BuildingName;
		BuildingHiddenObj.value = BuildingID;
	}

	
	var resultsID = document.getElementById('owning_org_lookup_results');
	resultsID.style.visibility = "hidden";
}


function copy_address_fields() {
	var physAddress1Obj = document.getElementById('phys_address1');
	var mailAddress1Obj = document.getElementById('mail_address1');
	mailAddress1Obj.value = physAddress1Obj.value;

	var physAddress2Obj = document.getElementById('phys_address2');
	var mailAddress2Obj = document.getElementById('mail_address2');
	mailAddress2Obj.value = physAddress2Obj.value;

	var physCityObj = document.getElementById('phys_city');
	var mailCityObj = document.getElementById('mail_city');
	mailCityObj.value = physCityObj.value;

	var physStateObj = document.getElementById('phys_state');
	var mailStateObj = document.getElementById('mail_state');
	mailStateObj.value = physStateObj.value;

	var physZipObj = document.getElementById('phys_zip');
	var mailZipObj = document.getElementById('mail_zip');
	mailZipObj.value = physZipObj.value;
}

function disable_enter(e) {
	if(window.event && e.keyCode == 13) // IE
	{	
		var enter_pressed = true;
	}
	else if(e.which && e.which == 13) // Netscape/Firefox/Opera
	{ 
		var enter_pressed = true;
	}

	if (enter_pressed) {
		//alert ("Enter key pressed"); 
		return false;
	}
	else {
		return true;
	}
}

//confirm_action - produces javascript alert with passed text
function confirm_action (confirmtext) {
	var answer = confirm(confirmtext);
	if (answer) {
		return true;
	}
	else {
		return false;
	}
}

//confirm_action_checkbox - produces javascript alert with passed text for checkbox selection
//checks to see if checkbox is being checked or unchecked
//confirmtext and alert box only displayed if checking the box
function confirm_action_checkbox (checkBoxID,confirmtext) {
	var checkBoxObj = document.getElementById(checkBoxID);
	if (checkBoxObj.checked) {
		var answer = confirm(confirmtext);

		if (answer) {
			return true;
		}
		else {
			return false;
		}
	}
}


function chk_slip_date (thisField) {
	var inString = thisField.value;
	if (inString != '') {
		var date_regexp = new RegExp(/^([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4})$/i);
		var validEntry = "Use Format: mm/dd/yyyy";
		if (inString.match(date_regexp)) {
			thisField.style.backgroundColor = "";
//			alert('Good Date');
			return (true);
		}
		else {
 			thisField.style.backgroundColor = "#ffcccc";
//			thisField.value = validEntry;
			var FMsg = "Please enter a valid date.\n\n Valid entries are:\n" + validEntry;
			alert(FMsg + validEntry);
			thisField.focus();
			return (false);
		}
	}
	else {
		return (true);
	}
}

//Function to set radio button to checked
function select_report(field_id) {
	var thisField = document.getElementById(field_id);
	thisField.checked = true;

}


// Function to open a new window with a given url
function open_win(url) {
	var popWin =                                             window.open(url,"QF","scrollbars=Yes,menubar=Yes,location=yes,status=Yes,height=500,width=700,resizable=yes");
}


