var lastDiv = null;
var searchObj = null;
function initAdminValues(){
  var items = getTags("IMG");
  for (var i = 0; i < items.length; i++){
	if (items[i].src.toLowerCase().indexOf("images/delete.gif") > 0){
      addCustomEvent(items[i], "click", ConfirmDelete);
    }
  }
  var items = getTags("DIV");
  for (var i = 0; i < items.length; i++){
	if (items[i].className.toLowerCase() == "noexpand" || items[i].className.toLowerCase() == "noexpand2"){
      addCustomEvent(items[i], "click", onDivClick);
	  items[i].style.width = items[i].parentNode.clientWidth + "px";
    }
  }
  addCustomEvent(document.body, "click", onDivClick);
}

function ConfirmDelete(e) {
   var msg = "Are you sure you want to delete?";	
   sConfirmMessage = msg;
   execScript('n = msgbox(sConfirmMessage, "4132", "Confirm")', "vbscript");
   return(n == 6);
}

function onDivClick(e)
{
	if (lastDiv != null)
	{
		lastDiv.className = lastDiv.getAttribute("lastclassName");
	}
	if (e != null && getSrcObj(e) != null)
	{
		lastDiv = getSrcObj(e);
		lastDiv.setAttribute("lastclassName", lastDiv.className);
		lastDiv.className = "";
	}
}

function getPageName(sLink){
  sLink = sLink.toLowerCase();
  if (sLink.indexOf("/") > -1){
    sLink = sLink.substring(sLink.lastIndexOf("/") + 1);
  }
  return sLink;
}

function getTags(tName){
  if (document.all == null){
	var items = document.getElementsByTagName(tName);
  }
  else {
	var items = document.all.tags(tName);
  }
  return items;
}

function addCustomEvent(obj, sEventName, CustomeEvent){
  if (obj.addEventListener){
	obj.addEventListener(sEventName, CustomeEvent, false);
  }
  else {
	obj.attachEvent("on" + sEventName, CustomeEvent);
  }
}

function aMouseOver(e){
  var btn = getSrcObj(e);
  btn.setAttribute("oldColour", btn.style.color);
  btn.style.color = "black";
}

function aMouseOut(e){
  var btn = getSrcObj(e);
  btn.style.color = btn.getAttribute("oldColour");
}

function getSrcObj(e){
  if (e.srcElement){
      var obj = event.srcElement;
    }
    else {
      var obj = e.target;
  }
  return obj;
}

function Show_Login(e)
{
	getTB("divLogin").style.top = ((findPosY(getSrcObj(e)) + getSrcObj(e).clientHeight) + 19) + "px";
	getTB("divLogin").style.left = (findPosX(getSrcObj(e)) - 180) + "px";
	getTB("divLogin").className = "loginDiv";
	try { getTB("tbUser").focus(); } catch (e) {}
	return false;
}

function Hide_Login(e)
{
	getTB("divLogin").className = "hide";
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj)
	{
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curleft += obj.offsetLeft - obj.scrollLeft;
				obj = obj.offsetParent;
			}
		}
		else if (obj.x) { curleft += obj.x; }
	}
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj)
	{
		if (obj.offsetParent)
		{
			while (obj.offsetParent)
			{
				curtop += obj.offsetTop - obj.scrollTop;
				obj = obj.offsetParent;
			}
		}
		else if (obj.y) { curtop += obj.y; }
	}
	return curtop;
}

function Add_Click(AddID)
{
	var frmPostBack = getTB("frmPostBack");
	frmPostBack.src = "ADD_Click.php?AddID=" + AddID;
}

function Do_Not_Email_Click(bChange)
{
	var Do_Not_Email_Ind = getTB("Do_Not_Email_Ind").checked;
	getTB("Send_Web_Updates_Ind").disabled = Do_Not_Email_Ind;
	getTB("Send_Event_Updates_Ind").disabled = Do_Not_Email_Ind;
	getTB("Send_News_Updates_Ind").disabled = Do_Not_Email_Ind;
	
	if (bChange){
		getTB("Send_Web_Updates_Ind").checked = !Do_Not_Email_Ind;
		getTB("Send_Event_Updates_Ind").checked = !Do_Not_Email_Ind;
		getTB("Send_News_Updates_Ind").checked = !Do_Not_Email_Ind;
	}
}

function Recalc_Time()
{
	setTimeout('Recalc_Time();', 1000);
	Incriment_Second(getTB("NZ_Time"));
	Incriment_Second(getTB("SA_Time"));
}

