﻿function GetElement(id, tagName)
{
	if(id != null)
	{
		//if(debug){alert("document.getElementById: " + (document.getElementById ? true : false) + "\ndocument.all: " + (document.all ? true : false));}
		
		var element = null;
		if((document.getElementById ? true : false)){element = document.getElementById(id);}
		else if((document.all ? true : false)){element = document.all[id];}

		// if document.all and document.getElementById didnt work and a tagName is specified
		if(element == null && tagName != null)
		{
			if(debug){alert("Could not use getElementById\nID: " + id);}
			
			var elements = document.getElementsByTagName(tagName);			
			for(var i = 0; i < elements.length; i++)
			{
				var tempElement = elements[i];
				if(tempElement.id != null && tempElement.id == id){element = tempElement; break;}
			}
		}
		
		if(debug){alert("Element: " + element + "\nID: " + element.id);}
		
		return element;
	}else{ return null; }
}

function progressActivate(){
	var img = document.getElementById('imageProgress');
	if(img){
		img.src = '../../Pages/Images/progress.gif';
		img.style.display = 'inline';
	}
	if(document.getElementById('textProgress'))
		document.getElementById('textProgress').style.display = 'inline';
}

function progressDisableTags(tagName){
	var tag = document.getElementsByTagName(tagName);
	for(i=0;i<tag.length;i++)
		tag[i].disabled = true;
}

function HideTags(thetagName){
	var tag = document.getElementsByTagName(thetagName);
	for(i=0;i<tag.length;i++){tag[i].style.visibility = "hidden";}
}

function ShowTags(thetagName){
	var tag = document.getElementsByTagName(thetagName);
	for(i=0;i<tag.length;i++){tag[i].style.visibility = "";}
}

function DisablePupUpButtons(){
	var tag = document.getElementsByTagName("img");
	for(i=0;i<tag.length;i++){if(tag[i].id.indexOf("selectPopUp") != -1){tag[i].style.visibility = "hidden";}}
}

function EnablePupUpButtons(){
	var tag = document.getElementsByTagName("img");
	for(i=0;i<tag.length;i++){if(tag[i].id.indexOf("selectPopUp") != -1){tag[i].style.visibility = "";}}
}

function ValidateTextbox(textBoxID, amountPerPackage)
{
	var box = GetElement(textBoxID, "INPUT");
	if(box != null)
	{
		if(box.value == ""){return false;}
		else
		{
			if(IsNumeric(box.value))
			{
				var number = parseInt(box.value / amountPerPackage);
				if(number == (box.value / amountPerPackage))
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			return false;
		}
	}
	else{return false;}
}

function ValidateInput(e)
{
	var key = "";
	key = GetKeyCode(e);
	
	if(key != null)
	{
		if(key == 8 || key == 0){return true}
		else
		{
			var keychar = String.fromCharCode(key);
			return IsNumeric(keychar);
		}
		// backspace = 8
		// del = 0
	}
	else{return false;}
}

function IsNumeric(sText)
{
	if(sText != null && sText != "")
	{
		//var ValidChars = "0123456789.";
		var ValidChars = "0123456789";
		var IsNumber=true;
		var Char;

		for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				IsNumber = false;
			}
		}
		return IsNumber;
	}else{return false;}
}

function GetKeyCode(e)
{
	if (window.event)
		return window.event.keyCode;
	else if (e)
		return e.which;
	else
	return null;
}

// When client browser is IE6, this funtions puts a transparent layer over the whole
// window because IE6 will stop loading images when user clicks a "View more" button
function DimWindow()
{
	if($.browser.msie && navigator.userAgent.toLowerCase().indexOf("6.0") != -1)
	{
		if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX)
		{
			yScroll = window.innerHeight + window.scrollMaxY;
			xScroll = window.innerWidth + window.scrollMaxX;
			var deff = document.documentElement;
			var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
			var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
			xScroll -= (window.innerWidth - wff);
			yScroll -= (window.innerHeight - hff);
			} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
			yScroll = document.body.scrollHeight;
			xScroll = document.body.scrollWidth;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			yScroll = document.body.offsetHeight;
			xScroll = document.body.offsetWidth;
		}
		
		$(".firewall").css({height: (yScroll + 42), width: xScroll});
		$(".firewall").css({filter: "alpha(opacity=40)"});
		$(".firewall").show();
		
		$(".loadingdiv").css({width: xScroll, height: $(window).height()});
		$(".loadingImage").center();
		$(".loadingdiv").show();
	}
}

function OnInputEnterEvent(e, uniqueID, clientID)
{
	if (!e){var e = window.event;}
	
	if (e.keyCode){code = e.keyCode;}
	else if (e.which){code = e.which;}
	
	if(code == 13)
	{
		//alert("UniqueID: " + uniqueID + "\n" + "ClientID: " + clientID + "\n" + "Event: " + e.keyCode + "\n" + "IsButton: " + $("#" + clientID).is("input"));
		$("#" + clientID).click();
		return false;
	}
	return true;
}