/* 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")

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)

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
*/
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);
		var thisField = document.getElementById(valField);

// 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')


		
	} // 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 createSelectFromSelect (source_select,target_select,listValues) {
	//create array from listValues
	var list_values_array = new Array;
	list_values_array = listValues.split(";");
	
	//get element id for source_select element
	var SourceSelObj = document.getElementById(source_select);
			
	//get element id for target_select element
	var TargetSelObj = document.getElementById(target_select);

	//clear out previous options in select list
	TargetSelObj.length = 0;

	//populate TargetSelObj with values
	for (i=0; i<list_values_array.length; ++i) {
		var splitValues = list_values_array[i].split("|");
		var optionText = splitValues[0];
		var optionValue = splitValues[1];

		TargetSelObj.options[i] = new Option(optionText,optionValue);
	}
}


function estimator_calc_total () {
	//Fees
	var setup_fee = parseFloat(200.00);
	var minimum_fee = parseFloat(500.00);
	

	//get transactions
	var num_trans = parseInt(document.getElementById('transactions').value);


	if (num_trans < 200000) { //.03/record
		var transaction_cost = .03;
	} else if (num_trans < 500000) { // .02/record
		var transaction_cost = .02;
	} else {
		var transaction_cost = .01;
	}


	var transaction_fee = transaction_cost * num_trans;	
	var total_fee = setup_fee + transaction_fee;	

	
	total_fee = Math.max(500.00,total_fee);
	total_fee = total_fee.toFixed(2);

	var transaction_fee_field = document.getElementById('transaction_fee');	
	transaction_fee = transaction_fee.toFixed(2);
	transaction_fee_field.innerHTML = transaction_fee;

	var total_fee_field = document.getElementById('total_cost_div');
	total_fee_field.innerHTML = "$" + total_fee;
	
	var total_cost_field = parseFloat(document.getElementById('total_cost'));
}	