if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

function formSubmit(form,url){
	var f = document.getElementById(form);
	f.action = url;
	f.submit();
}

function rememberAndSubmit(form, array1, array2){
	var form = document.getElementById(form);
	var html = form.innerHTML;
	for(x in array1){
		html = html + '<input type="hidden" name="' + array1[x] + '" value="' + array2[x] + '">';
	}
	form.innerHTML = html;
	form.submit();
}

function tableadd(value, IdFieldProperty, IdRecord, tabletype){
	var url = "form_ajax.php";
	var post = "Action=AddSelected&IdFieldProperty=" + IdFieldProperty + "&IdRecord=" + IdRecord + "&Value=" + value + "&TableType=" + tabletype;
	AjaxRequestV2(url, post, function(responseText) { document.getElementById(IdFieldProperty+"_table").parentNode.innerHTML = responseText } );
	if(tabletype==1) {
		comments = document.getElementById(IdFieldProperty + "_comments");
		if(comments){
			document.getElementById(comments.id + "name").innerHTML = "Comments";
			comments.value = "";
			comments.name = "";
		}
	}
}

var objectInFocus;
function formSelectToggle(obj,toggle) {
	switch(toggle) {
		case 1:
			obj.style.width = "100%";
			break;
		case 2:
			if(objectInFocus != obj) {
				obj.style.width = "auto";
				if(obj.clientWidth < obj.parentNode.clientWidth)
					obj.style.width = "100%";
				objectInFocus = obj;
			}
			break;
	}
}

function getSelectedRow(id) {
	table = document.getElementById(id);
	var rows = table.parentNode.getElementsByTagName("TR");
	for(i = 0; i < rows.length; i++) {
		if(rows[i].className == 'formtablerowselected')
			return rows[i];
	}
	return false;
}

function addInputTableRow(IdFieldProperty, IdRecord) {
	if(document.getElementById(IdFieldProperty+'_input').value!=''){
		tableadd(document.getElementById(IdFieldProperty+'_input').value, IdFieldProperty, IdRecord, 3);
		document.getElementById(IdFieldProperty+'_input').value = '';
	}
}

function removeInputTableRow(IdFieldProperty) {
	id = getSelectedRow(IdFieldProperty+'_table').id;
	if(id) {	
		tableremove(id, IdFieldProperty);
	}
}

function toggleSelectedSelectTableRow(IdFieldData,IdFieldProperty){
	selected = document.getElementById(IdFieldData);
	if(selected) {
		var rows = selected.parentNode.getElementsByTagName("TR");
		for(i = 0; i < rows.length; i++){
			rows[i].className = "formtablerow";
		}
		selected.className = "formtablerowselected";
		if(IdFieldProperty) {
			document.getElementById(IdFieldProperty + "_comments").value =  "";
			name = selected.cells[0].innerHTML;
			document.getElementById(IdFieldProperty + "_commentsname").innerHTML = name + " Comments";
			getComments(IdFieldData, IdFieldProperty);
		}
	}
}

function toggleSelectedAssociatedEvent(row,IdFieldProperty) {
	function clearTable(id) {
		var table = document.getElementById(id);
		for(var i = 1; i < table.rows.length; i++) {
			table.rows[i].className = "formtablerow";
		}
	}
	clearTable(IdFieldProperty+"_1");
	clearTable(IdFieldProperty+"_2");
	row.className = "formtablerowselected";
}

function displaySampleData(selectedRow){
	headers = selectedRow.cells.length;
	for(i = 1; i <= headers; i++){
		var x = document.getElementById(selectedRow.id + "_" + i).innerHTML;
		var y = document.getElementById(i + "_input");
		if(x != "No Data")
			y.value = x;
		else
			y.value = "";
	}
	toggleSelectedSelectTableRow(selectedRow.id);
}

