
function initQty(form,field_cur,field_orig_val,qty){
	if (field_cur.value != field_orig_val){
		for(i=0;i<form.elements.length;i++){
			if (form.elements[i].name == qty){
				if (form.elements[i].value == 0){
					form.elements[i].value = 1;
					break;
				}
			}
		}
	}
/*
	if(field_cur.value == field_cur.value.toUpperCase()){
		var str;
		field_cur.value = '';
		str = "The text you just entered was entirely in upper case.  It has been cleared.  Check that the 'caps lock' key has not been pressed and re-enter the text.";
		alert(str);
	}
*/
	return;
}

function updateQty(order_item_id, new_val){
	var i = 1;  //order_item_ids start at 1
	var len = 0;
	len = order_item_qty.length;

	while(i < len){
		if(order_item_id == i){
			var product_id,type,old_val,diff;
			product_id = order_item_qty[i][0];
			type = order_item_qty[i][1];
			old_val = order_item_qty[i][2];

			diff = new_val - old_val;
			if(type == 'w'){
				document.getElementById('w_product_total').value = parseInt(document.getElementById('w_product_total').value) + (diff);
			} else if(type == 'l'){
				document.getElementById('l_product_total').value = parseInt(document.getElementById('l_product_total').value) + (diff);
			}
			else if(type == 'a'){
				if(document.getElementById('a_product_total'))	//only try to access this if it exists
				{
					document.getElementById('a_product_total').value = parseInt(document.getElementById('a_product_total').value) + (diff);
				}
			}
			order_item_qty[i][2] = new_val;
			return true;
		}
		i++;
	}
	return false;
}


function getFieldId (form,name) {
	var x = form.elements;
	for (var i=0; i<x.length; i++) {
		if (x[i].name == name) {
			return i;
		}
	}
	return -1;
}

//var needPageSave=true;
var showconfirm=true;

