/*********************************************************
**
** check quantity. then update all totals
**
*/
function quantitycheck(qty, nlid, xp0, xp1, xp2, xp3, xp4, xq1, xq2, xq3, xq4, xp5, xp6){
/*
qty = the id of the product
xp0 = retail price
xp1 to xp4 = price of bulktype 1 to 4
xq1 to xq4 = quantity needed to acheive bulktype 1 to 4
xp5 & xp6 = bv & pv */
var productq = document.getElementById(qty).value //passed quantity value
var bulktype = 0
var totalcost = 0
var qtyorder
var e //element
var xp = new Array()
var xq = new Array()

xp[0] = parseFloat(xp0)
xp[1] = parseFloat(xp1)
xp[2] = parseFloat(xp2)
xp[3] = parseFloat(xp3)
xp[4] = parseFloat(xp4)
xq[1] = parseFloat(xq1)
xq[2] = parseFloat(xq2)
xq[3] = parseFloat(xq3)
xq[4] = parseFloat(xq4)
xp[5] = parseFloat(xp5)
xp[6] = parseFloat(xp6)

bulktype = findbulktype(xq[1], xq[2], xq[3], xq[4])

//reset default color for retail and bulk quantities
for(i = 0; i <= 4 ; i++) document.getElementById(nlid+'colorpq'+i).style.color='#000000'

if(!IsNumeric(productq)) productq = '' //clear input boxes if invalid keystrokes
	

if(productq.length > 3){ //Reduce order to less than 1000 units
	document.getElementById(qty).value = Left(productq, 3)
	productq = Left(productq, 3)
}

if(Left(productq, 1) <= 0) { //If start with 0, revert
	document.getElementById(qty).value = ''
	
}

result = findtotalcost(productq, xp[1], xp[2], xp[3], xp[4], xq[1], xq[2], xq[3], xq[4], bulktype)

// color the bulk price
switch(bulktype)
{
	case 1:
	document.getElementById(nlid+'colorpq1').style.color='#FF0000';
	break
	
	case 2:
	if (productq < xq[2])	document.getElementById(nlid+'colorpq1').style.color='#FF0000';
	else document.getElementById(nlid+'colorpq2').style.color='#FF0000';
	break
	
	case 3:
	if(productq < xq[2]) document.getElementById(nlid+'colorpq1').style.color='#FF0000';
	else if (productq < xq[3]) document.getElementById(nlid+'colorpq2').style.color='#FF0000';
	else document.getElementById(nlid+'colorpq3').style.color='#FF0000';
	break
	
	case 4:
	if(productq < xq[2]) document.getElementById(nlid+'colorpq1').style.color='#FF0000';
	else if (productq < xq[3]) document.getElementById(nlid+'colorpq2').style.color='#FF0000';
	else if (productq < xq[4]) document.getElementById(nlid+'colorpq3').style.color='#FF0000';
	else document.getElementById(nlid+'colorpq4').style.color='#FF0000';
	break
}

	if(result != 0){
		//put result for the total cost, bv and pv for each individual items
		document.getElementById(nlid+'totalcost').innerHTML = formatCurrency(result)
		document.getElementById(nlid+'totalbv').innerHTML = formatCurrency(productq*xp[5])
		document.getElementById(nlid+'totalpv').innerHTML = formatCurrency(productq*xp[6])
		
		qtyorder = document.getElementById(nlid+'qty')
		if(qtyorder){
		qtyorder.value = productq
		}
		
		qtyorder = document.getElementById(nlid+'qty2')
		if(qtyorder){
		qtyorder.value = productq
		}

		updateGrandTotal()
	}else{
		document.getElementById(nlid+'totalcost').innerHTML = ''
		document.getElementById(nlid+'totalbv').innerHTML = ''
		document.getElementById(nlid+'totalpv').innerHTML = ''
		
		e = document.getElementById(nlid+'qty')
		if(e) e.value = ''
		
		e = document.getElementById(nlid+'qty2')
		if(e) e.value = ''
		
		updateGrandTotal()
		for(i = 0; i <= 4 ; i++) document.getElementById(nlid+'colorpq'+i).style.color='';
	}
	
confirmOrder(document.getElementById('allnlid').value)	
}

