
//alert('here');
//Popup Window function
function NewWindowOpen(url,name,width,height,resiz, scrollbar){
	var options = '';
	var winl = (screen.width - width) / 2;
	var wint = (screen.height - height) / 2;

	options = 'scrollbars=' + scrollbar + ',';
	options = options + 'width=' + width + ',';
	options = options + 'height=' + height + ',';
	options = options + 'top=' + wint + ',';
	options = options + 'left=' + winl + ',';
	options = options + 'resizable=' + resiz;
	
	if (name == 'export')
	{
		options = options + ',menubar=yes';
		
		if (window.popupWin && window.popupWin.open){
			window.popupWin.close();
		}
	}
	
	if (name == 'additem'){
		if (window.popupWin && window.popupWin.open){
			window.popupWin.close();
		}
	}
		
	popupWin = window.open (url, name, options);
	popupWin.opener.top.name = "opener";
	popupWin.window.focus();
}



function updateBottomFrame(aname){
	if (window.parent.frames[1].location.href != 'frm_orderbottomframe.aspx?sname=' + aname)
	{
		window.parent.frames[1].location.href = 'frm_orderbottomframe.aspx?sname=' + aname;
	}
	else
	{
		window.parent.frames[1].location.reload();
	}
	
	window.parent.document.frm_ordertop.Itemcode.focus();
	
}


function confirmRedirect(msg,url){
	if (confirm(msg)){
		location.href = url;
	}
}


function clean_for_num_only(val_entered){
	var counter = 0;
	var retVal = "";
	
	for(counter = 0; counter < val_entered.length; counter++){
			if(val_entered.charCodeAt(counter) > 47 && val_entered.charCodeAt(counter) < 58){
				retVal += val_entered.charAt(counter);
			}
	}
	
	return retVal;
}

function count_numbers(filter_mask){
	var retVal = 0;
	var counter = 0;
	for(counter = 0; counter < filter_mask.length; counter++){
		
		if(filter_mask.charAt(counter) == '#'){
			retVal += 1;
		}
	}	
	return retVal;
}

function masked_filter (key, textbox, filter_mask){
	var val_entered = "";
	var new_val = "";
	var current_entered_index = 0;
	var filter_index = 0;

	if(key != 8 && key != 9 && key != 35 && key != 36 && key != 46
	&& key != 37 && key != 38 && key != 39 && key != 40 && key != 13 && key != 16){
		if( key > 95 && key < 106 ){
			key = key - 48;	
		}

		val_entered = clean_for_num_only(textbox.value + String.fromCharCode(key));
		if(val_entered.length <= count_numbers(filter_mask) && val_entered.length > 0){
			for(filter_index = 0; filter_index < filter_mask.length; filter_index++){
				if(filter_mask.charAt(filter_index) == '#'){
					new_val += val_entered.charAt(current_entered_index++);
				}else{
					new_val += filter_mask.charAt(filter_index);
				}	
			}

			textbox.value = new_val;
		}		
		return false;
	}else{
		
		return true
	}	
}

function replace(fullString,text,by) {
    var strLength = fullString.length;
    var txtLength = text.length;
    
    if(strLength == 0 || txtLength == 0){
		return fullString;
	}

    var i = fullString.indexOf(text);
    if(!i && text != fullString.substring(0,txtLength)){
		return fullString;
	}
    if(i == -1){
		return fullString;
	}

    var newstr = fullString.substring(0,i) + by;

    if(i + txtLength < strLength){
        newstr += replace(fullString.substring(i + txtLength, strLength), text, by);
    }

    return newstr;
}



function intOnly(key, textbox, bAllowNegatives){
	var modkeys_pressed = false;
	var ikeycode;
	var isNav = (navigator.appName.indexOf("Netscape") != -1) || (navigator.appName.indexOf("Mozilla") != -1) || (navigator.appName.indexOf("Opera") != -1);
	var isIE = (navigator.appName.indexOf("Microsoft") != -1);
	
	if (isIE){
		ikeycode = window.event.keyCode;
		modkeys_pressed = window.event.shiftKey;
	}
	if (isNav){
		ikeycode = evt.which;
		modkeys_pressed = e.modifiers & Event.SHIFT_MASK;
	}
	
	//alert(bAllowNegatives);
	return ((ikeycode > 47 && ikeycode < 58 && !modkeys_pressed) 
	|| (ikeycode == 109 && window.event.srcElement.value.indexOf("-") == -1 && bAllowNegatives) || 
	(ikeycode > 95 && ikeycode < 106 && !modkeys_pressed) || ikeycode == 8 || ikeycode == 46 || ikeycode == 37 || ikeycode == 39 || (ikeycode == 189 && window.event.srcElement.value.indexOf("-") == -1 && bAllowNegatives) || ikeycode == 36 || ikeycode == 35 || ikeycode == 9);

}

function limit_entry_already_entered(textbox, maxlength){
	if(textbox.value.length > maxlength){
		textbox.value = textbox.value.substring(0, maxlength);
	}
}

