// 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] = '';


function targetModal(passid,wdth,hght) {
    modwidth = 450;
    modheight = 450;
    if(wdth){
        modwidth = wdth;
    }
    if(hght){
        modheight = hght;
    }
     m = new Control.Modal($(passid),{
         iframe: true,
         opacity: 0.6,
         position: 'absolute',
         width: modwidth,
         height: modheight,
         overlayCloseOnClick: false
         });
     m.open();
}
function targetModalArt(passid,wdth,hght) {
    modwidth = 450;
    modheight = 450;
    if(wdth){
        modwidth = wdth;
    }
    if(hght){
        modheight = hght;
    }
     m = new Control.Modal($(passid),{
         iframe: true,
         opacity: 0.6,
         position: 'absolute',
         width: modwidth,
         height: modheight,
         overlayCloseOnClick: true
         });
     m.open();
}

// 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_art(url,cat) {
	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;
		}

		newUrl = url + "&aid=" + document.getElementById("fin_article").options[document.getElementById("fin_article").selectedIndex].value + "&ac=" + cat;
        document.articles.action = newUrl;
		document.articles.submit();
	}
}

function getaid(reqtype,cat){
    selectObj = document.getElementById('fin_article');
    aid = selectObj.options[selectObj.selectedIndex].value;
    newurl = '/page.php?page=pop&reqtype=' + reqtype + '&ac=' + cat + '&aid=' + aid;
    window.location = newurl;
}

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('aid',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 + "&aid=" + 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");

    //document.location = newurl;
}

// 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 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,prefix) {
//    alert(location.hostname);
    if (typeof prefix == 'undefined' ) {
  //      url = 'http://nefcu.dev3.l9.com/speedbump.php?nw=' + newwin + '&url=' + url;
        url = 'http://' + location.hostname + '/speedbump.php?nw=' + newwin + '&url=' + url;
    } else {
        url = url;
    }
     m = new Control.Modal('',{
         iframe: true,
         opacity: 0.6,
         gotohref: url,
         position: 'absolute',
         width: 450,
         height: 450,
         overlayCloseOnClick: true
         });
     m.open();
}

function JSSpeedBump(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();
	}
}

// function to open email a friend window
function emailafriend(page,title) {
    popwidth = 220;
    popheight = 300;
    findcenter();
	newurl = "emailafriend.php?page=" + encodeURIComponent(page) + "&title=" + encodeURIComponent(title);
    windowprop = "toolbar=no,statusbar=no,location=no,directories=no,scrollbars=no,resizable=yes,width=" + popwidth + ",height=" + popheight;
	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 OpenPodCastWin(URL,winwidth,winheight){
        if(!winwidth){
           winwidth = 330;
        }
        if(!winheight){
           winheight = 320;
        }
        newparams = 'location=no,toolbar=no,status=no,scrollbars=no,resizable=yes,menubar=no,width=' + winwidth + ',height=' + winheight;
        newwinname = 'PodWin' + winwidth;
        aWindow=window.open(URL,newwinname,newparams);
}







    function printit(id){
        url = "/kiosk/printable_rates.php?id=" + id + "&prt=1";
        WinId = window.location = url;
}




function confirmclose(){
    if(parent.Control){
        parent.Control.Modal.close();
    } else {
        self.close();
    }
}
function openWin(URL,winwidth,winheight){
        if(!winwidth){
           winwidth = 600;
        }
        if(!winheight){
           winheight = 780;
        }
        newparams = 'location=yes,toolbar=yes,status=yes,scrollbars=yes,resizable=yes,menubar=no,width=' + winwidth + ',height=' + winheight;
        newwinname = 'OpenWin' + winwidth;
        aWindow=window.open(URL,newwinname,newparams);
}

function valSurvey(){
//  document.getElementById('SurveyForm').submit();
//  return;
    encoded_params = readynamevalue(document.getElementById("SurveyForm"));
    var oXHR = zXmlHttp.createRequest();

    oXHR.open("post","/php/procsurvey.php", false);
    oXHR.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    oXHR.send(encoded_params);


    if(oXHR.status == 200){
        response=oXHR.responseText;
        response_array = response.split('~');
         if(response_array[0] == '0'){
              document.getElementById("SurveyFormDiv").innerHTML = response_array[1];
          } else {
              document.getElementById("surveyerrors").innerHTML = response_array[1];
              document.getElementById("fkimage").src = '/scripts/formkey.php?key=' + response_array[2];
              document.getElementById("formkey_sys").value = response_array[2];
              document.getElementById("formkey_user").value = '';
          }
    } else {
        alert("We're Sorry, The Survey system is currently unavailable for sign-up.");
    }

    resetoneclick();
}

function chkpc(){
    if(document.getElementById("pcagree1") && document.getElementById("pcagree1").checked){
        document.getElementById("pc_form").action = document.getElementById("pcagree_url").value;
        document.getElementById("pc_form").submit();
    } else if(document.getElementById("pcagree0") && document.getElementById("pcagree0").checked){
        document.getElementById("pc_form").action = document.getElementById("pcdecline_url").value;
        document.getElementById("pc_form").submit();
    } else {
        alert("Please Accept or Decline the Terms of Use.");
    }
}