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

var wleft = 0;
var wtop = 0;
var popwidth = 400;
var popheight = 200;

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] = '';
errorArray[16] = 'Please use only letters, numbers, spaces, hyphens, underscores, exclamation points, or apostrophes.';

$(document).ready(function(){
//    $('#fadingbanners').after('<div id="fbpager">').cycle({
    $('#fadingbanners').cycle({
        fx:     'fade',
		random: '1',
        speed:  'fast',
        speedIn:  2000,
        speedOut:  2000,
        timeout: 6000
//        ,pager:  '#fbpager'
    });
});
// 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) {
		eduwin.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 + '#edu_anchor';
		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 to open email a friend window
// function emailafriend(pageid,page,title) {
function emailafriend(mmpageid,pageid) {
    popwidth = 500;
    popheight = 280;
    findcenter();
//	newurl = "emailafriend.php?page=" + page + "&title=" + title + "&mm=" + pageid;
    newurl = "emailafriend.php?page=" + pageid + "&mm=" + mmpageid;
    windowprop = "toolbar=no,statusbar=no,location=no,directories=no,scrollbars=no,resizable=yes,width=" + popwidth + ",height=" + popheight + ",top=" + wtop + ",left=" + wleft;
	WinId = window.open(newurl,target="previewWin", windowprop);
    // Just in case width and height are ignored
    //WinId.resizeTo(popwidth, popheight);
    // Just in case left and top are ignored
    //WinId.moveTo(wleft, wtop);
    WinId.focus();
}

function findcenter(){
    wleft = (screen.width - popwidth) / 2;
    wtop = (screen.height - popheight) / 2;
    if (wleft < 0) {
        popwidth = screen.width;
        wleft = 0;
    }
    if (wtop < 0) {
        popheight = screen.height;
        wtop = 0;
    }

}

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]/; // 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 simplePreload()
{
  var args = simplePreload.arguments;
  document.imageArray = new Array(args.length);
  for(var i=0; i<args.length; i++)
  {
    document.imageArray[i] = new Image;
    document.imageArray[i].src = args[i];
  }
}