function Incriment_Second(Time_Object)
{
	if (Time_Object.getAttribute("seconds") == 59)
	{
		Time_Object.setAttribute("seconds", "0");
		Incriment_Minute(Time_Object);
	}
	else 
	{
		Time_Object.setAttribute("seconds", getTimeVal(parseFloat(Time_Object.getAttribute("seconds")) + 1));
	}
	if (getTimeVal(parseFloat(Time_Object.getAttribute("seconds"))) %2){
		Time_Object.innerHTML = Time_Object.getAttribute("hour") + ':' + Time_Object.getAttribute("minute") + '  ' + Time_Object.getAttribute("ap");
	}
	else
	{
		Time_Object.innerHTML = Time_Object.getAttribute("hour") + ' ' + Time_Object.getAttribute("minute") + '  ' + Time_Object.getAttribute("ap");
	}
}

function Incriment_Minute(Time_Object)
{
	if (Time_Object.getAttribute("minute") == 59)
	{
		Time_Object.setAttribute("minute", "00");
		Incriment_Hour(Time_Object);
	}
	else 
	{
		Time_Object.setAttribute("minute", getTimeVal(parseFloat(Time_Object.getAttribute("minute")) + 1));
	}
}

function Incriment_Hour(Time_Object)
{
	if (Time_Object.getAttribute("hour") == 12)
	{
		Time_Object.setAttribute("hour", "01");
		if (Time_Object.getAttribute("ap") == "am")
		{
			Time_Object.setAttribute("ap", "pm");
		}
		else 
		{
			Time_Object.setAttribute("ap", "am");
		}
	}
	else 
	{
		Time_Object.setAttribute("hour", getTimeVal(parseFloat(Time_Object.getAttribute("hour")) + 1));
	}
}

function getTimeVal(val)
{
	if (val < 10)	{ val = "0" + val; }
	return val;
}

function hideUpdate(e)
{
	var frmUpdate = getTB("frmUpdate");
	frmUpdate.className = "hide";
}

function getNumber(val)
{
	return parseFloat(val.replace(",", "").replace("$", "").replace("R", ""));
}

function hidePostBack(e)
{
	var frmPostBack = getTB("frmPostBack");
	frmPostBack.className = "hide";
}

function SubmitForm(e)
{
	var el = getSrcObj(e);
	for (i = 0; i < 20; i++)
	{
		if (el.nodeName.toLowerCase().indexOf("form") > -1)
		{
			el.submit();
			i = 20;
		}
		else 
		{
			el = el.parentNode;
		}
		if (el == null)
		{
			i = 100;
		}
	}
}

function setRating(e, Rating)
{
	getTB("tbRating").value = Rating;
	SubmitForm(e);
}

function resizeFrame(e)
{
	var frm = getSrcObj(e);
	if (frm.contentDocument)
	{
		frm.style.height = frm.contentDocument.body.scrollHeight + "px";
	}
	else
	{
		frm.style.height = (frm.document.body.scrollHeight + 50) + "px";
	}
	if (parseInt(frm.style.height) < 300)
	{
		frm.style.height = 300;
	}
}

function KeyCode(e){
    if (e == null){ e = event; }
    if (e.which != null){
		var code = e.which;
	 }
	 else {
        var code = e.keyCode;
	}
	return code;
}

function initMainValues()
{
	var tbs = getTags("INPUT");
	for (var p = 0; p < tbs.length; p++){
		if (tbs[p].getAttribute("SearchTable") != null && tbs[p].getAttribute("SearchTable") != "")
		{
			addCustomEvent(tbs[p], "keypress", DoQuickSearch);
			addCustomEvent(tbs[p], "blur", hidePostBackInASec);
		}
	}
}

function DoQuickSearch(e)
{
	var frmPostBack = getTB("frmPostBack");
	searchObj = getSrcObj(e);
	var key = "";
	try
	{
		key = String.fromCharCode(KeyCode(e));
	}
	catch (e) {}
	if (KeyCode(e)  == 13)
	{
		if (frmPostBack.className != "hide")
		{
			try
			{
				var firstVal = frmPostBack.contentWindow.getTB("tblSearchValues0").innerHTML;
				searchObj.value = firstVal;
				frmPostBack.className = "hide";
				e.cancelBubble = true;
				return false;
			}
			catch (e) {}
		}
	}
	else if (searchObj.value != "")
	{
		frmPostBack.style.top = findPosY(searchObj) + 21;
		frmPostBack.style.left = findPosX(searchObj);
		frmPostBack.src = "QuickSearch.php?SearchTable=" 
			+ searchObj.getAttribute("SearchTable") + "&SearchField=" 
			+ searchObj.getAttribute("SearchField") + "&Value=" 
			+ Check_Query_Value(searchObj.value + key);
	}
	else
	{
		hidePostBack();
	}
}

