/**
 * $Id: kbb_utilities.js 16 2008-08-27 20:00:18Z randys $
 */
 
/*
 * @var	isIE
 * variable to store boolean value for Internet Exploder
 */
	var isIE;
/*
 * @var	isMZ
 * variable to store boolean value for Mozilla type browsers
 */
	var isMZ;
/*
 * @var	isNS
 * variable to store boolean value for old Netscape versions
 */
	var isNS;

/*
 * @var	doc1
 * variable to store string value of DOM element
 */
	var doc1;
/*
 * @var	doc2
 * variable to store string value of DOM element
 */
	var doc2;
/*
 * @var	doc3
 * variable to store string value of DOM element
 */
	var doc3;
/*
 * Test for browsers that don't support getElementById() but DO support
 * the deprecated document.all (IE)
 */
	isIE = (!document.getElementById && document.all) ? true:false;
/*
 * Test for browsers that support getElementById()
 */
	isMZ = (document.getElementById) ? true:false;
/*
 * Test for browsers that support document.layers
 */
	isNS = (document.layers) ? true:false;
	
/*
 * This bit forces enables older versions of IE to use getElementById()
 */
	if(isIE)
	{
	    document.getElementById = function(id) {
	        return document.all[id];
	    }
	}
	
/*
 * If we're dealing with old NS vers. < 6, setup the DOM element string
 * else use the preferred getElementById() function
 */
	if (isNS)
	{
	    doc1 = "document.";
	    doc2 = ".document";
	    doc3 = "";
	}
	else
	{
	    doc1 = "document.getElementById('";
	    doc2 ="')";
	    doc3 ="')";
	} 
/*
 * kbb_getElement	Get's the kbb equipment form element object
 * @param	string	KBB Equipment Code
 *
 * @return	object	Form Element Object (input type='checkbox')
 * @author	randys(at)autobytel.com
 */
	function kbb_getElement(EquipmentCode)
	{
		var elm = eval(doc1 + EquipmentCode + doc2);
		
		return elm;
	}///~
	
/*
 * String.trim()
 */
 	String.prototype.trim = function()
 	{
 		return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
 	}
 	
/*
 * String.ltrim()
 */
	String.prototype.ltrim = function()
	{
		return this.replace(/^\s\s*/, '');
	}
/*
 * String.rtrim()
 */
	String.prototype.rtrim = function()
	{
		return this.replace(/\s\s*$/, '');
	}
/*
 * String.isNumeric()
 */
	String.prototype.isNumeric = function()
	{
		if (this.match(/^\d+$/))
		{
			return true;
		}
		return false;
	}
/*
 * String.isZipCode()
 */
	String.prototype.isZipCode = function()
	{
		if (this.match(/^\d{5}$/))
		{
			return true;
		}
		return false;
	}
/*
 * kbb_checkForm	checks the current checkbox against the included and excluded
 *					arrays for validity and acts accordingly
 * @param	string	KBB Equipment Code
 *
 * @return	null	returns nothing
 * @author	randys(at)autobytel.com
 */
	function kbb_checkForm(EquipmentCode)
	{
		var e = kbb_getElement(EquipmentCode);
	
		var key;
		var keyName;
		var valName;
		
		if(excluded[e.id])
		{
			if(excluded[e.id].indexOf(',') != -1)
			{
				eArray = excluded[e.id].split(',');
				for(i = 0; i < eArray.length; i++)
				{
					e_elemName = eArray[i];
					e_chk = kbb_getElement(e_elemName);
					if(e_chk.checked == true)
					{
						e_chk.checked = false;
					}
				}
			}
			else
			{
				e_elemName = excluded[e.id];
				e_chk = kbb_getElement(e_elemName);
				if(e_chk.checked == true)
				{
					e_chk.checked = false;
				}
			}
					
		}
		
		if(included[e.id])
		{
			if(included[e.id].indexOf(',') != -1)
			{
				iArray = included[e.id].split(',');
				for(j = 0; j < iArray.length; j++)
				{
					i_elemName = iArray[j];
					i_chk = kbb_getElement(i_elemName);
					if(e.checked == true)
					{
						i_chk.checked = true;
					}
				}
			}
			else
			{
				i_elemName = included[e.id];
				i_chk = kbb_getElement(i_elemName);
				if(e.checked == true)
				{
					i_chk.checked = true;
				}
			}
		}
		
		for(key in included)
		{
			if(e.id == included[key])
			{
				if(e.checked == false)
				{
					
					kbb_getElement(key).checked = false;
				}
			}
		}
	}///~
	
