<!--

/* ********************************************************************
This script provides reusable miscellenious utility methods to handle 
environment-centric details such as parsing a queryString, a file header,
and time stamp. These methods are all tested to be HTML 4 complaint and 
work one MSIE 4/5, and Netscape 4.

01. getBrowser()
02. getBrowserVersion() 
03. getOS()
04. getQueryStrValue(sVarArg)
05. getTime()
06. getTimeStamp()
07. normalize(sArg)
08. visibleLayer(layerName,TrueOrFalse)
09. movefromto(fbox,tbox)
10. BumpUp(abox)
11. emailcheck(emailstr)
12. ValidateNewsLetterManagementForm()
13. checkemailfield(stremailaddress,objfocusobject)
14. checkandaddsubscriber(stremailaddress,fbox,tbox)
15. generatesubscriptioncclistxml();
16. validateunsubscribeform();


Don.Irwin@homestore.com 06/19/2002
******************************************************************** */

function getBrowser() {
//returns browser name (eg Netscape, Explorer, Opera)

	sRaw 	= navigator.userAgent.toLowerCase();
	sType 	= "unknown";

	if (sRaw.indexOf("mozilla") !=-1) {sType="Netscape"}
	if (sRaw.indexOf("msie") !=-1) {sType="Explorer"}
	if (sRaw.indexOf("opera") !=-1) {sType="Opera"}

	return(sType);
}

function getBrowserVersion() {
//returns version of software (eg 5,5.5,6)

	iRaw = parseFloat(navigator.appVersion);
	return(iRaw); 
}

function getOS() {
//Returns OS (eg Mac, win3.1, win95, win98, winNT
	sRaw 	= navigator.userAgent.toLowerCase();
	sType 	= "unknown";

	if (sRaw.indexOf("mac")!=-1)  {sType="Mac"}
	if (sRaw.indexOf("win")!=-1) {
		if ( (sRaw.indexOf("win16")!=-1) || (sRaw.indexOf("win31")!=-1) ) {sType = "Win3.1"}
		if (sRaw.indexOf("98")!=-1) {sType = "Win98"}
		if (sRaw.indexOf("95")!=-1) {sType = "Win95"}
		if (sRaw.indexOf("nt")!=-1) {sType = "WinNT"}
	}
	return(sType);
}


function getQueryStrValue(sVarArg) {
//Pass in a queryString keyName and recieve back it's value.

	qs = new String(location.search);
	qs=qs.substr(1,qs.length).split("&");
	qsKeys = new Array();
	qsVals = new Array();
	myValue = "";

	//Parse values into 2 arrays for search
	for (i=0;i<qs.length;i++){
	   qsElem=qs[i].split("=");
	   key=qsElem[0]; value=qsElem[1];
	   qsKeys=qsKeys.concat(key);
	   qsVals=qsVals.concat(value);
 	}

	//Search for key that matches request
	for (i=0;i<qsKeys.length;i++){
	   if (qsKeys[i].valueOf() == sVarArg) {
		myValue=qsVals[i].valueOf(); break;	   		
	   } 
	} 
	return(myValue);
}

function getTime() {
//Create a timeStamp for clientSide clock evaluation.
	
	dNow = new Date();
	dDate = dNow.getMonth()+1 +"/"+ dNow.getDate() +"/"+ dNow.getYear();
	dTime = dNow.getHours() +":"+ dNow.getMinutes() +":"+ dNow.getSeconds();
	dDateValue = dDate +" "+ dTime;
	return(dDateValue);
}

function getTimeStamp() {
//Creates a compact timeStamp to be used in Qstring when override of browser cache is needed.

	d = new Date();
	dStr = d.getHours()+""+d.getMinutes()+""+d.getSeconds();
	return (dStr);
}

function normalize(sArg){
//Remove spaces from a string.
	myString = new String(sArg);
	rExp = / /g;
	myOutput = myString.replace(rExp,"");
	return(myOutput);
}

function visibleLayer(layerName,TrueOrFalse){
//Adjust layer visibility, provided layer name and boolean state.
	
	//Which state to show.		
	if (TrueOrFalse) {sVis = "visible"}
	else{sVis = "hidden"}
	
	//Set visibility of layer.
	if (getBrowser()=="Explorer") {
		eval("document.all." + layerName + ".style.visibility='" + sVis +"'");
	}else if (getBrowser()=="Netscape" && getBrowserVersion()<5) {
		eval("document." + layerName + ".visibility='" + sVis + "'");
	}else if (getBrowser()=="Netscape" && getBrowserVersion()>=5) {		
		document.getElementById(layerName).style.visibility=sVis;
	}
}

function movefromto(fbox,tbox) {
var i = 0;
if(fbox.value != "") {
var no = new Option();
no.value = fbox.value;
no.text = fbox.value;
tbox.options[tbox.options.length] = no;
fbox.value = "";
   }
}

