/* TCA-Specific Functions ... */
/*
	goto_issue		handles redirection when someone selects an issue from the 
					drop-down in the main menu.
*/

/* goto_issue handles the redirection when someone selects an issue from the
drop-down in the main menu. */
function goto_issue(baseurl) {
	var vol_issue = document.selectIssue.issue.value;
	
	var vol_iss_arr = vol_issue.split(",");
	
	var vol = vol_iss_arr[0];
	vol.toString();
	var iss = vol_iss_arr[1];
	iss.toString();
	var iss_year = vol_iss_arr[2];

/*	
	var vol = vol_issue.substring(0,1);
	vol.toString();
	var iss = vol_issue.substring(2,3);
	iss.toString();
	var iss_year = vol_issue.substr(4,4);
*/	
	var vol_tlen = 8;
	var iss_tlen = 8;
	var vol_pad = vol_tlen - vol.length;
	var iss_pad = iss_tlen - iss.length;
	var vol_string = '';
	var iss_string = '';
	for (i = 1; i <= vol_pad; i++) {
		vol_string += '0';
	}
	vol_string += vol;
	
	for (i = 1; i <= iss_pad; i++) {
		iss_string += '0';
	}
	iss_string += iss;
	
	
	var targeturl = baseurl + iss_year + "/" + vol_string + "/" + iss_string;
//	alert("Going to: " + targeturl);
	window.location = targeturl;
}	

/* Generic Functions... */
/*
	make_addr		Creates a mailto: link when passed a name and domain
	bld_mail_link	Creates a mailto: link to name@quipugroup.com
	checkForm		Validates form data
	isBlank			Checks to see if passed string is blank
	isUSPN			Checks to see if passed string is a US Phone number
	isEmail			Checks to see if passed string is a valid email address.
*/



/* 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.

*/
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 (yyyy-mm-dd, mm/dd/yyyy, mm-dd-yyyy)
	-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;
			var date_regexp = new RegExp(/^([0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4})$|^([0-9]{1,2}\-[0-9]{1,2}-\/[0-9]{4})$|^([0-9]{4}\-[0-9]{1,2}\-[0-9]{1,2})$/i);
			var validEntry = "\tmm/dd/yyyy\n\tmm-dd-yyyy\n\tyyyy-mm-dd";
			var fieldStatus = chkFieldRE(inString,date_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 = 'match';
			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';
}
