/*
* For the nav menu
*/

activateMenu2 = function(leftMenuCatId) {
	var nav = "leftnav" + leftMenuCatId;
	//if (document.all && document.getElementById(nav).currentStyle) {
            //alert("document.getElementsByName(nav) length = " 
            //	+ document.getElementsByName(nav).length);
            var navroot = document.getElementsByName(nav)[0];
            if (navroot == undefined || navroot == null) {
                //var leftMenuTD = document.getElementById("leftNavMenuList"); 
                //alert(leftMenuTD + "==" + leftMenuTD.getElementsByName(nav)[0]);
                var leftNavCatList = document.getElementsByTagName("ul"); 
                for (var i = 0; i < leftNavCatList.length; i++) { 
                    //alert(leftNavCatList[i].name + "===" + leftNavCatList[i].id + "--" + nav);
                    if ( leftNavCatList[i].name == nav) {
                        navroot = leftNavCatList[i];
                    }
                }
            }
            if (navroot == undefined || navroot == null) {
                return false;
            }
            /* Get all the list items within the menu */
            var lis=navroot.getElementsByTagName("LI");
            for (var i=0; i<lis.length; i++) {
                    if(lis[i].lastChild.tagName=="DIV"){
                            lis[i].onmouseover=function() {
                                    //alert("showing " + this);
                                    this.lastChild.style.display="block";
                                    this.className += " over";
                            }
                            lis[i].onmouseout=function() {
                                    //alert("hiding " + this);
                                    this.lastChild.style.display="none";
                                    this.className = this.className = "";
                            }
                    }
            }
        //}
}

function addEvent(object, type, handler){
/*
    Add any event handler to the browser, such as an onload function.
    Stable and cross-browser safe.
        object = where to attach event; >window< almost every time, though not necessarily
        type = what kind of handler to add, eg: "load"
        handler = the code (anonymous function) that will be called when event is fired.
*/
	if (object.addEventListener)
		object.addEventListener(type, handler, false);
	else if (object.attachEvent)
		object.attachEvent(['on',type].join(''),handler);
	else
		object[['on',type].join('')] = handler;
}

//addEvent(window, "load", activateMenu);
addEvent(window, "load", activateMenu2);
/*
* end of functions for the left menu
*/



function showItem(whichLayer) {
    if (document.getElementById) {
        // this is the way the standards work
        var style2 = document.getElementById(whichLayer).style;
        style2.display = "block";
        style2.visibility = "visible";
    }  else if (document.all) {
        // this is the way old msie versions work
        var style2 = document.all[whichLayer].style;
        style2.display = "block";
        style2.visibility = "visible";
    } else if (document.layers) {
        // this is the way nn4 works
        var style2 = document.layers[whichLayer].style;
        style2.display = "block";
        style2.visibility = "visible";
    }
}

function hideItem(whichLayer) {
    if (document.getElementById) {
        // this is the way the standards work
        var style2 = document.getElementById(whichLayer).style;
        style2.display = "none";
        style2.visibility = "hidden";
    }  else if (document.all) {
        // this is the way old msie versions work
        var style2 = document.all[whichLayer].style;
        style2.display = "none";
        style2.visibility = "hidden";
    } else if (document.layers) {
        // this is the way nn4 works
        var style2 = document.layers[whichLayer].style;
        style2.display = "none";
        style2.visibility = "hidden";
    }
}

var rootLocation = "";

function printPage() {
   window.print();
}

function proceedToCheckout() {
   top.location.href= rootLocation 
   		+ "/order/payment.jsp?randID=" 
   		+ Math.random();
}

function editPastOrder() {
   top.location.href= rootLocation 
   		+ "/cart/cart.jsp?randID=" 
   		+ Math.random();
}

function finalizeOrder() {
   top.location.href= rootLocation + "/order/thankyou.jsp?currTrxnId=0120&randID="
   		+ Math.random();
}

function cancelDelete(folderId) {
    top.location.href= rootLocation + "/admin/filemanager/list.jsp?folderId="
                + folderId + "&randID=" + Math.random();
}