function deletefromlist(box) 
{
for(var i=0; i<box.options.length; i++) {
if(box.options[i].selected && box.options[i] != "") {
box.options[i].value = "";
box.options[i].text = "";
   }
}
BumpUp(box);
} 


function BumpUp(abox) 
{
	for(var i = 0; i < abox.options.length; i++) 
		{
			if(abox.options[i].value == "")  
			{
				for(var j = i; j < abox.options.length - 1; j++)  
				{
					abox.options[j].value = abox.options[j + 1].value;
					abox.options[j].text = abox.options[j + 1].text;
				}

				var ln = i;
				break;
			}
		}

	if(ln < abox.options.length)  
	{
		abox.options.length -= 1;
		BumpUp(abox);
	}

}


function emailcheck (emailStr) 
{
		var strerrmsg = "";
		/* The following pattern is used to check if the entered e-mail address
		   fits the user@domain format.  It also is used to separate the username
		   from the domain. */
		var emailPat=/^(.+)@(.+)$/
		/* The following string represents the pattern for matching all special
		   characters.  We don't want to allow special characters in the address. 
		   These characters include ( ) < > @ , ; : \ " . [ ]    */
		var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
		/* The following string represents the range of characters allowed in a 
		   username or domainname.  It really states which chars aren't allowed. */
		var validChars="\[^\\s" + specialChars + "\]"
		/* The following pattern applies if the "user" is a quoted string (in
		   which case, there are no rules about which characters are allowed
		   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
		   is a legal e-mail address. */
		var quotedUser="(\"[^\"]*\")"
		/* The following pattern applies for domains that are IP addresses,
		   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
		   e-mail address. NOTE: The square brackets are required. */
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
		/* The following string represents an atom (basically a series of
		   non-special characters.) */
		var atom=validChars + '+'
		/* The following string represents one word in the typical username.
		   For example, in john.doe@somewhere.com, john and doe are words.
		   Basically, a word is either an atom or quoted string. */
		var word="(" + atom + "|" + quotedUser + ")"
		// The following pattern describes the structure of the user
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
		/* The following pattern describes the structure of a normal symbolic
		   domain, as opposed to ipDomainPat, shown above. */
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
		
		/* Finally, let's start trying to figure out if the supplied address is
		   valid. */

		/* Begin with the coarse pattern to simply break up user@domain into
		   different pieces that are easy to analyze. */
		var matchArray=emailStr.match(emailPat)
		if (matchArray==null) {
		  /* Too many/few @'s or something; basically, this address doesn't
			 even fit the general mould of a valid e-mail address. */
			strerrmsg = "Email address seems incorrect (check @ and .'s)";
			return strerrmsg;
		}
		var user=matchArray[1]
		var domain=matchArray[2]

		// See if "user" is valid 
		if (user.match(userPat)==null) {
			// user is not valid
			strerrmsg = "The username doesn't seem to be valid.";
			return strerrmsg;
		}

		/* if the e-mail address is at an IP address (as opposed to a symbolic
		   host name) make sure the IP address is valid. */
		var IPArray=domain.match(ipDomainPat)
		if (IPArray!=null) {
			// this is an IP address
			  for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					strerrmsg = "Destination IP address is invalid!";
				return strerrmsg;
				}
			}
			return true
		}


		// Domain is symbolic name
		var domainArray=domain.match(domainPat)
		if (domainArray==null) {
			strerrmsg = "The domain name doesn't seem to be valid.";
			return strerrmsg;
		}

		/* domain name seems valid, but now make sure that it ends in a
		   three-letter word (like com, edu, gov) or a two-letter word,
		   representing country (uk, nl), and that there's a hostname preceding 
		   the domain or country. */

		/* Now we need to break up the domain to get a count of how many atoms
		   it consists of. */
		var atomPat=new RegExp(atom,"g")
		var domArr=domain.match(atomPat)
		var len=domArr.length
		if (domArr[domArr.length-1].length<2 || 
			domArr[domArr.length-1].length>3) {
		   // the address must end in a two letter or three letter word.
		   strerrmsg = "The address must end in a three-letter domain, or two letter country.";
		   return strerrmsg;
		}

		// Make sure there's a host name preceding the domain.
		if (len<2) {
		   strerrmsg = "This address is missing a hostname!"
		   return strerrmsg;
		}

		// If we've gotten this far, everything's valid!
		return strerrmsg;
}