/*
 * kbb_checkZip		makes sure the user enters a valid ZIP code string
 *
 * @return	string	if invalid, returns error string, else, returns nothing
 * @author	randys(at)autobytel.com
 */
	function kbb_checkZip()
	{
		var zipo = eval("document.forms['kbb'].elements['Entered_Postal_Code_vch']");
		var zipstring = zipo.value.trim();
	
		if(zipstring.length == 0)
		{
			zipo.style.backgroundColor = "#FFFF99";
			zipo.focus();
			return 'Please enter ZIP Code.\n';
		}
		else if(!zipstring.isZipCode())
		{
			zipo.style.backgroundColor = "#FFFF99";
			zipstring = '';
			zipo.focus();
			return 'ZIP Code is not valid.  Please check the ZIP Code and try again.\n';
		}
		else
		{
			return '';
		}
	}///~
	
/*
 * kbb_checkNextCar		makes sure the user selects a vehicle
 *
 * @return	string	if invalid, returns error string, else, returns nothing
 * @author	zaner(at)autobytel.com
 */
	function kbb_checkNextCar()
	{
		var dropbox = eval("document.forms['kbb'].elements['nextvehicle']");
		if(dropbox == undefined) return '';
		var chosencar = dropbox.value.trim();
	
		if(chosencar.length == 0)
		{
			dropbox.style.backgroundColor = "#FFFF99";
			dropbox.focus();
			return 'Please select your next vehicle.\n';
		}
		else
		{
			return '';
		}
	}///~
	
/*
 * kbb_checkMiles	makes sure the user enters a valid mileage
 *
 * @return	string	if invalid, returns error string, else, returns nothing
 * @author	randys(at)autobytel.com
 */
	function kbb_checkMiles()
	{
		var mi = eval("document.forms['kbb'].elements['mileage']");
        /***
        * We shall use a regexp to strip some non-standard 'things'
        * from our mileage string before we submit
        */
		var mistring = mi.value.trim();
		if(mistring.length == 0)
		{
			mi.style.backgroundColor = "#FFFF99";
            mi.value = '';
			mi.focus()
			return 'Please enter Mileage.\n';
		}
		else if(!mistring.isNumeric())
		{
			mi.style.backgroundColor = "#FFFF99";
			mi.value = '';
			mi.focus();
			return 'Mileage field contains a non-numeric character. Please use numbers only.\n';
		}
		else if(parseInt(mistring) > 999999)
		{
			mi.style.backgroundColor = "#FFFF99";
			mi.focus();
			return 'Mileage too high to allow vehicle valuation.\n';
		}
		else
		{
			mi.value = mistring;
			return '';
		}
	}///~
	
/*
 * kbb_checkCondition	checks that a condition was chosen
 *
 * @return	string	if invalid, returns error string, else, returns nothing
 * @author	randys(at)autobytel.com
 */
	function kbb_checkCondition()
	{
		var fCond = eval("document.forms['kbb'].elements['conditionType']");
		if(fCond.type == 'hidden')
		{
			return '';
		}
		var checkedCond = '';
		for(var i = 0; i < fCond.length; i++)
		{
			if(fCond[i].checked == true)
			{
				checkedCond = fCond[i];
				break;
			}
		}
		
		if(checkedCond)
		{
			if(checkedCond.value.indexOf('Q') != -1)
			{
				document.forms['kbb'].Action.value = 'conditionQuiz';
			}
			else
			{
				document.forms['kbb'].Action.value = 'valueReport';
			}
			return '';
		}
		else
		{
			return 'Please select a Condition.\n';
		}
	}///~
	