function goBackToReferrer() {
   document.address.reset();
   document.address.submit();
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function postSpan(fieldId, textMessage) {
    var spanE = document.getElementById(fieldId);
    if(spanE.innerText) {
        spanE.innerText = textMessage;
    } else {
        spanE.innerHTML = textMessage;
    }
}

function postSpanError(fieldId, errorMessage) {
    postSpan(fieldId + "Err", errorMessage);
}
function checkBlankPostError(fieldId, errorMessage) {
    if (document.getElementById(fieldId) != 'undefined'
        && isBlank(document.getElementById(fieldId).value)) {
        postSpanError(fieldId, errorMessage);
        return false;
    }
    return true;
}
function isBlank(fieldValue) {
    return (fieldValue == null || fieldValue == '');
}

function isPositiveInteger(val) {
    if (isBlank(val)) {
        return false;
    }
    var digitNumberList = "0123456789";
    for (var i=0; i < val.length; i++) {
        var x = val.charAt(i);
        if (digitNumberList.indexOf(x,0) == -1) {
            return false;
        }
    }
    return true;
}

function isNumber(val) {
    if (isNaN(val)) {
        return false;
    } else {
        return true;
    } 
}

function isWithinRange(val, min, max) {
    if (val >= min && val <= max) {
        return true;
    } else {
        return false;
    } 
}

function isNumberWithinRange(val, min, max) {
    if (isNumber(val) && isWithinRange(val, min, max)) {
        return true;
    } else {
        return false;
    }
}

function isAlphabetic(val) {
    if (isBlank(val)) {
        return false;
    }
    if (val.match(/^[a-zA-Z]+$/)) {
        return true;
    } else {
        return false;
    } 
}

function isAlphaNumeric(val) {
    if (isBlank(val)) {
        return false;
    }
    if (val.match(/^[a-zA-Z0-9]+$/)) {
        return true;
    } else {
        return false;
    } 
}

function isEmailAddress(val) {
    if (isBlank(val)) {
        return false;
    }
    var emailFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,7})+$/;
    if (emailFilter.test(val)) {
        return true;
    } else {
        return false;
    } 
}

function checkPhone(val) {
    if (isBlank(val) || val.length < 10) {
        return false;
    }
      //var re = /^[0-9]{3,3}\-[0-9]{3,3}\-[0-9]{4,4}$|^[0-9]{3,3} [0-9]{3,3} [0-9]{4,4}$|^[0-9]{10,10}$|^[0-9]{3,3} \-[0-9]{3,3} \- [0-9]{4,4}$|^\([0-9]{3,3}\) [0-9]{3,3}\-[0-9]{4,4}$|^\([0-9]{3,3}\)[0-9]{3,3}\-[0-9]{4,4}$|^[0-9]{10,10}\-[0-9]{4,4}$|^[0-9]{3,3}\-[0-9]{4,4}$/;
      //return re.test(val);
      return true;
}

function checkFormAddress (form, addrInputType) {
    var retVal = true;
    var lastErrorFieldId = null;
    if (!checkBlankPostError(addrInputType + "firstName",
                            "Cannot be blank")) {
        retVal = false;
        lastErrorField = addrInputType + "firstName";
    }
    if (!checkBlankPostError(addrInputType + "lastName",
                            "Cannot be blank")) {
        lastErrorField = addrInputType + "lastName";
        retVal = false;
    }
    if (document.getElementById(addrInputType + "phone") != 'undefined'
        && !checkPhone(document.getElementById(addrInputType + "phone").value)) {
        postSpanError(addrInputType + "phone", "Invalid phone number");
        lastErrorField = addrInputType + "phone";
        retVal = false;
    }
    if (document.getElementById(addrInputType + "email") != 'undefined'
        && !isEmailAddress(document.getElementById(addrInputType + "email").value)) {
        postSpanError(addrInputType + "email", "Invalid email address");
        lastErrorField = addrInputType + "email";
        retVal = false;
    }
    if (!checkBlankPostError(addrInputType + "address1",
                            "Cannot be blank")) {
        lastErrorField = addrInputType + "address1";
        retVal = false;
    }
    if (!checkBlankPostError(addrInputType + "city",
                            "Cannot be blank")) {
        lastErrorField = addrInputType + "city";
        retVal = false;
    }
    if (!checkBlankPostError(addrInputType + "state",
                            "Cannot be blank")) {
        lastErrorField = addrInputType + "state";
        retVal = false;
    } else if ('out' == document.getElementById(addrInputType + "state").value
            && !checkBlankPostError(addrInputType + "intlProvince",
                            "State/Province cannot be blank.")) {
        lastErrorField = addrInputType + "intlProvince";
        retVal = false;
        
    }
    if (!checkBlankPostError(addrInputType + "zip",
                            "Cannot be blank")) {
        lastErrorField = addrInputType + "zip";
        retVal = false;
    }
    if (!retVal && document.getElementById(lastErrorField) != 'undefined') {
        document.getElementById(lastErrorField).focus();
    }
    return retVal;
}

//
// This function checks the input for tcpip address format
// param1 is the input tcpip string
function isTcpipAddr(param1) {
  var tcpipText = param1;
  if (!isBlank(tcpipText)) {
    var tcpipPattern = /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/;
    var matchResult = tcpipText.match(tcpipPattern);
    if ( (null != matchResult) && (5 == matchResult.length) && (param1 == matchResult[0]) )    {
      for (var ii = 1; ii <= 4; ii++)   {
        if (!isTcpipElem(matchResult[ii]))  {
          return false;
        }
      }
      return true;
    }
  }
  return false;
}


//
// This function returns true if the argument string is between 0 and 255.
//
function isTcpipElem(inputNumber)   {
  if ( (0 <= inputNumber) && (255 >= inputNumber) )   {
    return true;
  } 
  return false;
}

