//  Developed by Roshan Bhattarai 
//  Visit http://roshanbh.com.np for this script and more.
//  This notice MUST stay intact for legal use

//fuction to return the xml http object
function getXMLHTTP() { 
		var xmlhttp=false;	
		try{
			xmlhttp=new XMLHttpRequest();
		}
		catch(e)	{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e){
				try{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1){
					xmlhttp=false;
				}
			}
		}
		 	
		return xmlhttp;
	}
	
	
	
function getProjectLocation(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('Location').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getProjectName(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('ProjectName').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getOriginatorCompany(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('OriginatorCompany').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getOriginatorTitle(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('OriginatorTitle').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getOriginatorEmail(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('OrigEmail').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getOriginatorStreet(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('OrigStreet').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getOriginatorCity(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('OrigCity').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getOriginatorState(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('OrigState').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getOriginatorPhone(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('OrigPhone').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getContractorPO(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('AffectedContractor1PO').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getVendorPO(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('VendorPO').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getEngrApprovalTitle(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('EngrApprovalTitle').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getAuthorizedTitle(strURL)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('PMAuthorizedTitle').value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getTotalHours(strURL)
{		
	var req = getXMLHTTP();
	var CostMhrsD = parseFloat(document.getElementById('CostMhrsD').value);
	var CostMhrsID = parseFloat(document.getElementById('CostMhrsID').value);
	var CostMhrsSC = parseFloat(document.getElementById('CostMhrsSC').value);
	var result 
	if (isNaN(CostMhrsD))
	{
		CostMhrsD = 0;
	}
	if (isNaN(CostMhrsID))
	{
		CostMhrsID = 0;
	}
	if (isNaN(CostMhrsSC))
	{
		CostMhrsSC = 0;
	}
	result = CostMhrsD + CostMhrsID + CostMhrsSC;
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('CostMhrsTotal').value=result.toFixed(2);
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getFinalHours(strURL)
{		
	var req = getXMLHTTP();
	var CostFinalMhrsD = parseFloat(document.getElementById('CostFinalMhrsD').value);
	var CostFinalMhrsID = parseFloat(document.getElementById('CostFinalMhrsID').value);
	var CostFinalMhrsSC = parseFloat(document.getElementById('CostFinalMhrsSC').value);
	var result
	if (isNaN(CostFinalMhrsD))
	{
		CostFinalMhrsD = 0;
	}
	if (isNaN(CostFinalMhrsID))
	{
		CostFinalMhrsID = 0;
	}
	if (isNaN(CostFinalMhrsSC))
	{
		CostFinalMhrsSC = 0;
	}
	result = CostFinalMhrsD + CostFinalMhrsID + CostFinalMhrsSC;
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('CostFinalMhrsTotal').value=result.toFixed(2);
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getTotalCost(strURL)
{		
	var req = getXMLHTTP();
	var CostLaborD = parseFloat(document.getElementById('CostLaborD').value);
	var CostLaborID = parseFloat(document.getElementById('CostLaborID').value);
	var CostMatl = parseFloat(document.getElementById('CostMatl').value);
	var CostEqpt = parseFloat(document.getElementById('CostEqpt').value);
	var CostSC = parseFloat(document.getElementById('CostSC').value);
	var CostOther = parseFloat(document.getElementById('CostOther').value);
	var result
	if (isNaN(CostLaborD))
	{
		CostLaborD = 0;
	}
	if (isNaN(CostLaborID))
	{
		CostLaborID = 0;
	}
	if (isNaN(CostMatl))
	{
		CostMatl = 0;
	}
	if (isNaN(CostEqpt))
	{
		CostEqpt = 0;
	}
	if (isNaN(CostSC))
	{
		CostSC = 0;
	}
	if (isNaN(CostOther))
	{
		CostOther = 0;
	}
	result = CostLaborD + CostLaborID + CostMatl + CostEqpt + CostSC + CostOther;
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('CostTotal').value=result.toFixed(2);
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getFinalCost(strURL)
{		
	var req = getXMLHTTP();
	var CostFinalLaborD = parseFloat(document.getElementById('CostFinalLaborD').value);
	var CostFinalLaborID = parseFloat(document.getElementById('CostFinalLaborID').value);
	var CostFinalMatl = parseFloat(document.getElementById('CostFinalMatl').value);
	var CostFinalEqpt = parseFloat(document.getElementById('CostFinalEqpt').value);
	var CostFinalSC = parseFloat(document.getElementById('CostFinalSC').value);
	var CostFinalOther = parseFloat(document.getElementById('CostFinalOther').value);
	var result
	if (isNaN(CostFinalLaborD))
	{
		CostFinalLaborD = 0;
	}
	if (isNaN(CostFinalLaborID))
	{
		CostFinalLaborID = 0;
	}
	if (isNaN(CostFinalMatl))
	{
		CostFinalMatl = 0;
	}
	if (isNaN(CostFinalEqpt))
	{
		CostFinalEqpt = 0;
	}
	if (isNaN(CostFinalSC))
	{
		CostFinalSC = 0;
	}
	if (isNaN(CostFinalOther))
	{
		CostFinalOther = 0;
	}
	result = CostFinalLaborD + CostFinalLaborID + CostFinalMatl + CostFinalEqpt + CostFinalSC + CostFinalOther;
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById('CostFinalTotal').value=result.toFixed(2);
					if (document.getElementById('CO_Invoice')!= null) { //Check if field exists
						document.getElementById('CO_Invoice').value=result.toFixed(2);
					}
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}

function getFieldValue(strURL,FieldName)
{		
	var req = getXMLHTTP();		
	if (req) 
	{
		//function to be called when state is changed
		req.onreadystatechange = function()
		{
			//when state is completed i.e 4
			if (req.readyState == 4) 
			{			
				// only if http status is "OK"
				if (req.status == 200)
				{						
					document.getElementById(FieldName).value=req.responseText;
				} 
				else 
				{
					alert("There was a problem while using XMLHTTP:\n" + req.statusText);
				}
			}				
		 }			
		 req.open("GET", strURL, true);
		 req.send(null);
	}			
}
