var g_formup;

var req;

function lookup(qs) {
}

// function accepts a URL(With querystring) and a function to hand off processing 
// of response.
function loadXMLDoc(url, funct) {
 // branch for native XMLHttpRequest object
 if (window.XMLHttpRequest) {
 req = new XMLHttpRequest();
 req.onreadystatechange = funct;
 req.open("GET", url);
 req.send(null);
 // branch for IE/Windows ActiveX version
 } else if (window.ActiveXObject) {
 req = new ActiveXObject("Microsoft.XMLHTTP");
 if (req) {
 req.onreadystatechange = funct;
 req.open("GET", url);
 req.send(null);
 }
 }
}

// function to process XHR response and update table cells with new amounts.
function processReqChange() {
 // only if req shows "loaded"
 if (req.readyState == 4) {
 // only if "OK"
 if (req.status == 200) {
 var restext = req.responseText;
  var theForm = document.getElementById('editForm');
	var td = theForm.parentNode;
	clearControls();
	td.firstChild.innerHTML =  restext + '';
	var tr = td.parentNode
	Fat.fade_element(tr.id, 60, 2000, "#006699","")
	updateTotals();
 	g_formup = '';										

 //var DivElement = document.getElementById("dyn");
 //DivElement.innerHTML = restext;
 } else {
 alert("The webserver couldn\'t be contacted.  Please check your internet connection and try again in a few minutes.\n" + req.statusText);

 }
 }
}

function writeTotals() {
 // only if req shows "loaded"
 if (req.readyState == 4) {
 // only if "OK"
 if (req.status == 200) {
  var restext = req.responseText;
	var arrTotals = restext.split('|');
	for (var i=0; i<arrTotals.length; i++) {
			var arrPairs = arrTotals[i].split("~");
			var targetTd = document.getElementById(arrPairs[0]);
			targetTd.innerHTML = arrPairs[1];
	}
 //var DivElement = document.getElementById("dyn");
 //DivElement.innerHTML = restext;
 } else {
 alert("The webserver couldn\'t be contacted.  Please check your internet connection and try again in a few minutes.\n" + req.statusText);

 }
 }
}


function stripEdit() {
  var anchors = document.getElementsByTagName('a');
	for (var i=0;i<anchors.length;i++) {
	  if (anchors[i].className == 'editlink') {
		  anchors[i].style.display = 'none';
		}
	}
}
function clearControls(cell) {
		var theForm = document.getElementById('editForm');
		if (theForm != null && theForm != 'undefined') {
  		var parent = theForm.parentNode;
  		parent.removeChild(theForm);
			formup = null;
			parent.className = parent.className.replace(new RegExp("( ?|^)hj_active\\b"), "");				 
		}
}
function updateTotals() {
		var qs = 'ajax.asp?ajax=totals';
		var req = loadXMLDoc(qs, writeTotals);
}
function getForm(id) {
		var qs = 'ajax.asp?ajax=selectbox&id=' + id;
		var req = loadXMLDoc(qs, setForm);
}

function setForm() {
 // only if req shows "loaded"
 if (req.readyState == 4) {
 // only if "OK"
 if (req.status == 200) {
  var restext = req.responseText;
	var targetForm = document.getElementById('editForm');
	targetForm.innerHTML = restext;
	var editSubmit = document.getElementById('controlSave');
	editSubmit.onclick = saveData;
	var editReset = document.getElementById('controlCancel');
	editReset.onclick = clearControls;
	var currentLabel = targetForm.parentNode.parentNode.getElementsByTagName('td')[0].firstChild.firstChild.nodeValue;
	var editLabel = document.getElementById('editLabel');
	editLabel.innerHTML = 'Enter amount for <strong>' + currentLabel + '</strong>';

	var editAmount = document.getElementById('editAmount');
	editAmount.focus();
	editAmount.select();

	}else {
 alert("The webserver couldn\'t be contacted.  Please check your internet connection and try again in a few minutes.\n" + req.statusText);
 //var DivElement = document.getElementById("dyn");
 //DivElement.innerHTML = restext;
 } 
 }
}
function saveData () {
		var theForm = document.getElementById('editForm');
		var lineID = theForm.parentNode.parentNode.getAttribute('id');
		var amount = document.getElementById('editAmount').value;
		var checkboxes = theForm.getElementsByTagName('input');
		var period = 'w';
		for (var i=0; i<checkboxes.length; i++) {
				if (checkboxes[i].checked == true) {
				  period = checkboxes[i].value;
				}
		}
		var href = ''
		var qs = 'ajax.asp?id=' + lineID + '&' + lineID + '=' + amount + '&p' + lineID + '=' + period + '&ajax=save';
		var req = loadXMLDoc(qs, processReqChange);
		//alert(qs);

		return false;
}

