﻿//Misc Functions

function PostBack(eventTarget, eventArgument) {
    
   var theform;  
  
    if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) 
    {   
        theform = document.aspnetForm;    
    }    
    else 
    {    
        theform = document.forms["aspnetForm"];  
    }  
  
    theform.MyEventTarget.value = eventTarget;
    theform.MyEventArgument.value = eventArgument;
    theform.submit();
  
}

//Message Box Functions

function ShowMessage()
{
    var c;

    if (document.getElementById)
    { // DOM3 = IE5, NS6
        c = document.getElementById('MessageBox');
    }
    else
    {
        if (document.layers)
        { // Netscape 4
            c = document.MessageBox;
        }
        else
        { // IE 4
            c = document.all.MessageBox;
        }
    }
               
    if (c) 
    {            
                grayOut(true,{'opacity':'25'});
				c.style.left = (document.body.clientWidth - 200) / 2 + 'px'
				c.style.top = ((document.body.clientHeight - 220) / 2) + document.body.scrollTop + 'px'
				c.style.visibility = "visible"                       
     }
}

function HideMessage()
{
    var c;
   
    if (document.getElementById)
    { // DOM3 = IE5, NS6
        c = document.getElementById('MessageBox');
    }
    else
    {
        if (document.layers)
        { // Netscape 4
            c = document.MessageBox;
        }
        else
        { // IE 4
            c = document.all.MessageBox;
        }
    }
               
    if (c) c.style.visibility = "hidden";
   
}

