/*
 * PROJECT:	Intercom
 * AUTHOR:	Edward West
 * FUNCTION: Contains javascript functions that are used throughout the casls-site section.  This file
 * is then included in the inc-header-site tpl, so that they can be invoked throughout the site.  Not all
 * javascript functions are included here, as some javscript needs to be dynamically generated according
 * to the state of a PHP script.  Thus, this file contains only all of the static javascript functions.
 */
 
// Date validation ////////////////////////////////////////////////////////////////////////////////////
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){	
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

// inc-header-site ////////////////////////////////////////////////////////////////////////////////////
strVersion = navigator.appVersion;
if (navigator.appName == "Netscape"){
	if (parseInt(strVersion.substr(0,1)) < 5){
		window.location = "badbrowser.html";
	}
}else{
	if (navigator.appName == "Microsoft Internet Explorer"){
		if (parseInt(strVersion.substr(0,1)) < 4){
			window.location = "badbrowser.html";
		}
	}
}

// search-article-site.php ////////////////////////////////////////////////////////////////////////////////////
function searchCheck(form)	{

		var sTitle = form.txt_ArticleTitle.value;
		var sAuthor = form.txt_Author.value;
		var sInput1 = form.txt_InputDate1.value;
		var sInput2 = form.txt_InputDate2.value;
		var sLang = form.txtLanguage.options.length;
		var sLevel = form.txtLevel.options.length;
		var sContent = form.txtContentArea.options.length;
		var sState = form.txtState.options.length;
		var strError = "";

		if(sTitle=="" && sAuthor=="" && sInput1=="" && sInput2=="" && sLang<1 && sContent<1 && sLevel<1 && sState<1)
		{
			strError += "You must search by at least 1 field.\n";
		}

		//validate ANY in Language
	if (form.txtLanguage.options.length > 1)	{
		for (i=0; i<2; i++)	{
			form.txtLanguage.options[i].selected = true;
			if (form.txtLanguage.options[document.ArticleForm.txtLanguage.selectedIndex].text == 'ANY'){			//oSelect.options[oSelect.selectedIndex][b].text
				strError += "Please remove 'ANY' from your Language choices or remove every other tag besides 'ANY'.\n";
			}
			form.txtLanguage.options[i].selected = false;
		}
	}

	//validate ANY in Level
	if (form.txtLevel.options.length > 1)	{
		for (i=0; i<2; i++)	{
			form.txtLevel.options[i].selected = true;
			if (form.txtLevel.options[document.ArticleForm.txtLevel.selectedIndex].text == 'ANY'){			//oSelect.options[oSelect.selectedIndex][b].text
				strError += "Please remove 'ANY' from your Level choices or remove every other tag besides 'ANY'.\n";
			}
			form.txtLevel.options[i].selected = false;
		}
	}

	//validate ANY in ContentArea
	if (form.txtContentArea.options.length > 1)	{
		for (i=0; i<2; i++)	{
			form.txtContentArea.options[i].selected = true;
			if (form.txtContentArea.options[document.ArticleForm.txtContentArea.selectedIndex].text == 'ANY'){			//oSelect.options[oSelect.selectedIndex][b].text
				strError += "Please remove 'ANY' from your Content Area choices or remove every other tag besides 'ANY'.\n";
			}
			form.txtContentArea.options[i].selected = false;
		}
	}

	//validate ANY in State
	if (form.txtState.options.length > 1)	{
		for (i=0; i<2; i++)	{
			form.txtState.options[i].selected = true;
			if (form.txtState.options[form.txtState.selectedIndex].text == 'ANY'){			//oSelect.options[oSelect.selectedIndex][b].text
				strError += "Please remove 'ANY' from your State choices or remove every other tag besides 'ANY'.\n";
			}
			form.txtState.options[i].selected = false;
		}
	}

	if(strError !== "")	{
		alert(strError);
	}else if((sInput1 == "" || isDate(sInput1)) && (sInput2 == "" || isDate(sInput2))){
		fillAll();
		form.submit();
	}
}

// reactivate-sub.php ////////////////////////////////////////////////////////////////////////////////////
<!-- comment out Javascript for older browsers -->
var bformSubmitted = 0;

function reactivateSubVF(form)	{

//declare variables to validate email field
		var amp="@";
		var dot=".";
		var strError = "";

	//holds the overall length of email string
		var sEmail=form.txt_Email.value;
		var intLstr = sEmail.length;

	//holds the numberic position of the @ char in email string
		var intLat=sEmail.indexOf(amp);

	//holds the numeric position of the "." char in email string
		var intLdot=sEmail.indexOf(dot);

	//alert(intLstr + '\n' + intLat + '\n' + intLdot);

	//if Email field is empty, then alert
		if(sEmail=="" || sEmail==null)	{
		strError += "Please enter an address in the Email field.\n";
		}

	//if indexOf method returns -1 this means that hasn't found @ sign in email string
		if (intLat==-1)	{
			strError += "Please place the '@' sign in the correct place in the Email field.\n";
		}

	//if @ doesn't appear at all, appears as the very first char in email string, or is the only char
	//that appears in the email sting, then show alert
		if (intLat==-1 || 	intLat==0 || intLat==intLstr)	{
		strError += "Invalid email address. Please change the position of the '@' sign.\n";
		}

	//same check as above, except for dot char
		if (intLdot==-1 || intLdot==0 || intLdot==intLstr)	{
		strError += "Please check that the '.' in the email address is in the right place.\n";
		}

	//after you've located the @ char once, search for it again in the letters after the first occurrence
	//if @ appears again, then email is invalid
		 if (sEmail.indexOf(amp,(intLat+1))!=-1)	{
		strError += "Invalid email address because address contains more than one '@' sign.\n";
		 }

	//if "." appears immediately before @ char or immediately following @ char, then isn't valid
		 if (sEmail.substring(intLat-1,intLat)==dot || sEmail.substring(intLat+1,intLat+2)==dot)	{
	    strError += "Invalid email address. Please change the position of the '.' in the email address.\n";
		 }

	//if "." doesn't appeat after the @ char then email is invalid
		 if (sEmail.indexOf(dot,(intLat+2))==-1)	{
		 strError += "Invalid email address because a '.' doesn't appear anywhere after the '@' sign.\n";
		 }

	//if spaces appear in the email address, then isn't valid
		if (sEmail.indexOf(" ")!=-1)	{
	    strError += "Invalid email address because spaces appear in the address.\n";
		}

		if(strError !== "")	{
			alert(strError);
		}
		else	{
			form.submit();
		}
}

// forgot-login.php ////////////////////////////////////////////////////////////////////////////////////
<!-- comment out Javascript for older browsers -->
var bformSubmitted = 0;

function forgotLoginVF(form)	{

//declare variables to validate email field
		var amp="@";
		var dot=".";
		var strError = "";

	//holds the overall length of email string
		var sEmail=form.txt_Email.value;
		var intLstr = sEmail.length;

	//holds the numberic position of the @ char in email string
		var intLat=sEmail.indexOf(amp);

	//holds the numeric position of the "." char in email string
		var intLdot=sEmail.indexOf(dot);

	//alert(intLstr + '\n' + intLat + '\n' + intLdot);

	//if Email field is empty, then alert
		if(sEmail=="" || sEmail==null)	{
		strError += "Please enter an address in the Email field.\n";
		}

	//if indexOf method returns -1 this means that hasn't found @ sign in email string
		if (intLat==-1)	{
			strError += "Please place the '@' sign in the correct place in the Email field.\n";
		}

	//if @ doesn't appear at all, appears as the very first char in email string, or is the only char
	//that appears in the email sting, then show alert
		if (intLat==-1 || 	intLat==0 || intLat==intLstr)	{
		strError += "Invalid email address. Please change the position of the '@' sign.\n";
		}

	//same check as above, except for dot char
		if (intLdot==-1 || intLdot==0 || intLdot==intLstr)	{
		strError += "Please check that the '.' in the email address is in the right place.\n";
		}

	//after you've located the @ char once, search for it again in the letters after the first occurrence
	//if @ appears again, then email is invalid
		 if (sEmail.indexOf(amp,(intLat+1))!=-1)	{
		strError += "Invalid email address because address contains more than one '@' sign.\n";
		 }

	//if "." appears immediately before @ char or immediately following @ char, then isn't valid
		 if (sEmail.substring(intLat-1,intLat)==dot || sEmail.substring(intLat+1,intLat+2)==dot)	{
	    strError += "Invalid email address. Please change the position of the '.' in the email address.\n";
		 }

	//if "." doesn't appeat after the @ char then email is invalid
		 if (sEmail.indexOf(dot,(intLat+2))==-1)	{
		 strError += "Invalid email address because a '.' doesn't appear anywhere after the '@' sign.\n";
		 }

	//if spaces appear in the email address, then isn't valid
		if (sEmail.indexOf(" ")!=-1)	{
	    strError += "Invalid email address because spaces appear in the address.\n";
		}

		if(strError !== "")	{
			alert(strError);
		}
		else	{
			form.submit();
		}
}

// subscribe_confirm.php ////////////////////////////////////////////////////////////////////////////////////
function subscribeConfirmVF()	{
	var strError ="";

	//--------------------------------email ----------------------
	//declare variables to validate email field
		var amp="@";
		var dot=".";

	//holds the overall length of email string
		var sEmail=document.form_SubConfirm.txt_Email.value;
		var intLstr = sEmail.length;

	//holds the numberic position of the @ char in email string
		var intLat=sEmail.indexOf(amp);

	//holds the numeric position of the "." char in email string
		var intLdot=sEmail.indexOf(dot);

	//alert(intLstr + '\n' + intLat + '\n' + intLdot);

	//if Email field is empty, then alert
		if(sEmail=="" || sEmail==null)	{
		strError += "Please enter an address in the Email field.\n";
		}

	//if indexOf method returns -1 this means that hasn't found @ sign in email string
		if (intLat==-1)	{
			strError += "Please place the '@' sign in the correct place in the Email field.\n";
		}

	//if @ doesn't appear at all, appears as the very first char in email string, or is the only char
	//that appears in the email sting, then show alert
		if (intLat==-1 || 	intLat==0 || intLat==intLstr)	{
		strError += "Invalid email address. Please change the position of the '@' sign.\n";
		}

	//same check as above, except for dot char
		if (intLdot==-1 || intLdot==0 || intLdot==intLstr)	{
		strError += "Please check that the '.' in the email address is in the right place.\n";
		}

	//after you've located the @ char once, search for it again in the letters after the first occurrence
	//if @ appears again, then email is invalid
		 if (sEmail.indexOf(amp,(intLat+1))!=-1)	{
		strError += "Invalid email address because address contains more than one '@' sign.\n";
		 }

	//if "." appears immediately before @ char or immediately following @ char, then isn't valid
		 if (sEmail.substring(intLat-1,intLat)==dot || sEmail.substring(intLat+1,intLat+2)==dot)	{
	    strError += "Invalid email address. Please change the position of the '.' in the email address.\n";
		 }

	//if "." doesn't appeat after the @ char then email is invalid
		 if (sEmail.indexOf(dot,(intLat+2))==-1)	{
		 strError += "Invalid email address because a '.' doesn't appear anywhere after the '@' sign.\n";
		 }

	//if spaces appear in the email address, then isn't valid
		 if (sEmail.indexOf(" ")!=-1)	{
	    strError += "Invalid email address because spaces appear in the address.\n";
		 }


		var sRandom=document.form_SubConfirm.txt_ConCode.value;
		if (sRandom=="" || sRandom==null)	{
		strError += "Please enter the Confirmation Code that we sent you.\n";
		}

		var sPassword=document.form_SubConfirm.txt_Password.value;
		if (sPassword=="" || sPassword==null)	{
		strError += "Please enter a password.\n";
		}

		var sConfirm=document.form_SubConfirm.txt_Confirm.value;
		if (sConfirm=="" || sConfirm==null)	{
		strError += "Please confirm the password you entered.\n";
		}

		if (document.form_SubConfirm.txt_Confirm.value != document.form_SubConfirm.txt_Password.value)	{
		strError += "The Password and Password Confirmation fields don't match."
		}

		if(strError !== "")	{
			alert(strError);
		}
		else	{
			document.form_SubConfirm.submit();
		}
}

// subscribe_login.php ////////////////////////////////////////////////////////////////////////////////////
function disableField(form_field) {
	form_field.blur;
	alert('You must download at least NS 6.0 to use this section of InterCom. Please use the link to go to the Netscape download site. Thanks!');
	document.form_SubLogin.txt_Email.focus();
}

function subscribeLoginVF(form)	{

	var strError ="";

	//--------------------------------email ----------------------
	//declare variables to validate email field
	var amp="@";
	var dot=".";

	//holds the overall length of email string
	var sEmail=form.txt_Email.value;
	var intLstr = sEmail.length;

	//holds the numberic position of the @ char in email string
	var intLat=sEmail.indexOf(amp);

	//holds the numeric position of the "." char in email string
	var intLdot=sEmail.indexOf(dot);

	//if Email field is empty, then alert
	if(sEmail=="" || sEmail==null)	{
		strError += "Please enter an address in the Email field.\n";
	}

	//if indexOf method returns -1 this means that hasn't found @ sign in email string
	if (intLat==-1)	{
		strError += "Please place the '@' sign in the correct place in the Email field.\n";
	}

	//if @ doesn't appear at all, appears as the very first char in email string, or is the only char
	//that appears in the email sting, then show alert
	if (intLat==-1 || 	intLat==0 || intLat==intLstr)	{
		strError += "Invalid email address. Please change the position of the '@' sign.\n";
	}

	//same check as above, except for dot char
	if (intLdot==-1 || intLdot==0 || intLdot==intLstr)	{
		strError += "Please check that the '.' in the email address is in the right place.\n";
	}

	//after you've located the @ char once, search for it again in the letters after the first occurrence
	//if @ appears again, then email is invalid
	if (sEmail.indexOf(amp,(intLat+1))!=-1)	{
		strError += "Invalid email address because address contains more than one '@' sign.\n";
	}

	//if "." appears immediately before @ char or immediately following @ char, then isn't valid
	if (sEmail.substring(intLat-1,intLat)==dot || sEmail.substring(intLat+1,intLat+2)==dot)	{
		strError += "Invalid email address. Please change the position of the '.' in the email address.\n";
	}

	//if "." doesn't appeat after the @ char then email is invalid
	if (sEmail.indexOf(dot,(intLat+2))==-1)	{
		strError += "Invalid email address because a '.' doesn't appear anywhere after the '@' sign.\n";
	}

	//if spaces appear in the email address, then isn't valid
	if (sEmail.indexOf(" ")!=-1)	{
		strError += "Invalid email address because spaces appear in the address.\n";
	}

	var sPassword=form.txt_Password.value;
	if (sPassword=="" || sPassword==null)	{
	strError += "Please enter your password.\n";
	}

	if(strError !== "")	{
		alert(strError);
	} else	{
		form.submit();
	}
}

// subscribe_update.php, update_email.php, update_password.php /////////////////////////////////////////////
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// update_email.php ///////////////////////////////////////////////////////////////////////////////////////
function updateEmailVF()
{
	//var chUnsub=form.optActive.checked;
	var strError ="";
	var nEmail=document.ArticleForm.txt_nEmail.value;
	var cEmail=document.ArticleForm.txt_newEmail.value;
	var amp="@";
	var dot=".";

	//holds the overall length of email string
	var sEmail=document.ArticleForm.txt_nEmail.value;
	var intLstr = sEmail.length;

	//holds the numberic position of the @ char in email string
	var intLat=sEmail.indexOf(amp);

	//holds the numeric position of the "." char in email string
	var intLdot=sEmail.indexOf(dot);

	//alert(intLstr + '\n' + intLat + '\n' + intLdot);


	if (nEmail == "" || cEmail == "")	{
		strError += "Please enter information in both fields.\n";
	}	else	{
		//if Email field is empty, then alert
		if(sEmail=="" || sEmail==null)	{
			strError += "Please enter an address in the Email field.\n";
		}

		//if indexOf method returns -1 this means that hasn't found @ sign in email string
		if (intLat==-1)	{
			strError += "Please place the '@' sign in the correct place in the Email field.\n";
		}

		//if @ doesn't appear at all, appears as the very first char in email string, or is the only char
		//that appears in the email sting, then show alert
		if (intLat==-1 || 	intLat==0 || intLat==intLstr)	{
			strError += "Invalid email address. Please change the position of the '@' sign.\n";
		}

		//same check as above, except for dot char
		if (intLdot==-1 || intLdot==0 || intLdot==intLstr)	{
			strError += "Please check that the '.' in the email address is in the right place.\n";
		}

		//after you've located the @ char once, search for it again in the letters after the first occurrence
		//if @ appears again, then email is invalid
		if (sEmail.indexOf(amp,(intLat+1))!=-1)	{
			strError += "Invalid email address because address contains more than one '@' sign.\n";
		}

		//if "." appears immediately before @ char or immediately following @ char, then isn't valid
		if (sEmail.substring(intLat-1,intLat)==dot || sEmail.substring(intLat+1,intLat+2)==dot)	{
			strError += "Invalid email address. Please change the position of the '.' in the email address.\n";
		}

		//if "." doesn't appeat after the @ char then email is invalid
		if (sEmail.indexOf(dot,(intLat+2))==-1)	{
			strError += "Invalid email address because a '.' doesn't appear anywhere after the '@' sign.\n";
		}

		//if spaces appear in the email address, then isn't valid
		if (sEmail.indexOf(" ")!=-1)	{
			strError += "Invalid email address because spaces appear in the address.\n";
		}
	}

	if (nEmail != "" && cEmail == "")	{
		strError += "Please confirm your new email address.\n"
	}

	if (nEmail != cEmail)	{
		strError += "The new email address field doesn't contain the same text as the confirm email field.\n";
	}

if(strError != "")
	{
		alert(strError);
	}
	else
	{
	document.ArticleForm.submit();
	}
}

// update_password.php ///////////////////////////////////////////////////////////////////////////////////////
function updatePasswordVF()
{
	var strError = "";
	var oldPass = document.ArticleForm.txt_oldPassword.value;
	var cPass = document.ArticleForm.txt_cPassword.value;
	var newPass = document.ArticleForm.txt_newPassword.value;

	if (newPass == "")	{
		strError += "Please enter your new password.\n";
	}

	/*if (oldPass != document.ArticleForm.txt_Password.value)	{
		strError += "Your current password isn't correct.\n";
	}*/

	if (newPass != "" && cPass == "")	{
			strError += "Please confirm your new Password.\n"
		}

		if (newPass != cPass)	{
			strError += "Your new Password doesn't match your Confirm password.\n";
		}

	if(strError != "")		{
			alert(strError);
		}
		else	{
			document.ArticleForm.submit();
	}
}