// This file contains javascript functions used by channel search forms

// Used by the main search forms and quick search forms on main website
// Alerts user if all mandatory fields have not been entered
// frm: The form object
// category: The category the search was made from 
// Return Value: true if all mandatory fields have been entered in search form, false otherwise
function validateMandatoryFields(frm, category)
{
        return validateMandatoryFields(frm, category, false);
}

// Used by the main search forms and quick search forms on main website
// Alerts user if all mandatory fields have not been entered
// frm: The form object
// category: The category the search was made from 
// isQuickSearch: (boolean) Did the search originate from a quick search form
// Return Value: true if all mandatory fields have been entered in search form, false otherwise
function validateMandatoryFields(frm, category, isQuickSearch)
{
    var postcodePopulated = true;
    var minpricePopulated = true;
    var maxpricePopulated = true;
    var makePopulated = true;

    // Bike quick search form doesn't have the min price on it, so we need different validation for
    // this case 
    if (isQuickSearch && category=='BIKE')
    {
            if(frm.postcode.value=="" || frm.postcode.value.toLowerCase()=="full postcode" || frm.postcode.value.length<=4)
            {
              postcodePopulated = false;
            }
            if(frm.make.value=="" || frm.make.value=="ANY")
            {
              makePopulated = false;
            }
            if (!postcodePopulated && !makePopulated)
            {
              alert("Please enter a full postcode and a make");
              return false;
            } 
            else
            {
              if (!postcodePopulated && makePopulated)
              {
                alert("Please enter a full postcode");
                return false;
              } 
              else
              {
                if (postcodePopulated && !makePopulated)
                {
	                alert("Please enter a make");
	                return false;
                }
              }
            }
    }
    else
    {

	    switch(category)
	    {
	      // Agriculture channel does not have a make so we do not validate on that
	      case "PLANT":
	              if(frm.postcode.value=="" || frm.postcode.value.toLowerCase()=="full postcode" || frm.postcode.value.length<=4)
	              {
	                postcodePopulated = false;
	              }
	              if(frm.min_pr.value=="" || frm.min_pr.value=="0")
	              {
	                minpricePopulated = false;
	              }
  		          if(frm.max_pr.value=="")
		          {
		            maxpricePopulated = false;
		          }
	
	              if (!postcodePopulated && (!minpricePopulated && !maxpricePopulated))
	              {
	                  alert("Please enter a full postcode and a max or min price");
	                  return false;
	              }
                  else
                  {
                    if (postcodePopulated && (!minpricePopulated && !maxpricePopulated))
                    {
                        alert("Please enter a max or min price");
                        return false;
                    }
                    else
                    {
	                    if (!postcodePopulated && (minpricePopulated || maxpricePopulated))
	                    {
	                        alert("Please enter a full postcode");
	                        return false;
	                    }
                    }
                  }                  
	      break;
	
	      default:
	
	            if(frm.postcode.value=="" || frm.postcode.value.toLowerCase()=="full postcode" || frm.postcode.value.length<=4)
	            {
	              postcodePopulated = false;
	            }
	            if(frm.min_pr.value=="" || frm.min_pr.value=="75" || frm.min_pr.value=="1" || frm.min_pr.value=="0")
	            {
	              minpricePopulated = false;
	            }
                if(frm.max_pr.value=="")
                {
                  maxpricePopulated = false;
                }
              
	            if(frm.make.value=="" || frm.make.value=="ANY")
	            {
	              makePopulated = false;
	            }
	
	            if (!postcodePopulated && (!minpricePopulated && !maxpricePopulated && !makePopulated))
	            {
	               alert("Please enter a full postcode and a make or a max or min price");
	               return false;
	            }
                else
                {
                  if (postcodePopulated && (!minpricePopulated && !maxpricePopulated && !makePopulated))
                  {
                     alert("Please enter a make or a max or min price");
                     return false;
                  }
                  else
                  {
                    if (!postcodePopulated && (minpricePopulated || maxpricePopulated || makePopulated))
                    {
                       alert("Please enter a full postcode");
                       return false;
                    }
                  }
                }                
	        break;
	    }
    }
    return true;
}

//Global Variables //
					var num=0;
					var interval = null;
					var mkIndex=0;
//End//
					function doLists(make,model)  {
					model = model.toUpperCase();
					make = make.toUpperCase();
					num++;
						if (num < 100)  {
							if (typeof get_makes == 'function' && typeof get_range =='function' && typeof _model=='object' && typeof make_v=='object' && typeof make_d=='object')  {
								get_makes(make);
								if (document.qSearch && document.qSearch.make && typeof document.qSearch.make.selectedIndex == 'number')  {
									mkIndex = document.qSearch.make.selectedIndex;
								}
								get_range(_model[mkIndex], model);
								clearInterval(interval);
							}
						}
						else {
							clearInterval(interval);
						}
					}
