var timeout	= 0;
var closetimer	= 0;
var ddmenuitem	= 0;

function mopen(id)
{	
	mcancelclosetime();

	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';
}

function mclose()
{
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

function mclosetime()
{
	closetimer = window.setTimeout(mclose, timeout);
}

function mcancelclosetime()
{
	if(closetimer)
	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

document.onclick = mclose; 

function ValidateForm(sPage) {
    var fldObj
    var i = 0
    var j = 0
    var blnReturn = true
    var sCurrentField = ""
    var fldChildObj
    var tempObjName
    var blnChecked = true 
    var blnValidEmail = false
    
    var browserName=navigator.appName; 
    if (browserName=="Netscape") { 
        fldObj = document.getElementsByTagName("INPUT")
    } else if (browserName=="Microsoft Internet Explorer") {
        fldObj = document.all
    }

    for (i=0;i<fldObj.length;i++) {
        sCurrentField = fldObj[i].title
        
        if (document.getElementById("txtOldPass")) {
            if (document.getElementById("txtOldPass").value != "") {
                if (document.getElementById("txtPass1").value == "") {
                    sCurrentField = "NEW_PASSWORD"
                    blnReturn = false
                    break
                } else {
                    if (fldObj[i].id == "txtPass2") {
                        if (fldObj[i].value != document.getElementById("txtPass1").value) {
                            sCurrentField = "PASSWORD_CONFIRM"
                            document.getElementById("txtPass1").style.backgroundColor = '#ececf0'
                            document.getElementById("txtPass2").style.backgroundColor = '#ececf0'
                            blnReturn = false
                            break
                        } else {
                            document.getElementById("txtPass1").style.backgroundColor = ''
                            document.getElementById("txtPass2").style.backgroundColor = ''
                        }
                    }
                }
            }
        }
        
        if (sCurrentField != "") {
            fldChildObj = document.getElementsByName(fldObj[i].name)
            
            //emailCheck
            if (fldChildObj.length == 1) {
                if (sCurrentField == "Email") {
                    blnValidEmail = emailCheck(fldObj[i].value)
                    if (!blnValidEmail) {
                        fldObj[i].style.backgroundColor = '#ececf0'
                        fldObj[i].focus()
                        blnReturn = false
                        break;
                    } else {
                        fldObj[i].style.backgroundColor = ''
                    }   
                }
                
                if ((fldObj[i].selectedIndex == 0)||(fldObj[i].value == "")) {
                    if (fldObj[i].style.display != "none") {
                        fldObj[i].style.backgroundColor = '#ececf0'
                        fldObj[i].focus()
                    } else {
                        location.href = "#anchor_"+ fldObj[i].name
                    }
                    
                    blnReturn = false
                    break
                } else {
                    fldObj[i].style.backgroundColor = ''
                }
                
                if (fldObj[i].id == "txtPass2") {
                    if (fldObj[i].value != document.getElementById("txtPass1").value) {
                        sCurrentField = "PASSWORD_CONFIRM"
                        document.getElementById("txtPass1").style.backgroundColor = '#ececf0'
                        document.getElementById("txtPass2").style.backgroundColor = '#ececf0'
                        blnReturn = false
                        break
                    } else {
                        document.getElementById("txtPass1").style.backgroundColor = ''
                        document.getElementById("txtPass2").style.backgroundColor = ''
                    }
                }
                
            } else {
                if (tempObjName != fldObj[i].name) {
                    for (j=0;j<fldChildObj.length;j++) {
                        if(fldChildObj[j].checked) {
                            blnChecked = true
                            break
                        } else {
                            blnChecked = false
                        }
                    }
                    
                    if (!blnChecked) {
                        blnReturn = false
                        break
                    }
                }
            }
            
            tempObjName = fldObj[i].name
        }
    }
    
    if (!blnReturn) {
        if (sCurrentField == "NEW_PASSWORD") {
            alert("Please enter your new password")
            blnReturn = false
        } else if (sCurrentField == "PASSWORD_CONFIRM") {
            alert("Your password did not match")
            blnReturn = false
        } else if (sCurrentField == "CHANGE_PASSWORD") {
            alert("Please enter your old password")
            blnReturn = false
        } else {
            if (sCurrentField == "Email") {
                if (!blnValidEmail) {
                    
                } else {
                    alert(sCurrentField +" is required")
                    blnReturn = false
                }
            } else {
                alert(sCurrentField +" is required")
                blnReturn = false
            }
        }
    }
    
    if (sPage == "order") {
        var blnOrder = false
        if(!document.getElementById("chkSpecialOffer").checked) {
            for (i=1;i<document.getElementById("hdnTotalProducts").value+1;i++) {
                if (document.getElementById("hdnProductId"+ i)) {
                    if(document.getElementById("hdnProductId"+ i).value == "") {
                    } else {
                        blnOrder = true
                        break
                    }
                }
            }
        } else {
            blnOrder = true
        }
        
        if (document.getElementById("chkSpecialOffer").checked) {
            if (document.getElementById("drpBlend1").selectedIndex == 0) {
                alert("Please select coffee blend (Beans/Ground) for the 10 x 250g GAGGIA")
                blnReturn = false
            } else if (document.getElementById("drpBlend2").selectedIndex == 0) {
                alert("Please select coffee blend (Beans/Ground) for the free 2 x 250g GAGGIA")
                blnReturn = false
            }
        }
        
        if (blnReturn) {
            if (!blnOrder) {
                alert("Please select a product you want to order.")
                blnReturn = false
            }
        }
    } else {
    
    }
    
    return blnReturn
}

function validateDate(fld) {
    if (fld.value == '') return false;
    
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    var errorMessage = 'Please enter valid date as month, day, and four digit year.\nYou may use a slash to separate the values.\nThe date must be a real date, format should be mm/dd/yyyy.';
    if ((fld.value.match(RegExPattern)) && (fld.value!='')) {
        
    } else {
        alert(errorMessage);
        fld.focus();
    } 
}

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;
}