function openLegalWindow(lang) {
    stringMyURL = "/html/" +lang + "/terms_cond_external.html";
    theLegalWindow = window.open(stringMyURL,'','');
    theLegalWindow.focus();
}
function openPrivacyWindow(lang) {
    stringMyURL = "/html/" +lang + "/privacy.html";
    thePrivacyWindow = window.open(stringMyURL,'','');
    thePrivacyWindow.focus();
}
function openDisclaimerWindow(lang) {
    stringMyURL = "/html/" +lang + "/legal_disclaimer.html";
    theDisclaimerWindow = window.open(stringMyURL,'','');
    theDisclaimerWindow.focus();
}
function getCookieVal (offset) {
    
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    x=unescape(document.cookie.substring(offset, endstr));
//    document[getNetuiTagName("loginForm", this)][getNetuiTagName("userId", this)].value=x;
    return x;
}
function FixCookieDate (date) {
    var base = new Date(0);
    var skew = base.getTime(); // dawn of (Unix) time - should be 0
    if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
function GetCookie (userId) {
    var arg = userId + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
          return getCookieVal (j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}
function RemoveCookie (userId) {
    var arg = userId + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
          var k=document.cookie.indexOf (";", j);
          if (k == -1)
            k=document.cookie.length;
          document.cookie = document.cookie.substring(0, i)+document.cookie.substring(k, clen);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
}
function CheckCookie (name) {
    RemoveCookie(name);
    path="/";
    if(!document["loginForm"].remember.checked) {
        DeleteCookie(name,path);
    }
    else{
        //document.cookie=name + "=" +document.forms["loginForm"].userId.value +
        //((path) ? "; path=" + path : "") +"; expires="+ expdate;
        value=document["loginForm"][getNetuiTagName("userId", this)].value;
        Set_Cookie(name,value,expdate,path)
    }
}
function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
                    ( (expires) ? ";expires=" + expires.toGMTString() : "") +
                    ( (path) ? ";path=" + path : "") +
                    ( (domain) ? ";domain=" + domain : "") +
                    ( (secure) ? ";secure" : "");
}
function DeleteCookie (name,path,domain) {
    if (GetCookie(name)) {
    document.cookie = name + "=" +
                      ((path) ? "; path=" + path : "") +
                      ((domain) ? "; domain=" + domain : "") +
                      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
function initpage() {
    document.login.userId.focus()
    if ((navigator.appName=="Netscape")&&(navigator.appVersion.charAt(0)<="4")){
        window.blur();
    }
}
function setButtonEvent(event) 
{
    document[getNetuiTagName("add_assistant",this)][getNetuiTagName("buttonevent",this)].value = event;
    return true; 
}        

function check_phone_no(infield,msg){

	var phone_no=infield.value;
	phone_no=phone_no.replace(/\D/g,"");

	if(phone_no.length == 0){return true};

	if(phone_no.length != 10) {
		alert("The following information is incorrect: "+msg+" must be of the form 999-999-9999.");
		return false;
	}
	return true;
}

function check_phone_nos(){
	if (check_phone_no(document.rep_info.phoneBus,"Phone Number") && check_phone_no(document.rep_info.faxNumber,"Fax Number") && check_phone_no(document.rep_info.tollFree,"Toll-free Number")) {
		return true;
	}
	else { return false; }
}

//verify post_code
function check_postalCode(){
	var postalCode=document.rep_info.postalCode.value.toUpperCase();
	var flag = false;

	if (postalCode.length == 0)
		return true;

	postalCode=postalCode.replace(/(\W*)/g,"");

	if(postalCode.length == 6){
		check_pc_form=postalCode.replace(/[A-Z]/g,"X");
		check_pc_form=check_pc_form.replace(/[0-9_]/g,"9");

		if(check_pc_form.match(/X9X9X9/g) != null)
			flag=true;
		else
			alert("The following information is incorrect: Postal Code must be of the form X9X9X9.");
	}
	else {
		alert("The following information is incorrect: Postal Code must contain six alpha numeric characters.");
	}

	if (flag == false)
		document.rep_info.postalCode.focus();
	else
		document.rep_info.postalCode.value=postalCode;

	return flag;
}

//verify email address has acceptable form
function check_email () {
	var emailStr=document.rep_info.email.value;
	if (emailStr=="") {return true;}

	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var firstChars=validChars;
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom="(" + firstChars + validChars + "*" + ")";
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) { alert("The following information is incorrect: Email address is incorrect (check @ and .'s)."); return false;}

	var user=matchArray[1];
	var domain=matchArray[2];

	if (user.match(userPat)==null) {alert("The following information is incorrect: The username is invalid."); return false;}

	var IPArray=domain.match(ipDomainPat);

	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) { alert("The following information is incorrect: Destination IP address is invalid."); return false;}
		}
    	return true;
	}

	var domainArray=domain.match(domainPat);

	if (domainArray==null) {alert("The following information is incorrect: The domain name is invalid."); return false; }

	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) {
   		alert("The following information is incorrect: Email address must end in a three-letter domain, or two letter country.");
	   return false;
	}

	if (domArr[domArr.length-1].length==3 && len<2) {var errStr="The following information is incorrect: Email address is missing a hostname."; alert(errStr); return false;}

	return true;
}