function updateSampleData(IdFieldProperty){
	table =  document.getElementById(IdFieldProperty);
	rows = table.getElementsByTagName("TR");
	IdFieldData = 0;
	for(i = 0; i < rows.length; i++) {
		if(rows[i].className == "formtablerowselected") {
			IdFieldData = rows[i].id;
			break;
		}
	}
	if(IdFieldData != 0){
		values = new Array();
		for(i = 1; i <= table.rows[0].cells.length; i++) {
			value = document.getElementById(i + "_input").value;
			document.getElementById(i + "_input").value = '';
			document.getElementById(IdFieldData + "_" + i).innerHTML = value ? value : "No Data";
			values[i] = value;
		}
		for(i = 1; i < values.length; i++) {
			AjaxRequest("T", "form_ajax.php", "Action=Save&IdFieldData=" + IdFieldData + "&Value=" + values[i] + "&Name=" + i);
		}
		document.getElementById(IdFieldData).className = 'formtablerow';
	};
}

function newSampleRow(IdRecord, IdFieldProperty){
	var post = "Action=NewSample&IdFieldProperty=" + IdFieldProperty + "&IdRecord=" + IdRecord + "&Values=";
	headers = document.getElementById(IdFieldProperty).rows[0].cells.length;
	for(i = 0; i < headers; i++) {
		post += i+"|"+document.getElementById((i+1)+"_input").value+",";
		document.getElementById((i+1)+"_input").value = '';
	}
	post = post.substr(0,post.length-1);
	var url = "form_ajax.php";
	AjaxRequestV2(url, post, function(responseText) { document.getElementById(IdFieldProperty).parentNode.innerHTML = responseText } );
}

function associateEvent(table, myrecord, record, row, idf) {
	toggleSelectedAssociatedEvent(row,idf);
	cells = row.cells;
	post = 'Action=SelectAssociatedEvent&IdRecord='+myrecord+'&SelectedIdRecord='+record+'&Table='+table+'&IdFieldProperty='+idf;
	url = 'form_ajax.php';
	loc = 'T';
	AjaxRequest(loc,url,post);
}

function removeSample(IdFieldProperty) {
	table = document.getElementById(IdFieldProperty);
	rows = table.getElementsByTagName("tr");
	row = false;
	for(i = 0; i < rows.length; i++) {
		if(rows[i].className == "formtablerowselected"){
			row = rows[i];
			break;
		}
	}
	if(row) {
		AjaxRequest("T", "form_ajax.php", "Action=RemoveSample&IdFieldData=" + row.id);
		inputs = document.getElementById(IdFieldProperty+"_inputs").getElementsByTagName("INPUT");
	        for(i = 0; i < inputs.length; i++){
	                inputs[i].value = "";
	        }
	        table.deleteRow(row.rowIndex);
	}
}

function tableremove(IdFieldData, IdFieldProperty){
	url = "form_ajax.php";
	post =  "Action=RemoveSelected&IdFieldData="+IdFieldData+"&IdFieldProperty="+IdFieldProperty;
	comments = document.getElementById(IdFieldProperty + "_comments");
	if(comments) {
		post += "&TableType=1";
	}
	AjaxRequestV2(url, post, function(responseText) { document.getElementById(IdFieldProperty+"_table").parentNode.innerHTML = responseText; 
							if(comments) {
								document.getElementById(comments.id + "name").innerHTML = "Comments";
								comments.value = "";
							}
							} );
}

function restrictChars(myfield, e){
var key;
var keychar;

if (window.event)
 key = window.event.keyCode;
else if (e)
 key = e.which;
else
 return true;

keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==8)/* || (key==0) ||
 (key==9) || (key==13) || (key==27)*/ ) 
 return true;


 else if ((("1234567890").indexOf(keychar) > -1))

 return true;

else
 return false;
}

function forceDate(myfield, e) {

var key;
if (window.event)
 key = window.event.keyCode;
else if (e)
 key = e.which;
else
 return true;

var append = String.fromCharCode(key);

// if the user pressed backspace allow them to delete
if(key == 8) { 
	// if the last char is a slash, delete both it and the character before it
	if(myfield.value.length == 3 || myfield.value.length == 6) {
		myfield.value = myfield.value.substr(0,myfield.value.length-2);
		return false;
	} else
		return true;
       // using the format mm/dd/yyyy, implants a slash in the correct spots
} else if(myfield.value.length == 1 || myfield.value.length == 4) { 
	append += "/";
} 
// restrict the field to 10 characters
if(myfield.value.length >= 10)
	return false;

// the characters must be numbers to be added to the field
if(restrictChars(myfield, e)) {
	myfield.value += append;
}

return false;

}

