// JavaScript Document
if( document.getElementById ) {
	getElemById = function( id ) {
		return document.getElementById( id );
	}
} else if( document.all ) {
	getElemById = function( id ) {
		return document.all[ id ];
	}
} else if( document.layers ) {
	getElemById = function( id ) {
		return document.layers[ id ];
	}
}

function selectAll(id) {
    getElemById(id).focus();
    getElemById(id).select();
}

function check_uncheck_all(obj, form) {
	var checkboxes_form = getElemById(form);
	if(obj.checked)
		checkit = true;
	else
		checkit = false;
	for (var i =0; i < checkboxes_form.elements.length; i++) 
		checkboxes_form.elements[i].checked = checkit;
}

function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}

function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
		thisfield.value = defaulttext;
	}
}

function showhide_swap(id) {
	if ( getElemById( id ).style.display == 'none' ) {
		getElemById( id ).style.display = '';
	}
	else
	{
		getElemById( id ).style.display = 'none';
	}
}

function hide_elem(id) {
	if(getElemById( id ).style.display == '')
	{
		getElemById( id ).style.display = 'none';
		return true;
	}
	else
	{
		return false;	
	}
}

function show_elem(id) {
	if(getElemById( id ).style.display == 'none')
	{
		getElemById( id ).style.display = '';
		return true;
	}
	else
	{
		return false;	
	}
}

// kinda sux because it wont let you backspace or delete once you hit the max...
function maxLengthTextArea(Object, MaxLen)
{
	return (Object.value.length <= MaxLen);
}

function changeVal( id, id_val) {
	getElemById( id ).value = id_val;
}

function showPopup(page_path)
{
    window.open(page_path, 'mypopup', 'toolbar=no, directories=no, location=no, status=no, menubar=no, resizable=yes, scrollbars=yes, width=700, height=500');
}

function getRandomNumber(range)
{
	return Math.floor(Math.random() * range);
}

function getRandomChar()
{
	var chars = "0123456789abcdefghijklmnopqurstuvwxyzABCDEFGHIJKLMNOPQURSTUVWXYZ";
	return chars.substr( getRandomNumber(62), 1 );
}

function randomID(size)
{
	var str = "";
	for(var i = 0; i < size; i++)
	{
		str += getRandomChar();
	}
	return str;
}

function getRandomReadableChar()
{
	var chars = "23456789abcdefghijkmnopqurstuvwxyzABCDEFGHJKLMNPQURSTUVWXYZ";
	return chars.substr( getRandomNumber(57), 1 );
}

function randomReadableID(size)
{
	var str = "";
	for(var i = 0; i < size; i++)
	{
		str += getRandomReadableChar();
	}
	return str;
}

function generate_password(size)
{
	var new_password = randomReadableID(size);
	return new_password;
}

function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



// RFQ Pro Site Specific JavaScript
function delete_item(id) 
{
	getElemById(id).innerHTML = '';
	var count = parseInt( getElemById('item_count').value );
	// decrease and change value of item_count
	//count--;
	//getElemById('item_count').value = count;
}

// if company_id is not 0 we sub in the flexbox for standard input
function add_newitem(company_id) 
{	
	// create new div object
	var divTag = document.createElement("div");
	divTag.id = "newitem_" + getElemById('item_count').value;
	var count = parseInt( getElemById('item_count').value );
	// create div inner html
	var html = '\
<table width="100%" cellpadding="3" cellspacing="1" border="0" align="center">\
	<tr>\
		<td width="40" height="50" align="center" class="tableCol">' + count + '</td>\
		<td width="140" align="left" class="tableCol">';
	if( company_id == 0 )
		html += '<input type="text" id="account_no[' + count + ']" name="account_no[' + count + ']" value="" maxlength="50" class="width130" />';
	else
		html += '<div id="account_no[' + count + ']" class="flexbox' + count + '"></div>';
	html += '\
		</td>\
		<td width="310" align="left" class="tableCol">\
			<input type="text" id="description[' + count + ']" name="description[' + count + ']" value="" class="width300" />\
		</td>\
		<td width="110" align="left" class="tableCol">\
			<input type="text" id="part_no[' + count + ']" name="part_no[' + count + ']" value="" maxlength="50" class="width100" />\
		</td>\
		<td width="50" align="center" class="tableCol">\
			<input type="text" id="qty[' + count + ']" name="qty[' + count + ']" value="" maxlength="5" class="width40" />\
		</td>\
		<td width="90" align="center" class="tableCol">\
			<input type="text" id="unit[' + count + ']" name="unit[' + count + ']" value="" maxlength="32" class="width80" />\
		</td>';
	if( company_id != 0 )
	{
		html += '\
		<td width="100" align="center" class="tableCol">\
			<input type="text" id="location[' + count + ']" name="location[' + count + ']" value="" maxlength="100" class="width90" />\
		</td>';
	}
	html += '\
		<td width="25" align="center" class="tableCol">\
			<a onclick="delete_item(\'newitem_' + count + '\');" style="cursor:pointer;">\
			<img src="/img/buttons/delete-circle.gif" border="0" />\
			</a>\
		</td>\
	</tr>\
</table>';
	divTag.innerHTML = html;
	// append new div to parent div
	getElemById('items').appendChild(divTag);
	// now we have to initialize the new flexbox if it was added
	if( company_id != 0 )
		initializeFlexBox(".flexbox"+count);
	// increment and change value of item_count
	count++;
	getElemById('item_count').value = count;
}