function validDetailForm(form,option_count) {
	clearErr(form);

	/////////////////rewritten JS validation code 11-10-2010 Mat Auricchio
	//****how to handle situations where customer has data entered in row's fields but erases the qty value???
	//do we need to handle it? or will the system just not add the product if there is no/invalid qty values
	// yes this ^
	
	//get page num
	var page_num=0;
	var page_h=document.getElementById('page_num');
	if(!(typeof page_h == "undefined") && !(page_h==null)) page_num = page_h.value;
	
	//get product id from hidden form field
	var product_id=0;
	var pid_h=document.getElementById('product_id');
	if(!(typeof pid_h == "undefined") && !(pid_h==null)) product_id = pid_h.value;
	
	//get ids of text fields that are required for this particular product
	var req_text_ids;	// = new Array();
	var rti_h = document.getElementById('req_text_fields');
	if(!(typeof rti_h== "undefined") && !(rti_h==null)) 
	{
		var rtf_str = rti_h.value;	//get the string of required text fields
		rtf_str=rtf_str.slice(0, -1);	//drop trailing comma
		//now parse the string and store it in our array
		req_text_ids = rtf_str.split(',');
	}

	//alert(product_id);
	if(page_num==2 && product_id>0 )	//if we found a product id (we dont really need to know this since each products required txt fields are given)
	{
		//get number of rows that we are showing
		var fields_shown = 50;
		var disp_h = document.getElementById('fields_to_show');
		if(!(typeof disp_h== "undefined") && !(disp_h==null)) fields_shown = disp_h.value;
		//alert(fields_shown);	
		
		//loop through qty fields, and get the ones with a value
		for(var i=0;i<fields_shown;i++)
		{
			var qtyidstr='qty['+i+']['+product_id+']';
			var qty_h=document.getElementById(qtyidstr);
			if(!(typeof qty_h== "undefined") && !(qty_h==null) && (parseInt(qty_h.value) > 0))	//if we have a qty value
			{
				//loop through our array of required text fields and make sure they got something in them]
				for(var rq in req_text_ids)
				{
					var fieldname='text['+i+']'+'['+product_id+']['+req_text_ids[rq]+'][0]';
					//alert(fieldname);
					var field_h=document.getElementById(fieldname);
					if((typeof field_h == "undefined") || (field_h==null))
					{
						alert('There was an error validating some of your order. Please review it very carefully before submitting');
						continue;
					}
					else
					{
						if(isEmpty(field_h.value))
						{
							pushErr('Required field in row '+(i+1)+' is missing.',field_h);
						}
					}
				}
			}
		}
		
	
	}	//end if page 2 and product id > 0
	else if(page_num==1 && product_id>0)
	{
			
		//we still have to check the required fields by hand because some options may be marked as required but not actually be required	
		var required = new Array();
		required[4] = new Array('sr_height', 'text[0][4][86][0]', 'text[0][4][87][0]','text[0][4][90][0]','text[0][4][337][0]');	//showrooms
		required[61] = new Array('sr_height','text[0][61][344][0]', 'text[0][61][345][0]','text[0][61][349][0]','text[0][61][350][0]');
		required[56] = new Array('text[0][56][320][0]');
		required[62] = new Array('text[0][62][355][0]');
		required[63] = new Array('text[0][63][358][0]');	//logo description
		required[64] = new Array('text[0][64][362][0]');	//dstyle description
		
		
			
		//loop through our array of required text fields and make sure they got something in them]
		for(var fi in required[product_id])
		{
			var fieldname=required[product_id][fi];
			//alert(fieldname);
			var field_h=document.getElementById(fieldname);
			if((typeof field_h == "undefined") || (field_h==null))
			{
				//alert('There was an error validating some of your order. Please review it very carefully before submitting');
				//ignore these because a bunch of these fields might be just static text
				continue;
			}
			else
			{
				if(isEmpty(field_h.value))
				{
					pushErr('Required field is empty',field_h);
				}
			}
		}
	
		//dont allow user to submit logo notes unless 'logo needed' radio is selected
		//otherwise the work order grouping will ignore the notes and fuck up the grouping
		//remember these fields get pushed to page 2 as hidden fields, so they will still exist
		//logo radios: option[0][48][243]	W1
		//		option[0][50][243]			W2
		//		option[0][46][243]			W3
		//		option[0][47][241]			W4
		//		option[0][51][241]			W5
		//		option[0][49][241]			W6
		//logo needed notes textarea: 
		//		text[0][48][244][0]			W1
		//		text[0][50][244][0]			W2
		//		text[0][46][244][0]			W2
		//		text[0][47][242][0]			W4
		//		text[0][51][242][0]			W5
		//		text[0][49][242][0]			W6
		//
		//option[0][44][243] text[0][44][244][0]	L1
		//option[0][43][243] text[0][43][244][0]	L2
		//option[0][45][243] text[0][45][244][0]	L3
		//L4 is 241-242, l5, l6 same (53,54,55)
		//paring 243 and 244;
		//products 43,44,45,46,48,50
		var fap=new Array('43','44','45','46','48','50');
		//	241 and 242;
		//products 47,49,51,53,54,55,56(ancillary)
		var sap=new Array('47','49','51','53','54','55','56');
		
		//foreach product in our pairing array (doing 244,243 pair)
		for(var z=0;z<fap.length;z++)
		{
			var tfname='text[0]['+fap[z]+'][244][0]';	//name of text field
			if(form.elements[tfname] && form.elements[tfname].type !='hidden')//check if field exists and isnt hidden (page 2)
			{
				var tfnameval = form.elements[tfname].value;
				if(tfnameval!='')	//check if field has text in it
				{
					var radname='[0]['+fap[z]+'][243][4]';	//radio foption name
					var rad_h=document.getElementById(radname);
					if(rad_h.checked!=true){
						pushErr('You may only enter notes if you have selected Logo/Artwork Needed',form.elements[tfname]);
					}
				}
			}
		}
		
		//doing (241,242 pair)
		for(var c=0;c<sap.length;c++)
		{
			var tfname='text[0]['+sap[c]+'][242][0]';
			if(form.elements[tfname] && form.elements[tfname].type !='hidden')//check if field exists
			{
				var tfnameval = form.elements[tfname].value;
				if(tfnameval!='')	//check if field has text in it
				{
					var radname='[0]['+sap[c]+'][241][4]';	//radio foption name
					var rad_h=document.getElementById(radname);
					if(rad_h.checked!=true){
						pushErr('You may only enter notes if you have selected Logo/Artwork Needed',form.elements[tfname]);
					}
				}
			}
		
		}
		
		//and now we have to do the same for design style
		//Design style radio: 236    notes field: 239
		var dsp=new Array('43','44','45','46','47','48','49','50','51','53','54','55');	//all W and L products
		for(var d=0;d<dsp.length;d++)
		{
			var tfname='text[0]['+dsp[d]+'][239][0]';
			if(form.elements[tfname] && form.elements[tfname].type !='hidden')//check if field exists
			{
				var tfnameval = form.elements[tfname].value;
				if(tfnameval!='')	//check if field has text in it
				{
					var radname='[0]['+dsp[d]+'][236][1]'; //radio foption name
					var rad_h=document.getElementById(radname);
					if(rad_h.checked!=true){
						pushErr('You may only enter notes if you have selected Design Style Needed',form.elements[tfname]);
					}
				}
			}
		
		}
		
		/*	handled above in required array
		//make sure ancillary message is not blank
		var apname='text[0][56][320][0]';	//this is the id of the form text input field that holds the ancillary message (from live server)
		if(form.elements[apname] && form.elements[apname].type !='hidden')//check if field exists and isnt a hidden field on page 2
		{
			var apmessageval = form.elements[apname].value;
			if(apmessageval=='')
			{
				pushErr('You must enter a message',form.elements[apname]);
			}
		}
		*/
		
		var qtsToCheck = Array('qty[0][57]','qty[0][56]','qty[0][62]');
		for(var fi in qtsToCheck)
		{
			var curcheck=qtsToCheck[fi];	//id of product we are checking
			if(form.elements[curcheck] && form.elements[curcheck].type !='hidden')//check if field exists and isnt a hidden field on page 2
			{
				var min_qty_n='min_qty';
				var min_qty=51;
				if(form.elements[min_qty_n]) min_qty=form.elements[min_qty_n].value;
				var theqty = form.elements[curcheck].value;
				//gotta convert qtys to ints and not compare them as strings
				theqty=parseInt(theqty);
				min_qty=parseInt(min_qty);
				if(theqty<min_qty)
				{
					pushErr('The minimum quantity of this product is '+min_qty,form.elements[curcheck]);
				}
			}
		}
		
		
		if(product_id==4 || product_id==61)	//showrooms
		{
			updatesum(form);//force showroom size recalculations incase onkeyup didnt get fired
			
			//and enforce minimum height of 18"
			var srht_h=document.getElementById('sr_height');
			if(!(typeof srht_h == "undefined") || (srht_h!=null))
			{
				var theht = srht_h.value;
				theht=parseInt(theht);
				if(theht<24)
				{
					pushErr('The minimum height is 24 inches',srht_h);
				}
			}
			
		}	//showroom checks done
		
	}	//end if page 1
	
	window.needPageSave=false;
	window.onbeforeunload=null;
	
	if (!showconfirm)	//if we are moving back, dont do any error checking/confirm boxes	
	{
		return true;
	}
	
	var rc;
	rc = showErr('The following must be addressed before submitting:');
	
	if (rc) {
		if(confirm("Please check your input for typos and errors before proceeding. Does everything look okay?"))
		{
			return true;
		}
		else
		{
			return false;
		}
	} else {
		return false;
	}
}