/////////////////////////////////////////////////////////////////////////
// RETURN BULK TYPE
function findbulktype(xq1, xq2, xq3, xq4){
var bulktype = 0
if(xq1 > 0) bulktype++
if(xq2 > 0) bulktype++
if(xq3 > 0) bulktype++
if(xq4 > 0) bulktype++
return bulktype
	}
	
/////////////////////////////////////////////////////////////////////////
// RETURN TOTAL COST
function findtotalcost(productq, xp1, xp2, xp3, xp4, xq1, xq2, xq3, xq4, bulktype){
	var result = 0
	xp1 *= 1; xp2 *= 1; xp3 *= 1; xp4 *= 1; xq1 *= 1; xq2 *= 1; xq3 *= 1; xq4 *= 1;
	switch(bulktype){
			case 1:
			result = productq * xp1
			break
			
			case 2:
			if (productq < xq2){
				result = productq * xp1
			}else{
				result = productq * xp2
			}
			break
			
			case 3:
			if(productq < xq2){
				result = productq * xp1
			}else if (productq < xq3){
				result = productq * xp2
			}else{
				result = productq * xp3
			}
			break
			
			case 4:
			if(productq < xq2){
				result = productq * xp1
			}else if (productq < xq3){
				result = productq * xp2
			}else if (productq < xq4){
				result = productq * xp3
			}else{
				result = productq * xp4
			}
			break
		}
		return result
	}
	
/////////////////////////////////////////////////////////////////////////
// UPDATE GRAND TOTAL
function updateGrandTotal(){
var allnlid = document.getElementById('allnlid').value.split(',')

//var grand = new Array()
var grand = new Array(0, 0, 0, 0) //totalcost, totalbv, totalpv, qty
var temp;
var i;
var hiddenvalues;
var bulktype = 0
var productq = 0
var e

for(i = 0; i <= allnlid.length ; i++){
	e = document.getElementById(allnlid[i]+'qty')
	if(e){ 
	productq = e.value
	productq *= 1
	for(j = 0; j <=3; j++) grand[j] *= 1
	
		hiddenvalues = document.getElementById(allnlid[i]+'values').value.split(",")
		bulktype = findbulktype(hiddenvalues[7], hiddenvalues[8], hiddenvalues[9], hiddenvalues[10])
	
		grand[0] += findtotalcost(productq, hiddenvalues[3], hiddenvalues[4], hiddenvalues[5], hiddenvalues[6], hiddenvalues[7], hiddenvalues[8], hiddenvalues[9], hiddenvalues[10], bulktype)
		grand[1] += productq * hiddenvalues[11]
		grand[2] += productq * hiddenvalues[12]
		grand[3] += productq		
	}
}

for(i = 1; i <= 3; i++){
	e = document.getElementById('grandvalue'+i)
	if(e) e.innerHTML = formatCurrency(grand[0])
	
	e = document.getElementById('grandbv'+i)
	if(e) e.innerHTML = formatCurrency(grand[1])
	
	e = document.getElementById('grandpv'+i)
	if(e) e.innerHTML = formatCurrency(grand[2]) 
	
	e = document.getElementById('granditems'+i)
	if(e) e.innerHTML = grand[3]+' item(s)'
}

}	

/////////////////////////////////////////////////////////////////////////
// Format Currency
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
//return (((sign)?'':'-') + '$' + num + '.' + cents);
return (((sign)?'':'-') + num + '.' + cents);
}

/////////////////////////////////////////////////////////////////////////
// Is Numeric
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;   
}

/////////////////////////////////////////////////////////////////////////
// Left of a String
function Left(str, n){
	if (n <= 0) return "";
	else if (n > String(str).length) return str;
	else return String(str).substring(0,n);
}

