/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
var changesMadeToForm = false;

function init()
{
	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
	try 
	{
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
	  try 
	  {
	    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  catch (e2) 
	  {
	    xmlHttp = false;
	  }
	}
	@end @*/

	if (!xmlHttp && typeof XMLHttpRequest!='undefined') 
	{
		try {
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			xmlHttp=false;
		}
	}
	if (!xmlHttp && window.createRequest) 
	{
		try {
			xmlHttp = window.createRequest();
		} catch (e) {
			xmlHttp=false;
		}
	}
}

function filterTable(phrase, _id)
{
	var words = phrase.value.toLowerCase().split(" ");
	var table = document.getElementById(_id);
	var ele;
	for (var r = 1; r < table.rows.length-1; r++){
		ele = table.rows[r].innerHTML.replace(/<[^>]+>/g,"");
	        var displayStyle = 'none';
	        for (var i = 0; i < words.length; i++) {
		    if (ele.toLowerCase().indexOf(words[i])>=0)
			displayStyle = '';
		    else {
			displayStyle = 'none';
			break;
		    }
	        }
		table.rows[r].style.display = displayStyle;
	}
}
function parseNodeForForm(obj)
{
	var getstr = "";
	  for (var i=0; i<obj.childNodes.length; i++) 
	  {
		if (obj.childNodes[i].hasChildNodes())
		{
			getstr = getstr + parseNodeForForm(obj.childNodes[i]);
		}
		 if (obj.childNodes[i].tagName == "INPUT") 
		 {
			if (obj.childNodes[i].type == "text" || obj.childNodes[i].type == "hidden" || obj.childNodes[i].type == "submit") 
			{
			   getstr += obj.childNodes[i].name + "=" + encodeURI(obj.childNodes[i].value) + "&";
			}
			if (obj.childNodes[i].type == "checkbox") 
			{
			   if (obj.childNodes[i].checked) 
			   {
				  getstr += obj.childNodes[i].name + "=" + encodeURI(obj.childNodes[i].value) + "&";
			   } else {
				  getstr += obj.childNodes[i].name + "=&";
			   }
			}
			if (obj.childNodes[i].type == "radio") 
			{
			   if (obj.childNodes[i].checked) 
			   {
				  getstr += obj.childNodes[i].name + "=" + encodeURI(obj.childNodes[i].value) + "&";
			   }
			}
		 }   
		 if (obj.childNodes[i].tagName == "SELECT") 
		 {
			var sel = obj.childNodes[i];
			getstr += sel.name + "=" + encodeURI(sel.options[sel.selectedIndex].value) + "&";
		 }

	  }
	  return getstr;
}

function get(obj) 
{
	if (!xmlHttp)
	{
		return true;
	}
	var getstr = "?";
	getstr = getstr + parseNodeForForm(obj);
  	var curLoc = location.href.substring(0, location.href.indexOf('?'));
	var url = curLoc + getstr + 'style=ajax';
	
	go(url);
	return false;
}

function go(url)
{
	return true;
	
	if (!xmlHttp)
	{
		return true;
	}
	
	// Open a connection to the server and send our data
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = updatePage;
	xmlHttp.send(null);	
	return false;
}
function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
	if (obj.getAttribute && obj.value.length>mlength)
	{
		obj.value=obj.value.substring(0,mlength);
	}
}


function gotoPage(whatPage)
{
	if (!xmlHttp)
	{
		return true;
	}
	
	var curLoc = location.href.substring(0, location.href.indexOf('?'));
	var url = curLoc + '?style=ajax&action=' + encodeURI(whatPage);

	go(url);
	
	document.getElementById(curPage).className = "";
	document.getElementById(whatPage).className = "current"; 
	curPage = whatPage;
	return false;
}