function updateFundingRemaining(x) {
	var func = function(response) {
				document.getElementById('fundingremaining').value = response;
				}
	AjaxRequestV2('form_ajax.php','Action=GetFundingRemaining&IdRecord='+x.value,func);
}

function saveComments(IdFieldProperty, value) {
	rows = document.getElementById(IdFieldProperty+"_table").getElementsByTagName("TR");
	row = 0;
	for(i = 0; i < rows.length; i++) {
		if(rows[i].className.indexOf("formtablerowselected") != -1) {
			row = rows[i];
			break;
		}
	}
	if(row != 0) {
		saveFieldDataProperty(row.id, IdFieldProperty, value, "Comments");
	}
}

function saveFieldDataProperty(IdFieldData, IdFieldProperty, value, name) {
	var post = "Action=Save&IdFieldData=" + IdFieldData + "&Value=" + value + "&Name=" + name;
        var url = "form_ajax.php";
        var http_request = false;
        if (window.XMLHttpRequest) {
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) {
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.open('POST',url, true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http_request.send(post);
}


function getComments(IdFieldData, IdFieldProperty){
	var post = "Action=GetComments&IdFieldData=" + IdFieldData;
	var url = "form_ajax.php";
        var http_request = false;
        if (window.XMLHttpRequest) {
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) {
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() {changeStatus(http_request,IdFieldProperty); };
        http_request.open('POST',url, true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http_request.send(post);
    function changeStatus(http_request,IdFieldProperty) {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
            	document.getElementById(IdFieldProperty + "_comments").value = http_request.responseText;
            }
        }
   }
}

function stripe(id) {
  var even = false;
  var evenColor = arguments[1] ? arguments[1] : "#bdecfe";
  var oddColor = arguments[2] ? arguments[2] : "#d9fbfc";
  var table = document.getElementById(id);
  var off  = document.getElementById('noStripe');
  if (! table || off) { return; }
  var tbodies = table.getElementsByTagName("tbody");

  for (var h = 0; h < tbodies.length; h++) {
    var trs = tbodies[h].getElementsByTagName("tr");

    for (var i = 0; i < trs.length; i++) {
      if (! trs[i].style.backgroundColor) {
        var tds = trs[i].getElementsByTagName("td");

        for (var j = 0; j < tds.length; j++) {
          var mytd = tds[j];
 //         if (! mytd.style.backgroundColor) {
            mytd.style.backgroundColor = even ? evenColor : oddColor;
 //         }
        }
      }
      even =  ! even;
    }
  }
}



function OpenRequestedPopup(strUrl, strTarget,width,height)
{
 var WindowObjectReferenceOfRequestedPopup;
var windowWidth, windowHeight, windowLeft, windowTop;

if(typeof window.screenX == "number" && typeof window.innerWidth == "number")
        {
        windowWidth = window.innerWidth * .68;
        windowHeight = window.innerHeight * .68;
        windowLeft = window.screenX + window.innerWidth * .16;
        windowTop = window.screenY + window.innerHeight * .16;
        }
else if(typeof window.screenTop == "number" && typeof document.documentElement.offsetHeight == "number")
        {
        windowWidth = document.documentElement.offsetWidth * .68;
        windowHeight = document.documentElement.offsetHeight * .68;
        windowLeft = window.screenLeft + document.documentElement.offsetWidth * .16;
        windowTop = window.screenTop - 50;
        }
else
        {
        windowWidth = 800;
        windowHeight = 250;
        windowLeft = 60;
        windowTop = 40;
        };

/* The above code is just to define reasonable sizes and initial positions to the popup to be. */

if (WindowObjectReferenceOfRequestedPopup == null || WindowObjectReferenceOfRequestedPopup.closed)
        {
        WindowObjectReferenceOfRequestedPopup = window.open(strUrl, strTarget, "top=" + windowTop + ",left=" + windowLeft + ",width=" + windowWidth * width + ",height=" + windowHeight * height + ",menubar,toolbar,location,resizable,scrollbars,status");
        }
else
        {
        WindowObjectReferenceOfRequestedPopup.focus();
        };

/*
 * The above 9 lines of code creates the popup; if the popup is already opened, then it is only brought on top. This feature is possible only if the user allows it via the setting Edit/Preferences.../category:Advanced/Scripts & Plugins/Allow webpages to:/Raise or lower windows
 * */
}


function AddLink(title, url) {
	var count = document.getElementById("LinkCount");
	count.value++;
	divobj = document.createElement("DIV");
	divobj.id="Link"+count.value;
	document.getElementById("LinkContainer").appendChild(divobj);
	document.getElementById("Link"+count.value).innerHTML += "<table width='100%'><tr> "+
				"<td width='30px'>Title</td><td width='250px'><input name=\"Link"+count.value+"[]\" style='width:90%' value=\""+ title + "\"></td>"+
				"<td width='30px;'>URL</td><td width='450px'><input name=\"Link"+count.value+"[]\" style='width:90%' value=\""+ url + "\"></td> "+
				"<td width='75px'><input type='checkbox' name=\"Link"+count.value+"[]\"  value='1'>Popup</td></tr></table>";
}

function AddLinkToSelectedLink(id) {
	var request = new XMLHttpRequest(); 
        	
        	request.onreadystatechange = function() {  
        		if(request.readyState == 4) {
        			if (request.status == 200)  {
        				var resp = request.responseText;
        				var startTxt = '<itemLink>';
        				var itemsStart = resp.indexOf(startTxt) + startTxt.length;
        				var itemsEnd = resp.indexOf('</itemLink>');
        				var idArray = resp.substring(itemsStart, itemsEnd).split(",");
					AddLink(idArray[0],idArray[1]);
				}
				else 
					alert("There was a problem retrieving the XML data:\n" + request.statusText);
			}
  
			}; 
        	request.open("POST", "getItemLink.php", true);  
  		request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  		request.send('id=' + id);   
}

function AjaxProgramSelect(location,url,post) {
        var http_request = false;
	try {
		document.getElementById(location).innerHTML='<div style="width:100%;text-align:center;margin-top:40px"><img src="/images/ajax-loader.gif"><br>Loading...</div>';
	}
	catch(e) { 
	}
        if (window.XMLHttpRequest) {
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) {
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() {changeStatus(http_request); };
        http_request.open('POST',url, true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http_request.send(post);
    function changeStatus(http_request,redirect) {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
            	 if(location != null){
                 	try { document.getElementById(location).innerHTML = http_request.responseText; }
		 	catch(err) {}
			AjaxRequest("CS","/bittoolbox/siteinventorydata/choosesite_ajax.php","");
                        AjaxRequest("SF","/bittoolbox/siteinventorydata/sitefeatures_ajax.php","");
		}
            }
        }
   }
}

function AjaxRedirect(location,url,post,redirect) {
        var http_request = false;
	document.getElementById(location).innerHTML='<div style="width:100%;text-align:center;margin-top:40px"><img src="/images/ajax-loader.gif"><br>Loading...</div>';
        if (window.XMLHttpRequest) {
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) {
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() {changeStatus(http_request,redirect); };
        http_request.open('POST',url, true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http_request.send(post);
    function changeStatus(http_request,redirect) {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
            	 if(location != null)
			window.location.href=redirect;
            }
        }
   }
}

function toggleParentVisible(val) {
	select = document.getElementById("fundingparent");
	for(var i = 0; i < select.options.length; i++) {
		if(select.options[i].value == val)
			select.options[i].disabled = true;
		else
			select.options[i].disabled = false;
		if(select.options[select.selectedIndex].value == val)
			select.selectedIndex = 0;
	}
}

function addNewFundingRow(idFieldProperty,idRecord) {
	amount = document.getElementById('fundingamount').value;
	activity = document.getElementById('fundingactivity').value;
	fundingrecord = document.getElementById('fundingrecord').value;
	start = document.getElementById('fundingstart').value;
	end = document.getElementById('fundingend').value;
	fparent = document.getElementById('fundingparent').value;
	if(amount=='' || start=='' || end=='' || activity=='' || amount==0 || start==0 || end==0 || activity==0) {
		if(amount == 0)
			alert('You must fill in a funding anount to add this record');
		else
			alert('You must fill in all fields to add this record');
		return;
	}
	var func = function(response) { document.getElementById('fundingtable').parentNode.innerHTML = response; updateFundingRemaining(document.getElementById('fundingrecord')); };
	if(checkFundingRemaining(amount)) {
		AjaxRequestV2('form_ajax.php','Action=NewFundingRow&Activity='+activity+
								  '&FundingRecord='+fundingrecord+
								  '&Start='+start+
								  '&End='+end+
								  '&Amount='+amount+
								  '&IdFieldProperty='+idFieldProperty+
								  '&Parent='+fparent+
								  '&IdRecord='+idRecord,func);
	} else {
		alert('Insufficient funds avaiable to allocate');
	}
}

function deleteFundingRow() {
	table = document.getElementById('fundingtable');
	rows = table.getElementsByTagName('TR');
	row = 0;
	for(i = 0; i < rows.length; i++) {
		if(rows[i].className.indexOf('selectedRow') != -1) {
			row = rows[i];
			break;
		}
	}
	if(row != 0) {
		var func = function() { updateFundingRemaining(document.getElementById('fundingrecord')) };
		AjaxRequestV2('form_ajax.php','Action=DeleteFundingRow&IdFieldData='+row.id.substr(11),func);
		table.deleteRow(row.rowIndex);
	}
}

function updateFundingRow() {
	amount = document.getElementById('fundingamount').value;
	fa =  document.getElementById('fundingactivity');
	activity = fa.value;
	activityname = fa.options[fa.selectedIndex].text;
	fr =  document.getElementById('fundingrecord');
	fundingrecord = fr.value;
	fundingrecordname = fr.options[fr.selectedIndex].text;
	start = document.getElementById('fundingstart').value;
	end = document.getElementById('fundingend').value;
	table = document.getElementById('fundingtable');
	fparent = document.getElementById('fundingparent').value;
	fparenttext = document.getElementById('fundingparent').options[document.getElementById('fundingparent').selectedIndex].text;
	rows = table.getElementsByTagName('TR');
	row = 0;
	if(amount=='' || start=='' || end=='' || activity=='' || amount==0 || start==0 || end==0 || activity==0) {
		if(amount == 0)
			alert('You must fill in a funding anount to update this record');
		else
			alert('You must fill in all fields to add update record');
		return;
	}
	for(i = 0; i < rows.length; i++) {
		if(rows[i].className.indexOf("selectedRow") != -1) {
			row = rows[i];
			break;
		}
	}
	if(row != 0) {
		if(checkFundingRemaining(amount)) {
			var func = function(response) {
				row.cells[5].innerHTML = response;
				updateFundingRemaining(document.getElementById('fundingrecord'));
			};
			AjaxRequestV2('form_ajax.php','Action=UpdateFundingRow&Activity='+activity+
		                                                             '&FundingRecord='+fundingrecord+
		                                                             '&Start='+start+
		                                                             '&End='+end+
		                                                             '&Amount='+amount+
									     '&Parent='+fparent+
		                                                             '&IdFieldData='+row.id.substr(11),func);
			row.cells[0].innerHTML = activityname;
			row.cells[1].innerHTML = start;
			row.cells[2].innerHTML = end;
			row.cells[3].innerHTML = fundingrecordname;
			row.cells[4].innerHTML = amount;
			row.cells[6].innerHTML = fparenttext;
			document.getElementById('oldfundingamount').value = amount;
		} else {
			alert('Insufficient funds avaiable to allocate');
		}
	}
}

function checkFundingRemaining(amount) {
	remaining = parseInt(document.getElementById('fundingremaining').value);
	old = document.getElementById('oldfundingamount').value;
	if(old != '') {
		remaining = parseInt(remaining + parseInt(old));

	}
	return remaining >= amount;
}

function toggleSelectedFundingRow(row) {
	table = document.getElementById('fundingtable');
	if(table) {
		rows = table.getElementsByTagName('TR');
		for(i=0;i<rows.length;i++) {
			if(rows[i].className.indexOf("selectedRow") != -1) 
				rows[i].className = rows[i].className.replace('selectedRow','');
		}
		row.className += ' selectedRow';
		fa = document.getElementById('fundingactivity');
		for(i=0;i<fa.options.length;i++) {
			if(fa.options[i].text == row.cells[0].innerHTML) {
				fa.options[i].selected = true;
				break;
			}
		}
		fr = document.getElementById('fundingrecord');
		for(i=0;i<fr.options.length;i++) {
			if(fr.options[i].text == row.cells[3].innerHTML) {
				fr.options[i].selected = true;
				break;
			}
		}
		fp = document.getElementById('fundingparent');
		for(i=0;i<fp.options.length;i++) {
			if(fp.options[i].text == row.cells[6].innerHTML) {
				fp.options[i].selected = true;
				break;
			}
			fp.options[0].selected = true;
		}
		document.getElementById('fundingstart').value = row.cells[1].innerHTML;
		document.getElementById('fundingend').value = row.cells[2].innerHTML;
		document.getElementById('fundingamount').value = row.cells[4].innerHTML;
		document.getElementById('oldfundingamount').value = row.cells[4].innerHTML;
		updateFundingRemaining(document.getElementById('fundingrecord'));
		toggleParentVisible(document.getElementById('fundingrecord').options[document.getElementById('fundingrecord').selectedIndex].value);
	}
}

function removeParticipant(aid, i) {
	url = 'removeParticipantajx.php';
	post = 'IdApplicant=' + aid;
	func = function(response) {
			if(response == '1')
				document.getElementById('participanttable').deleteRow(document.getElementById('row'+i).rowIndex);
		}
	AjaxRequestV2(url, post, func);
}

function AjaxRequestV2(url,post,func) {
        var http_request = false;
        if (window.XMLHttpRequest) {
            http_request = new XMLHttpRequest();	
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) {
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() {changeStatus(http_request,func); };
        http_request.open('POST',url, true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http_request.send(post);
    function changeStatus(http_request,func) {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                 if(func != null)
			func(http_request.responseText);
            }
        }
   }
}

function AjaxRequest(location,url,post) {
        var http_request = false;
	try { document.getElementById(location).innerHTML='<div style="width:100%;text-align:center;margin-top:40px"><img src="/images/ajax-loader.gif"><br>Loading...</div>'; }
	catch (err) {}
        if (window.XMLHttpRequest) {
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) {
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() {changeStatus(http_request); };
        http_request.open('POST',url, true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http_request.send(post);
    function changeStatus(http_request) {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
            	if(location != null)
                       document.getElementById(location).innerHTML = http_request.responseText;
		attachDatePickers();
            }
        }
   }
}
//Exactly like AjaxRequest except no loading graphic
function AjaxRequestNoGraphic(location,url,post) {
        var http_request = false;
        if (window.XMLHttpRequest) {
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) {
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }
        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() {changeStatus(http_request); };
        http_request.open('POST',url, true);
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        http_request.send(post);
    function changeStatus(http_request) {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
            	if(location != null)
                       document.getElementById(location).innerHTML = http_request.responseText;
		attachDatePickers();
            }
        }
   }
}

function attachDatePickers() {
	var inputs = document.getElementsByTagName('INPUT');
	for(i = 0; i < inputs.length; i++) {
		if(inputs[i].type == "text" && inputs[i].className.indexOf("datepicker") != -1)
			$('#'+inputs[i].id).datepicker({ dateFormat: 'mm/dd/yy' });
	}
}


function RemoveRecord(tid,msg) {
        msg = msg ? msg : "delete";
        if (confirm('Are you sure you want to '+msg+' ?')) {
                document.delform.DelID.value=tid;
                timer =  setTimeout('document.delform.submit();', 500);
        }
}

function ConfirmDeleteSite(/*name*/) {
        var answer = confirm("Are you sure you want to remove this site?");

        if(answer) {
                window.location.href="/bittoolbox/siteinventorydata/removeSite.php";
       }
}

function SaveAndBack() {
	document.getElementById("SAB").value = 1;	
	timer =  setTimeout('document.EditSection.submit();', 300);
	return true;
}

function VerifySave() {

	if(confirm('Leaving the page will caused unsaved work to be lost. Are you sure you want to leave? ')) {
		return true;
	} else {
		return false;
	}
}
