var menusEnabled = false;
var currentLocation = window.location.href;

function getElementById(id) {
	var ns4 = (document.layers);
	var ie4 = (document.all && !document.getElementById);
	var ie5 = (document.all && document.getElementById);
	var ns6 = (!document.all && document.getElementById);
	if (ns4)
		return document.layers[id];
	else if (ie4)
		return document.all[id];
	else if (ie5 || ns6)
		return document.getElementById(id);
}

function setVisibility(id, visibility) {
	var element = getElementById(id)
	if (element != null)
		element.style.visibility = (visibility ? "visible" : "hidden");
	else
		throw "element " + id + " not found!";
}

function getElementByEvent(event) {
    var element;
    if (!event)
    	var event = window.event;
    if (event.target)
        element = event.target;
    else if (event.srcElement)
        element = event.srcElement;
    if (element.nodeType != null && element.nodeType == 3) // defeat Safari bug
        element = element.parentNode;
    return element;
}

function invalidDataMsg(msg) {
	alert(msg);
}

function doRowHighlight(e) {
    e.style.color='black';
    e.style.backgroundColor='#f9f7e8';
    try {
        e.style.cursor='pointer';
    } catch (e) {
    }
}

function doRowNormal(e) {
    e.style.color='#000099';
    e.style.backgroundColor='white';
    try {
        e.style.cursor='default';
    } catch (e) {
    }
}

function cancelEvent(e) {
	if (!e)
		var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation)
		e.stopPropagation();
}

function blankOut(control){
	control.value="";
}


function showSpinner(e) {
	var spinner = getElementById("spinner-container");
	spinner.style.display = "block";
	spinner.style.visibility = "visible";
	setTimeout(resetSpinner, 200);
}

function hideSpinner(e) {
	var spinner = getElementById("spinner-container");
	spinner.style.display = "none";
	spinner.style.visibility = "hidden";
}

function resetSpinner() {
	var spinner = getElementById("spinner");
	spinner.src = "/commerce/includes/images/icons/spinner.gif";
	return true;
}

function trim(s) {
	var re = /^\s*(.+?)\s*$/;
	s = s.replace(re, "$1");
	if (/^\s+$/.test(s))
		s = "";
	return s;
}

function popup(name, url, width, height) {
     var win = window.open(url, name, "width=" + width + ",height=" + height
             + ",resizable=yes,status=no,location=no,menubar=no,scrollbars=yes,toolbar=no,top=20,left=20", true);
     win.focus();
     return win;
}

function isMainMenu() {
	return (menusEnabled && typeof(mainMenu) != "undefined");
}

function hideAllMenus() {
	if (isMainMenu())
		mainMenu.hideAll();
}

function setIFrameHeight(id) {
	var frame = getElementById(id);
	if (frame) {
        var doc = frame.contentWindow.document;
        var scrollHeight;
        var offsetHeight;
		if (doc.documentElement) {
			scrollHeight = doc.documentElement.scrollHeight;
			offsetHeight = doc.documentElement.offsetHeight;
		} 
        if (!doc.documentElement || (scrollHeight == 150 && offsetHeight == 150)) {
    		scrollHeight = doc.body.scrollHeight;
    		offsetHeight = doc.body.offsetHeight;
        }
		if (scrollHeight >= offsetHeight)
			frame.height = scrollHeight;
		else
			frame.height = offsetHeight;
		return true;
	}
	return false;
}

function copyIFrameHTML(frameId, elementId) {
    var frame = getElementById(frameId);
    var element = getElementById(elementId);
    if (frame && element) {
    	element.innerHTML = frame.contentWindow.document.body.innerHTML;
    	frame.className = "hide";
    	return true;
    }
    return false;
}

function setInnerHTML(elementId, html) {
    var element = getElementById(elementId);
    if (element) {
        element.innerHTML = html;
        return true;
   	}
   	return false;
}

