// error array
var WrkInput = '';
var WrkMonth = '';
var WrkDay = '';
var WrkYear = '';
var num_of_submits = 0;

var err_num = '';
var errorArray = new Array(15);
errorArray[0] = 'no errors detected.';
errorArray[1] = 'does not appear to be valid.';
errorArray[2] = 'does not appear to be a valid date.';
errorArray[3] = 'does not appear to be a valid email address.';
errorArray[4] = 'contains illegal characters.';
errorArray[5] = 'contains illegal characters. Only letters, numbers, hyphens and underscores are allowed.';
errorArray[6] = 'contains illegal characters. Only letters, numbers, spaces, hyphens, underscores and apostrophes are allowed.';
errorArray[7] = '';
errorArray[8] = '';
errorArray[9] = '';
errorArray[10] = 'must not be less than 6 or more than 12 characters.';
errorArray[11] = 'must contain at least one lower case letter, one upper case letter and one number';
errorArray[12] = 'must be numeric.';
errorArray[13] = 'is required';
errorArray[14] = '';
errorArray[15] = '';


// education topic display
function edu_topic(catid,id) {
	newurl = "/edu_discussion_topics.php?cat=" + catid + "&id=" + id;
	eduwin = window.open(newurl,target="EDUWind", "toolbar=no,location=no,directories=no,scrollbars=yes,resizable=yes,width=780,height=400");
	if (window.focus) {
		EDUWind.focus();
	}
}

function newaction(url,fc) {
	if(document.getElementById("fin_lib")){
		if(document.getElementById("fin_lib").selectedIndex < 1 || document.getElementById("fin_lib").options[document.getElementById("fin_lib").selectedIndex].value == ''){
			alert("Please select another Discussion Topic.");
			return;
		}
		
		newUrl = url + "&fc=" + fc + "&fid=" + document.getElementById("fin_lib").options[document.getElementById("fin_lib").selectedIndex].value;
		document.eduform.action = newUrl;
		document.eduform.submit();
	}
}

function fin_edu(url) {

	indiv_art='no';
	urlquery=url.split("?");
	if(urlquery[1]){
		urlterms=urlquery[1].split("&");
			
		if(urlterms.length){
			for(i=0;i < urlterms.length;i++){
				valuepairs = urlterms[i];
				if(valuepairs.indexOf('id',0) != -1){
					indiv_art='yes';
				}
			}
		}
	}

   newurl = url;
	
	if(indiv_art=='no'){
		if(document.getElementById("fin_article")){
			if(document.getElementById("fin_article").selectedIndex < 1 || document.getElementById("fin_article").options[document.getElementById("fin_article").selectedIndex].value == ''){
				alert("Please select another Article.");
				return;
			} else {
				newurl = url + "&id=" + document.getElementById("fin_article").options[document.getElementById("fin_article").selectedIndex].value;
			}
		}
	}
	eduwin = window.open(newurl,target="artwin", "toolbar=no,location=no,directories=no,scrollbars=yes,resizable=yes,width=780,height=400");
}

// function to open the calculator window
function calcWin(calc) {
	calcurl = "/calculators/" + calc;
	WinId = window.open(calc,target="calcWind", "toolbar=no,location=no,directories=no,scrollbars=yes,resizable=yes,width=780,height=400");
}

// function to validate email a friend form
function valemailform(email_form) {
	calcurl = "/calculators/" + calc;
	WinId = window.open(calc,target="calcWind", "toolbar=no,location=no,directories=no,scrollbars=yes,resizable=yes,width=780,height=400");
}

function alertError(disp_text){
	if(isNumeric(err_num) ){
		if(err_num == 0 || err_num > errorArray.length){
		return;
		}
	}
	alert(disp_text + " " + errorArray[err_num]);
	return;
}

// sanitize input
function isPassword(string){
	err_num = 0;
	var illegalChars = /[^\.\!\*a-zA-Z0-9_-]/; // allow only letters, numbers, underscores,periods and hyphens 
	if(illegalChars.test(string)){ // illegal characters found
		err_num = 4;
		return false;
	}
	if ((string.length < 6) || (string.length > 12)) {
		err_num = 10;
      return false;
   }
  return true;
}

function isEmail(string) {
	err_num = 0;
	if(string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,6}$/)!= -1){
		return true; 
	} else {  
		err_num = 3;
		return false;
	}
}

