function getHTTPObject() {
  var xmlhttp;

  if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject){
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    if (!xmlhttp){
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    }

}
  return xmlhttp;


}
var http = getHTTPObject(); // We create the HTTP Object

/*
	Funtion Name=requestInfo
	Param = url >> Url to call : id = Passing div id for multiple use ~ as a seprator for eg. div1~div2 :
	redirectPage >> if you like to redirect to other page once the event success then
	the response text = 1 and the redirectPage not left empty
*/
    function requestInfo(url,id,redirectPage) {
    sessioncheck();
		var temp=new Array();
			http.open("GET", url, true);
		    if (document.getElementById("divloading"))
		    {
				document.getElementById("divloading").style.visibility="visible";
		    }

			http.onreadystatechange = function() {
				if (http.readyState == 4) {
				  if(http.status==200) {
			  		var results=http.responseText;


					if(redirectPage=="" || results!="1") {

						var temp=id.split("~"); // To display on multiple div

						var r=results.split("~"); // To display multiple data into the div

						if(temp.length>1) {
							for(i=0;i<temp.length;i++) {
								document.getElementById(temp[i]).innerHTML=r[i];
							}
						} else {

							if(id=="div_valid_unitno")
							{
								if(results.indexOf("~")<0)
								{

									document.getElementById("djsequip_rate").value="";
									document.getElementById("djsequip_equip_id").value="";
									document.getElementById("equip_type").focus();

									alert("Invalid Equipment Model / Unit Number. You seem to be adding a type/unit number outside of the master database. Please choose the one from the drop down  ");
								} else {

									// 1###data###fieldnames###data

									var ardata = results.split(/###/gi);
									var listype = ardata[0];
									var arfieldname = ardata[1].split(/,/gi);
									var arvalue = ardata[2].split(/~/gi);

									document.getElementById("djsequip_rate").value=arvalue[2];
									document.getElementById("djsequip_equip_id").value=arvalue[0];

								}

							} else {
							 document.getElementById(id).innerHTML = results;
							}
						}

						if (document.getElementById("divloading"))
						{
							document.getElementById("divloading").style.visibility="hidden";
						}

						if (id=="div_popmessage")
						{
							loadmsgbox(400,150);
						}
						
						if(url.indexOf("datagrid/")>-1)
						{
							subtotal();
							if(url.indexOf("mode=delete")>-1 || url.indexOf("mode=update_data")>-1)
								{
                                         totalAmount=document.getElementById("total_cost").value;
										 var str=url.split("&djs_id=");
										 if(str[1]!=null)
											 {

								              Updatedjs_delete(str[1],totalAmount);
											}

						}
						}

					} else {
						alert(results);
						window.location.href=redirectPage;
					}
				  }
  				}
			};
			http.send(null);
       }


/*
function requestInfo(url,id,redirectPage) {

			http = new getHTTPObject(); // We create the HTTP Object

			http.open("GET", url, true);
	alert(id);
			http.onreadystatechange = function() {
				if (http.readyState == 4) {

				  if(http.status==200) {
			  		var results=http.responseText;

					if(redirectPage=="" || results!="1") {
												alert(results);
						document.getElementById(id).innerHTML = results;

					} else {
						//alert(results);
						window.location.href=redirectPage;

					}
				  }
  				}
			};
			http.send(null);
       }
*/
/*
	Function Name= emptyValidation
	Desc = This function is used to validation for the empty field
	Param fieldList = This arguments set as a string varialble. you just need to supply the textbox name
	if the textbox is multiple then supply with | separator for eg. username|password
*/
function emptyValidation(fieldList,divname) {
valmsg="The field could not left empty";
if(divname == "div_djsequipment")
{
   valmsg="You cannot have any of the following fields Empty \n\n1) Unit, 2) Equipment Model, 3) Rate, 4) QTY/HRS. \n\n Please ensure that all the fields are filled in for proper output";
}
if(divname == "div_djssubcon")
{
   valmsg="You cannot have any of the following fields Empty \n\n1) Sub Equip / Materials, 2) Contracted From 3) QTY/HRS 4) Rate/Cost. \n\n Please ensure that all the fields are filled in for proper output";
}
if(divname == "div_djslabour")
{
   valmsg="You cannot have any of the following fields Empty \n\n1) Employee/Person, 2) QTY/HRS 3) Rate/Cost. \n\n Please ensure that all the fields are filled in for proper output";
}
if(divname == "div_djsmisc")
{
   valmsg="You cannot have any of the following fields Empty \n\n1) Item 2) QTY/HRS 3) Rate/Cost. \n\n Please ensure that all the fields are filled in for proper output";
}
if(divname == "div_foremanstime")
{
   valmsg="You cannot have any of the following fields Empty \n\n1) Employee 2) Travel Hrs 3) Labour Hrs 4) Operator Hrs 5) Service Hrs 6) Start Time 7) End Time. \n\n Please ensure that all the fields are filled in for proper output";
}
if(divname == "div_foremansrevenue")
{
   valmsg="You cannot have any of the following fields Empty \n\n1) Bid Item Number 2) Description 3) Estimated Quantity 4) KM Section From 5) KM Section To. \n\n Please ensure that all the fields are filled in for proper output";
}
if(divname == "div_foremansequip")
{
   valmsg="You cannot have any of the following fields Empty \n\n1) Company/Employee 2) Equipment Hired 3) PO Number 4) Ticket Number 5) Comments. \n\n Please ensure that all the fields are filled in for proper output";
}
if(divname == "div_sendforauthorize")
{
	valmsg="You cannot have any of the following fields Empty \n\n1) First name 2) Last Name 3) Email . \n\n Please ensure that all the fields are filled in for proper output";
}

		var field=new Array();
		field=fieldList.split("|");
		var counter=0;
		var fieldname;
		for(i=0;i<field.length;i++) {
			if(document.getElementById(field[i])){
				if(trim(document.getElementById(field[i]).value)=="") {			
					if(field.length>2)
					{						
						if(document.getElementById(field[i]).type!="hidden")
						{						
							if(document.getElementById(field[i]).type!="select-one")
							{
								//document.getElementById(field[i]).style.backgroundColor="#FF0000";
								document.getElementById(field[i]).focus();
							}
						}

					}					
					fieldname=field[i];
					counter++;

					if(i==0 && field.length>2) // ignore first field of djs_id  - for djs and forman form
					{
						counter=counter-1;
					}

					if(fieldname == "djsequip_operatorname")
					{
						counter=counter-1;
					}

					if(fieldname == "djsequip_fuelsur")
					{
						counter=counter-1;
					}
					
					if(fieldname == "djsequip_phase")
					{
						counter=counter-1;
					}

					if(fieldname == "djssubcon_fuelsur")
					{
						counter=counter-1;
					}
					
					if(fieldname == "djssubcon_phase")
					{
						counter=counter-1;
					}
					
					if(fieldname == "djslabour_phase")
					{
						counter=counter-1;
					}

					if(fieldname == "djsmisc_unit")
					{
						counter=counter-1;
					}
					
					if(fieldname == "djsmisc_phase")
					{
						counter=counter-1;
					}

					if(fieldname == "foremanstime_jobno")
					{	
						counter=counter-1;
					}

					if(fieldname == "foremanstime_biditem")
					{
						counter=counter-1;
					}
					
					if(fieldname == "foremanstime_comments")
					{
						counter=counter-1;
					}
					
					if(fieldname == "foremanstime_loadcount")
					{
						counter=counter-1;
					}					
					
					if(fieldname == "foremanstime_rate")
					{
						counter=counter-1;
					}
					
					if(fieldname == "foremanstime_equip")
					{
						counter=counter-1;
					}
					if(fieldname == "emp_id" || fieldname == "consultant_comp" || fieldname == "emp_phno")
					{
						counter=counter-1;
					}
					if(fieldname == "foremanstime_travelhrs")
					{
						counter=counter-1;
						document.getElementById('foremanstime_travelhrs').value = '0';
					}
					if(fieldname == "foremanstime_labourhrs")
					{
						counter=counter-1;
						document.getElementById('foremanstime_labourhrs').value = '0';
					}
					if(fieldname == "foremanstime_operatorhrs")
					{
						counter=counter-1;
						document.getElementById('foremanstime_operatorhrs').value = '0';
					}
					if(fieldname == "foremanstime_servicehrs")
					{
						counter=counter-1;
						document.getElementById('foremanstime_servicehrs').value = '0';
					}					
				} else {					
					document.getElementById(field[i]).style.backgroundColor="#FFFFFF";
				}
			}
		}

		if(counter==1) {

			// check whether Equipment Model and unit is exist
								
			if(fieldname=="djsequip_equip_id")
			{
				//document.getElementById("equip_type").focus();

				document.getElementById("djsequip_unit_id").focus();
				
				alert("Invalid Equipment Model / Unit Number. \n\n You seem to be adding a type/unit number outside of the master database. \n\n Please choose the one from the drop down");

				return false;
			 // check whether Equipment Model and unit is exist
			} else if(fieldname=="foremanstime_equip_id" && document.getElementById("foremanstime_equip").value!="")
			 {
				document.getElementById("foremanstime_equip").focus();
				alert("Invalid Equipment Unit Number. \n\n You seem to be adding a type/unit number outside of the master database. \n\n Please choose the one from the drop down");
				return false;
			} 
			else if(fieldname!="foremanstime_equip_id") {
				alert(valmsg);
				return false;
			}else if(fieldname=="foremanstime_equip_id"){
			    return true;
			}
			
			

		}else if(counter>0) {
				//alert("The Field mark as red could not left empty");
				alert(valmsg);
				return false;
		}  else {
			return true;
		}
	
}

function init_table(url) {
		//requestInfo('.php?mode=list','showTable','');
		requestInfo(url,'showTable','');
	}

	//fields='id|fname|lname' ,action='save_new' or  action='update_data'
	function save_data(fieldnames,action,pagename,divname) {
		/*
			var id=document.getElementById("id").value;
			var username=document.getElementById("username").value;
			var password=document.getElementById("password").value;
			var checkValidation=emptyValidation('id|username|password');
		*/
					
	//	alert(foremanstime_servicehrs|foremanstime_equip|);		
			sessioncheck();
			var checkValidation=emptyValidation(fieldnames,divname);
			var arfield = fieldnames.split('|');
			var str;
			str="";
			
			for(i=0;i<arfield.length;i++)
		    {
				if(document.getElementById(arfield[i]).type=="checkbox")
				{
					 if(document.getElementById(arfield[i]).checked)
					 {
						 str = str + "&" + arfield[i] +'=1';
					 }else{
					     str = str + "&" + arfield[i] +'=0';
					 }					
				} else{				
					arfield_value = document.getElementById(arfield[i]).value;
				
					arfield_value = encodeURIComponent(arfield_value);					
				str = str + "&" + arfield[i] +'='+arfield_value;				
				}										
		    }
			if(action=='update_data') {
  			  str = str + "&prev_id="+document.getElementById("prev_id").value;
  			  			}			  			
		if(checkValidation==true) {		
			requestInfo(pagename+'?mode='+action+str+'&djs_id='+document.getElementById(arfield[0]).value,divname,divname,'');
		}
	}
	
	function savedata(fieldnames,action,pagename,divname,djsid) {
			sessioncheck();					
			var checkValidation=emptyValidation(fieldnames,divname);
			var arfield = fieldnames.split('|');
			var str;
			str="";			
			for(i=0;i<arfield.length;i++)
		    {
				if(document.getElementById(arfield[i]).type=="checkbox")
				{
					 if(document.getElementById(arfield[i]).checked)
					 {
						 str = str + "&" + arfield[i] +'=1';
					 }else{
					     str = str + "&" + arfield[i] +'=0';
					 }					
				} else{				
					arfield_value = document.getElementById(arfield[i]).value;				
					arfield_value = encodeURIComponent(arfield_value);					
				str = str + "&" + arfield[i] +'='+arfield_value;				
				}										
		    }		  			
		if(checkValidation==true) {					
			requestInfo(pagename+'?mode='+action+str+'&djs_id='+djsid,divname,'');
		}
	}
	

/*	function update_data(fields) {
			var prev_id=document.getElementById("prev_id").value;

			var id=document.getElementById("id").value;
			var username=document.getElementById("username").value;
			var password=document.getElementById("password").value;
			var checkValidation=emptyValidation('id|username|password');


		if(checkValidation==true) {
			requestInfo('showTable.php?mode=update_data&id='+id+'&username='+username+'&password='+password+'&prev_id='+prev_id,'showTable','');
		}
	}

*/
function confirmLink(theLink)
{
    var is_confirmed = confirm('You are about to Delete a record, are you sure to delete this record?\n\nThe record will be deleted permanently');
    if (is_confirmed) {
        theLink.href += '';
    }

    return is_confirmed;
}

function showunitno(obj,e,unit_url)
{
  ajax_showOptions(obj,'getUnitNo',e,unit_url);
}

  
function trim(strText) 
{
  while('' + strText.charAt(0) == ' ') 
  {
	strText = strText.substring(1, strText.length);
  }

  while('' + strText.charAt(strText.length-1)==' ') 
  {
    strText = strText.substring(0, strText.length-1);
  }

 return strText;

}