function addControls(cell) {
	//cell.onclick = function () {};
	var currentValue = cell.getElementsByTagName('p')[0].firstChild.nodeValue;
	currentValue = currentValue.replace('$', '');
	var lineID = cell.parentNode.getAttribute('id');
	var editForm = document.createElement('form');
	editForm.setAttribute('id', 'editForm');
	editForm.onsubmit = saveData;
	cell.appendChild(editForm);
	getForm(lineID);
	
	
	//alert(cell.className);
}

function cellOnclick(cell) {
		if ((g_formup == 'undefined' || g_formup == null) || (cell != g_formup)) { 
			 clearControls(cell);
			 g_formup = cell;
			 addControls(cell);
			 } else {
			 }
}

function addEvents() {
  var tds = document.getElementsByTagName('td');
	for (var i=0;i<tds.length;i++) {
			if (tds[i].className == 'data') {
				 tds[i].onmouseover = function () {
  				 this.className+=(this.className.length>0? " ": "") + "hj_active";
				 }
				 tds[i].onmouseout = function () {
           this.className=this.className.replace(new RegExp("( ?|^)hj_active\\b"), "");				 
				 }
				 tds[i].onclick = function() {
           this.className=this.className.replace(new RegExp("( ?|^)hj_active\\b"), "");				 
				 		cellOnclick(this);
				 };
			}
	}
}
function doAlive() {}

function keepalive() {
  window.setInterval(function () { 
    	var qs = 'ajax.asp?ajax=keepalive';
    	var req = loadXMLDoc(qs, doAlive);
	 }, 300000);
}

function striper(passid, element) {
  var target = document.getElementById(passid);
	var ele = target.getElementsByTagName(element);
	var flag = true;
	for (var i=0;i<ele.length;i++) {
	  if (flag==true) {
		  ele[i].style.backgroundColor = '#efefef';
		} else {
		  ele[i].style.backgroundColor = '#e0e0e0';
		}
		flag = !flag;
	}
}

function toggle(formid) {
  var saveform = document.getElementById(formid);
	if (saveform.style.display == 'none') {
		 saveform.style.display = 'block';
  } else {
		 saveform.style.display = 'none';
  }
}


function emailSave() {
   var saveLink = document.getElementById('savelink');
	 var saveform = document.getElementById('saveform');
	 saveform.style.display = 'none';
	 saveLink.onclick = function () { toggle('saveform'); return false; }
}

//start the striper 

function striperinit() {
  striper('exp', 'tr');
	striper('inc', 'tr');
	striper('pay', 'tr');
	striper('grand-totals', 'tr');
}

function checkClear() {
  var clearlink = document.getElementById('clear');
	clearlink.onclick = function () {
	if (confirm('Are you sure you want to clear your data (this cannot be undone)')) {
		 return true;
		 } else {
		 return false;
		 }
	}
		
}

function initAjax () {
	if (document.getElementById) {
    stripEdit();
  	g_formup = null;
  	addEvents();
  	keepalive();
		emailSave();
  	striperinit();
		checkClear();
	}
}



addLoadListener( initAjax );

