// this function is needed to work around 
  // a bug in IE related to element attributes
  function hasClass(obj) {
	 var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  }   
 /*
  * stripe table colours
  */
 function stripe(id) {
    // the flag we'll use to keep track of 
    // whether the current row is odd or even
    var appliedColour = false;
	var even = false;
  
    // if arguments are provided to specify the colours
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var evenColor = arguments[1] ? arguments[1] : "#fff";
    var oddColor = arguments[2] ? arguments[2] : "#efefef";
  
    // obtain a reference to the desired table
    // if no such table exists, abort
    var table = document.getElementById(id);
    if (! table) { return; }
    
    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // &lt;tbody&gt;s 
    var tbodies = table.getElementsByTagName("tbody");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
    
     // find all the &lt;tr&gt; elements... 
      var trs = tbodies[h].getElementsByTagName("tr");
      
      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {

        // avoid rows that have a class attribute
        // or backgroundColor style
        if (! hasClass(trs[i]) &&
            ! trs[i].style.backgroundColor) {
 		  
          // get all the cells in this row...
          var tds = trs[i].getElementsByTagName("td");
        
          // and iterate through them...
          for (var j = 0; j < tds.length; j++) {
        
            var mytd = tds[j];

            // avoid cells that have a class attribute
            // or backgroundColor style
            if (! hasClass(mytd) &&
                ! mytd.style.backgroundColor &&
                mytd.style.display != 'none') {
        
              mytd.style.backgroundColor =
                even ? evenColor : oddColor;
				
				appliedColour = true;
            }
          }
        }
        // flip from odd to even, or vice-versa
        if (appliedColour) 
		{
			even =  ! even;
			appliedColour = false;
		}
      }
    }
  }
  

/*
 * select a select box option by value
 */
function set_value_selected(select_id, value)
{
	sel = $(select_id);
	for(var i = 0; i < sel.options.length; i++)
	{
		opt = sel.options[i];
		if (opt.value == value)
		{
			sel.selectedIndex = i;
			return;
		}
	}
}

function load_models_ranges(strType,strValue,strManuName){
	var objLoader = document.getElementById("loader");
	var objOldDiv = document.getElementById("range_listing");
	if(strType != ''){
		objLoader.style.display = "";
		objOldDiv.style.display = "none";
	}
	$.ajax({
		type: "POST",
		url: HTTP_JS_SERVER + "new_car_model_range.php",
		data: "action=get_models&type=" + strType + "&typeValue="+strValue + "&manuName=" + strManuName,
		error: function(errorResponse){alert(errorResponse.statusText); alert("error");},
		success: function(xmlReponseResult) {

			var objNewDiv = document.createElement("div");
				objNewDiv.id="range_listing";
				objNewDiv.className = "float_left";
				objNewDiv.style.width ="375px";
					
					if(xmlReponseResult.getElementsByTagName('modelID').item(0).firstChild.nodeValue != 'NO_RECORDS')
					{
						for(var cnt=0; cnt < xmlReponseResult.getElementsByTagName('modelID').length; cnt++)
						{
							/* To assign values to variables from response xml tags */
							var modelID = xmlReponseResult.getElementsByTagName('modelID').item(cnt).firstChild.nodeValue;
							var modelName = xmlReponseResult.getElementsByTagName('modelName').item(cnt).firstChild.nodeValue;
							var modelYear = xmlReponseResult.getElementsByTagName('modelYear').item(cnt).firstChild.nodeValue;
							var modelImage = xmlReponseResult.getElementsByTagName('modelImage').item(cnt).firstChild.nodeValue;
							var modelMaxPrice = xmlReponseResult.getElementsByTagName('modelMaxPrice').item(cnt).firstChild.nodeValue;
							var modelMinPrice = xmlReponseResult.getElementsByTagName('modelMinPrice').item(cnt).firstChild.nodeValue;
							var modelUrl = xmlReponseResult.getElementsByTagName('modelUrl').item(cnt).firstChild.nodeValue;

							var strClassName = "blackBg";
							if(cnt % 2 == 0){
								strClassName = "yellowBg";
							}
							var objShortModelDiv = document.createElement("div")
								objShortModelDiv.id = "model_list";
								objShortModelDiv.name = cnt;

								var objListDiv = document.createElement("div")
									objListDiv.className = strClassName;
									
									var objSpan = document.createElement("span");
										objSpan.className = "rangeImage";
										
										var objLink = document.createElement("a");
											objLink.href = modelUrl

											var objImage = document.createElement("img");
												objImage.src = modelImage;
												objImage.height = "80";
												objImage.width = "140";

											objLink.appendChild(objImage);
										objSpan.appendChild(objLink);
									objListDiv.appendChild(objSpan);

								var objDescDiv = document.createElement("div");
									objDescDiv.id = "car1Content_short";
									var objSpanDesc = document.createElement("span");
										var objLinkText = document.createElement("a");
											objLinkText.href = modelUrl
											objLinkText.appendChild(document.createTextNode(modelYear+ " " + modelName));
										objSpanDesc.appendChild(objLinkText);
										var objBreak = document.createElement("br");
										objSpanDesc.appendChild(objBreak);
										var objStrong = document.createElement("strong");
											objStrong.appendChild(document.createTextNode("Price : "));
										objSpanDesc.appendChild(objStrong);
										objSpanDesc.appendChild(document.createTextNode(modelMinPrice + "-"  + modelMaxPrice));
									objDescDiv.appendChild(objSpanDesc);

								objShortModelDiv.appendChild(objListDiv);
								objShortModelDiv.appendChild(objDescDiv);

							objNewDiv.appendChild(objShortModelDiv);

							var objBreak2 = document.createElement("br");
							objNewDiv.appendChild(objBreak2);
							
						}
					}
				objLoader.style.display = "none";
				objOldDiv.parentNode.replaceChild(objNewDiv, objOldDiv);
				
		}
	});
}



