﻿// JScript File

var xmlHttp;
var prefillCarlineId = -1, prefillModelId = -1; //to be filled from primary vehicle (user account) 
var makeId = -1;

//General function to get XMLHttpRequest object depending on browser type.
function GetXmlHttpObject()
{	
    var xmlHttp=null;
    try
      {   // Firefox, Opera 8.0+, Safari
          xmlHttp = new XMLHttpRequest();
      }
    catch (e)
      {
          // Internet Explorer
          try
            {   xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");    }
          catch (e)
            {   xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
      }
    return xmlHttp;
}

//Common function to fill combo with id and values.
//XML should contain single 'tid' element(target id) with id of target combo.
//Combo entries are within 'e' elements. 0 child - value(name), 1 child - id.
function FillDropDown()
{
    if (xmlHttp.readyState==4 && xmlHttp.status==200)
    {	
		var xmlDoc = xmlHttp.responseXML.documentElement;
		
		//Get target combo to fill ('tid' - target id) (subject to browser type IE/FF)
		var xTid = xmlDoc.getElementsByTagName("tid");
		var targetComboId = xTid[0].text;
		if (targetComboId == null) {targetComboId = xTid[0].textContent}
		
		var oList = document.getElementById(targetComboId);
		oList.disabled = false
		
		//Which combo do we have?
		var bCarline=false,bModel=false,bService=false;
		if(targetComboId.search('cboCar')>-1){bCarline=true}
		if(targetComboId.search('cboMod')>-1){bModel=true}
		if(targetComboId.search('cboSer')>-1){bService=true}
		
		//Fill combo with data
		var xRows = xmlDoc.getElementsByTagName('e');
		var comboText, comboValue;//subject to browser type IE/FF
		var carlineIndex=-1, modelIndex=-1;//index for preselected carline/model 
		for (i=0;i<xRows.length; i++)
		{
			comboText = xRows.item(i).childNodes[0].text;
			if (comboText == null){comboText = xRows.item(i).childNodes[0].textContent}
			comboValue = xRows.item(i).childNodes[1].text
			if (comboValue == null){comboValue = xRows.item(i).childNodes[1].textContent}
			
			if(bCarline && comboValue==prefillCarlineId){carlineIndex=i}
			if(bModel && comboValue==prefillModelId){modelIndex=i}
			
			oList.options[oList.options.length]=new Option(comboText, comboValue);//TEXT,VALUE
			
		}
	    
		//Set selected Item and initiate next dropdown fill.
		if (carlineIndex>0 && bCarline){
		    oList.selectedIndex = carlineIndex;
		    var oListModel = document.getElementById(targetComboId.replace('cboCarline','cboModel'));		    
		    if (oListModel){FillModel(oList,oListModel,makeId)}
		}
		if (modelIndex>0 && bModel){
		    oList.selectedIndex = modelIndex;
		    var oListService = document.getElementById(targetComboId.replace('cboModel','cboService'));		    
		    if (oListService){FillService(oList,oListService)}
		}
    }    
}

//Common function used to clear combo values.
function clearCombo(oList)
{
	//alert('clearCombo');
  for (var i = oList.options.length - 1; i >= 0; i--)
  {
    oList.options[i] = null;
  }
  oList.selectedIndex = -1;
}


//Send a request to get Mode data (ModelId, ModelName)
function FillModel(oElem,oTarget,iMakeId)
{
    xmlHttp=GetXmlHttpObject();
	var strValue
	if (oElem.type == 'select-one'){strValue=oElem.options[oElem.selectedIndex].value;}
	else {strValue = oElem};
    
    var url="util/FillModelXML.aspx";
    //var strParams ="makeid=" + iMakeId + "&fid=" + oTarget.form.id + "&tid=" + oTarget.id + "&carlineid=" + strValue;
    var strParams ="makeid=" + iMakeId + "&tid=" + oTarget.id + "&carlineid=" + strValue;
    url=url + "?" + strParams ; 
    //alert('url:' + url);
    clearCombo(oTarget);
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=FillDropDown;  
    xmlHttp.send(null);	
   
}

//Send a request to get Carline data (CarlineId, CarlineName)
function FillCarline(oElem,oTarget)
{
	if (oTarget.options.length == 0){
    xmlHttp=GetXmlHttpObject();
	var strValue
	//alert(oElem.type)
	if (oElem.type == 'select-one'){strValue=oElem.options[oElem.selectedIndex].value;}
	else {strValue = oElem};
    //alert(strValue)
    
    var url="util/FillCarlineXML.aspx";
    //var strParams ="makeid=" + strValue + "&fid=" + oTarget.form.id + "&tid=" + oTarget.id;
    var strParams ="makeid=" + strValue + "&tid=" + oTarget.id;
    url=url + "?" + strParams ; 
    //alert('url:' + url);
    clearCombo(oTarget);
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=FillDropDown;  
    xmlHttp.send(null);	
	}

}

function FillService(oElem,oTarget)
{
    xmlHttp=GetXmlHttpObject();

    var strValue=oElem.options[oElem.selectedIndex].value;
    var url="util/FillServiceXML.aspx";
    //var strParams ="modelid=" + strValue + "&fid=" + oTarget.form.id + "&tid=" + oTarget.id;
    var strParams ="modelid=" + strValue + "&tid=" + oTarget.id;
    url=url + "?" + strParams ; 
    clearCombo(oTarget);
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=FillDropDown;  
    xmlHttp.send(null);
}

// ******************* WebRequest area - Begin *******************************

function makeWebRequest(url){    
    var request = new Sys.Net.WebRequest();
    request.set_url(url);
    request.add_completed(onRequestCompleted);
    request.invoke();    
}

function onRequestCompleted(executor, args){  
    var e = executor.checkError();   
    if (e) {
        returnWebResults("error: " + e.message);
    }
    else {
        returnWebResults(executor.get_responseData());
    }             
}

Sys.Net.WebRequestExecutor.prototype.checkError = function() {
    
// Status Code Range	Meaning
// 100-199	Informational status codes. These status codes will not normally arise during ASP.NET Ajax development and can be ignored.
// 200-299	The request was received and successfully processed.
// 300-399	The request needs to be redirected. The most common code in this range is HTTP status code 302 which is sent when Response.Redirect is called from ASP.NET code.
// 400-499	The request contains an error. A Common error code is 404 which indicates that the resource (file) was not found on the server.
// 500-599	The request was valid but the server failed to process the request. The most common error code in this range is 500, which is returned by ASP.NET when an exception occurs during request processing.                        
    
var e = null;

if (this.get_aborted()) {
    e = Error.create('Request Aborted.', 
        { name : 'RequestAbortedError' });
}
else if (this.get_timedOut()) {
    e = Error.create('Request timed out.', 
        { name : 'RequestTimeoutError' } );
}
else {
    var statusCode;
    try {
        statusCode = this.get_statusCode();
    }
    catch(e) {
        statusCode = 0;
    }

    if (statusCode < 100 || statusCode >= 600) {
        e = Error.create('Connection Error.', 
            {name : 'ConnectionError' });   
    }
    else if ((statusCode < 200) || (statusCode >= 300)) {
        e = Error.create('HTTP Error.', 
            { name : 'HTTPError', 
            "statusCode" : statusCode, 
            statusText : this.get_statusText() } );
    }	
}

return e;
}

// ******************* WebRequest area - End *******************************

//****************** Webrequest 2 JS *****************************
function makeWebRequest2(url){  
    xmlHttp=GetXmlHttpObject();
    xmlHttp.open("GET",url,true);
    xmlHttp.onreadystatechange=handleHTTPRequest2;  
    xmlHttp.send(null);	
}

function handleHTTPRequest2(){
    if (xmlHttp.readyState==4 && xmlHttp.status==200){
        //alert('handleHTTPRequest2');
    }
}
//****************** Webrequest 2 JS ****************************</>*

//************* Windows dimension : width & height ******************
function screenDimension() {
    this._width = 0;
    this._height = 0;
    this._browser = Sys.Browser.name;
    this._version = Sys.Browser.version;

    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        this._width = window.innerWidth;
        this._height = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        this._width = document.documentElement.clientWidth;
        this._height = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        this._width = document.body.clientWidth;
        this._height = document.body.clientHeight;
    }
}
//************* Windows dimension : width & height ******************
