﻿function OpenNewPopupWindow(url, width, height) {
  window.open(url, '_blank', 'toolbar=no, location=yes, directories=no, status=no, menubar=no, copyhistory=yes, width=' + width + ', height=' + height);
}
function OpenNewPopupWindow(url) {
  window.open(url, '_blank');
}

function GetPricePrefix() {
   return document.getElementById('ctl00_hidPricePrefix').value;
}

function GetTotalPrefix() {
   return document.getElementById('ctl00_hidTotalPrefix').value;
}

function PriceWithCurrency(num) {
   return NumberToCurrency(num, GetPricePrefix());
}
function TotalWithCurrency(num) {
   return NumberToCurrency(num, GetTotalPrefix());
}

function NumberToCurrency(num, currencySymbol) {
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
        num = num.substring(0, num.length - (4 * i + 3)) + ',' +
num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + currencySymbol + num + '.' + cents);
}
function PriceCurrencyToNumber(num) {
   return CurrencyToNumber(num, GetPricePrefix());
}
function TotalCurrencyToNumber(num) {
   return CurrencyToNumber(num, GetTotalPrefix());
}
function CurrencyToNumber(num, currencySymbol) {
   if (num == null) {
      return ParseFloat(0.0);
   }
   else {
      num = num.replace(currencySymbol, "");
      return ParseFloat(num.replace(/[%,+]+/g, ""));
   }
}
function TaxAmount(totalAmount, taxRate) {
    if (taxRate == 0.0)
        return 0.0;
    return totalAmount - TaxExclusive(totalAmount, taxRate);
}
function TaxExclusive(totalAmount, taxRate) {
    return Round(totalAmount / (1.0 + (taxRate / 100.0)), 2);
}    
function ParseFloat(str) {
    return (isNaN(parseFloat(str))) ? 0.0 : parseFloat(str);
}

function ParseInt(str) {
    return (isNaN(parseInt(str))) ? 0 : parseInt(str);
}