function ValidateNewsLetterManagementForm()
{
//declare an error message string variable.
var strerrmsg = "";
var strtempstring = "";

//Check to see that the user's email address is correct;
strtempstring = emailcheck(document.form.email.value);

//Check to see if the emailcheck function returned an error string;
	if (strtempstring !="")
	{
		strerrmsg = "There was a problem with the value in your email address field:\n******\n" + strtempstring + "\n******\n";
		strtempstring ="";
	}

/*We will assume that all of the addresses in the box are correct 
as they are run through an email validation before they are added.
*/

//Check to see if they are unsubscribing, if so alert them that is what they are doing for a UI confirm.


var strmodifyaction = new String(getcheckedradiovalue(document.form.modifyaction));

//Check to see if the individual is trying to unsubscribe
if(strmodifyaction.toLowerCase() == "unsubscribe")
	{
		//Are they trying to unsubscribe from something are not subsribed to?
		//If so let them know they are but bogus users.
		var strcurrentstatus = new String(document.form.currentstatus.value);
		if(strcurrentstatus.toLowerCase() == "notsubsribed")
		{
			strerrmsg = strerrmsg + "You are trying to unsubscribe from a Newsletter\nYou are not subscribed to.\nPlease select another action.";
			alert(strerrmsg);
			return false;
		}
		else
		{
			var strnewslettername = document.form.newslettername.value;
			var strconfirmmessage = "Are you sure you want to unsubscribe \nyourself from the\"" + strnewslettername + "\"newsletter?\nIf so click\"OK\"\nOtherwise click \"CANCEL\".";
			var answer = confirm(strconfirmmessage);
			if(answer)
			{
			
			document.form.action.value = "unsubscribe";
			document.form.domodification.value = "true";
			document.form.submit();
			return true;
			}
			else
			{
			return;
			}
		}
	}

	/*If there are no error messages go ahead and submit, if not tell the individual 
	that there are some problems and he or she must adjust accordingly.
	*/
	if(strerrmsg =="")
	{

		var strmodifyaction = new String(getcheckedradiovalue(document.form.modifyaction));
		var strcurrentstatus = new String(document.form.currentstatus.value);
		if((strcurrentstatus.toLowerCase() != "notsubsribed") && (strmodifyaction.toLowerCase()=="subscribe"))
		{
			document.form.action.value ="modify";
		}
		else
		{
			document.form.action.value = "subscribe";
		}

		//alert(document.form.action.value);

		var strxmlcclist = generatesubscriptioncclistxml();
		purgespaces(document.form.cclist);
		document.form.cclistxml.value = strxmlcclist;
		document.form.domodification.value = "true";

		document.form.submit();
	}
	else
	{
		alert(strerrmsg);
	}


}

function checkandaddsubscriber(stremailaddress,fbox,tbox)
{

var strerrormessage = "";

strerrormessage = emailcheck(stremailaddress);


	if (strerrormessage !="")
	{
		strerrormessage = "The email address you are attempting to add to\nyour subscriber list is incorrectly formed\nPlease re-enter the email address and try again";
		alert(strerrormessage);
	}
	else
	{
		movefromto(fbox,tbox);
		purgespaces(tbox);
	}


}

/*
Checks the email field and returns focus to the calling field if the address entered is incorrect.
*/
function checkemailfield(stremailaddress,objfocusobject)
{

var strerrormessage = "";

strerrormessage = emailcheck(stremailaddress);

	if (strerrormessage !="")
	{
		strerrormessage = "The email address you are attempting to add to\nyour subscriber list is incorrectly formed\nPlease re-enter the email address and try again";
		alert(strerrormessage);
		objfocusobject.focus();
	}


}

//Accepts a radio button array and returns a string of the value of the radio button which is checked.  If no radio button is checked
//it doth simply return a null.

function getcheckedradiovalue(objradio)
{

var strreturnstring = "";
var ilen = objradio.length;

	for (var i = 0;i < ilen ;i++ )
	{
		if (objradio[i].checked)
		{
		strreturnstring = objradio[i].value;
		}
	}
return strreturnstring;

}

function validateunsubscribeform()
{
	var strerrormessage = "";

	strerrormessage = emailcheck(document.form.emailaddress.value);

	if (strerrormessage !="")
	{
		alert(strerrormessage);
	}
	else
	{
		document.form.action.value="true";
		document.form.submit();
	}

}

function generatesubscriptioncclistxml()
{
	var strxmlstring = "";
	var i = 0;
	var ilen = document.form.cclist.options.length;

	if (ilen > 0)
	{
		for (i=0; i < ilen ; i++ )
		{
			var tempstring = document.form.cclist.options[i].value;
			if (tempstring !="")
			{
			strxmlstring = strxmlstring + "<subscriptioncc><email>" + tempstring + "</email></subscriptioncc>";
			}
			
		}
	}
	else
	{
		strxmlstring = "<subscriptioncc/>";
	}

	return strxmlstring;

}


function purgespaces(box)
{

	var i = 0;
	var emailPat=/^(.+)@(.+)$/;
	var ilen = box.options.length;

	if (ilen > 0)
	{
		for (i=0; i < ilen ; i++ )
		{

		var strvalue = box.options[i].value;
		var matcharray = strvalue.match(emailPat);
			if(matcharray==null)
			{
				box.options[i].value = "";
				box.options[i].text = "";
			}

		}

	}

BumpUp(box);

}




//-->