function getcookieField(FLDval)
{
	var ourcookie = "user";
	var allcookies = document.cookie;
	var FIELD = null;
	if (allcookies != "")
	{
		var start = allcookies.indexOf(ourcookie + "=");
		if (start != -1)
		{
			start += ourcookie.length + 1;
			var end = allcookies.indexOf(";",start);
			if (end == -1) end = allcookies.length;
			var usercookie = allcookies.substring(start,end);
			var celements = usercookie.split('&')
			for (cnt=0;cnt<celements.length;cnt++)
			{
				celements[cnt] = celements[cnt].split('=');
				if (celements [cnt][0] == FLDval)
				FIELD = unescape(celements[cnt][1]);
			}
		}
	}
	return FIELD;
}
function get_makes(req_make)	{
	var dropDown;
	selectedIndex = 0;
	if (document.qSearch && document.qSearch.make)  {
		dropDown = document.qSearch.make;
	}
	else if(document.search && document.search.make)  {
		dropDown=document.search.make;
	}
	else if (document.forms[0] && document.forms[0].make)  {
		dropDown = document.forms[0].make;
	}
	if (dropDown)  {
			dropDown.length = 0;
			for(index=0; index<make_d.length; index++)	{
				dropDown[index] = new Option(make_d[index],make_v[index]);
				if (make_d[index]=="Make" || make_d[index]=="MAKE")  {
					dropDown[index] = new Option(make_d[index],"");
				  }
				if (make_d[index]=="ANY MAKE")  {
					dropDown[index] = new Option(make_d[index],"ANY");
				  }
				if(make_v[index].toUpperCase() == req_make){
					selectedIndex = index;
				}
			  }
			dropDown.options[selectedIndex].selected = true;
		}
		select_make(req_make);
}
function get_range(ranges,req_model)	{
	selectedIndex = 0;
	var dropDown;
	if (document.qSearch && document.qSearch.model)  {
		dropDown = document.qSearch.model;
	}
	else if(document.search && document.search.model)  {
		dropDown=document.search.model;
	}
	else if (document.forms[0] && document.forms[0].model)  {
		dropDown = document.forms[0].model;
	}
	if (dropDown)  {
		dropDown.length = 0;
		for(index=0; index<ranges.length; index++)	{
			dropDown[index] = new Option(ranges[index],ranges[index]);
			if (ranges[index]=="Model" || ranges[index]=="MODEL")  {
				dropDown[index] = new Option(ranges[index],"");
			  }
			  if (ranges[index]=="ANY MODEL")  {
				dropDown[index] = new Option(ranges[index],"ANY");
			  }
			if(ranges[index].toUpperCase() == req_model){
				selectedIndex = index;
			}
			else if (ranges[index].toUpperCase() == "ANY MODEL" && req_model == "ANY")  {
				selectedIndex = index;
			}
		}
		dropDown.options[selectedIndex].selected = true;
	}
}
function select_make(req_make)  {
	var dropDown;
	if (req_make == null)  {
		req_make="";
	}
	if (document.qSearch && document.qSearch.make)  {
		dropDown = document.qSearch.make;
	}
	else if(document.search && document.search.make)  {
		dropDown=document.search.make;
	}
	else if (document.forms[0] && document.forms[0].make)  {
		dropDown = document.forms[0].make;
	}
	if (dropDown)  {
		for (x=0; x<dropDown.length; x++)  {
			mk=dropDown.options[x].value;
			if ( mk.toUpperCase()==req_make)  {
				if (dropDown.options[x].selected==false)   {
					dropDown.options[x].selected=true;
				}
			}
		}
	}
}
function postCheck(){
				var users_postcode = getcookieField("PC");
				if (users_postcode && users_postcode != '') {
				document.qSearch.postcode.value= users_postcode.replace('+', ' ');
				}
	}
	
		
function aV(){	
	var make = document.qSearch.make.value;
	var model = document.qSearch.model.value;
	var min_pr = document.qSearch.min_pr.value;
	var max_pr = document.qSearch.max_pr.value;
	var postcode = document.qSearch.postcode.value;
	if (document.qSearch.nU[0].checked)
	{
	parent.location="http://atsearch.autotrader.co.uk/WWW/cars_search.asp?nU=0&make=" + make + "&model=" + model + "&min_pr=" + min_pr + "&max_pr=" + max_pr + "&postcode=" + postcode + "&miles=40&max_records=50&modelexact=1";	
        }
	
	if (document.qSearch.nU[1].checked)
	{
	    parent.location="";	
        }
        }
        
function openWindow() 
	{
	wiwwindow=window.open("","wiw","toolbar=no,location=no,directories=no,margin=0,status=no,menubar=no,scrollbars=yes,resizable=no,width=630,height=470");
	wiwwindow.focus();
	}
  