function hideBlockIfEmpty(blockId) {
    var elementToCheck = getElementById(blockId + "-container");
    if (elementToCheck && trim(elementToCheck.innerHTML) == "") {
    	var title = getElementById(blockId + "-title");
    	var container = getElementById(blockId + "-container");
    	if (title || container) {
    		if (title)
    			title.className = "hide";
    		if (container)
    			container.className = "hide";
        	return true;
    	}
   	}
   	return false;
}

function setClassNameIfNotEmpty(elementIdToCheck, elementIdToSet, className) {
    var elementToCheck = getElementById(elementIdToCheck);
    if (elementToCheck && trim(elementToCheck.innerHTML) != "") {
    	var elementToSet = getElementById(elementIdToSet);
    	if (elementToSet) {
    		elementToSet.className = className;
        	return true;
    	}
   	}
   	return false;
}

function link(url) {
	window.opener.location=url;
	window.opener.focus();
	return false;
}

function getQueryStringParameter(name) {
	var value = false;
	var re = new RegExp('[&?]' + name + '=([^&]*)');
	if (window.location.href.match(re))
		value = RegExp.$1;
	return value;
}

function MouseDown(imgName, imgFile)
{
        if (document.images) {
        document[imgName].src = imgFile;
		}
}

function MouseUp(imgName, imgFile) {
	    if (document.images) {
        document[imgName].src = imgFile;
        }

}

function MouseOver(imgName, imgFile) {
	    if (document.images) {
        document[imgName].src = imgFile;
        }

}

function MouseOut(imgName, imgFile) {
	    if (document.images) {
        document[imgName].src = imgFile;
        }

}

// Provide the XMLHttpRequest class for IE 5.x-6.x:
// Other browsers (including IE 7.x-8.x) ignore this
//   when XMLHttpRequest is predefined
if (typeof XMLHttpRequest == "undefined") {
	XMLHttpRequest = function () {
		try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
  		catch (e1) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
  		catch (e2) {}
		try { return new ActiveXObject("Msxml2.XMLHTTP"); }
  		catch (e3) {}
		try { return new ActiveXObject("Microsoft.XMLHTTP"); }
  		catch (e4) {}
		throw new Error("This browser does not support XMLHttpRequest.");
	};
}

//
// Validate the file extension of the upload order file
//
var validFilesTypes=["xlsx","xls"];
function checkFileExt(form){
	var path;
	var file = document.getElementById('leftNavInputFile');
	if (file) {
		path = file.value;
	}
	if (!path) {
		file = document.getElementById('rightNavInputFile');
		path = file.value;
	}
	var ext=path.substring(path.lastIndexOf(".")+1,path.length).toLowerCase();
	var isValidFile = false;
	for (var i=0; i<validFilesTypes.length; i++){                
        if (ext==validFilesTypes[i])
        {
        isValidFile=true;
        break;
        }
    }
	if (!isValidFile) {
        alert("Please Upload Excel File");
        } 
	else {
		form.dispatch.value="checkCart";
		form.submit();
	}
     
}

function confirmUploadExcelOrder(okAction, cancelAction, msg) {	
	var submitForm = window.document.createElement("form");
	window.document.getElementsByTagName('body')[0].appendChild(submitForm);
	submitForm.setAttribute('method',"POST");
	if (confirm(msg)) {		
		submitForm.setAttribute('action',okAction);
	} else {
		submitForm.setAttribute('action',cancelAction);
	}	
	submitForm.submit();      
}

//
// These implements the trim(), ltrim() and rtrim() functions for IE
//
if(typeof String.prototype.trim !== 'function') {
	String.prototype.trim = function() {
	     return this.replace(/^\s+|\s+$/g,""); 
	} 
}

if(typeof String.prototype.ltrim !== 'function') {
	String.prototype.ltrim = function() {
	     return this.replace(/^\s+/,""); 
	} 
}

if(typeof String.prototype.rtrim !== 'function') {
	String.prototype.rtrim = function() {
	     return this.replace(/\s+$/,""); 
	} 
}