function updateFreight()
{
	var subtotal = parseFloat( getElemById('subtotal').value );
	var state_tax = parseFloat( getElemById('state_tax').value );
	var fed_tax = parseFloat( getElemById('fed_tax').value );
	var freight = parseFloat( getElemById('freight').value );
	
	// update total
	formatPriceVal('total', subtotal + state_tax + fed_tax + freight);
}

function updateTotals(id, qty)
{
	// grab original subtotal, and original line item total first
	var subtotal = parseFloat( getElemById('subtotal').value );
	var line_org = parseFloat( getElemById('pit_' + id).value );
	
	// price calculations
	var price = parseFloat( getElemById( id ).value );
	if( isNaN(price) )
		price = 0.00;
	var pit_total = price * parseFloat( qty );
	formatPriceVal('pit_' + id, pit_total);
	
	// update subtotal and other total fields
	subtotal = subtotal - line_org + pit_total;
	formatPriceVal('subtotal', subtotal);
	var stp = parseFloat( getElemById('stp').value );	// state tax percentage rate
	var ftp = parseFloat( getElemById('ftp').value );	// fed tax percentage rate
	// following run into rounding problems on occasion... 
	// EPIC GAY JAVASCRIPT, fix it sometime
	var state_tax = subtotal * (stp / 100);
	var fed_tax = subtotal * (ftp / 100);
	formatPriceVal('state_tax', state_tax);
	formatPriceVal('fed_tax', fed_tax);
	var freight = parseFloat( getElemById('freight').value );
	
	formatPriceVal('total', subtotal + state_tax + fed_tax + freight);
}

// account for different percisions levels and special string values
function formatPrice(id, places)
{
	var num = parseFloat( getElemById( id ).value );
	var val = getElemById( id ).value;
	if( isNaN(num) )
	{
		var upper = val.toUpperCase();
		if( "NC" == upper || "UTQ" == upper )
			getElemById( id ).value = upper;
		else {
			getElemById( id ).value = upper;
			alert('Unsupported price value "' + upper + '" entered.');
		}
	}
	else 
	{
		// for price fields we do not allow 0, instead it must be abreviated NC for no cost
		if(num == 0 && (id.indexOf("freight") == -1) && (id.indexOf("other") == -1))
			getElemById( id ).value = 'NC';
		else
		{
			if(num.toFixed)
				getElemById( id ).value = num.toFixed(places);
			else
				getElemById( id ).value = num;
		}
	}
}

function formatPriceVal(id, val)
{
	if( isNaN(val) )
		val = 0.00;
	if(val.toFixed)
		getElemById( id ).value = val.toFixed(2);
	else
		getElemById( id ).value = val;
}

// adds val to date in datepicker1 for quick closing date selection
function quickSelectDate(val)
{	
	var today = new Date( getElemById('datepicker1').value );
	if(val == 24)
		today.setDate(today.getDate()+1); 
	if(val == 48)
		today.setDate(today.getDate()+2); 
	if(val == 1)
		today.setDate(today.getDate()+7); 
	
	var month = today.getMonth()+1;
	if( month < 10 ) month = '0' + month;
	var day = today.getDate();
	if( day < 10 ) day = '0' + day;
		
	var close_date = month + '/' + day + '/' + today.getFullYear();
	getElemById('datepicker2').value = close_date + ' 05:00 pm';
}