function grayOut(vis, options) {
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  
 if (!dark) {
   var tbody = document.getElementsByTagName("body")[0];
   var tnode = document.createElement('div');           // Create the layer.
   tnode.style.position='absolute';                 // Position absolutely
   tnode.style.top='0px';                           // In the top
   tnode.style.left='0px';                          // Left corner of the page
   tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
   tnode.style.display='none';                      // Start out Hidden
   tnode.id='darkenScreenObject';                   // Name it so we can find it later
   tbody.appendChild(tnode);                            // Add it to the web page
   dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}
   
// AJAX Model Dropdown functions

var AjaxServerPage = "ajaxservice.aspx";
var XmlHttp;

function CreateXmlHttp()
{

    //Creating object of XMLHTTP in IE
    try
    {
        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
        try
        {
            XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            XmlHttp = null;
        }
    }
    
    //Creating object of XMLHTTP in Mozilla and Safari
    if(!XmlHttp && typeof XMLHttpRequest != "undefined")
        XmlHttp = new XMLHttpRequest();
    
    //Create MyHttpRequest if all else fails
    if(XmlHttp == null)
        XmlHttp = new MyHttpRequest();

    
}

function HandleResponse()

{

    if(XmlHttp.readyState == 4)
    {
        
        SetMakeList(XmlHttp.responseText);
    }
}


function GetModels(cboMake)
{
    var i = cboMake.selectedIndex;
    var t = cboMake.options[i].value;
    var requestUrl = AjaxServerPage +"?cmd=getmodels&make=" + t; 
    var ModelList = document.getElementById("ctl00_cphMain_SelectDetails1_cboModel");


    if (ModelList != null)
    {
        if (document.all)
        {
	        for (i = ModelList.options.length - 1; i >= 0; i--)
	            ModelList.options.remove(i);
        }
        else
        {
	        for (i = ModelList.options.length - 1; i >= 0; i--)
	        {
	            var o = ModelList.options[i];
	            ModelList.removeChild(o);
	        }
        }

	    optionItem = new Option("Loading Models ...", "", false, false);
		ModelList.options.add(optionItem);
	    ModelList.disabled = true;
	  }

    CreateXmlHttp();

        
    if(XmlHttp)
    {
        XmlHttp.onreadystatechange = HandleResponse;
        XmlHttp.open("GET", encodeURI(requestUrl),  true);
        XmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        XmlHttp.setRequestHeader("Content-length", 0);  
        XmlHttp.setRequestHeader("Connection", "close");
        XmlHttp.send(null);           
    }

}

function SetMakeList(Models)
{
    var ModelList = document.getElementById("ctl00_cphMain_SelectDetails1_cboModel");
	//Clears the state combo box contents.
	
    if (document.all)
    {
	    for (i = ModelList.options.length - 1; i > 0; i--)
	        ModelList.options.remove(i);
    }
    else
    {
	    for (i = ModelList.options.length - 1; i > 0; i--)
	    {
	        var o = ModelList.options[i];
	        ModelList.removeChild(o);
	       }
    }

	var i1 = Models.indexOf("(Model)");
	var i2 = Models.indexOf("(/Model)");


	var textValue; 
	var optionItem;
	
	//Add new states list to the state combo box.
	
	while (i1 > 0 && i2 > 0)
	{
	    var m = Models.substring(i1 + 7, i2);
		optionItem = new Option(m, m,  false, false);
		ModelList.options.add(optionItem);
		i1 = Models.indexOf("(Model)", i2 + 6);
    	i2 = Models.indexOf("(/Model)", i2 + 6);
	}
	
    if (document.all)
    {
        ModelList.options.remove(0);
    }
    else
    {
        o = ModelList.options[0];
        ModelList.removeChild(o);
    }
     
	ModelList.disabled = false;
}

//Returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

// Fallback XMLHttpRequest Object

var MyHttpRequest = function()
{
	this.method = "POST";
	this.url = null;
	this.async = true;
	this.iframe = null;
	this.responseText = null;
	this.header = new Object();
	this.id = "_xmlhttp_" + new Date().getTime();
	this.container = document.body;
}

MyHttpRequest.prototype.open = function(method, url, async)
{
    var oldframe = document.getElementById("httpframe");
	if (oldframe != null)
	    this.container.removeChild(oldframe);
	
	this.method = method;
	this.url = url;
	this.async = async;
	this.readyState = 0;
	
	this.iframe = document.createElement("IFRAME");
	this.iframe.style.visibility = "hidden";
	this.iframe.id = "httpframe";
	this.container.appendChild(this.iframe);
		
}

MyHttpRequest.prototype.setRequestHeader = function(name, value)
{
	// if(typeof(this.header[name]) == "undefined")
		this.header[name] = value;
}

MyHttpRequest.prototype.send = function(data)
{
	var html = [];
	
	html[html.length] = '<html><body><form method="' + this.method + '" action="' + this.url + '">';
	html[html.length] = '<s'+'cript>document.forms[0].submit();</s'+'cript>';
	html[html.length] = '</form></body></html>';

	this.iframe._xmlhttp = this;
	this.iframe._xmlhttp._fix = -1;
	this.iframe._xmlhttp.responseText = null;
	this.iframe.onreadystatechange = this._onreadystatechange;
	this.iframe.src = "javascript:document.write('" + html.join('').replace(/\'/g,"\\'").replace(/\r\n/g, "\\r\\n") + "');void(0);";
}

MyHttpRequest.prototype._onreadystatechange = function()
{
	this._xmlhttp._fix++;
	
	
	if(this._xmlhttp._fix < 1)
		return;
	
	if(this._xmlhttp._fix == 1)
	{
		this._xmlhttp.readyState = 1;
	}
	else if(this._xmlhttp._fix > 1)
	{
		switch(this.readyState.toString())
		{
			case "loading":
				this._xmlhttp.readyState = 2;
				break;

			case "interactive":
				this._xmlhttp.readyState = 3;
				break;

			case "complete":
			    var i = document.getElementById(this.id);			    
				this._xmlhttp.responseText = i.contentWindow.document.body.innerHTML;
				this.onreadystatechange = function(){}
				this._xmlhttp.readyState = 4;
				break;
		}	
	}
	
	if(typeof(this._xmlhttp.onreadystatechange) == "function")
			this._xmlhttp.onreadystatechange();
			
			
}




