
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;
}

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

	// Define an array of product_ids that have required text fields
	var product_ids = new Array();
	product_ids[0] = '12';
	product_ids[1] = '13';
	product_ids[2] = '4';  // sr solutions
	product_ids[3] = '27'; // curb 2
	product_ids[4] = '31'; // ws toppers - stock
	product_ids[5] = '30'; // ws toppers - brand specific brand certified
	product_ids[6] = '33'; // ws strips - stock
	product_ids[7] = '35'; // ws ovals - stock
	product_ids[8] = '23'; // curb 3
	product_ids[9] = '37'; // lg4
	product_ids[10] = '38'; // ws4
	product_ids[11] = '39'; // ws5
	product_ids[12] = '40'; // ws6

	product_ids[13] = '48'; // w1
	product_ids[14] = '50'; // w2
	product_ids[15] = '46'; // w3
	product_ids[16] = '47'; // w4
	product_ids[17] = '51'; // w5
	product_ids[18] = '49'; // w6
	product_ids[19] = '44'; // l1
	product_ids[20] = '43'; // l2
	product_ids[21] = '45'; // l3
	//New Code: L4-L6
	product_ids[22] = '53'; // l4
	product_ids[23] = '54'; // l5
	product_ids[24] = '55'; // l6

	// require stores ids of required options x-ref'd to product_ids array indices
	var require = new Array();
	require[0] = new Array('[12][83][0]','[12][97][0]','[12][121][0]','[12][122][0]');
	require[1] = new Array('[13][108][0]','[13][109][0]','[13][110][0]','[13][118][0]','[13][119][0]','[13][120][0]');
	require[2] = new Array('[4][86][0]','[4][87][0]','[4][88][0]','[4][89][0]','[4][90][0]');
	require[3] = new Array('[27][163][0]','[27][165][0]','[27][164][0]','[27][167][0]');
	require[4] = new Array('[31][177][0]','[31][180][0]','[31][181][0]');
	require[5] = new Array('[30][174][0]','[30][176][0]');
	require[6] = new Array('[33][188][0]','[33][190][0]');
	require[7] = new Array('[35][194][0]','[35][195][0]');
	require[8] = new Array('[23][144][0]','[23][146][0]','[23][145][0]','[23][148][0]');
	require[9] = new Array('[37][246][0]','[37][247][0]','[37][248][0]','[37][245][0]','[37][249][0]','[37][250][0]');
	require[10] = new Array('[38][252][0]','[38][253][0]','[38][254][0]');
	require[11] = new Array('[39][258][0]','[39][257][0]');
	require[12] = new Array('[40][260][0]','[40][259][0]','[40][261][0]');

	require[13] = new Array('[48][299][0]','[48][298][0]','[48][300][0]');
	require[14] = new Array('[50][305][0]','[50][304][0]');
	require[15] = new Array('[46][292][0]','[46][293][0]','[46][294][0]');
	require[16] = new Array('[47][296][0]','[47][295][0]','[47][297][0]');
	require[17] = new Array('[51][307][0]','[51][306][0]');
	require[18] = new Array('[49][301][0]','[49][302][0]','[49][303][0]');
	require[19] = new Array('[44][288][0]','[44][287][0]','[44][286][0]');
	require[20] = new Array('[43][275][0]','[43][273][0]');
	require[21] = new Array('[45][291][0]','[45][289][0]','[45][290][0]');
	//New Code: L4-L6
	require[22] = new Array('[53][314][0]','[53][313][0]','[53][312][0]');
	require[23] = new Array('[54][316][0]','[54][315][0]');
	require[24] = new Array('[55][317][0]','[55][318][0]','[55][319][0]');




	var field_id;
	var product_id;
	var field_name;
	var opt;
	var qty_name;
	var x = form.elements;
	for (var i=0; i<x.length; i++) {
		if (x[i].disabled) continue;
		if (x[i].name == 'qty[0][16]' && x[i].value == '1' && x[i].checked) {
			for (var j=0; j<x.length; j++) {
				if (x[j].name == 'text[0][16][112][0]' && !validEmail(x[j].value)) {
					pushErr('If requesting a proof, you must enter a valid email address.',x[j]);
				}
			}
		}

		// Deal with required fields
		// first, loop 20 times, once for each set of product options
		if (x[i].value=='') continue;
		if(typeof(window[option_count])=="undefined") var option_count = 100;
		for (j=0; j<option_count; j++) {
			// loop through product_ids array
			for (prod=0; prod < product_ids.length; prod++){
				product_id = product_ids[prod];
				qty_name = 'qty['+j+']['+product_id+']';
				if (x[i].name == qty_name) {
					if (!x[i]) break;
					if (x[i].value==0) continue;

					// now loop through each required option
					for (opt=0; opt < require[prod].length; opt++){

						field_name = 'text['+j+']'+require[prod][opt];
						id = getFieldId(form,field_name);

						// if field exists and has no content
						if (id > -1 && isEmpty(x[id].value)) {
							var row;
							row = j+1;
							if (row==1)
								pushErr('Required field is missing.',x[id]);
							else
								pushErr('Required field in row '+row+' is missing.',x[id]);
						}
					}
				}
			}
		}
	}
	
	//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='option[0]['+fap[z]+'][243]';	//radio foption name
				if(form.elements[radname][4].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='option[0]['+sap[c]+'][241]';
				if(form.elements[radname][4].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='option[0]['+dsp[d]+'][236]';
				if(form.elements[radname][1].checked!=true){
					pushErr('You may only enter notes if you have selected Design Style Needed',form.elements[tfname]);
				}
			}
		}
	
	}
	
	//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 for your Windshield Ancillaries',form.elements[apname]);
		}
	}
	
	var rc;
	rc = showErr('The following must be addressed before submitting:');
	if (rc) {
		return confirm("Please check your input for typos and errors before proceeding. Does everything look okay?");
	} 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';
}