function Check_Query_Value(Value)
{
   Value = ReplaceAll(Value, "&", "@AND");
   Value = ReplaceAll(Value, "?", "@QUE");
   Value = ReplaceAll(Value, "#", "@HAS");
   Value = ReplaceAll(Value, "\r\n", "@NL");
   Value = ReplaceAll(Value, "\n", "@NL");
   return Value;
}

function ReplaceAll(sMessage, sReplace, sWith)
{
   sMessage = sMessage + "";
   var p = 0;
   while (sMessage.indexOf(sReplace) > -1 && p < 100){
      sMessage = sMessage.replace(sReplace, sWith);
      p++;
   }
   return sMessage;
}

function setTBValue(e)
{
	var tb = getSrcObj(e);
	parent.searchObj.value = tb.innerHTML;
	parent.hidePostBack();
}

function hidePostBackInASec(e)
{
	setTimeout("hidePostBack();", 100);
}

function showCalculator(e)
{
	var a = getSrcObj(e);
	var tCalculator = getTB("tCalculator");
	tCalculator.className = "";
	tCalculator.style.left = (findPosX(a) - 125) + "px";
}

function hideCalculator()
{
	var tCalculator = getTB("tCalculator");
	tCalculator.className = "hide";
}

function calcNow(bShowError)
{
	var spCalcResult = getTB("spCalcResult");
	var tbCalcAmount = getTB("tbCalcAmount");
	var amt = getNumber(tbCalcAmount.value);
	if (isNaN(amt))
	{
		if (bShowError)
		{
			spCalcResult.innerHTML = "invalid number";
		}
	}
	else
	{
		if (getTB("rblCalcTypeN").checked)
		{
			getVal(amt, "SARate", "NZRate");
		}
		else
		{
			getVal(amt, "NZRate", "SARate");
		}
	}
}

function getVal(amt, sFrom, sTo)
{
	var aRate = getTB("aRate");
	spCalcResult.innerHTML = "Answer: " + getSymbol(!getTB("rblCalcTypeN").checked) + round_decimals((amt * aRate.getAttribute(sFrom) / aRate.getAttribute(sTo)), 2);
}

function setCalcSymbol()
{
	getTB("spCalcSymbol").innerHTML = getSymbol(getTB("rblCalcTypeN").checked);
	calcNow(false);
}

function getSymbol(bDollar)
{
	if (bDollar) { return "$"; }
	else { return "R"; }
}

function getTB(val)
{
	return document.getElementById(val);
}

function round_decimals(original_number, decimals) {
   var result1 = original_number * Math.pow(10, decimals);
   var result2 = Math.round(result1);
   var result3 = result2 / Math.pow(10, decimals);
   return pad_with_zeros(result3, decimals);
}

function pad_with_zeros(rounded_value, decimal_places) {
   var value_string = rounded_value.toString();
   var decimal_location = value_string.indexOf(".");
   if (decimal_location == -1) {
      decimal_part_length = 0
      value_string += decimal_places > 0 ? "." : ""
   }
   else {
      decimal_part_length = value_string.length - decimal_location - 1
   }
   var pad_total = decimal_places - decimal_part_length

   if (pad_total > 0) {
      for (var counter = 1; counter <= pad_total; counter++)
         value_string += "0"
   }
   return value_string
}

function CheckFrameSize(e)
{
	var i = getSrcObj(e);
	if (i.clientHeight < 200)
	{
		i.style.height = "600px";
	}
}

function Expand(e) {
    var txt = getSrcObj(e);
   txt.style.height = "200px";
   txt.style.width = "300px";
   txt.style.position = "absolute";
   txt.style.zIndex = 100000;
   txt.style.top = findPosY(txt) - 20;
   txt.style.left = findPosX(txt) - 50;
   txt.select();
}

function UnExpand(e) {
   var txt = getSrcObj(e);
   txt.style.position = "";
   txt.style.height = "14px";
   txt.style.width = "100px";
   txt.style.top = "";
   txt.style.left = "";
}