function isZip(string){
	var regex = /^\d{5}([\-]\d{4})?$/; // 5 digits followed by the optional - and 4 digits
	if(regex.test(string)){
		return true;
	} else {
		err_num = 1;
		return false;
	}
}
function isPhone(string){
	var regex = /^(?:\+\d)*\s*(?:\(*\d{3}\)*)*\s*\d{3}\-\d{4}$/; // in the format of (999) 999-9999 with (999) being optional
	if(regex.test(string)){
		return true;
	} else {
		err_num = 1;
		return false;
	}
}
function isillegalChars(string){
	var illegalChars = /[^\.\-&,\(\)_ ''a-zA-Z0-9[\s]]/; // allow only letters, numbers, spaces, underscores, hyphens,periods and apostrophe
	if(illegalChars.test(string)){
		err_num = 4;
		return true; // illegal characters found
	} else {
		return false; // no illegal characters found
	}
}

function isDate(PassField){
	err_num = 0;
	var re = /\//gi;
	var re1 = /-/gi;
	
	WrkInput = PassField.replace(re, "-");
	tempInput = WrkInput.replace(re1,"");
	if( !isNumeric(tempInput) ){
		err_num = 2;
		return false;
	}
	
	WrkLength = PassField.length;

	if (WrkLength < 8 || WrkLength > 10) {
		err_num = 2;
		return false;
	}
	
	string_array = WrkInput.split("-");
	if(string_array.length != 3){
		err_num = 2;
		return false;
	}
	
	WrkMonth = string_array[0];
	WrkDay = string_array[1];
	WrkYear = string_array[2];
	
	// valid day check to include leap year validation
	if (WrkMonth == 2){
		if ((WrkYear % 4) != 0) {
			daylimit = 28;
		} else if((WrkYear % 400) == 0) {
			daylimit = 29;
		} else if((WrkYear % 100) == 0) {
			daylimit = 28;
		} else {
			daylimit = 29;
		}
	} else {
		if (WrkMonth == 4 || WrkMonth == 6 || WrkMonth == 9 || WrkMonth ==11){
			daylimit = 30;
		} else {
			daylimit = 31;
		}
	}
		
	if (WrkMonth < 1 || WrkMonth > 12) {
		err_num = 2;
		return false;
	}
	
	if (WrkDay < 1 || WrkDay > daylimit) {
		err_num = 2;
		return false;
	}
	
	if(WrkYear.length != 4){
		err_num = 2;
		return false;
	}
	
	if (WrkYear < 1) {
		err_num = 2;
		return false;
	}
	return true;
}

function isNumeric(PassField) {
	var legalNum = /\D/; // allow only numbers
	if (PassField.length == 0){
		return false;
	}
	if(legalNum.test(PassField)) {
		return false; // non numeric character found
	} else {
		return true;
	}	
}

function isAllSpaces(inputValue) {
  if(inputValue.search(/^\s*$/) != -1) {
    // the string contains just spaces or is empty
    return true;
  } else {
    // valid input
    return false;
  }
}


function goback() {
    history.go(-1);
}

function SpeedBump(url,newwin) {

    br = String.fromCharCode(10);
    if (newwin == 1) {
        winmsg = "(The new website will open in a new window.)";
    } else {
        winmsg = "";
    }

    // Choose one and delete the unwanted version of the message:

    // long version:
    dmsg =  "The website you are about to visit is solely the responsibility of the merchant " +
            "or other party providing the site. The content of this third-party site, including " +
            "materials and information, is solely the responsibility of the provider of the site. " +
            "We are not responsible for any such third-party content. " +
            "Any transactions that you enter into with a vendor, merchant or other party that you " +
            "access through this third-party site are solely between you and that vendor, " +
            "merchant or other party. We do not endorse the content contained in this third-party site, " +
            "nor the organization publishing the site, and hereby disclaims any responsibility for such content. " +
            "Our Privacy Policy does not apply to this third-party site, and for further information you should " +
            "consult the privacy disclosures of the third-party site. " +
            br + br +
            "You are now leaving the site.  Click \"OK\" to leave, \"Cancel\" to stay." + br +
            winmsg + br;

    // short version:
    dmsg =  "The link you have just clicked is taking you to a different website. " + br +
            "The content of this third-party site, including " +
            "materials and information, is solely the responsibility of the provider of the site. " +
            br +
            "Our Privacy Policy does not apply to this third-party site, and for further information you should " +
            "consult the privacy disclosures of the third-party site. " +
            br + br +
            "You are now leaving the site.  Click \"OK\" to leave, \"Cancel\" to stay." + br +
            winmsg + br;



		response = confirm(dmsg);
		if(response){
            if (newwin == 0) {
                document.location = url;
            } else {
                this.open(url,"NewExternalWin","toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes");
            }
		}
}

// functions to limit submitting of the form to one time
function setoneclick(func2invoke){
	num_of_submits = num_of_submits + 1;
	if(num_of_submits > 1){
		return false;
	}
	eval(func2invoke);
}

function resetoneclick(){
	num_of_submits = 0;
	return false;
}
function prtpage (){
    if(!window.print){
		alert("To print this page, please close this window and hit either CTRL + P or Command + P");
		return;
	} else {
		window.print();
	}
}
