var MenuTimerId = 0;
var objName = ""
var objSubName = ""

function NewsletterSubscribe(e) {
    var sEmail  = document.getElementById(e).value
    var winNews
    
    if (sEmail == "") {
        alert("Please enter your email address")
    } else {
        if (emailCheck(sEmail)) {
            winNews = window.open("newsletter_subscribe.aspx?email="+ sEmail, "winNews", "width=1, height=1")
            window.focus();
        } else {
            document.getElementById(e).select();
        }
    }
}

function ValidateCode() {
    var sc1 = document.getElementById("hdnCode").value
    var sc2 = document.getElementById("txtConfirmCode").value
    var blnReturn = false
    
    if (sc2 == "") {
        alert("Please enter the confirmation code")
        document.getElementById("txtConfirmCode").focus()
    } else {
        if (sc1 == sc2) {
            blnReturn = true
        } else {
            alert("Sorry, the confirmation code you entered is incorrect")
            document.getElementById("txtConfirmCode").select()
        }
    }
  
    return blnReturn
}

function ShowMenu(objName) {

    document.getElementById("menuImporters").style.display = "none"
    document.getElementById("menuForwarders").style.display = "none"
    document.getElementById("menuResellers").style.display = "none"
    
    document.getElementById(objName).style.display = "block" 
}

function ShowSubMen(obj, objSub) {
    objName = obj
    objSubName = objSub
    
    document.getElementById(obj).style.display = "block"
    clearTimeout(MenuTimerId);
}

function HideSecondMenu(obj) {
    MenuTimerId = setTimeout(function() {HideIt(obj); },300);
}

function HideSubMen(obj, objSub) {
    if (objSub != "") {
        if (document.getElementById(objSub).style.visibility != "visible") {
            MenuTimerId = setTimeout(function() {HideIt(obj); },300);
            objSub = ""
        }        
    } else {
        
    }   
}

function HideIt(obj) {
    document.getElementById(obj).style.display = "none"
}

function ShowTab(iItem) {
    var objTab1 = document.getElementById("tab1")
    var objTab2 = document.getElementById("tab2")
    var objTab3 = document.getElementById("tab3")
    var objTabC1 = document.getElementById("tab1_content")
    var objTabC2 = document.getElementById("tab2_content")
    var objTabC3 = document.getElementById("tab3_content")
    
    objTab1.className = "tab"
    objTab2.className = "tab"
    objTab3.className = "tab"
    
    objTabC1.style.display = "none"
    objTabC2.style.display = "none"
    objTabC3.style.display = "none"
    
    for(i=1;i<4;i++) {
        if (i == iItem) {
            document.getElementById("tab"+ i).className = "tab_selected"
            document.getElementById("tab"+ i +"_content").style.display = "block"
            break;
        }
    }
}

function ValidateForm() {
    var sReturn = false
    
    if (document.getElementById("txtFirstName").value == "") {
        alert("Please enter your first name");
        sReturn = false;
    } else if (document.getElementById("txtLastName").value == "") {
        alert("Please enter your last name");
        sReturn = false;
    } else if (document.getElementById("txtEmail").value == "") {
        alert("Please enter your email address");
        sReturn = false;
    } else if (document.getElementById("drpHearAboutUs").selectedIndex == 0) {
        alert("How did you hear about us");
        sReturn = false;
    } else {
        sReturn = true;
    }
    
    return sReturn
}

function emailCheck(sEmail) {
	if (sEmail != "") {
		var checkTLD=1;
		var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
		var emailPat=/^(.+)@(.+)$/;
		var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
		var validChars="\[^\\s" + specialChars + "\]";
		var quotedUser="(\"[^\"]*\")";
		var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
		var atom=validChars + '+';
		var word="(" + atom + "|" + quotedUser + ")";
		var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
		var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
		var matchArray=sEmail.match(emailPat);
		
		if (matchArray==null) {
			alert("Email address seems incorrect (check @ and .'s)");
			blnValidEmail = false
			return false;
		}

		var user=matchArray[1];
		var domain=matchArray[2];

		for (i=0; i<user.length; i++) {
			if (user.charCodeAt(i)>127) {
				alert("Ths username contains invalid characters.");
				blnValidEmail = false
				return false;
			}
		}

		for (i=0; i<domain.length; i++) {
			if (domain.charCodeAt(i)>127) {
				alert("Ths domain name contains invalid characters.");
				blnValidEmail = false
				return false;
			}
		}


		if (user.match(userPat)==null) {
			alert("The username doesn't seem to be valid.");
			blnValidEmail = false
			return false;
		}

		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {
			for (var i=1;i<=4;i++) {
				if (IPArray[i]>255) {
					alert("Destination IP address is invalid!");
					blnValidEmail = false
					return false;
				}
			}
			
			blnValidEmail = true
			return true;
		}

		
		var atomPat=new RegExp("^" + atom + "$");
		var domArr=domain.split(".");
		var len=domArr.length;
		
		for (i=0;i<len;i++) {
			if (domArr[i].search(atomPat)==-1) {
				alert("The domain name does not seem to be valid.");
				blnValidEmail = false
				return false;
			}
		}

		if (checkTLD && domArr[domArr.length-1].length!=2 && 
			domArr[domArr.length-1].search(knownDomsPat)==-1) {
			alert("The address must end in a well-known domain or two letter " + "country.");
			blnValidEmail = false
			return false;
		}

		if (len<2) {
			alert("This address is missing a hostname!");
			blnValidEmail = false
			return false;
		}
	}
    
	blnValidEmail = true
	return true;
}

function checkNum(data) {      // checks if all characters 
    var valid = "0123456789.";     // are valid numbers or a "."
    var ok = 1; var checktemp;

    for (var i=0; i<data.length; i++) {
        checktemp = "" + data.substring(i, i+1);
        
        if (valid.indexOf(checktemp) == "-1") return 0; 
    }
    
    return 1;
}
