// JavaScript Document
// Show and Hide Layers For Error Message
function MM_showHideLayers() { //v9.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3)
  with (document) if (getElementById && ((obj=getElementById(args[i]))!=null)) { v=args[i+2];
	if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
	obj.visibility=v; }
}
//This Is The AJAX NEEDED VARIABLES
var xmlHttp;
var requestINFO = '/content/accountInfo.jsp?edit=';
var requestInfoUpdate = '/content/accountInfoUpdate.jsp?';
var requestNEWSLETTER = '/content/accountNewsletter.jsp?newLetterID=';

var is_ie = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0;
var is_ie5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0;
var is_opera = ((navigator.userAgent.indexOf("Opera 6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0;
//netscape, safari, mozilla behave the same???
var is_netscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0;


function editInfo(strName){
	if (strName.length > 0){
		var url = requestINFO + strName;
		xmlHttp = GetXmlHttpObject(editInfoChange);
		xmlHttp_Get(xmlHttp, url);
	}
	else {
		document.getElementById('contact').innerHTML = 'Contact Information Could Not Be Updated';
	}
}
function editInfoChange()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete'){
		var str = xmlHttp.responseText;
		document.getElementById('contact').innerHTML = str;
	}
}

/*function getInputValue(name){
	var obj = document.getElementById(name);
	if ( obj != null){
		return obj.value;
	}
	return "error";
}*/
function updateInfo(){
	var firstName = document.getElementById("firstName").value;
	var lastName =document.getElementById("lastName").value;
	var company_name =document.getElementById("company_name").value;
	var phone =document.getElementById("phone").value;
	var address1 =document.getElementById("address1").value;
	var address2 =document.getElementById("address2").value;
	var city =document.getElementById("city").value;
	var state_province =document.getElementById("state_province").value;
	var zip_postal_code =document.getElementById("zip_postal_code").value;
	var country =document.getElementById("country").value;

	var reqParams = "";
	reqParams += "firstName="+encodeURIComponent(firstName) + "&";
	reqParams += "lastName="+encodeURIComponent(lastName) + "&";
	reqParams += "company_name="+encodeURIComponent(company_name) + "&";
	reqParams += "phone="+encodeURIComponent(phone) + "&";
	reqParams += "address1="+encodeURIComponent(address1) + "&";
	reqParams += "address2="+encodeURIComponent(address2) + "&";
	reqParams += "city="+encodeURIComponent(city) + "&";
	reqParams += "state_province="+encodeURIComponent(state_province) + "&";
	reqParams += "zip_postal_code="+encodeURIComponent(zip_postal_code) + "&";
	reqParams += "country="+encodeURIComponent(country) + "&";
	var url = requestInfoUpdate + reqParams;
	xmlHttp = GetXmlHttpObject(updateInfoChange);
	xmlHttp_Get(xmlHttp, url);
}

function updateInfoChange()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete'){
		var str = xmlHttp.responseText;
		document.getElementById('contact').innerHTML = str;
	}
}

// XMLHttp send GET request
function xmlHttp_Get(xmlhttp, url) {
	var path = "";
	var queryStr = "";
	var pos = url.indexOf("?");
	if ( pos == -1) {
		path = url;
		queryStr = "";
	} else {
		path = url.substring(0, pos);
		queryStr = url.substring(pos+1);
	}
	//queryStr = encodeURIComponent(queryStr);
	//alert(queryStr);
	xmlhttp.open("POST",path,true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.send(queryStr);
}

function newLetterSelect(strName){
	if (strName.length > 0){
		var url = requestNEWSLETTER + strName;
		xmlHttp = GetXmlHttpObject(editNewletterChange);
		xmlHttp_Get(xmlHttp, url);
	}
	else {
		document.getElementById('newsletter').innerHTML = 'Newsletters Could Not Be Updated';
	}
}
//stateChangeHandler will fire when the state has changed, i.e. data is received back

// This is non-blocking (asynchronous)
function editNewletterChange()
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 'complete'){
		var str = xmlHttp.responseText;
		document.getElementById('newsletter').innerHTML = str;
	}
}

function GetXmlHttpObject(handler) {
	var objXmlHttp = null;    //Holds the local xmlHTTP object instance

	//Depending on the browser, try to create the xmlHttp object
	if (is_ie){
		//The object to create depends on version of IE
		//If it isn't ie5, then default to the Msxml2.XMLHTTP object
		var strObjName = (is_ie5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP';

		//Attempt to create the object
		try{
			objXmlHttp = new ActiveXObject(strObjName);
			objXmlHttp.onreadystatechange = handler;
		}
		catch(e){
		//Object creation errored
			alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled');
			return;
		}
	}
	else if (is_opera){
		//Opera has some issues with xmlHttp object functionality
		alert('Opera detected. The page may not behave as expected.');
		return;
	}
	else{
		// Mozilla | Netscape | Safari
		objXmlHttp = new XMLHttpRequest();
		objXmlHttp.onload = handler;
		objXmlHttp.onerror = handler;
	}

	//Return the instantiated object
	return objXmlHttp;
}



/* 1.1.2: Fixed a bug where trailing . in e-mail address was passing
            (the bug is actually in the weak regexp engine of the browser; I
            simplified the regexps to make it work).
   1.1.1: Removed restriction that countries must be preceded by a domain,
            so abc@host.uk is now legal.  However, there's still the
            restriction that an address must end in a two or three letter
            word.
     1.1: Rewrote most of the function to conform more closely to RFC 822.
     1.0: Original  */


function emailCheck (emailStr) {
/* 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. */
	alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid
if (user.match(userPat)==null) {
    // user is not valid
    alert("The username doesn't seem to be valid.")
    return false
}

/* 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) {
	        alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.")
    return false
}

/* 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.
   alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}
