// JavaScript Document

var ajax = null; 

// Create AJAX Object
function createObject()
{
	var ajaxObj;
	if (window.XMLHttpRequest)
	{
		ajaxObj = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		ajaxObj = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return ajaxObj;	
}


// Pass the url and function name here
function makeRequest(url,functionName,veh_id,mileage_type,boxtype)
{
	
	document.getElementById('pricediv_'+veh_id).innerHTML = "<img src='images/loading.gif'>";
	
	
		
	// This will populate the period selectboxes
	
	// only mileage box can initiate this
	if (boxtype==2)
	{
		makeRequest2('ajax_populate_periodbox.php?vehicle_id='+veh_id+'&mileage_type='+mileage_type,'populate_periodbox',veh_id);
		
		document.getElementById('pricediv_'+veh_id).innerHTML = "Select Period";
		document.getElementById('pricediv_'+veh_id).style.fontSize = "10px";
		document.getElementById('pricediv_'+veh_id).style.fontWeight = "normal";
	}
	else
	{
		
		document.getElementById('pricediv_'+veh_id).style.fontSize = "13px";
		document.getElementById('pricediv_'+veh_id).style.fontWeight = "bold";
		ajax = createObject();
		ajax.onreadystatechange = eval(functionName);		
		ajax.open("GET", url, true);
		ajax.send(null);	
	}
}



// to populate vehicle box
function makeRequest2(url,functionName,veh_id)
{
	
	
	
	
	ajax2 = createObject();
	ajax2.onreadystatechange = eval(functionName);		
	ajax2.open("GET", url, true);
	ajax2.send(null);	
	
	eval("periodBox = document.form_vehicles.select_period_"+veh_id);
	periodBox.options.length = 0;
	periodBox.options[0] = new Option("Loading...", "-1");
	
	window["js_veh_id"] = veh_id;
	
	
}






// Specific Functions
function populate_periodbox()
{
	
	
	
	eval("periodBox = document.form_vehicles.select_period_"+js_veh_id);
	
	
	
	if (ajax2.readyState==4 && ajax2.status==200)
	{
		
		
		xmlData = ajax2.responseXML;
		rootNode = xmlData.documentElement;
		periodBox.options.length = 0;
		
		periodBox.options[0] = new Option("-Select Period-", "-1");
		
		
		if (rootNode != null)
		{
			
		
			if (rootNode.hasChildNodes())
			{
				
				total = rootNode.childNodes.length;
				
				periodBox.style.size = total;
				
				
				for(k=0;k<rootNode.childNodes.length;k++)
				{
				
				
					
				periodBox.options[k+1] = new Option(rootNode.childNodes[k].childNodes[0].firstChild.nodeValue, rootNode.childNodes[k].childNodes[1].firstChild.nodeValue);
				
				
				
				
				}
			}
		}
		else
		{
			periodBox.options[0] = new Option("-- No records found --", "-1");
		}
		
		
		
	}
	
}




// Specific Functions
function fetch_vehicle_price()
{
	
	
		
	if (ajax.readyState==4 && ajax.status==200)
	{
		xmlData = ajax.responseText;
		xmlArray = xmlData.split("@");
		divobj = "pricediv_"+xmlArray[1];
		
		a = eval(xmlArray[1]);
		document.getElementById('pricediv_'+a).innerHTML = xmlArray[0];
		
		
		
	}
	else
	{
		
		//document.getElementById(divname).innerHTML = "Processing...";
	}
	
}





// Specific Functions
function checkGroupAvailability()
{
	
	
		
	if (ajax.readyState==4 && ajax.status==200)
	{
		xmlData = ajax.responseText;
		document.getElementById("showresultG").innerHTML = xmlData;
		
	}
	else
	{
		
		document.getElementById("showresultG").innerHTML = "Processing...";
	}
	
}