function Round(num, dec) {
    var result = Math.round(Math.round(num * Math.pow(10, dec + 1)) / Math.pow(10, 1)) / Math.pow(10, dec);
    return result;
}
/* string format */
String.format = function() {
   if( arguments.length == 0 )
      return null;
   var str = arguments[0];
   for(var i=1;i<arguments.length;i++) {
      var re = new RegExp('\\{' + (i-1) + '\\}','gm');
      str = str.replace(re, arguments[i]);
   }
   return str;
}
String.prototype.format = function() {
   var str = this;		
   for(var i=0;i<arguments.length;i++) {
      var re = new RegExp('\\{' + (i) + '\\}','gm');
      str = str.replace(re, arguments[i]);
   }
   return str;
}
// Move an element directly on top of another element (and optionally
// make it the same size)
function GetScreenResolution() {
	var pos = { w: 0, h: 0 };

	if (window.innerHeight) {
		pos.h = window.innerHeight;
		pos.w = window.innerWidth;
	}
	else if ((document.documentElement) && (document.documentElement.clientHeight)) {
		pos.h = document.documentElement.clientHeight;
		pos.w = document.documentElement.clientWidth;
	}
	else if (document.body.clientHeight) {
		pos.h = document.body.clientHeight;
		pos.w = document.body.clientWidth;
	}
	return pos;
}
function ShowPopupContent(mpeBehaviorId, HTMLEditorId) {
	//there is a bug on html editor in ajax popup control in FF, otherwise does not need
	//waiting for new release
	$find(mpeBehaviorId).show();
	if (HTMLEditorId != null && HTMLEditorId != '') {
		if (oUtil.setEdit) oUtil.setEdit();
		eval('oEdit_' + HTMLEditorId + '.loadHTML(document.getElementById(HTMLEditorId).value);');
	}
}
//only allowed 0-9, space, -(hyphen) and brakets()
function PhoneTextValidator(e, sourceID) //onkeypress
{
	var unicode = e.charCode ? e.charCode : e.keyCode;
	//backspace || tab || end || home || left || up || right || down || delete
	if (unicode == 8 || unicode == 9 || unicode == 35 || unicode == 36 || unicode == 37 || unicode == 38 || unicode == 39 || unicode == 40 || unicode == 46) {
		return true;
	}
	else {
		//only allow numbers 0 to 9 and space
		if (unicode < 48 || unicode > 57) {
			if (String.fromCharCode(unicode) == " " || String.fromCharCode(unicode) == "-" || String.fromCharCode(unicode) == "(" || String.fromCharCode(unicode) == ")") // space
			{
				return true;
			}
			else //if not a number
			{
				return false; //disable key press
			}
		}
		else {
			return true;
		}
	}
}
//only allowed 0-9
function NumericText(e, sourceID) //onkeypress
{
	var unicode = e.charCode ? e.charCode : e.keyCode;
	//backspace || tab || end || home || left || up || right || down || delete
	if (unicode == 8 || unicode == 9 || unicode == 35 || unicode == 36 || unicode == 37 || unicode == 38 || unicode == 39 || unicode == 40 || unicode == 46) {
		return true;
	}
	else {
		//only allow numbers 0 to 9
		if (unicode < 48 || unicode > 57) {
			return false; //disable key press
		}
		else {
			return true;
		}
	}
}
function DoPostBack(objID, arg) {
	__doPostBack(objID, arg);
}
function OpenPostcodeFinderUrl() 
{
	var street = escape(document.getElementById(arguments[1]).value);
	var suburb = '';
	var city = document.getElementById(arguments[3]).value;
	var finderUrl = arguments[0];
	var ar = arguments[4];
	var addr = new Array();
	if (street.indexOf('%0D%0A') > -1) //Windows encodes returns as \r\n hex
	{
		addr = street.split('%0D%0A', 3);
	}
	else if (street.indexOf('%0A') > -1) //Unix encodes returns as \n hex
	{
		addr = street.split('%0A', 3);
	}
	else if (street.indexOf('%0D') > -1) //Macintosh encodes returns as \r hex
	{
		addr = street.split('%0D', 3);
	}
	street = unescape(addr[0]);
	if (addr[1] != null && addr[1] != '') {
		suburb = unescape(addr[1]);
	}
	else {
		suburb = unescape(addr[2]);
	}
	switch (ar) {
		case 1:
			finderUrl = String.format(finderUrl, street + suburb + city);
			break;
		case 2:
			finderUrl = String.format(finderUrl, street + suburb, city);
			break;
		case 3:
			finderUrl = String.format(finderUrl, street, suburb, city);
			break;
		case 4:
			finderUrl = String.format(finderUrl, street.substring(0, street.indexOf(' ')), street.substring(street.indexOf(' ') + 1), suburb, city);
			break;
		default:
			break;
	}
	window.open(finderUrl, '_blank');
}
function CreateOnclickTreeNode(treeviewId) {
	var treeview = document.getElementById(treeviewId);
	if (treeview == null) return;
	var treeLinks = treeview.getElementsByTagName("a");
    for(i=0;i<treeLinks.length;i++)
    {
    	treeLinks[i].onclick = function(){return false;}
    }
}
var timerID;
function FadeInLayer(popLayerID, qty, elemID) {
	clearTimeout(timerID);
	timerID = setTimeout("StartFading('" + popLayerID + "', " + qty + ", '" + elemID + "')", 25);
}

function StartFading(popLayerID, qty, elemID) {
	TopOnWindow(popLayerID, elemID);
	Opacity(popLayerID, 1, 100, 500);
	clearTimeout(timerID);
	timerID = setInterval("EndFading('" + popLayerID + "', " + qty + ", '" + elemID + "')", 5000); //display 5 seconds.
}

function FadeOutLayer(popLayerID, qty, elemID) {
	EndFading(popLayerID, qty, elemID);
}

