var http = getHTTPObject(); // We create the XMLHTTPRequest Object

/*-------------------------------------------------------------------------
 *    getHTTPObject -- Get the HTTP object
 *    Args: Nothing
 *    Returns: HTTP object
 -------------------------------------------------------------------------*/
function getHTTPObject(){
  var xmlhttp;
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } return xmlhttp;
}

/*-------------------------------------------------------------------------
 *    getCarModels -- get the car models details for the selected make
 *    Args:
 *    Returns:
 -------------------------------------------------------------------------*/
function getCarModels(tablePrefix)
{
  var makeId=document.ads.make.value;
  var sendVal = "";
  sendVal += "CreateAccountOperation.php?param=getCarModels&makeId=" + escape(makeId)+"&tablePerfix="+escape(tablePrefix);
  var lgth = document.ads.model.options.length;
  for(i=0;i<lgth;)
    {
      document.ads.model.remove(i);
       i=0;
       lgth = lgth-1;

    }
  document.ads.model.options[0] = new Option("Loading.....");
  /*
   * Send the details.
   */
  if(makeId !="")
    {
      http.open("GET", sendVal, true);
      http.onreadystatechange = getCarModelResponse;
      http.send(null);
    }
  else
    {
      document.ads.model.options[0] = new Option("--------------Select a make--------------");
      document.ads.model.options[i].value = "";
    }
}

/*-------------------------------------------------------------------------
 *    getCarModelResponse --
 *    Args:
 *    Returns:
 -------------------------------------------------------------------------*/
 function getCarModelResponse()
{
  /*
   * Check for the states and the status
   * according to the result display the message.
   */
  if (http.readyState == 4)
    {
      if (http.status == 200)
        {
          var message = http.responseText;
          //alert(message);
          if(message!="")
            {
              document.ads.model.remove(0);
              var modelDetails = message.split("@");
	      var k=0;
	      var oldVal= "";
              for(i=0;i<modelDetails.length-1;i++)
                {
                  var splitVal = modelDetails[i].split("^");
		  var splitModel = splitVal[1].split("-");
		  if(splitModel[0] != oldVal)
	          {		
		    document.ads.model.options[k] = new Option(splitModel[0]);
                    document.ads.model.options[k].value = splitModel[0];
		    k=k+1;
		  }
		  oldVal = splitModel[0];
                  //document.ads.model.options[i] = new Option(splitVal[1]);
                  //document.ads.model.options[i].value = splitVal[0];
                }
            }
          else
            {
                document.ads.model.options[i] = new Option("No Model Found");
                document.ads.model.options[i].value = "";
            }
        }
    }
}


/*-------------------------------------------------------------------------
 *    getDistricts -- get the district list for given state
 *    Args:
 *    Returns:
 -------------------------------------------------------------------------*/
function getDistricts()
{
       var state=document.ads.state.value;
       var lgth = document.ads.district.options.length;
          for(i=0;i<lgth;)
          {
            document.ads.district.remove(i);
            i=0;
            lgth = lgth-1;
          }

       if (state != "")
       {
         var sendVal = "";
         sendVal += "CreateAccountOperation.php?param=getDistrict&state=" + escape(state);
         http.open("GET", sendVal, true);
         http.onreadystatechange = districtList;
         http.send(null);
       }
       else
       {
	            
          document.ads.district.options[0] = new Option("----------------------Any------------------");
          document.ads.district.options[i].value = "";
       }
}

/*-------------------------------------------------------------------------
 *    districtList --
 *    Args:
 *    Returns:
 -------------------------------------------------------------------------*/
 function districtList()
{
  /*
   * Check for the states and the status
   * according to the result display the message.
   */
  if (http.readyState == 4)
    {
      if (http.status == 200)
        {
          var message = http.responseText;
          if(message!="")
            {
	      var districts = message.split("@");
	      document.ads.district.options[0] = new Option("------------------- Any --------------------");
              document.ads.district.options[0].value = "";
	      var j = 0;
	      for(i=0;i<districts.length-1;i++)
		{
		  j = i + 1;
		  document.ads.district.options[j] = new Option(districts[i]);
		  document.ads.district.options[j].value = districts[i];
		}
	    }else
	      {
		document.ads.district.options[i] = new Option("No Districts Found");
                document.ads.district.options[i].value = "";
	      }
        }
      else
        {
          alert("Unable to process the request");
        }
    }
}


