function formatcurrency(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 getElement(element) {
	if (document.getElementById) {
		return document.getElementById(element);
	}	
	else if (document.all) {
		return document.all(element);
	}
	else if (document.layers){
		return document.layers[element];
	}
	else {
		return null;
	}
}

function getDocElement(doc, element) {
	if (doc.getElementById) {
		return doc.getElementById(element);
	}	
	else if (doc.all) {
		return doc.all(element);
	}
	else if (doc.layers){
		return doc.layers[element];
	}
	else {
		return null;
	}
}

function setSpanText(id, text) {
	var spanObj = getElement(id);
	spanObj.innerHTML = text;
}

function changeCountry(akey, aval, theform, prefix) {
	var elname = "document." + theform.name + "." + prefix + "stateid";
	var selobj = eval(elname);

	for(j=selobj.length; j >= 0; j--) {
		selobj.options[j] = null;
	}

	selobj.options.length = 0;
	for(i=0; i < akey.length; i++) {
		selobj.options[i] = new Option( aval[i], akey[i] );
	}
}

function addressSetShippingSame(theform, el) {
	var aFields = Array('company','fname','minit','lname','title','address1','address2','city','countryid','stateid','zip');
	if(el.checked) {
		for(i=0; i < aFields.length; i++) {
			var billelement = 'document.' + theform.name + '.bill' + aFields[i];
			var shipelement = 'document.' + theform.name + '.ship' + aFields[i];
			var billobj = eval(billelement);
			var shipobj = eval(shipelement);
			if(billobj != null && shipobj != null) {
				shipobj.value = billobj.value;
			}
		}
	}
}

function borderit(which,color){
	//if IE 4+ or NS 6+
	if (document.all||document.getElementById){
		which.style.borderColor=color;
	}
}

function openwin(htmlFile, win_width, win_height) {
	thewin=open("","NewWindow","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width="+win_width+",height="+win_height);
	thewin.location = htmlFile;
}

function cvv2info() {
	msg =       "If you are using a Visa or MasterCard, please provide the 3-digit CVV\n";
	msg = msg + "(Customer Verification Value). This is the number printed on\n";
	msg = msg + "the signature area on the back of the card following the Visa\n"; 
	msg = msg + "or MC card account number.\n\n";
	
	msg = msg + "If your credit card is an American Express card please provide the\n";
	msg = msg + "4-digit CID (Confidential Identifier Number). This is the 4 digit number\n";
	msg = msg + "printed above your account number on the front of your card.\n";

	alert(msg);
}

function trim ( s ) {
	s = s.replace( /^\s*/, "" );
	s = s.replace( /\s*$/, "" );
	return s;
}

function textareaCount(field, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
}

function mfgSearch(theform) {
	var aPostVars = new Array();
	
	$('#processingsearch').show();

	var oTmp = getElement('partnummulti');
	if(oTmp != null) {
		aPostVars.push( 'searchvalue[partnummulti]=' + encodeURIComponent(oTmp.value) );
	}
	var oTmp = getElement('mfg');
	if(oTmp != null) {
		aPostVars.push( 'searchvalue[mfg]=' + encodeURIComponent(oTmp.value) );
	}
	var oTmp = getElement('description');
	if(oTmp != null) {
		aPostVars.push( 'searchvalue[description]=' + encodeURIComponent(oTmp.value) );
	}
	var oPrice1 = getElement('price1');
	var oPrice2 = getElement('price2');
	if(oPrice1 != null && oPrice2 != null) {
		aPostVars.push( 'searchvalue[price]=' + encodeURI(oPrice1.value) + '|' + encodeURI(oPrice2.value) );
	}
	var oTmp = getElement('datecode');
	if(oTmp != null) {
		aPostVars.push( 'searchvalue[datecode]=' + encodeURIComponent(oTmp.value) );
	}
	var oTmp = getElement('rohs');
	if(oTmp != null) {
		if(theform.rohs[0].checked) {
			aPostVars.push( 'searchvalue[rohs]=1' );
		}
		if(theform.rohs[1].checked) {
			aPostVars.push( 'searchvalue[rohs]=0' );			
		}
	}
	var oTmp = getElement('milspec');
	if(oTmp != null) {
		if(theform.milspec[0].checked) {
			aPostVars.push( 'searchvalue[milspec]=1' );
		} 
		if(theform.milspec[1].checked) {
			aPostVars.push( 'searchvalue[milspec]=0' );			
		}
	}
	
	var oReg1 = getElement('region1');
	if(oReg1 != null) {
		if(oReg1.checked) {
			aPostVars.push( 'region1=' + encodeURI(oReg1.value) );
		}
	}
	var oReg2 = getElement('region2');
	if(oReg2 != null) {
		if(oReg2.checked) {
			aPostVars.push( 'region2=' + encodeURI(oReg2.value) );
		}
	}
	var oReg3 = getElement('region3');
	if(oReg3 != null) {
		if(oReg3.checked) {
			aPostVars.push( 'region3=' + encodeURI(oReg3.value) );
		}
	}
	var oTmp = getElement('inva');
	if(oTmp != null) {
		if(oTmp.value == "1") {
			aPostVars.push('inva=1');
		}
	}
	ajaxPOST("ajax/mfggrids.php", "", "MfgInventorySearchGrid", aPostVars, true);	
}

function toggleAdvancedSearch() {
	var oMoreLess = getElement('advancedsearchmoreless');
	var oAdvSearch = getElement('advancedsearchform');

	if(oMoreLess != null && oAdvSearch != null) {
		if( oAdvSearch.style.display == 'none') {
			$( '#advancedsearchform' ).slideDown(500);
			oMoreLess.innerHTML = 'LESS';
		} else {
			oMoreLess.innerHTML = 'MORE';
			$( '#advancedsearchform' ).slideUp(500);		
		}
	}
}

function rfqDeleteToggle(chked) {
	$("input[name=adel\\[\\]]").each(function() {
		this.checked = chked;
	});
}

function mfgSearchRFQToggle(chked) {
	var aPostVars = new Array();
	count = 1;
	var chkbox = "rfq" + count;
	objChkbox = getElement("rfq" + count);
	objGridRow = getElement("grid" + count);
	while(objChkbox != null) {
		var prevChked = objChkbox.checked;
		objChkbox.checked = chked;
		if( objGridRow != null ) {
			var isavail = false;
			var oInvAvail = getElement('inva');
			if(oInvAvail != null) {
				if(oInvAvail.value == "1") {isavail = true;}
			}

			if(chked) {
				if(isavail) {
					objGridRow.className = 'availgridSEL';
				} else {
					objGridRow.className = 'gridSEL';					
				}
				if(!prevChked) {
					aPostVars.push( "inventoryid=" + encodeURI(objChkbox.value) );
					if(oInvAvail != null) {
						aPostVars.push( 'inva=' + encodeURI(oInvAvail.value) );
					}
					ajaxPOST("ajax/mfgcart.php", "action=add", "MfgMiniCart", aPostVars, true);
				}
			} else {
				if(isavail) {
					objGridRow.className = 'availgrid' + (count%2);
				} else {
					objGridRow.className = 'grid' + (count%2);
				}
				aPostVars.push( "inventoryid=" + encodeURI(objChkbox.value) );
				ajaxPOST("ajax/mfgcart.php", "action=del", "MfgMiniCart", aPostVars, true);
			}
		}
		count++;
		objChkbox = getElement("rfq" + count);
		objGridRow = getElement("grid" + count);
	}
}

function mfgSearchRFQClick(gridid, el) {
	var aPostVars = new Array();
	objGridRow = getElement("grid" + gridid);
	var isavail = false;
	var oInvAvail = getElement('inva');
	if(oInvAvail != null) {
		if(oInvAvail.value == "1") {isavail = true;}
	}	

	var oRfqSubmit1 = getElement('rfqsubmit1');
	var oRfqSubmit2 = getElement('rfqsubmit2');

	if(el.checked) {
		if(objGridRow != null) {
			if(isavail) {
				objGridRow.className = 'availgridSEL';				
			} else {
				objGridRow.className = 'gridSEL';
			}
		}
		
		if(oRfqSubmit1 != null) {
			oRfqSubmit1.style.color = '#FFFF00';
			oRfqSubmit1.style.fontWeight = 'bold';
		}
		if(oRfqSubmit2 != null) {
			oRfqSubmit2.style.color = '#FFFF00';
			oRfqSubmit2.style.fontWeight = 'bold';
		}
		
		aPostVars.push( "inventoryid=" + encodeURI(el.value) );
		if(oInvAvail != null) {
			aPostVars.push( 'inva=' + encodeURI(oInvAvail.value) );
		}
		ajaxPOST("ajax/mfgcart.php", "action=add", "MfgMiniCart", aPostVars, true);
	} else {
		if(objGridRow != null) {
			if(isavail) {
				objGridRow.className = 'availgrid' + (gridid%2);
			} else {
				objGridRow.className = 'grid' + (gridid%2);				
			}
			aPostVars.push( "inventoryid=" + encodeURI(el.value) );
			if(oInvAvail != null) {
				aPostVars.push( 'inva=' + encodeURI(oInvAvail.value) );
			}
			ajaxPOST("ajax/mfgcart.php", "action=del", "MfgMiniCart", aPostVars, true);
		}
	}
}

function cartDeleteItem(theform, orderdetailid) {
	if(orderdetailid != "") {
		var resp = confirm('Please confirm to delete this record.');
		if(resp) {
			theform.del.value = orderdetailid;
			theform.submit();
		}
	}
}

function handleEnterOnForm(theform, elGoBtn) {
	if( event.keyCode == 13 ) {
		elGoBtn.click();
		return false;
	} else {
		return true;
	}
}

function handleEnterOnMyInvSearch(theform) {
	if( event.keyCode == 13 ) {
		theform.btnGO.click();
		return false;
	} else {
		return true;
	}
}

function mfgMyInvSearch(theform) {
	var search = trim(theform.search.value);
	var aPostVars = new Array();
	aPostVars.push( 'search=' + encodeURIComponent(search) );

	ajaxPOST("ajax/mfggrids.php", "", "MfgMyInventoryGrid", aPostVars, true);
}

function admMfgSearch(theform) {
	var search = trim(theform.search.value);
	var aPostVars = new Array();
	aPostVars.push( 'search=' + encodeURIComponent(search) );

	ajaxPOST("ajax/admingrids.php", "", "AdminMfgGrid", aPostVars, true);
}

//var gTimerNewrfq = null;
function newRFQNotify() {
	newrfqObj = getElement('newrfq');
	if(newrfqObj != null) {
		if( newrfqObj.innerHTML == '' ) {
			ajax('ajax/mfggrids.php','', 'newrfq', true);
		}
	}

	//if(gTimerNewrfq == null) {
	//	gTimerNewrfq = setInterval("newRFQNotify()", 20000);
	//}
}

/*
function mfgPaymentTypeInfoChange(el) {
	ptObj = getElement('divPaymentTypeInfo');
	if(ptObj != null) {
		if( el.value == '') {
			ptObj.style.display = 'none';			
		} else {
			ptObj.style.display = '';
			ajax('ajax/mfgcheckout.php','paymenttype=' + el.value, 'MfgPaymentTypeInfo', true);
		}
	}
}
*/
/*
function mfgShippingTypeInfoChange(el) {
	ptObj = getElement('divShipTypeInfo');
	if(ptObj != null) {
		if( el.value == '') {
			ptObj.style.display = 'none';			
		} else {
			ptObj.style.display = '';
			ajax('ajax/mfgcheckout.php','shiptype=' + el.value, 'MfgShippingTypeInfo', true);
		}
	}
}
*/

//function showMfgInfo(theid) {
//	obj = getElement(theid);
//	if(obj != null) {
//		obj.style.display = '';
//	}
//}

//var isIE = document.all;
//var mouseX = 0;
//var mouseY = 0;
//function getMouseXY(e) { 
//	if (!e) e = window.event;
//	if (e) { 
//		mouseX = isIE ? (e.clientX + document.body.scrollLeft) : e.pageX;
//		mouseY = isIE ? (e.clientY + document.body.scrollTop) : e.pageY;
//	}
//}
//document.onmousemove = getMouseXY;

/*
function showMfgInfo(html) {
	var theid = 'vendorINFO';
	obj = getElement(theid);
	if(obj != null) {
		obj.style.display = '';
		obj.innerHTML = '<div style=\'float:right;padding-right:3px;\'><a onClick=\'hideMfgInfo();\' href="javascript:;"><b>X</b></a></div>' + html;

		//var x = mouseX - parseInt(obj.style.width) + 50;
		var y = mouseY + 10;

		//obj.style.left = x + "px"; 
		obj.style.right = "100px";
		obj.style.top = y + "px";

	}
}
*/

//function hideMfgInfo(theid) {
//	obj = getElement(theid);
//	if(obj != null) {
//		obj.style.display = 'none';
//	}
//}
/*
function hideMfgInfo() {
	var theid = 'vendorINFO';
	obj = getElement(theid);
	if(obj != null) {
		obj.style.display = 'none';
	}
}
*/

//function handleEnterOnSearch(theform) {
//	if( event.keyCode == 13 ) {
//		if(theform.mfgsearchfields.value == 'partnummulti') {
//			return true;
//		} else {
//			theform.btnGO.click();
//			return false;
//		}
//	} else {
//		return true;
//	}
//}

/*
var gaFilter = new Array();
var gaFilterRange = new Array();
function mfgSearchFilterClick(searchfield) {
	var searchvalue = '';

	// remove from filter if field already exists
	for(j = 0; j < gaFilter.length; j++) {
		if( gaFilter[j][0] == searchfield ) {
			gaFilter.splice(j, 1);
			break;
		}
	}
	for(j = 0; j < gaFilterRange.length; j++) {
		if( gaFilterRange[j][0] == searchfield ) {
			gaFilterRange.splice(j, 1);
			break;
		}
	}

	if(searchfield == 'rohs') {
		if( getElement('searchvalue0').checked ) {
			searchvalue = getElement('searchvalue0').value;
		} else if( getElement('searchvalue1').checked ) {
			searchvalue = getElement('searchvalue1').value;
		}
		gaFilter.push( Array(searchfield, searchvalue) );
	} else if(searchfield == 'milspec') {
		if( getElement('searchvalue0').checked ) {
			searchvalue = getElement('searchvalue0').value;
		} else if( getElement('searchvalue1').checked ) {
			searchvalue = getElement('searchvalue1').value;
		}
		gaFilter.push( Array(searchfield, searchvalue) );
	} else if(searchfield == 'price') {
		objSearchValue1 = getElement('searchvalue1');
		objSearchValue2 = getElement('searchvalue2');
		if( objSearchValue1 != null && objSearchValue2 != null ) {
			searchvalue1 = trim(objSearchValue1.value);
			searchvalue2 = trim(objSearchValue2.value);
			gaFilterRange.push( Array(searchfield, searchvalue1, searchvalue2) );
		}		
	} else {
		objSearchValue = getElement('searchvalue');
		if( objSearchValue != null) {
			searchvalue = trim(objSearchValue.value);
			gaFilter.push( Array(searchfield, searchvalue) );
		}
	}

	mfgSearchRefreshFilter();
}
*/
/*
function mfgSearchRefreshFilter() {
	var aPostVars = new Array();
	for(j = 0; j < gaFilter.length; j++) {
		aPostVars.push( 'searchvalue[' + gaFilter[j][0] + ']=' + encodeURIComponent(gaFilter[j][1]) );
	}
	for(j = 0; j < gaFilterRange.length; j++) {
		aPostVars.push( 'searchvalue[' + gaFilterRange[j][0] + ']=' + encodeURI(gaFilterRange[j][1]) + '|' + encodeURI(gaFilterRange[j][2]) );
	}

	var oReg1 = getElement('region1');
	if(oReg1 != null) {
		if(oReg1.checked) {
			aPostVars.push( 'region1=' + encodeURI(oReg1.value) );
		}
	}
	var oReg2 = getElement('region2');
	if(oReg2 != null) {
		if(oReg2.checked) {
			aPostVars.push( 'region2=' + encodeURI(oReg2.value) );
		}
	}
	var oReg3 = getElement('region3');
	if(oReg3 != null) {
		if(oReg3.checked) {
			aPostVars.push( 'region3=' + encodeURI(oReg3.value) );
		}
	}

	var oInvAvail = getElement('inva');
	if(oInvAvail != null) {
		aPostVars.push( 'inva=' + encodeURI(oInvAvail.value) );
	}
	
	ajaxPOST("ajax/mfggrids.php", "", "MfgInventorySearchFilterList", aPostVars, true);
	ajaxPOST("ajax/mfggrids.php", "", "MfgInventorySearchGrid", aPostVars, true);	
}
*/
/*
function mfgSearchRemoveFromFilter(searchfield) {

	for(j = 0; j < gaFilter.length; j++) {
		if( gaFilter[j][0] == searchfield ) {
			gaFilter.splice(j, 1);
			break;
		}
	}

	for(j = 0; j < gaFilterRange.length; j++) {
		if( gaFilterRange[j][0] == searchfield ) {
			gaFilterRange.splice(j, 1);
			break;
		}
	}
	
	mfgSearchRefreshFilter();
}
*/