function check_province() {
	for (var j=0; j < document.rep_info.province.options.length; j++) {
		if(document.rep_info.province.options[j].selected) {
			if (document.rep_info.province.options[j].value.length == 0)
				return false;
		}
	}
	return true;
}

function alertMsg(inputMsg) {
	var msg;

	msg  = "________________________________________________________________________\n\n";
	msg += "The rep information was not submitted because of the following errors(s).\n";
	msg += "Please correct these error(s) and re-submit\n";
	msg += "________________________________________________________________________\n\n";
	msg += inputMsg + "\n";

	return msg;
}

//verify phone number is only 10 digits
function verify(rep_info_form) {
	var msg;
	var field_name = "";
	var empty_fields = "";
	var error_flag = "0";

	for (var i = 0; i < rep_info_form.length; i++) {
		var e = rep_info_form.elements[i];
		if (((e.type == "text") || (e.type != "hidden")) && !e.optional) {
			if ((e.value == "" )){
				if (e.name == "firstName")
					field_name = "Representative First Name:";
				if (e.name == "lastName")
					field_name = "Representative Last Name:";
				if (e.name == "address1")
					field_name = "Address:";
				if (e.name == "city")
					field_name = "City:";
				if (e.name == "postalCode")
					field_name = "Postal Code:";
				if (field_name.length > 0){
					empty_fields += "\n     " + field_name;
					errors_flag = "1";
					field_name = "";
				}
				continue;
			}
		}
	}

	if (!check_province()){
		empty_fields += "\n     Province";
		errors_flag = "1";
	}
	if (!empty_fields && check_postalCode() && check_email() && check_phone_nos()) return true;

	if (empty_fields) {
		msg = "The following fields(s) require an entry:" + empty_fields + "\n";
		alert(alertMsg(msg));
		return false;
	}
}

function chg_info_submit () {
	if (verify(document.rep_info))
		document.rep_info.submit();
}

//verify phone number is only 10 digits
function check_phone_no(infield,msg){

	var phone_no=infield.value;
	phone_no=phone_no.replace(/\D/g,"");

	if(phone_no.length == 0){return true};

	if(phone_no.length != 10) {
		alert("The following information is incorrect: "+msg+" must be of the form 999-999-9999.");
		return false;
	}
	return true;
}

function check_phone_nos(){
	if (check_phone_no(document.rep_info.phoneBus,"Phone Number") && check_phone_no(document.rep_info.faxNumber,"Fax Number") && check_phone_no(document.rep_info.tollFree,"Toll-free Number")) {
		return true;
	}
	else { return false; }
}

//verify post_code
function check_postalCode(){
	var postalCode=document.rep_info.postalCode.value.toUpperCase();
	var flag = false;

	if (postalCode.length == 0)
		return true;

	postalCode=postalCode.replace(/(\W*)/g,"");

	if(postalCode.length == 6){
		check_pc_form=postalCode.replace(/[A-Z]/g,"X");
		check_pc_form=check_pc_form.replace(/[0-9_]/g,"9");

		if(check_pc_form.match(/X9X9X9/g) != null)
			flag=true;
		else
			alert("The following information is incorrect: Postal Code must be of the form X9X9X9.");
	}
	else {
		alert("The following information is incorrect: Postal Code must contain six alpha numeric characters.");
	}

	if (flag == false)
		document.rep_info.postalCode.focus();
	else
		document.rep_info.postalCode.value=postalCode;

	return flag;
}

//verify email address has acceptable form
function check_email () {
	var emailStr=document.rep_info.email.value;
	if (emailStr=="") {return true;}

	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var firstChars=validChars;
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom="(" + firstChars + validChars + "*" + ")";
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) { alert("The following information is incorrect: Email address is incorrect (check @ and .'s)."); return false;}

	var user=matchArray[1];
	var domain=matchArray[2];

	if (user.match(userPat)==null) {alert("The following information is incorrect: The username is invalid."); return false;}

	var IPArray=domain.match(ipDomainPat);

	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) { alert("The following information is incorrect: Destination IP address is invalid."); return false;}
		}
    	return true;
	}

	var domainArray=domain.match(domainPat);

	if (domainArray==null) {alert("The following information is incorrect: The domain name is invalid."); return false; }

	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) {
   		alert("The following information is incorrect: Email address must end in a three-letter domain, or two letter country.");
	   return false;
	}

	if (domArr[domArr.length-1].length==3 && len<2) {var errStr="The following information is incorrect: Email address is missing a hostname."; alert(errStr); return false;}

	return true;
}

function check_province() {
	for (var j=0; j < document.rep_info.province.options.length; j++) {
		if(document.rep_info.province.options[j].selected) {
			if (document.rep_info.province.options[j].value.length == 0)
				return false;
		}
	}
	return true;
}

function alertMsg(inputMsg) {
	var msg;

	msg  = "________________________________________________________________________\n\n";
	msg += "The rep information was not submitted because of the following errors(s).\n";
	msg += "Please correct these error(s) and re-submit\n";
	msg += "________________________________________________________________________\n\n";
	msg += inputMsg + "\n";

	return msg;
}
function chg_info_submit () {
	if (verify(document.rep_info))
		document.rep_info.submit();
}