function createElementFromString(str) 
{
	var node, a = str.match(/<(\w+)(\s+)?([^>]+)?>/);
	if (a != null) 
	{
		node = document.createElement(a[1]);
		if (a[3] != null) 
		{
			var attrs = a[3].split(" ");
			if (attrs.length > 0) 
			{
				for ( var i = 0; i < attrs.length; i++) 
				{
					var att = attrs[i].split("=");
					if (att[0].length > 0 && att[0] != "/" && att[1].length != 2) 
					{
						var a_n = document.createAttribute(att[0]);
						a_n.value = att[1].replace(/^['"](.+)['"]$/, "$1");
						node.setAttributeNode(a_n);
					}
				}
			}
		}
	}
	return node;
}

function updatePage()
{
  if (xmlHttp.readyState == 4) 
  {
    var response = xmlHttp.responseText;
	if (response.substring(0,1) != '+')
	{
		document.getElementById("mainDiv").innerHTML = response;
	}
	else
	{
		responseNoPlus = response.substring(1);
		divID = responseNoPlus.substring(0, responseNoPlus.indexOf('+'));
		response = responseNoPlus.substring(responseNoPlus.indexOf('+')+1);
		if (document.getElementById(divID))
		{
			var htmlObject = createElementFromString(response);
			responseNoOuter = response.substring(response.indexOf('>')+1, response.lastIndexOf('<'));
			document.getElementById(divID).innerHTML = responseNoOuter;
			document.getElementById(divID).className = htmlObject.className;
		}
		else
		{
			document.getElementById("mainDiv").innerHTML =  response + document.getElementById("mainDiv").innerHTML;
		}
	}
	if (response.indexOf("class=\"data\"") != -1 || response.indexOf("class='data'") != -1)
	{
		setupEdits();
	}
  }
}

function setupEdits()
{
	var list = document.getElementsByClassName("data");
	var ipeOptionsStd = {
		okButton:       false, 
		cancelLink:     false, 
		savingText:    " &nbsp; &nbsp; &nbsp;Saving...", 
		submitOnBlur:   true, 
		highlightcolor: "",
		callback:     function(form,value)
					  {
						var id = form.id;
						id = id.substring(0,id.indexOf("-inplaceeditor"));
						return "id="+id+"&value="+escape(value);
					  }
		};
	for(var i=0;i<list.length;i++)
	{
		new Ajax.InPlaceEditor(list[i], "echo.pl", ipeOptionsStd );
	}
}

function changeWasUnChecked(obj)
{
	obj.parentNode.style.backgroundColor = (obj.parentNode.style.backgroundColor != "green") ? "green":"white";
	changesMadeToForm = true;
}

function changeWasChecked(obj)
{
	obj.parentNode.style.backgroundColor = (obj.parentNode.style.backgroundColor != "red") ? "red":"white";
	changesMadeToForm = true;
}

function getElementsByClass(cls) 
{
    var ret=[], a = document.getElementsByTagName("*");
    for (var i=0;i<a.length;i++) 
	{
	 var c=' '+a[i].className+' '; //cheat our way out of a more expensive regexp match/split
	 if (c.indexOf(' '+cls+' ')!=-1)  ret.push(a[i]);
    }
    return ret;
}

function makeBetter(obj)
{
	var el = document.getElementById(obj);
	if (el)
	{
		el.style.tableLayout = "auto";
		
		var newWidth = document.getElementById("newSizeTable");
		if (newWidth.offsetWidth)
		{
			var wid = parseInt(newWidth.offsetWidth);	
			var widToSub = (document.all) ? 218 : 200;
			while((wid - widToSub) % 16 != 0)
			{
				wid--;
			}
			el.style.width = wid + "px";
			if (!document.all)
			{
				var cols = document.getElementById("firstColHeader");
				cols.style.width = "200px";
				while (cols.nextSibling)
				{
					cols = cols.nextSibling;
					cols.style.width = wid/17 + "px";
				}
			}
		
			wid = wid - 202;
			var timeSlotDivs =  getElementsByClass("timeslotDiv");
			for (var i=0; i < timeSlotDivs.length; i++)
			{
				var curLeft = timeSlotDivs[i].style.left;
				var curWidth = timeSlotDivs[i].style.width;
				if (!timeSlotDivs[i].oldLeft)
				{
					timeSlotDivs[i].oldLeft = parseFloat(curLeft) / 100;
				}
				if (!timeSlotDivs[i].oldWidth)
				{
					timeSlotDivs[i].oldWidth = parseFloat(curWidth) / 100;
				}
				timeSlotDivs[i].style.left = parseInt(parseFloat(timeSlotDivs[i].oldLeft) * wid) + "px";
				timeSlotDivs[i].style.width = parseInt(parseFloat(timeSlotDivs[i].oldWidth) * wid) + "px";
			}
		}
	}

}

function makeBetter2(obj)
{
	var elLis = getElementsByClass(obj);
	for (var q=0; q<elLis.length; q++)
	{
		el = elLis[q];
		var newWidth = document.getElementById("newSizeTable");
		if (newWidth.offsetWidth)
		{
			var wid = parseInt(newWidth.offsetWidth);		
			var widToSub = (document.all) ? 218 : 200;
			while((wid - widToSub) % 16 != 0)
			{
				wid--;
			}
			el.style.width = wid + "px";
			if (!document.all)
			{
				var cols = el.getElementsByTagName("thead")[0].firstChild.getElementsByTagName("th")[0];
				//var cols = document.getElementById("firstColHeader");
				cols.style.width = "200px";
				while (cols.nextSibling)
				{
					cols = cols.nextSibling;
					cols.style.width = wid/17 + "px";
				}
			}
		
			wid = wid - 202;
			timeSlotDivs =  getElementsByClass("timeslotDiv");
			for (var i=0; i < timeSlotDivs.length; i++)
			{
				var curLeft = timeSlotDivs[i].style.left;
				var curWidth = timeSlotDivs[i].style.width;
				if (!timeSlotDivs[i].oldLeft)
				{
					timeSlotDivs[i].oldLeft = parseFloat(curLeft) / 100;
				}
				if (!timeSlotDivs[i].oldWidth)
				{
					timeSlotDivs[i].oldWidth = parseFloat(curWidth) / 100;
				}
				timeSlotDivs[i].style.left = parseInt(parseFloat(timeSlotDivs[i].oldLeft) * wid) + "px";
				timeSlotDivs[i].style.width = parseInt(parseFloat(timeSlotDivs[i].oldWidth) * wid) + "px";
			}
		}
	}

}

function fixUp(obj)
{
	var el = document.getElementById(obj);
	
	if (el)
	{
		var width = 100;
		if (document.body.clientWidth && document.body.clientWidth > width)
		{ width = document.body.clientWidth;}
		if (document.body.offsetWidth && document.body.offsetWidth > width)
		{ width = document.body.offsetWidth;}
		if (document.body.scrollWidth && document.body.scrollWidth > width)
		{ width = document.body.scrollWidth;}
		el.style.width = width + "px";
		
		var height = 100;
		if (document.body.clientHeight && document.body.clientHeight > height)
		{ height = document.body.clientHeight; }
		if (document.body.offsetHeight && document.body.offsetHeight > height)
		{ height = document.body.offsetHeight; }
		if (document.body.scrollHeight && document.body.scrollHeight > height)
		{ height = document.body.scrollHeight; }
		if (document.documentElement.offsetHeight && document.documentElement.offsetHeight > height)
		{ height = document.documentElement.offsetHeight; }
		if (window.innerHeight && window.innerHeight > height)
		{ height = window.innerHeight; }
		el.style.height = height + "px";
	}
}

function toggleVis(obj) 
{
	var el = document.getElementById(obj);	
	el.style.display = (el.style.display != 'none' ? 'none' : '' );
	return false;
}


function toggleVisForm(obj) 
{
	var el = document.getElementById(obj);	
	el.style.display = (el.style.display != 'none' ? 'none' : '' );
	fixUp(obj);
	return false;
}

function setFocused(el)
{
	el.parentNode.parentNode.style.background = '#B7E6F5';
	if (el.parentNode.getElementsByTagName('span')[0])
	{
		el.parentNode.getElementsByTagName('span')[0].style.display = 'inline';
	}
}
function setBlurred(el)
{
	el.parentNode.parentNode.style.background = '#ffffff';
	if (el.parentNode.getElementsByTagName('span')[0])
	{
		el.parentNode.getElementsByTagName('span')[0].style.display = 'none';
	}
}