// ---------------- JAVASCRIPT AJAX FOR SITE -------------------------------//

//===================================================================================================================
// General Function for the Ajax
//===================================================================================================================
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;
}
//===================================================================================================================

//===================================================================================================================
// Get Final Total Days 
//===================================================================================================================

function send_form(rezID)
{
	var answer = confirm("are you sure you wnat to send the reservation form to this client?")
	if (answer)
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
		 	 alert ("Your browser does not support AJAX!");
		 	 return;
		  } 
		
			var url="fm_ajax.asp";
			url=url+"?Idrez="+rezID;
			url=url+"&task=3";
			url=url+"&sidy="+Math.random();
		
			xmlHttp.onreadystatechange=function()
			{ 
				if (xmlHttp.readyState==4)
				{ 
					resultsx= xmlHttp.responseText;
					alert(resultsx);
				}
			}
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}







function develop_days()
{


xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
 	 alert ("Your browser does not support AJAX!");
 	 return;
  } 


var pd=document.getElementById('pd').value;
var tide=document.getElementById('theidelement').value;
var rd=document.getElementById('rd').value;
var hcount=document.getElementById('hcount').value;
var pth=document.getElementById('pth').options[document.getElementById('pth').selectedIndex].value;
var rth=document.getElementById('rth').options[document.getElementById('rth').selectedIndex].value;
var rtm=document.getElementById('rtm').options[document.getElementById('rtm').selectedIndex].value;
var ptm=document.getElementById('ptm').options[document.getElementById('ptm').selectedIndex].value;
var mtd=document.getElementById('mtd').value;
var ftd;
var td,sx1;
var new_total_rental_costs,dObj,idval,idcosty,genx,sxy,temp;



//===================================================================================================================
// Elements needed to  update the prices with relation to the change of date
//===================================================================================================================
	var teo=document.getElementById('teo').value; //Total ExtraCost
	var trc=document.getElementById('trc').value;  // Total Rental Costs
	var dcost=document.getElementById('cost').value; //  The Rental Cost Per day
	var oc=document.getElementById('oc').value;
	var doc=document.getElementById('doc').value;
	var puc=document.getElementById('puc').value;
	var dis=document.getElementById('dis').value;
	var Kkm = document.getElementById('dkd').value;
//===================================================================================================================

//===================================================================================================================