/*-------------------------------------------------------------------------
 *    getDistricts -- get the district list for given state
 *    Args:
 *    Returns:
 -------------------------------------------------------------------------*/
function getDistrictsSeller()
{
       var state=document.forms[0].state.value;
       /*var lgth = document.forms[0].district.options.length;
          for(i=0;i<lgth;)
          {
            document.forms[0].district.remove(i);
            i=0;
            lgth = lgth-1;
          }*/

       if (state != "")
       {
         var sendVal = "";
         sendVal += "CreateAccountOperation.php?param=getDistrictSeller&state=" + escape(state);
         http.open("GET", sendVal, true);
         http.onreadystatechange = districtListSeller;
         http.send(null);
       }
       else
       {
	            
          document.forms[0].district.options[0] = new Option("----------------------Select------------------");
          document.forms[0].district.options[i].value = "";
       }
}

/*-------------------------------------------------------------------------
 *    districtList --
 *    Args:
 *    Returns:
 -------------------------------------------------------------------------*/
 function districtListSeller()
{
  /*
   * Check for the states and the status
   * according to the result display the message.
   */
  if (http.readyState == 4)
    {
      if (http.status == 200)
        {
          var message = http.responseText;
          var districts = message.split("@");
          for(i=0;i<districts.length-1;i++)
          {
             document.forms[0].district.options[i] = new Option(districts[i]);
             document.forms[0].district.options[i].value = districts[i];
          }
        }
      else
        {
          alert("Unable to process the request");
        }
    }
}


/*-------------------------------------------------------------------------
 *    createPassword -- Create a new password for the user if they forget
 *                      the old password
 *    Args:
 *    Returns:
 -------------------------------------------------------------------------*/
function createPassword()
{
  var userName=document.forget.userName.value;
  var sendVal = "";

  sendVal += "CreateAccountOperation.php?param=forgetPwd&userName=" + escape(userName);

  /*
   * Send the details.
   */
  if(userName != "")
    {
      //document.forget.userName.value ="Loading.....";
      http.open("GET", sendVal, true);
      http.onreadystatechange = getPasswordResponse;
      http.send(null);
    }
  else
    {
     alert("Please enter your Login Id");
    }
}

/*-------------------------------------------------------------------------
 *    getPasswordResponse --
 *    Args:
 *    Returns:
 -------------------------------------------------------------------------*/
 function getPasswordResponse()
{
  /*
   * Check for the states and the status
   * according to the result display the message.
   */

  if (http.readyState == 4)
    { 
      if (http.status == 200)
        {
          var message = http.responseText;
          splitVal = message.split("-");
          message = splitVal[1];
          if(message=="SENT")
            {
              alert("Your new password has been sent to your email id");
              document.forget.userName.value ="";
            }
          if(message=="FAILED")
            {
              alert("Error while sending mail. Please try after some time");
              document.forget.userName.value ="";
            }
          if(message=="NOTVALID")
            {
              alert("Given Login Id is not valid");
              document.forget.userName.value ="";
            }
        }
    }
}


/*-------------------------------------------------------------------------
 *    getMakeIndex -- get the make details for the selected category
 *    Args:
 *    Returns:
 -------------------------------------------------------------------------*/