function IsNumeric(sText){
	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;
}

function CurrencyFormatted(amount){
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function confirm_underpay(){
	return "This order has been underpaid.  Click OK to Continue saving the Order, Click Cancel to stay at the payments screen.";
}
function confirm_overpay(){
	return "This order has been overpaid.  Click OK to Continue saving the Order, Click Cancel to stay at the payments screen.";
}

function set_drop_down_value(drop_down_control, val) {
    var found = 0;
    if(drop_down_control){
        val = strim(val.toLowerCase());
        for(var i = 0; i < drop_down_control.options.length; i++ ){
            drop_down_control.options[i].selected = false;
        }
        for(var i = 0; i < drop_down_control.options.length && found == 0; i++ ){
            if (strim(drop_down_control.options[i].value.toLowerCase()) == val) {
                drop_down_control.options[i].selected = true;
			    found = 1;
            }
        }
	    if(found == 0){
	        drop_down_control.options[0].selected = true;
	    }
	}
}


 
function ScrollableTable (tableEl, tableHeight, tableWidth) {
 
	this.initIEengine = function () {
 
		this.containerEl.style.overflowY = 'auto';
		if (this.tableEl.parentElement.clientHeight - this.tableEl.offsetHeight < 0) {
			this.tableEl.style.width = this.newWidth - this.scrollWidth +'px';
		} else {
			this.containerEl.style.overflowY = 'hidden';
			this.tableEl.style.width = this.newWidth +'px';
		}
 
		if (this.thead) {
			var trs = this.thead.getElementsByTagName('tr');
			for (x=0; x<trs.length; x++) {
				trs[x].style.position ='relative';
				trs[x].style.setExpression("top",  "this.parentElement.parentElement.parentElement.scrollTop + 'px'");
			}
		}
 
		if (this.tfoot) {
			var trs = this.tfoot.getElementsByTagName('tr');
			for (x=0; x<trs.length; x++) {
				trs[x].style.position ='relative';
				trs[x].style.setExpression("bottom",  "(this.parentElement.parentElement.offsetHeight - this.parentElement.parentElement.parentElement.clientHeight - this.parentElement.parentElement.parentElement.scrollTop) + 'px'");
			}
		}
 
		eval("window.attachEvent('onresize', function () { document.getElementById('" + this.tableEl.id + "').style.visibility = 'hidden'; document.getElementById('" + this.tableEl.id + "').style.visibility = 'visible'; } )");
	};
 
 
	this.initFFengine = function () {
		this.containerEl.style.overflow = 'hidden';
		this.tableEl.style.width = this.newWidth + 'px';
 
		var headHeight = (this.thead) ? this.thead.clientHeight : 0;
		var footHeight = (this.tfoot) ? this.tfoot.clientHeight : 0;
		var bodyHeight = this.tbody.clientHeight;
		var trs = this.tbody.getElementsByTagName('tr');
		if (bodyHeight >= (this.newHeight - (headHeight + footHeight))) {
			this.tbody.style.overflow = '-moz-scrollbars-vertical';
			for (x=0; x<trs.length; x++) {
				var tds = trs[x].getElementsByTagName('td');
				tds[tds.length-1].style.paddingRight += this.scrollWidth + 'px';
			}
		} else {
			this.tbody.style.overflow = '-moz-scrollbars-none';
		}
 
		var cellSpacing = (this.tableEl.offsetHeight - (this.tbody.clientHeight + headHeight + footHeight)) / 4;
		this.tbody.style.height = (this.newHeight - (headHeight + cellSpacing * 2) - (footHeight + cellSpacing * 2)) + 'px';
 
	};
 
	this.tableEl = tableEl;
	this.scrollWidth = 16;
 
	this.originalHeight = this.tableEl.clientHeight;
	this.originalWidth = this.tableEl.clientWidth;
 
	this.newHeight = parseInt(tableHeight);
	this.newWidth = tableWidth ? parseInt(tableWidth) : this.originalWidth;
 
	this.tableEl.style.height = 'auto';
	this.tableEl.removeAttribute('height');
 
	this.containerEl = this.tableEl.parentNode.insertBefore(document.createElement('div'), this.tableEl);
	this.containerEl.appendChild(this.tableEl);
	this.containerEl.style.height = this.newHeight + 'px';
	this.containerEl.style.width = this.newWidth + 'px';
 
 
	var thead = this.tableEl.getElementsByTagName('thead');
	this.thead = (thead[0]) ? thead[0] : null;
 
	var tfoot = this.tableEl.getElementsByTagName('tfoot');
	this.tfoot = (tfoot[0]) ? tfoot[0] : null;
 
	var tbody = this.tableEl.getElementsByTagName('tbody');
	this.tbody = (tbody[0]) ? tbody[0] : null;
 
	if (!this.tbody) return;
 
	if (document.all && document.getElementById && !window.opera) this.initIEengine();
	if (!document.all && document.getElementById && !window.opera) this.initFFengine();
 
 
}