Array.prototype.in_array = function(p_val) {
	for(var i = 0, l = this.length; i < l; i++) {
		if(this[i] == p_val) {
			return true;
		}
	}
	return false;
}




function show_models(strDivIDs){
	var arrShowDivs = strDivIDs.split(",");
	var regExMatch = new RegExp( '^model_[0-9]+' );
	var objParentDiv = document.getElementById("range_listing");
	var objFilterListing = document.getElementById("filter_listing");
	if(objFilterListing.hasChildNodes())
	{
		while(objFilterListing.childNodes.length >= 1)
		{
			objFilterListing.removeChild(objFilterListing.firstChild);       
		} 
	}

	var arrDivs = objParentDiv.getElementsByTagName('div');
	var cntFound = 0;
	for(var cnt=0; cnt<arrDivs.length; cnt++){
		if(arrDivs[cnt].id.match(regExMatch)){
			if(arrShowDivs.in_array(arrDivs[cnt].id)){
				cntFound++;
				var objCloneElement = arrDivs[cnt].cloneNode(true);
				objFilterListing.appendChild(objCloneElement);
				var objClearDiv = document.createElement("div");
					objClearDiv.className = "clear";
					objClearDiv.innerHTML = '&nbsp;';
				objFilterListing.appendChild(objClearDiv);
				/*
				if(cntFound % 2 == 0){
					objCloneElement.firstChild.className = "blackBg";
				}else{
					objCloneElement.firstChild.className = "yellowBg";
				}
				*/
			}
		}
	}
	//document.getElementById('mytab').tabber.tabShow(1);
	objParentDiv.style.display = "none";
	objFilterListing.style.display = "";
}

function viewAll(){
	var objAllDiv = document.getElementById("range_listing");
	var objFilterDiv = document.getElementById("filter_listing");
	objAllDiv.style.display = "";
	objFilterDiv.style.display = "none";
}

function quick_compare_add(objForm){
	var objSelectedCheckBox = $("input[@name='chkCompare']:checked");
	var strNum = objSelectedCheckBox.size();
	if(strNum < 1){
			alert("You need to select atleast one car.");
	}else if(strNum > 4){
		alert("You can compare only four cars.");
	}
	else if(strNum > 0 && strNum < 5){
		var cnt = 0;
		for(i=0;i<strNum;i++){
			cnt = i +1;
			var objHidden = document.getElementById("hidCompare_" + cnt);
			objHidden.value = objSelectedCheckBox[i].value;
		}

		var modelID1 = document.getElementById("hidCompare_1").value;
		var modelID2 = document.getElementById("hidCompare_2").value;
		var modelID3 = document.getElementById("hidCompare_3").value;
		var modelID4 = document.getElementById("hidCompare_4").value;
		$.ajax({
			type: "POST",
			url: NEW_STR_URL,
			data: "action=get_new_car_compare_url&model_id1="+modelID1+ "&model_id2="+modelID2+ "&model_id3="+modelID3+"&model_id4="+modelID4,
			error: function(errorResponse){alert(errorResponse.statusText);},
			success: function(xmlReponseResult) {
				var strURL = HTTP_SERVER + xmlReponseResult;
				window.location = strURL;
			}
		});
	}
}

function add_single_compare(objForm,strTrimID){
	if(strTrimID != ''){
		$.ajax({
			type: "POST",
			url: NEW_STR_URL,
			data: "action=get_home_compare_url&style_id=" + strTrimID,
			error: function(errorResponse){alert(errorResponse.statusText);},
			success: function(xmlReponseResult) {
				window.location = xmlReponseResult;
			}
		});
	}
}

function submitModel(objForm, modelName)
{
	objForm.hidSortModel.value = modelName;
	objForm.submit();
}