var url="fm_ajax.asp";
url=url+"?pd="+pd;
url=url+"&rd="+rd;
url=url+"&pth="+pth;
url=url+"&rth="+rth;
url=url+"&ptm="+ptm;
url=url+"&rtm="+rtm;
url=url+"&task=1";
url=url+"&sidy="+Math.random();
xmlHttp.onreadystatechange=function()
{ 
	if (xmlHttp.readyState==4)
	{ 
		resultsx= xmlHttp.responseText
		document.getElementById('td').value = resultsx
		td = resultsx
		
		if (parseFloat(mtd) > parseFloat(td) || parseFloat(mtd) == parseFloat(td)) 
		{
			document.getElementById('mtd').value = 0;
			document.getElementById('ftd').value = td;
			ftd = td;
			sx1 = 1;
			do
			{
				document.getElementById('perday'+sx1).value = td;
				sx1++
			}
			while (sx1<=parseFloat(hcount));
			
			//======================================================================================
			//  At this point we have the new final Total Days WE UPDATE ALL FROM HERE (must duplicate for the else
			//======================================================================================
			// A. Updating The Total Rental Costs
				if (parseFloat(dcost) == 0 || parseFloat(dcost) < 0 )
				{
					document.getElementById('trc').value = 0
					document.getElementById('cost').value = 0
				}
				else
				{
					document.getElementById('trc').value = parseFloat(dcost) * parseFloat(td)
					new_total_rental_costs = parseFloat(dcost) * parseFloat(td)
					document.getElementById('gt').value = (parseFloat(new_total_rental_costs) + parseFloat(document.getElementById('teo').value) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat( (parseFloat(new_total_rental_costs) + parseFloat(document.getElementById('teo').value) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
					
				}
			//================================================================================================
			// B. We must loop through the checked Extra options and recalculate them in order for us to make the selection or better still just uncheck all arnd reset to zero
			//================================================================================================
				genx = tide.split("^");
				temp = 0;
				for (sxy = 0; sxy < genx.length; sxy++)
					{
						//At this point i can loop through all the checked boxes
						dObj = document.getElementById('theoption'+genx[sxy])
						idval =document.getElementById('theoption'+genx[sxy]).value
						idcosty =document.getElementById('costy'+genx[sxy]).value
						
						if (dObj.checked == 1)
						{
							temp = temp + (parseFloat(idcosty) * parseFloat(ftd))
							if (temp < 0)
							{
								document.getElementById('teo').value = 0
								document.getElementById('gt').value =(0 + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((0 + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
							}
							else
							{
								document.getElementById('teo').value = temp
								document.getElementById('gt').value =(parseFloat(temp) + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(temp) + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
							}
						}
					
					}
			//================================================================================================
			
		}
		else
		{
			ftd = parseFloat(td) - parseFloat(mtd)		
			document.getElementById('ftd').value = ftd;
			
			//sx1 = 1;
			//do
		//	{
		//		document.getElementById('perday'+sx1).value = ftd;
			//	sx1++
			//}
			//while (sx1<=parseFloat(hcount));
			
			//======================================================================================
			//  At this point we have the new final Total Days WE UPDATE ALL FROM HERE (must duplicate for the else
			//======================================================================================
			// A. Updating The Total Rental Costs
				if (parseFloat(dcost) == 0 || parseFloat(dcost) < 0 )
				{
					document.getElementById('trc').value = 0
					document.getElementById('cost').value = 0
				}
				else
				{
					document.getElementById('trc').value = parseFloat(dcost) * parseFloat(ftd)
					new_total_rental_costs = parseFloat(dcost) * parseFloat(ftd)
					document.getElementById('gt').value = (parseFloat(new_total_rental_costs) + parseFloat(document.getElementById('teo').value) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(new_total_rental_costs) + parseFloat(document.getElementById('teo').value) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
				}
			//================================================================================================
			// B. We must loop through the checked Extra options and recalculate them in order for us to make the selection or better still just uncheck all arnd reset to zero
			//================================================================================================
				genx = tide.split("^");
				temp = 0;
				for (sxy = 0; sxy < genx.length; sxy++)
					{
						//At this point i can loop through all the checked boxes
						dObj = document.getElementById('theoption'+genx[sxy])
						idval =document.getElementById('theoption'+genx[sxy]).value
						idcosty =document.getElementById('costy'+genx[sxy]).value
						
						if (dObj.checked == 1)
						{
							temp = temp + (parseFloat(idcosty) * parseFloat(ftd))
							if (temp < 0)
							{
								document.getElementById('teo').value = 0
								document.getElementById('gt').value =(0 + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((0 + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
							}
							else
							{
								document.getElementById('teo').value = temp
								document.getElementById('gt').value =(parseFloat(temp) + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(temp) + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
							}
						}
					
					}
			//================================================================================================
			
		}
		
		if (td == 0)
		{
			document.getElementById('idmodel').selectedIndex= 0;
			document.getElementById('idmodel').disabled = true;
			document.getElementById('cost').value = 0
		}
		else
		{
			document.getElementById('idmodel').disabled = false;
			
			if (document.getElementById('idmodel').selectedIndex > 0)
			{
				update_cost();
			}
			if(ftd <= parseFloat(document.getElementById('dck').value))
			{
				document.getElementById('kpd').value = document.getElementById('dkd').value;
			}
			else
			{
				document.getElementById('kpd').value = 0;
			}
		}
	}
}
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
//===================================================================================================================

//===================================================================================================================
// Updating notegroup in the system
//===================================================================================================================
function update_ftd()
{

var td=document.getElementById('td').value;
var mtd=document.getElementById('mtd').value;
var hcount=document.getElementById('hcount').value;
var ftd;
var td,sx1;
var new_total_rental_costs,dObj,idval,idcosty,genx,sxy,temp;
var tide=document.getElementById('theidelement').value;

if (mtd!= null && !mtd.toString().match(/^[-]?\d*\.?\d*$/) || mtd == "")
{
	mtd = 0;
	document.getElementById('mtd').value = 0;
}

//===================================================================================================================
// Elements needed to  update the prices with relation to the change of date
//===================================================================================================================
	var teo=document.getElementById('teo').value; //Total ExtraCost
	var trc=document.getElementById('trc').value;  // Total Rental Costs
	var dcost=document.getElementById('cost').value; //  The Rental Cost Per day
	var oc=document.getElementById('oc').value;
	var doc=document.getElementById('doc').value;
	var puc=document.getElementById('puc').value;
	var dis=document.getElementById('dis').value;
//===================================================================================================================

//===================================================================================================================


	
	if (parseFloat(mtd) > parseFloat(td) || parseFloat(mtd) == parseFloat(td)) 
	{
		document.getElementById('mtd').value = 0;
		document.getElementById('ftd').value = td;
		ftd = td;	
		
		
			if(ftd <= parseFloat(document.getElementById('dck').value))
			{
				document.getElementById('kpd').value = document.getElementById('dkd').value;
			}
			else
			{
				document.getElementById('kpd').value = 0;
			}
			
			
			sx1 = 1;
			do
			{
				document.getElementById('perday'+sx1).value = td;
				sx1++
			}
			while (sx1<=parseFloat(hcount));
			//alert("hi");
			//======================================================================================
			//  At this point we have the new final Total Days WE UPDATE ALL FROM HERE (must duplicate for the else
			//======================================================================================
			// A. Updating The Total Rental Costs
				if (parseFloat(dcost) == 0 || parseFloat(dcost) < 0 )
				{
					document.getElementById('trc').value = 0
					document.getElementById('cost').value = 0
				}
				else
				{
					document.getElementById('trc').value = parseFloat(dcost) * parseFloat(td)
					new_total_rental_costs = parseFloat(dcost) * parseFloat(td)
					document.getElementById('gt').value = (parseFloat(new_total_rental_costs) + parseFloat(document.getElementById('teo').value) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(new_total_rental_costs) + parseFloat(document.getElementById('teo').value) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
				}
			//================================================================================================
			// B. We must loop through the checked Extra options and recalculate them in order for us to make the selection or better still just uncheck all arnd reset to zero
			//================================================================================================
				genx = tide.split("^");
				temp = 0;
				for (sxy = 0; sxy < genx.length; sxy++)
					{
						//At this point i can loop through all the checked boxes
						dObj = document.getElementById('theoption'+genx[sxy])
						idval =document.getElementById('theoption'+genx[sxy]).value
						idcosty =document.getElementById('costy'+genx[sxy]).value
						
						if (dObj.checked == 1)
						{
							temp = temp + (parseFloat(idcosty) * parseFloat(ftd))
							if (temp < 0)
							{
								document.getElementById('teo').value = 0
								document.getElementById('gt').value =(0 + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((0 + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
							}
							else
							{
								document.getElementById('teo').value = temp
								document.getElementById('gt').value =(parseFloat(temp) + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(temp) + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
							}
						}
					
					}
			//================================================================================================
			
	}
	else
	{
		ftd = parseFloat(td) - parseFloat(mtd)		
		document.getElementById('ftd').value = ftd;
		
		
			if(ftd <= parseFloat(document.getElementById('dck').value))
			{
				document.getElementById('kpd').value = document.getElementById('dkd').value;
			}
			else
			{
				document.getElementById('kpd').value = 0;
			}
			
			
			//sx1 = 1;
			//do
			//{
			//	document.getElementById('perday'+sx1).value = ftd;
			//	sx1++
			//}
			//while (sx1<=parseFloat(hcount));
			
			//======================================================================================
			//  At this point we have the new final Total Days WE UPDATE ALL FROM HERE (must duplicate for the else
			//======================================================================================
			// A. Updating The Total Rental Costs
				if (parseFloat(dcost) == 0 || parseFloat(dcost) < 0 )
				{
					document.getElementById('trc').value = 0
					document.getElementById('cost').value = 0
				}
				else
				{
					document.getElementById('trc').value = parseFloat(dcost) * parseFloat(ftd)
					new_total_rental_costs = parseFloat(dcost) * parseFloat(ftd)
					document.getElementById('gt').value = (parseFloat(new_total_rental_costs) + parseFloat(document.getElementById('teo').value) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(new_total_rental_costs) + parseFloat(document.getElementById('teo').value) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
				}
			///================================================================================================
			// B. We must loop through the checked Extra options and recalculate them in order for us to make the selection or better still just uncheck all arnd reset to zero
			//================================================================================================
				genx = tide.split("^");
				temp = 0;
				for (sxy = 0; sxy < genx.length; sxy++)
					{
						//At this point i can loop through all the checked boxes
						dObj = document.getElementById('theoption'+genx[sxy])
						idval =document.getElementById('theoption'+genx[sxy]).value
						idcosty =document.getElementById('costy'+genx[sxy]).value
						
						if (dObj.checked == 1)
						{
							temp = temp + (parseFloat(idcosty) * parseFloat(ftd))
							if (temp < 0)
							{
								document.getElementById('teo').value = 0
								document.getElementById('gt').value =(0 + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((0 + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
							}
							else
							{
								document.getElementById('teo').value = temp
								document.getElementById('gt').value = (parseFloat(temp) + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(temp) + parseFloat(new_total_rental_costs) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
							}
						}
					
					}
			//================================================================================================
	}
	
	if (td == 0)
	{
		document.getElementById('idmodel').selectedIndex= 0;
		document.getElementById('idmodel').disabled = true;
	}
	else
	{
		document.getElementById('idmodel').disabled = false;
		if (document.getElementById('idmodel').selectedIndex > 0)
		{
			update_cost();
		}
	}
}
//===================================================================================================================

//===================================================================================================================
// Updating rental costs
//===================================================================================================================
function update_cost()
{

xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
 	 alert ("Your browser does not support AJAX!");
 	 return;
  } 

var oc=document.getElementById('oc').value;
	var doc=document.getElementById('doc').value;
	var puc=document.getElementById('puc').value;
var dis=document.getElementById('dis').value;
var ftd=document.getElementById('ftd').value;
var teo=document.getElementById('teo').value;
var idmodel=document.getElementById('idmodel').options[document.getElementById('idmodel').selectedIndex].value;

var url="fm_ajax.asp";
url=url+"?idmodel="+idmodel;
url=url+"&ftd="+ftd;
url=url+"&task=2"
url=url+"&sidy="+Math.random();

xmlHttp.onreadystatechange=function()
{ 
	if (xmlHttp.readyState==4)
	{ 
		resulty= xmlHttp.responseText
		document.getElementById('cost').value = resulty
		document.getElementById('trc').value = parseFloat(resulty) *parseFloat(ftd) 
		document.getElementById('gt').value = ((parseFloat(resulty) *parseFloat(ftd)) + parseFloat(teo) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)
		document.getElementById('gt_vat_plus').value= parseFloat(((parseFloat(resulty) *parseFloat(ftd)) + parseFloat(teo) + parseFloat(oc) + parseFloat(doc) + parseFloat(puc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
	}
}
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}
//===================================================================================================================

//===================================================================================================================
// Updating Extracost
//===================================================================================================================
function update_teo(IdElem)
{
	var oc=document.getElementById('oc').value;
	var doc=document.getElementById('doc').value;
	var puc=document.getElementById('puc').value;
	var dis=document.getElementById('dis').value;
	var teo=document.getElementById('teo').value;
	var trc=document.getElementById('trc').value;
	var ftd=document.getElementById('ftd').value;
	var dObj = document.getElementById(IdElem)
	var idval =document.getElementById(IdElem).value
	var idcosty =document.getElementById('costy'+idval).value
	var idday =document.getElementById('perday'+idval).value
	var temp
	
	//Fix Value
	if (idcosty!= null && !idcosty.toString().match(/^[-]?\d*\.?\d*$/) || idcosty == "")
		{
			idcosty = document.getElementById('costyH'+idval).value;
			document.getElementById('costy'+idval).value = document.getElementById('costyH'+idval).value;
		}
	if (idday!= null && !idday.toString().match(/^[-]?\d*\.?\d*$/) || idday == "")
		{
			idday = document.getElementById('perdayH'+idval).value;
			document.getElementById('perday'+idval).value = document.getElementById('perdayH'+idval).value;
		}
	
	if (dObj.checked == 1)
	{
		temp = parseFloat(teo) + (parseFloat(idcosty) * parseFloat(idday))
		if (temp < 0)
		{
			document.getElementById('teo').value = 0
			document.getElementById('gt').value = (0 + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)
			document.getElementById('gt_vat_plus').value= parseFloat((0 + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
		}
		else
		{
			document.getElementById('teo').value = temp
			document.getElementById('gt').value =(parseFloat(temp) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(temp) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
		}
	}
	else
	{
		temp = parseFloat(teo)-(parseFloat(idcosty) * parseFloat(idday))
		if (temp < 0)
		{
			document.getElementById('teo').value = 0
			document.getElementById('gt').value = (0 + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((0 + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
		}
		else
		{
			document.getElementById('teo').value = temp
			document.getElementById('gt').value = (parseFloat(temp) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)
					document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(temp) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
		}
	}
	
}
//===================================================================================================================

//===================================================================================================================
// Updating Price of extracost
// Check if its is checked, if YES uncheck and deduct from the price total
//===================================================================================================================


function update_teo_price(IdElem)
{
	
	var oc=document.getElementById('oc').value;
	var doc=document.getElementById('doc').value;
	var puc=document.getElementById('puc').value;
	var dis=document.getElementById('dis').value;
	var teo=document.getElementById('teo').value;
	var trc=document.getElementById('trc').value;
	var ftd=document.getElementById('ftd').value;
	var dObj = document.getElementById(IdElem);
	var idval =document.getElementById(IdElem).value;
	var idcosty =document.getElementById('costy'+idval).value;
	var idday =document.getElementById('perday'+idval).value;
	var temp, newteo;
	
	//=============================================================================
	//Fix Value if blank or non numeric
	//=============================================================================
	if (idcosty!= null && !idcosty.toString().match(/^[-]?\d*\.?\d*$/) || idcosty == "")
		{
			idcosty = document.getElementById('costyH'+idval).value;
			document.getElementById('costy'+idval).value = document.getElementById('costyH'+idval).value;
		}
	if (idday!= null && !idday.toString().match(/^[-]?\d*\.?\d*$/) || idday == "")
		{
			idday = document.getElementById('perdayH'+idval).value;
			document.getElementById('perday'+idval).value = document.getElementById('perdayH'+idval).value;
		}
	//=============================================================================
	
	
	if (dObj.checked == 1)
	{
		// REMOVE THE OLD
		temp = parseFloat(teo)-(parseFloat(document.getElementById('costyH'+idval).value) * parseFloat(document.getElementById('perdayH'+idval).value))
		
		//CALCULATE THE NEW T.E.O AND UPDATE VISIBLE VALUE
		newteo =parseFloat(temp)+(parseFloat(idcosty) * parseFloat(idday));
		document.getElementById('teo').value= newteo;
		document.getElementById('perdayH'+idval).value = parseFloat(idday)
		document.getElementById('costyH'+idval).value = parseFloat(idcosty)
				
		//CALCULATE AND UPDATE GRAND TOTAL
		document.getElementById('gt').value = (parseFloat(newteo) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis);
		mg_v = document.getElementById('gt_vat_plus').value;
		document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
	}
	
}




//===================================================================================================================

//===================================================================================================================
// Add / Subtract To Grand Total
//===================================================================================================================
function add_oc()
{
	var teo=document.getElementById('teo').value;
	var trc=document.getElementById('trc').value;
	var oc=document.getElementById('oc').value;
	var doc=document.getElementById('doc').value;
	var puc=document.getElementById('puc').value;
	var dis=document.getElementById('dis').value;
	var gt=document.getElementById('gt').value;
	var hvalue = document.getElementById('och').value
	
	if (oc != null && !oc.toString().match(/^[-]?\d*\.?\d*$/) || oc=="")
	{
		document.getElementById('oc').value = hvalue;
	}else
	{
		//Store the new value in the hidden place
		document.getElementById('och').value = oc;
		
		//Now Update the grand Total
		document.getElementById('gt').value = (parseFloat(teo) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)
		document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(teo) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
		mg_v = document.getElementById('gt_vat_plus').value;
		document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
	}
	
}

function add_doc()
{
	var teo=document.getElementById('teo').value;
	var trc=document.getElementById('trc').value;
	var oc=document.getElementById('oc').value;
	var doc=document.getElementById('doc').value;
	var puc=document.getElementById('puc').value;
	var dis=document.getElementById('dis').value;
	var gt=document.getElementById('gt').value;
	var hvalue = document.getElementById('doch').value
	
	if (doc != null && !doc.toString().match(/^[-]?\d*\.?\d*$/) || doc=="")
	{
		document.getElementById('doc').value = hvalue;
	}else
	{
		//Store the new value in the hidden place
		document.getElementById('doch').value = doc;
		
		//Now Update the grand Total
		document.getElementById('gt').value = (parseFloat(teo) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)
		document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(teo) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
		mg_v = document.getElementById('gt_vat_plus').value;
		document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
	}
	
}

function add_puc()
{
	
	var teo=document.getElementById('teo').value;
	var trc=document.getElementById('trc').value;
	var oc=document.getElementById('oc').value;
	var doc=document.getElementById('doc').value;
	var puc=document.getElementById('puc').value;
	var dis=document.getElementById('dis').value;
	var gt=document.getElementById('gt').value;
	var hvalue = document.getElementById('puch').value
	
	if (puc != null && !puc.toString().match(/^[-]?\d*\.?\d*$/) || puc=="")
	{
		document.getElementById('puc').value = hvalue;
	}else
	{
		//Store the new value in the hidden place
		document.getElementById('puch').value = puc;
		
		//Now Update the grand Total
		document.getElementById('gt').value = (parseFloat(teo) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)
		document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(teo) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc) + parseFloat(trc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
		mg_v = document.getElementById('gt_vat_plus').value;
		document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
	}
	
}


function sub_dis()
{
	var teo=document.getElementById('teo').value;
	var trc=document.getElementById('trc').value;
	var oc=document.getElementById('oc').value;
	var doc=document.getElementById('doc').value;
	var puc=document.getElementById('puc').value;
	var dis=document.getElementById('dis').value;
	var gt=document.getElementById('gt').value;
	var hvalue = document.getElementById('dish').value
	
	if (dis!= null && !dis.toString().match(/^[-]?\d*\.?\d*$/) || dis=="")
	{
		document.getElementById('dis').value = hvalue;
	}
		else
	{
		//Store the new value in the hidden place
		document.getElementById('dish').value = dis;
		
		//Now Update the grand Total
		document.getElementById('gt').value = (parseFloat(teo) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc)  + parseFloat(trc)) - parseFloat(dis)
		document.getElementById('gt_vat_plus').value= parseFloat((parseFloat(teo) + parseFloat(oc) + parseFloat(puc) + parseFloat(doc)  + parseFloat(trc)) - parseFloat(dis)) * parseFloat(document.getElementById('vat_multiplier').value)
					mg_v = document.getElementById('gt_vat_plus').value;
					document.getElementById('gt_vat_plus').value = mg_v.tofixed(2);
	}
	
}

function set_zero(myalue,element)
{
	
	if (myalue!= null && !myalue.toString().match(/^[-]?\d*\.?\d*$/) || myalue=="")
	{
		document.getElementById(element).value = 0;
	}
	
}
//===================================================================================================================