function isdefined(variable)
{
    return(typeof(window[variable])=="undefined")?false:true;
}

/* Added for page_2_bottom2 textareas */
function updateHidden(in_val,in_opt_id) {

	var theform = document.forms[0];
	var total_elements = theform.elements.length;

	/* regexp to find the assosiated hidden field by name */
	/* -> text[0][2][235][0] */
	var look_for = '^text.[0-9]+..[0-9]+..('+in_opt_id+')';

	// go through every element, and change the ones we care about
	var a = 0;
	while (a < total_elements) {
		var str = ""+theform.elements[a].name;
		if(str.match(look_for)) {
			theform.elements[a].value = in_val;
		}
		a = a + 1;
	}
}


/* TEST JAVASCRIPT */
function getElements() {
	var form = document.forms[0];
	alert(form.elements.length);

	look_for = 'text[0][2][235]';
        alert(look_for);

	look_for = look_for.replace(/[\[\]]/g,".");
	alert(look_for);

	for (var a in form.elements) {
		var str = form.elements[a].name;
		if(str.match('^'+look_for)) {
			alert('TRUE!');
			form.elements[a].value = 'CHANGED!';
		}
		else alert('false');
	}
}

function validateQuantities(){

	var l_total = document.getElementById('l_product_total').value;
	var w_total = document.getElementById('w_product_total').value;
	var l_current = document.getElementById('l_product_current').value;
	var w_current = document.getElementById('w_product_current').value;
	
	var a_current=0;
	var a_total=0;
	if(document.getElementById('a_product_current') && document.getElementById('a_product_total'))
	{
		a_current = document.getElementById('a_product_current').value;
		a_total = document.getElementById('a_product_total').value;
	}
	var l_message = '';
	var w_message = '';
	var a_message = '';

	//alert(l_total + ', ' + w_total + ', ' + l_current + ', ' + w_current);

	//umm, are you trying to compare ints here? because those were strings dumbass
	var l_total_num=parseInt(l_total);
	var w_total_num=parseInt(w_total);
	var l_current_num=parseInt(l_current);
	var w_current_num=parseInt(w_current);
	var a_total_num=parseInt(a_total);
	var a_current_num=parseInt(a_current);
	
	//if we get neg values, set to 0 so an order of 0 doesnt trigger a message
	if(l_current_num<0)l_current_num=0;
	if(w_current_num<0)w_current_num=0;
	if(a_current_num<0)a_current_num=0;
	
	if(l_total_num > l_current_num){
		l_message = '\nThe current available quantity of vehicle side merchandising is '+l_current_num;
		l_message += '\nAnd your current order is for '+l_total;
	}
	if(w_total_num > w_current_num){
		w_message = 'The current available quantity of windshield merchandising is '+w_current_num;
		w_message += '\nAnd your current order is for '+w_total;
	}
	/*
	if(a_total_num > a_current_num){
		a_message = 'The current available quantity of ancillary merchandising is '+a_current_num;
		a_message += '\nAnd your current order is for '+a_total;
	}
	*/
	message = 'CAUTION: Your order exceeds the current available quantity\n'+l_message+'\n'+w_message+'\n'+a_message;
	message = message + '\n\nOrders placed for products exceeding the current available quantities shall be invoiced separately and due payable upon receipt.  Click OK if you agree or Cancel to go back and edit your order.'

	/*IGNORE OVERAGES FOR NOW
	if(l_message.length > 0 || w_message.length > 0 || a_message.length > 0){
		if(confirm(message)){
			return true;
		} else {
			return false;
		}
	}
	*/
	return true;
}