/////////////////////////////////////////////////////////////////////////
// Right of a String
function Right(str, n){
    if (n <= 0) return "";
    else if (n > String(str).length) return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

/*************************************
** ALLOWS NUMBERS AND DECIMALS ONLY **
*************************************/
function NumericDecimal(id){
	var v = document.getElementById(id).value	
	document.getElementById(id).value = v.replace(/[^0-9\.]/g, '');	
	}
	
/************************
** ALLOWS NUMBERS ONLY **
************************/
function NumericOnly(id){
	var v = document.getElementById(id).value
	var ValidChars = "0123456789";
	var Char;
	
	for(i = 0; i < v.length; i++){
		Char = v.charAt(i);
		if(ValidChars.indexOf(Char) == -1){
		document.getElementById(id).value = v.replace(/[^0-9]/g, '');
		}
		}
	
	}
	
/*************************************
** AUTO CHANGE TO UPPERCASE **
*************************************/
function UpperCaseOnly(id){
	document.getElementById(id).value = document.getElementById(id).value.toUpperCase();
	}
	
	
/******************************************************
** GIVE RANDOM QUANTITY FOR MR. TRIGO'S SHOPPING BAG **
******************************************************/
function TrigoRandomShopping(nlid){
	var id = nlid.split(", ");
	for(i = 0; i < id.length; i++) document.getElementById(id[i]).value = Math.floor(Math.random()*101);
	}
	
/***************************
** LOAD THE LOADING IMAGE **
***************************/
function LoadingImg(thediv){
		document.getElementById(thediv).style.background = "#FFFFFF"
		document.getElementById(thediv).innerHTML = '<img src="/images/ajax_loader.gif">'
	}
	
/*******************************************************
** QUICK ORDERS TO PARSE PRODUCTS INTO THE ORDER FORM **
*******************************************************/
function quickOrders(orders){

		var i = 0
		var eachorder = orders.split("\n")
		var eachordersplit
		var ordername
		var orderqty
		var hiddenvalues
		var productname
		var qod //[q]uick [o]rder [d]escription
		var qo //[q]uick [o]rder
		var e //element
		var e2 //2nd element
		
		qo = document.getElementById('quickorders')
		//qo.value = orders.toUpperCase()

		qod = document.getElementById('quickordersdescription')
		qod.value = ''
		
		for(i = 0; i < eachorder.length; i++){
				//defaulted to 1 if no quantity is defined
				eachordersplit = eachorder[i].split(" ")
				ordername = eachordersplit[0].toUpperCase()
				orderqty = eachordersplit[1] * 1
				
				if(!orderqty) orderqty = 1
				
				e = document.getElementById(ordername+'qty')
				if(e){
				e.value = orderqty
				
				e2 = document.getElementById(ordername+'qty2')
				if(e2) e2.value = orderqty
				
				hiddenvalues = document.getElementById(ordername+'values').value.split(",")
				quantitycheck(ordername+'qty', hiddenvalues[0], hiddenvalues[2], hiddenvalues[3], hiddenvalues[4], hiddenvalues[5], hiddenvalues[6], hiddenvalues[7], hiddenvalues[8], hiddenvalues[9], hiddenvalues[10], hiddenvalues[11], hiddenvalues[12] )
				
				productname = hiddenvalues[1].replace(/^\s+|\s+$/g, '') //trim
				qod.value = qod.value + productname + ' * ' + orderqty + '\n' //display product name
				}
			
			}	
}

/*******************************************
** DISPLAY PRODUCT PICTURES ON MOUSE OVER **
*******************************************/
function productPictures(nlid, status){	
	if (!isIE()){
	LoadingImg('productPictures')
	pP = document.getElementById('productPictures')
	
	switch(status){
		case 1:
		if(!isFile("../../images/products/product-"+nlid+".png")) nlid = "NA"
		
		pP.innerHTML = "<img src='../images/products/product-"+nlid+".png' />"
		pP.style.left = "600px"
		pP.style.background = "#000000"
		pP.style.borderWidth = "thick"
		pP.style.borderStyle = "solid"
		pP.style.borderColor = "#004C8C"
		break;
		
		case 0:
		pP.innerHTML = ""
		pP.style.borderStyle = "none"
		break;
		}
	}
}

/***********************
** CONFIRM ORDER **
******************/
function confirmOrder(nlid){
	var eachnlid = nlid.split(',')
	var hiddenvalues
	
	var step = '<span class="hideable"><span class="orderquestion">Is Your Order Correct?</span><br><br><span class="step1">Easy 2 Steps Ordering System.</span> <span class="step2">Step: 1 &raquo; <span style="font-weight:bold;color:#FF0000;">2</span> &raquo; Finish!</span><br /></span>'
	
	var top = '<table class="displayproducts">'
	
	var head = '<tr>  <td class="header" width="55px">Code</td>  <td class="header" width="75px">Quantity</td>  <td class="header" width="300px">Product Name</td>  <td class="header" width="30px">Cost</td>  <td class="header" width="30px">BV</td>  <td class="header" width="30px">PV</td></tr>'
	
	var productquantity
	var pe = "" //[p]roduct[e]cho
	var pe_cis = "" //[p]roduct[e]cho unformatted for CIS system
	var te = "" //[t]otal[e]cho
	var totalcost = 0
	var totalbv = 0
	var totalpv = 0
	var bulktype = 0
	var grandcost = 0
	var grandbv = 0
	var grandpv = 0
	var grandquantity = 0
	var bodytext
	
	for(var i in eachnlid){
		productquantity = document.getElementById(eachnlid[i]+'qty').value
		if(productquantity){
			hiddenvalues = document.getElementById(eachnlid[i]+'values').value.split(",")
			bulktype = findbulktype(hiddenvalues[7], hiddenvalues[8], hiddenvalues[9], hiddenvalues[10])
			totalcost = findtotalcost(productquantity, hiddenvalues[3], hiddenvalues[4], hiddenvalues[5], hiddenvalues[6], hiddenvalues[7], hiddenvalues[8], hiddenvalues[9], hiddenvalues[10], bulktype)
			totalbv = productquantity * hiddenvalues[11]
			totalpv = productquantity * hiddenvalues[12]
			grandcost += parseFloat(totalcost)
			grandbv += parseFloat(totalbv)
			grandpv += parseFloat(totalpv)
			grandquantity += parseFloat(productquantity)
			pe += '<tr><td>'+eachnlid[i]+'</td><td align="center">'+productquantity+'</td><td>'+hiddenvalues[1]+'</td><td align="right">'+formatCurrency(totalcost)+'</td><td align="right">'+formatCurrency(totalbv)+'</td><td align="right">'+formatCurrency(totalpv)+'</td></tr>'
			
			pe_cis += '<tr><td>'+eachnlid[i]+'</td><td>'+productquantity+'</td><td>'+hiddenvalues[1]+'</td><td>'+formatCurrency(totalcost)+'</td><td>'+formatCurrency(totalbv)+'</td><td>'+formatCurrency(totalpv)+'</td></tr>'
			}
		//reset values to 0 to prevent carry over
		totalcost = 0; totalbv = 0; totalpv = 0;
		}
	
	te = '<tr><td style="font-weight:bold;">&nbsp;</td><td style="font-weight:bold;text-align:center;">'+grandquantity+' items</td><td></td><td style="font-weight:bold;text-align:right;">'+formatCurrency(grandcost)+'</td><td style="font-weight:bold;text-align:right;">'+formatCurrency(grandbv)+'</td><td style="font-weight:bold;text-align:right;">'+formatCurrency(grandpv)+'</td></tr>'
		
	document.getElementById('membersOrders').innerHTML = step + top + head + pe + te + '</table>'
	
	//display into a table for CIS system to auto paste orders
	var elem
	elem = document.getElementById('forCIS')
	
	if(elem) elem.innerHTML = '<textarea class="hideable" onclick="this.select()"><table>' + pe_cis + '</table></textarea>'
	
	//hide if there's no products selected
	if(grandquantity < 1){
	document.getElementById('membersOrders').innerHTML = ''	
	elem = document.getElementById('forCIS')
	if(elem) elem.innerHTML = ''	
	}
	}

/*********************
** RESET ORDER FORM **
*********************/
function resetOrderForm(nlid){
	var e
	e = document.getElementById('quickorders')
	if(e) e.value = ''
	
	e = document.getElementById('quickordersdescription')
	if(e) e.value = ''
	
	e = document.getElementById('forCIS')
	if(e) e.innerHTML = ''

	e = document.getElementById('membersOrders')
	if(e) e.innerHTML = ''
	
	var eachnlid = nlid.split(",")
		for(i = 0; i < eachnlid.length; i++){
		e = document.getElementById(eachnlid[i]+'qty')
		if(e) e.value = ''

		e = document.getElementById(eachnlid[i]+'qty2')
		if(e) e.value = ''

		document.getElementById(eachnlid[i]+'totalcost').innerHTML = ''
		document.getElementById(eachnlid[i]+'totalbv').innerHTML = ''
		document.getElementById(eachnlid[i]+'totalpv').innerHTML = ''
		
		for(j = 1; j <= 4; j++) document.getElementById(eachnlid[i]+'colorpq'+j).style.color = 'black'
		
		for(j = 1; j <= 3; j++){
			e = document.getElementById('grandvalue'+j)
			if(e) e.innerHTML = '0' 
			
			e = document.getElementById('grandbv'+j)
			if(e) e.innerHTML = '0' 
			
			e = document.getElementById('grandpv'+j)
			if(e) e.innerHTML = '0' 
			
			e = document.getElementById('granditems'+j)
			if(e) e.innerHTML = '0' 
		}	
	
		}
	}

/***************************
** Reset Admin Seacrh Box **
***************************/
function resetSearch(){
	if(document.getElementById('search')) document.getElementById('search').value = ''
	if(document.getElementById('membersFound')) document.getElementById('membersFound').innerHTML = ''
	if(document.getElementById('membersDetails')) document.getElementById('membersDetails').innerHTML = ''
	}


/****************
** Add Custom Set **
****************/
function customSet(mode){
	var hiddenvalues
	var productq
	var activefield

	switch(mode){
		case 'drpone':
		//one person set
		var products = new Array('HP004D', 'AC006A', 'AC098', 'AC003', 'AC008', 'AC008', 'HP031A', 'HP001', 'AC119', 'HP009A', 'NP008', 'NP014', 'AC024', 'HP005B', 'HP008E', 'NP021A', 'AC004')
		break;
		
		case 'drptwo':
		//two perseon set
		var products = new Array('HP004D', 'AC006A', 'AC006A', 'AC098', 'AC003', 'AC008', 'AC008', 'AC008', 'AC008', 'HP031A', 'HP001', 'AC119', 'HP009A', 'NP008', 'NP014', 'AC024', 'HP005B', 'HP008E', 'HP008E', 'NP021A', 'NP021A', 'AC004')
		break;
		
		case 'gihealthplus':
		//two perseon set
		var products = new Array('HP004D', 'NP014', 'HP001', 'HP009A', 'NP026')
		break;
		}
	
	for(var i in products){
		incrementquantity(products[i], 1, '+')
		}
	}

/***********************************************************************
** Works hand in hand with customSet() **
***********************************************************************/
function incrementquantity(nlid, quantity, operator){
	var activefield = document.getElementById(nlid+'qty')
	var activefield2 = document.getElementById(nlid+'qty2')
	var productq
	var hiddenvalues
		
	if(activefield){
			productq = activefield.value
			quantity *= 1
			productq *= 1			
			
			switch(operator){
				case '+':
				productq += quantity
				break;
				
				case '-':
				productq -= quantity
				break;
				
				case '=':
				productq = quantity
				break;
				}
				
			if(productq > 0){
				if(activefield){activefield.value = productq}
				if(activefield2){activefield2.value = productq}
			}else{
				if(activefield){activefield.value = ''}
				if(activefield2){activefield2.value = ''}
				}
			
			hiddenvalues = document.getElementById(nlid+'values').value.split(",")
				quantitycheck(nlid+'qty', hiddenvalues[0], hiddenvalues[2], hiddenvalues[3], hiddenvalues[4], hiddenvalues[5], hiddenvalues[6], hiddenvalues[7], hiddenvalues[8], hiddenvalues[9], hiddenvalues[10], hiddenvalues[11], hiddenvalues[12])
	}
}

/***********************************************************************
** Returns true if the browser is Internet Explorer, false otherwise. **
***********************************************************************/
function bookmarksite(title, url){
if (document.all)
window.external.AddFavorite(url, title);
else if (window.sidebar)
window.sidebar.addPanel(title, url, "")
}

/***********************************************************************
** Returns true if the browser is Internet Explorer, false otherwise. **
***********************************************************************/
function membersFillOut(){
	var memberfields = new Array('memberfullname', 'memberdelivery', 'membercontact')	
	//var existingfields = new Array('existingnlid', 'existingfullname', 'existingcontact')
	var elem
	
	var i = 0
		
	//if all required fields are filled out, mfv remains at 0
	var mfv = 'filled' //[m]ember[f]ields[v]alidation
	for(i = 0; i < memberfields.length; i++){
		elem = document.getElementById(memberfields[i])
		if(elem.value.length == 0) {
			mfv = 'empty'
			document.getElementById(memberfields[i]+'label').style.color = 'red'
		}else{
			document.getElementById(memberfields[i]+'label').style.color = 'black'	
		}
	}
	
	//var efv = 'filled' //[e]xisting[f]ields[v]alidation
	/*for(i = 0; i < existingfields.length; i++){
		elem = document.getElementById(existingfields[i])
		if(elem.value.length == 0) {
			efv = 'empty'
			document.getElementById(existingfields[i]+'label').style.color = 'red'
		}else{
			document.getElementById(existingfields[i]+'label').style.color = 'black'	
		}
	}*/
		
	elem = document.getElementById('membersFillOut')
	if(mfv == 'filled') { // || efv == 'filled'
		elem.style.color = 'green'
		elem.innerHTML = '<img src="/images/ico-apply.png" width="30px" height="30px" style="float:left;" /> Thank you for taking the effort to tell us who you are. <br />We\'ll do our best to serve you right after you click the big submit button below!'
		for(i = 0; i < memberfields.length; i++) document.getElementById(memberfields[i]+'label').style.color = 'black'	
		//for(i = 0; i < existingfields.length; i++) document.getElementById(existingfields[i]+'label').style.color = 'black'
		document.getElementById('placeorder').disabled=false;
	}else{
		elem.style.color = 'red'
		elem.innerHTML = '<img src="/images/ico-warning.png" width="30px" height="30px" style="float:left;" /> Empty entries has always confused us, so please do fill up the form as accurate as possible<br />or else we\'ll be just scratching our heads over at this end.'
		document.getElementById('placeorder').disabled=true;
	}
}

/***********************************************************************
** RESIZE THE ADMIN COMMENTS TEXT AREA **
***********************************************************************/
function resizeTextArea(textarea, operator, qty){
	var elem = document.getElementById(textarea)
	if(elem){
		var elemheight = elem.rows
		switch(operator){
			case '-':
			elem.rows = elem.rows - qty
			break;
			
			case '+':
			elem.rows = elem.rows + qty
			break;			
			}
		}
	}

/***********************************************************************
** Returns true if the browser is Internet Explorer, false otherwise. **
***********************************************************************/
function isIE(){
return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

/********************************
** Returns true if file exists **
********************************/
function isFile(str){
	var O= AJ();
	if(!O) return false;
	try
	{
		O.open("HEAD", str, false);
		O.send(null);
		return (O.status==200) ? true : false;
	}
	catch(er)
	{
		return false;
	}
}
function AJ()
{
	var obj;
	if (window.XMLHttpRequest)
	{
		obj= new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		try
		{
			obj= new ActiveXObject('MSXML2.XMLHTTP.3.0');
		}
		catch(er)
		{
			obj=false;
		}
	}
	return obj;
}

/*********************
** $_SESSION['msg'] **
*********************/
function highlight(divID){
	if(document.getElementById(divID) != null){
	obj = document.getElementById(divID);
	obj.style.background = "#FFFF00";
	setTimeout(dimmer, 6543);
	}
}

/************************************************************************
**
** DIMMER FOR $_SESSION['msg']
**
*/
function dimmer()
{
var obj = document.getElementById("msg");
//obj.style.color = "#CC6600";
//obj.style.background = "#FFFFFF";
obj.style.display = 'none';
}

/***********************
** PASSWORD GENERATOR **
***********************/
function generatePassword(id, length){
  chars = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789";
  pass = "";
  for(x=0;x<length;x++)
  {
    i = Math.floor(Math.random() * chars.length);
    pass += chars.charAt(i);
  }
  document.getElementById(id).value = pass;
}

/************************************************************************
**
** Product Modify Quantity Discount
**
** For Product Modify. Auto reduces "Price 1" by x%
**
**
*/
function bulkpurchasepercent(percent){
	percent = percent / 100
	
	// if quantity isn't 0
	if(document.getElementById('xq2').value > 0) 
	{
	dollar = document.getElementById('xp1').value
	document.getElementById('xp2').value = formatCurrency(dollar - (dollar * percent))
	}
	
	if(document.getElementById('xq3').value > 0)
	{
	dollar = document.getElementById('xp2').value		
		document.getElementById('xp3').value = formatCurrency(dollar - (dollar * percent))
	}
	
	if(document.getElementById('xq4').value > 0)
	{
	dollar = document.getElementById('xp3').value		
		document.getElementById('xp4').value = formatCurrency(dollar - (dollar * percent))
	}
	
	
	//BV is USUALLY the same value as xp1, and PV is USUALLY half of BV
	document.getElementById('bv').value = document.getElementById('xp1').value
	document.getElementById('pv').value = document.getElementById('bv').value / 2
							   
}