function EndFading(popLayerID, qty, elemID) {
	TopOnWindow(popLayerID, elemID);
	Opacity(popLayerID, 100, 0, 500);
	clearTimeout(timerID);
	if (qty == 0) {
		window.location.reload();
	}
}
function TopOnWindow(popLayerID, elemID) {
	var popLayer = document.getElementById(popLayerID);
	if (popLayer) {
		var elem = document.getElementById(elemID);
		var pos = GetObjectPos(elem);
		var scn = GetScreenResolution();
		var bodywrapper = GetObjectWidth(document.getElementById('bodywrapper'));
		var l = pos.x - ((GetObjectWidth(popLayer) - GetObjectWidth(elem)) / 2) - ((scn.w - bodywrapper) / 2);
		var t = pos.y - GetObjectHeight(popLayer) - 5;
		l = (l < 0) ? 0 : l;
		t = (t < 0) ? 0 : t;
		popLayer.style.left = l + 'px';
		popLayer.style.top = t + 'px';
		popLayer.style.visibility = "visible";
	}
}
function GetObjectPos(obj) {
	var pos = { x: 0, y: 0 };

	if (obj.offsetParent) {
		do {
			pos.x += obj.offsetLeft;
			pos.y += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	else if (obj.x && obj.y) {
		pos.x += obj.x;
		pos.y += obj.y;
	}
	return pos;
}

function GetObjectWidth(elem) {
	var result = 0;
	if (elem) {
		if (elem.offsetWidth) {
			result = elem.offsetWidth;
		}
		else if (elem.clip && elem.clip.width) {
			result = elem.clip.width;
		}
		else if (elem.style && elem.style.pixelWidth) {
			result = elem.style.pixelWidth;
		}
	}
	return parseInt(result);
}
function GetObjectHeight(elem) {
	var result = 0;
	if (elem) {
		if (elem.offsetHeight) {
			result = elem.offsetHeight;
		}
		else if (elem.clip && elem.clip.height) {
			result = elem.clip.height;
		}
		else if (elem.style && elem.style.pixelHeight) {
			result = elem.style.pixelHeight;
		}
	}
	return parseInt(result);
}

function Opacity(popElemID, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if (opacStart > opacEnd) {
		for (i = opacStart; i >= opacEnd; i--) {
			setTimeout("ChangeOpacity(" + i + ",'" + popElemID + "')", (timer * speed));
			timer++;
		}
	} else if (opacStart < opacEnd) {
		for (i = opacStart; i <= opacEnd; i++) {
			setTimeout("ChangeOpacity(" + i + ",'" + popElemID + "')", (timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function ChangeOpacity(opacity, popElemID) {
	var obj = document.getElementById(popElemID);
	if (obj) {
		var object = obj.style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
		if (opacity <= 0) {
			object.left = '-500px';
		}
	}
}

function GetCurrentScreenResolution() {
	var pos = GetScreenResolution();
	document.getElementById('ctl00_hidScreenHeight').value = pos.h;
}

function GetScrollTopPosition()
{
	if (document.all) {
		if (!document.documentElement.scrollTop)
			document.getElementById('ctl00_hidScrollTopPosition').value = document.body.scrollTop;
		else
			document.getElementById('ctl00_hidScrollTopPosition').value = document.documentElement.scrollTop;
	}
	else {
		document.getElementById('ctl00_hidScrollTopPosition').value = window.pageYOffset;
	}
}

function SetScrollTopPosition() {
	window.scrollTo(0, document.getElementById('ctl00_hidScrollTopPosition').value);
}

if (!document.getElementsByTagName) {
	window.onclick = function() {
		GetScrollTopPosition();
	}
}
window.onload = function() {
	GetCurrentScreenResolution();
	SetScrollTopPosition();
	if (document.getElementsByTagName) { //for IE
		document.getElementsByTagName("BODY")[0].onclick = function() {
			GetScrollTopPosition();
		}
	}
	
};