window.onload = function(){
		var element1,element2;
		element1 = document.getElementById('basket_button1');
		if(element1 != null) element1.style.display = 'block';
		element2 = document.getElementById('basket_button2');
		if(element2 != null) element2.style.display = 'block';
}

function disableEnterKey(e)
{
     var key;     
     if(window.event)
          key = window.event.keyCode; //IE
     else
          key = e.which; //firefox     

     return (key != 13);
}
/*
///////////////////////////toolbox row javascript functions
*/
var altDown = false;
var lost_focus_h;
var gained_focus_h;
var lost_focus_row;
var gained_focus_row;
var lost_focus_id;
var gained_focus_id;
function gainFocus(handle,row,field_name)
{
	altDown=false;
	gained_focus_h=handle;
	gained_focus_row=row;
	gained_focus_id=field_name;
	
	/*
	//display currently focused row
	var row_id = 'row'+row;
	var row_h = document.getElementById(row_id);
	if(!(typeof row_h == "undefined") || (row_h!=null))
	{
		row_h.className='unhidden';
	}
	
	//hide last focused row if we need to
	if(!(typeof lost_focus_row == 'undefined') && (gained_focus_row != lost_focus_row))
	{
		row_id= 'row'+lost_focus_row;
		row_h = document.getElementById(row_id);
		if(!(typeof row_h == "undefined") || (row_h!=null))
		{
			row_h.className='hidden';
		}
	
	}
	*/
}

function lostFocus(handle,row,field_name)
{
	lost_focus_h=handle;
	lost_focus_row=row;
	lost_focus_id=field_name;
	//alert(field_name);
	altDown=false;
}

function toolText(the_text,shortcut)
{
	var target_h=lost_focus_h;
	var target_row=lost_focus_row;
	var target_id=lost_focus_id;
	//gotta check for valid handles here
	
	if(shortcut==true)
	{
		target_h=gained_focus_h;
		target_row=gained_focus_row;
		target_id=gained_focus_id;
	}	
	if(the_text=='Copy Last')
	{
		if(target_row>0)
		{
			var prev_id=target_id.replace("["+target_row+"][","["+(target_row-1)+"][");
			var prev_h=document.getElementById(prev_id);
			if(!(typeof prev_h == "undefined") || (prev_h!=null))
			{
				target_h.value = prev_h.value;
				target_h.focus();
			}
		}
	}
	else if(the_text=='edit')
	{
		
	}
	else if(the_text=='help?')
	{
		var help_h=document.getElementById('toolbarhelp');
		if(!(typeof help_h == "undefined") && (help_h!=null))
		{
			help_h.className='toolunhidden';
		}
	}
	else
	{
		target_h.value += the_text;
		target_h.focus();
	}
	return false;
}
function handleKeyUp(e){
    if(e.keyCode == 18) altDown=false;

}
function toolHelpClose()
{
	var help_h=document.getElementById('toolbarhelp');
	if(!(typeof help_h == "undefined") && (help_h!=null))
	{
		help_h.className='hidden';
	}
	return false;
}
function categoryBlock(msg)
{
	alert(msg);
	window.location='/basket';
	return false;
}