/*
 * kbb_submit	checks that the options form was filled out completely before submitting
 *
 * @return	null	returns nothing
 * @author	randys(at)autobytel.com
 */
	function kbb_submit()
	{
		var msg = '';
			msg += kbb_checkZip();
			msg += kbb_checkMiles();
			msg += kbb_checkCondition();
			msg += kbb_checkNextCar();
		var frm = document.forms['kbb'];
		var unchecked = 0;

		for(var i = 0; i < frm.length; i++)
		{
			if(frm[i].name == 'Equipment')
			{
				if(frm[i].checked)
				{
					break;
				}
				else
				{
					unchecked++;
				}
			}
		}
		if(msg != '')
		{
			alert(msg);
		}
		else
		{
			document.forms['kbb'].submit();
		}
	}///~
	
	
	
/*
 * kbb_checkQuiz	checks that all questions in quiz were answered
 *
 * @return	null	returns nothing
 * @author	randys(at)autobytel.com, Nick(at)kbb.com?
 */
	//Nick 4/26/00   original code
	function kbb_checkQuiz()
	{
		var WaMessageAndItem = "and item: ";
		var WaMessageItem = "Please enter item: ";
		var WaMessageUnchecked = "Too many unchecked fields";
		var Position;
		//buffer to store count of checked buttons for each set
		var WaChecked         = new Array( 28 );
	
		//buffer to store form's index for first button in row
		var WaFormIndex       = new Array( 28 );
        var idx;
	
	
		//initialize arrays
		for ( idx = 0;  idx < WaChecked.length;  idx++ )
		{
			WaChecked[ idx ]   = 0;    // failure
			WaFormIndex[ idx ] = 0;    // zero
		}
		
		
		//traverse all elements of walkaround form, processing the 'vw' buttons
		for( idx = 0;  idx < document.kbb.length;  idx++ )
		{
			with(document.kbb[ idx ])
			{
				if(type == "radio") 
				{
					if(name.substring( 0, 2 ) == "CQ")
					{
						//button found
						Position = name.substring( 2 );
		
						//store index of first button in row
						if(WaFormIndex[ Position ] == 0)
						{
							WaFormIndex[ Position ] = idx;
						}
		
						if  (checked)
						{
							//adjust structure for checked status
							++WaChecked[ Position ];
						}
					}
				}
			}
		}
		
		
		//check results of walkaround button search
		var ErrorMessage = "";
		
		var MaxUnchecked = 3;
		var UncheckedCount = 0;
		var FirstUnchecked = 0;
		
		
		for( idx = 1; idx < WaChecked.length;  idx++ )
		{
			if(WaChecked[ idx ] == 0)
			{
				//no buttons checked for this row
				++UncheckedCount;
		
				if(UncheckedCount <= MaxUnchecked)
				{
					//add to error message
					if(ErrorMessage.length > 0)
					{
						//add new line character
						ErrorMessage += "\n";
						
						ErrorMessage += WaMessageAndItem + kbbq[ idx ];
					}
					else
					{
						ErrorMessage = WaMessageItem + kbbq[ idx ];
						FirstUnchecked = idx;
					}
				}
			}
		}
		
		
		//allow submit to continue, if no ommissions
		if(UncheckedCount == 0)
		{
			document.kbb.submit();
		}
		else
		{
			if(UncheckedCount > MaxUnchecked)
			{
				//set error message for too many fields
				ErrorMessage = WaMessageUnchecked
			}
			
			
			//tell user there are errors
			alert( ErrorMessage );
			
			
			//move focus to first missing button set
			document.kbb[ WaFormIndex[ FirstUnchecked ] ].focus();
		}
	}///~