function getMakeIndex()
{
  var category=document.ads.category.value;
  var sendVal = "";
  sendVal += "CreateAccountOperation.php?param=getMakeIndex&category=" +escape(category);
  var lgth = document.ads.make.options.length;
  for(i=0;i<lgth;)
    {
      document.ads.make.remove(i);
       i=0;
       lgth = lgth-1;
    }

  var lgth1 = document.ads.model.options.length;
  for(j=0;j<lgth1;)
    {
      document.ads.model.remove(j);
       j=0;
       lgth1 = lgth1-1;
    }

  document.ads.make.options[0] = new Option("Loading.....");

  /*
   * Send the details.
   */
  if(category !="")
    {
      http.open("GET", sendVal, true);
      http.onreadystatechange = getMakeIndexResponse;
      http.send(null);
    }
  else
    {
      document.ads.make.options[0] = new Option("--------------------- Any -------------------");
      document.ads.make.options[i].value = "";
      document.ads.model.options[0] = new Option("--------------------- Any --------------------");
      document.ads.model.options[j].value = "";
    }
}

/*-------------------------------------------------------------------------
 *    getMakeIndexResponse --
 *    Args:
 *    Returns:
 -------------------------------------------------------------------------*/
 function getMakeIndexResponse()
{
  /*
   * Check for the states and the status
   * according to the result display the message.
   */

  if (http.readyState == 4)
    {
      if (http.status == 200)
        {
          var message = http.responseText;
          if(message!="")
            {
              document.ads.make.remove(0);
              var makeDetails = message.split("@");

	      document.ads.make.options[0] = new Option("-------------------- Any ---------------------");
              document.ads.make.options[0].value = "";

	      document.ads.model.options[0] = new Option("-------------------- Any ---------------------");
              document.ads.model.options[0].value = "";

              var j=0;
              for(i=0;i<makeDetails.length-1;i++)
                {
	          j = i + 1;
                  splitVal = makeDetails[i].split("^");
                  document.ads.make.options[j] = new Option(splitVal[1]);
                  document.ads.make.options[j].value = splitVal[0];
                }
            }
          else
            {
                document.ads.make.options[i] = new Option("No Make Found");
                document.ads.make.options[i].value = "";
            }
        }
    }
}


/*-------------------------------------------------------------------------
 *    getModelndex -- get the make details for the selected category
 *    Args:
 *    Returns:
 -------------------------------------------------------------------------*/
function getModelIndex()
{
  var category=document.ads.category.value;
  var make=document.ads.make.value;
  var sendVal = "";
  sendVal += "CreateAccountOperation.php?param=getModelIndex&category=" +escape(category)+"&make=" +escape(make);
  var lgth = document.ads.model.options.length;
  for(i=0;i<lgth;)
    {
      document.ads.model.remove(i);
       i=0;
       lgth = lgth-1;
    }
  document.ads.model.options[0] = new Option("Loading.....");
  /*
   * Send the details.
   */
  if(category !="")
    {
      http.open("GET", sendVal, true);
      http.onreadystatechange = getModelIndexResponse;
      http.send(null);
    }
  else
    {
      document.ads.model.options[0] = new Option("-------- Any ---------");
      document.ads.make.options[i].value = "";
      document.ads.model.options[i].value = "";
    }
}


/*-------------------------------------------------------------------------
 *    getModelIndexResponse --
 *    Args:
 *    Returns:
 -------------------------------------------------------------------------*/
 function getModelIndexResponse()
{
  /*
   * Check for the states and the status
   * according to the result display the message.
   */

  if (http.readyState == 4)
    { 
      if (http.status == 200)
        {
          var message = http.responseText;
          if(message!="")
            {
              document.ads.model.remove(0);
              var makeDetails = message.split("@");
              document.ads.model.options[0] = new Option("-------------------- Any ---------------------");
              document.ads.model.options[0].value = "";

              var j = 0;
              for(i=0;i<makeDetails.length-1;i++)
                {
                  j = i + 1; 
                  splitVal = makeDetails[i].split("^");
                  document.ads.model.options[j] = new Option(splitVal[1]);
                  document.ads.model.options[j].value = splitVal[0];
                }
            }
          else
            {
                document.ads.model.options[i] = new Option("No Model Found");
                document.ads.model.options[i].value = "";
            }
        }
    }
}


