	function setMainDiv() {
	try
		{
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || 		document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
		}
		//window.alert( 'Width = ' + myWidth );
		//window.alert( 'Height = ' + myHeight );
		if (window.name == 'Spawn'){
			document.getElementById("mainpopup").style.height = (myHeight - 100) + 'px';
		} else {
			//document.getElementById("main").style.height = (myHeight - 230) + 'px';
		}
		} catch (ex){
		}
	}

	function toggleBox(szDivID, iState, centreDiv, showWait){
		// 1 visible, 0 hidden{
		var obj = document.layers ? document.layers[szDivID] :
		document.getElementById ?  document.getElementById(szDivID).style :
		document.all[szDivID].style;
		obj.visibility = document.layers ? (iState ? "show" : "hide") :
		(iState ? "visible" : "hidden");
		if (centreDiv==true && iState==1){
			obj.left = (document.body.offsetWidth - 250) / 2 + 'px';
			//obj.top = ((document.body.offsetHeight / 2) - 28) + 'px';
			obj.top='275px';

		}
		document.body.style.cursor= (showWait ? "wait" : "default");
		if (szDivID != 'divWait') {toggleShim('shim',iState)};
	}

	function toggleShim(szDivID, iState){
		// 1 visible, 0 hidden{
		var obj = document.layers ? document.layers[szDivID] :
		document.getElementById ?  document.getElementById(szDivID).style :
		document.all[szDivID].style;
		obj.visibility = document.layers ? (iState ? "show" : "hide") :
		(iState ? "visible" : "hidden");
		if (iState==1){
			obj.left = (document.body.offsetWidth - 250) / 2 + 'px';
			obj.top = (document.body.offsetHeight / 2) - 200 + 'px';
		}
	}

	function makeDiv(id){ 
		var mydiv = document.createElement("div"); 
		mydiv.id=id; 
		if (id=='divWait'){ 
			mydiv.innerHTML = "<p><font color='red' style='FONT-WEIGHT:bold'>Please wait...</font> <img align=absMiddle src='../images/wait.gif'></p></iframe>" 
		} else { 
			var ifr = document.createElement("iframe") 
			ifr.setAttribute('id','shim'); 
			ifr.setAttribute('scrolling','no'); 
			ifr.setAttribute('frameborder','0'); 
			ifr.setAttribute('zIndex',98); 
			ifr.style.visibility='hidden'; 
		    	ifr.style.position='absolute'; 
			ifr.style.width='250px'; 
			ifr.style.height='100px'; 
			ifr.style.display='block'; 
			document.body.appendChild(ifr); 
			
		} 
		document.body.appendChild(mydiv); 
	} 
	
	function setDivHtml(id,msg,iserror)
	{
		try
		{
			makeDiv(id);
			var close= "<div align=right onClick=hideDiv('" + id + "')>";
			close += "<img class=close alt=Close src=../images/close.gif align=absbottom /></div>";
			var hide = "<br/><br/><input type=button class=formbutton value='Hide' onClick=hideDiv('" + id + "')>"
			var img = null;
			if (iserror==true){
				img = "<img alt=Error src='../images/error.gif'/>&nbsp;";
			} else
			{
				img = "<img alt=Info src='../images/info.gif'/>&nbsp;";
			}
			var myDiv = document.getElementById(id);
			myDiv.innerHTML = close + img + '<b>' + msg + '</b>' + hide;
			toggleBox(id,1,true,false);
		} catch (ex){
			alert(ex.message);
		}
	}

	function showError(error)
	{
		setMainDiv();
		makeDiv('divError');
		setDivHtml('divError',error,true);
	}

	function showInfo(error)
	{
		setMainDiv();
		makeDiv('divInfo');
		setDivHtml('divInfo',error,true);
	}
	function showWait(){
		try
		{
			makeDiv('divWait');
			toggleBox('divWait',0,true,true);
		} catch (ex){
			alert(ex.message);
		}
	}
	function hideDiv(id){
		toggleBox(id,0,false,false);
	}
	
	function isNull(value){
		return (value==null) ? "&nbsp;" : value;
	}
	
	function round(number) {
		var decPattern = /\./; // a decimal
		var decimal = decPattern.exec(number); 
	    if (decimal != null){ 
			var C = Math.round(number * 100)
			// '\u00A3' GBP
			// '\u20AC' EUR
			return (C/100 + (C%100 ? C%10 ? '' : '0' : '.00'))
		}else{
			return number;
		}
	} 

	function formatValue(value){
		switch (typeof(value)){
			case "object":
				return value.toLocaleString();
			case "string":
				return value;
			case "number":
				return round(value);
		}
	}
	
	function spawnWindow(sURL){
		window.open(sURL,'Spawn','width=500,height=500,left=280,top=0,menubar=no,scrollbars=yes,toolbar=no')
	}
	
		
	function checkResponse(response){
		//this function always expects a dataset object
		var ds = response.value;
		if(ds != null && typeof(ds) == "object" && ds.Tables != null){
			return ds;
		} else {
			if (response.error!=null){
				showError(response.error);
			} else {
				showError(response.request.responseText);
			}
		}	
	}

	//This function will build a table by looping through all the rows in the dataset
	function buildTable(ds,headers,fields,list,title){
		try
		{
			var s = new Array();
		
			if (title!=null){s[s.length]="<h2>" + title + "</h2>"};
			s[s.length] = "<table ><tr>";
			if (list == true || list == null){
				for(var h=0; h<headers.length; h++){
					s[s.length] = "<th>" + headers[h] + "</th>";
				}
				s[s.length] = "</tr>";
				for(var i=0; i<ds.Tables[0].Rows.length; i++){
					s[s.length] = "<tr>";
					for(var f=0; f<fields.length; f++){
						if (ds.Tables[0].Rows[i][fields[f]] != null){
							s[s.length] = "<td>" + formatValue(ds.Tables[0].Rows[i][fields[f]]) + "</td>";
						} else {
							s[s.length] = "<td>&nbsp;</td>";
						}	
					}
					s[s.length] = "</tr>";
				}
			}
			s[s.length] = "</table>";
			return s.join("");
		} catch (ex){
			showError(ex.message);
		}
	}

	var count = new Object();
	var timerOn = false;
	
	function GetUpdates(){
		var recipients = document.getElementsByTagName("div");
		var num;
		var message;
		
		for(i=0;i<recipients.length;i++){
			if (recipients[i].id.substring(0,4) == 'div_') {
				num = recipients[i].id.split("_")[1];
				message = recipients[i].id.split("_")[2];
				count[num]=0;
				GetUpdate(num,message);
			}
		} 
	}

	function GetUpdate(num,message){
		//makeDiv('divWait');
		//toggleBox('divWait',1,true,true);
		//change img
		document.getElementById("img_" + num + "_" + message).src = "../images/watch.gif";
		//make async call
		sjmillerconsultants.AjaxMethods.GetUpdateForMessageRecipient(num,message,cb_GetUpdateForMessageRecipient);				
	}

	function GetSMSCreditsUsed(){
		//makeDiv('divWait');
		//toggleBox('divWait',1,true,true);
		sjmillerconsultants.AjaxMethods.GetSMSCreditsUsed(cb_GetSMSCreditsUsed);
	}
		
	function GetSMSMissingCallbacks(){
		//makeDiv('divWait');
		//toggleBox('divWait',1,true,true);
		sjmillerconsultants.AjaxMethods.GetSMSMissingCallbacks(cb_GetSMSMissingCallbacks);
	}
	
	function GetRegistrations(){
		//makeDiv('divWait');
		//toggleBox('divWait',1,true,true);
		sjmillerconsultants.AjaxMethods.GetRegisteredUsers(cb_GetRegistrations);
	}
	
	function cb_GetUpdateForMessageRecipient(response){
		//parse the response
		try
		{
			var num = response.value.split("|")[0];
			var message = response.value.split("|")[1];
			var desc = response.value.split("|")[2];
			var dte = response.value.split("|")[3];
			
			//rebuild the info
			document.getElementById("img_" + num + "_" + message).src = "../images/info.gif";
			document.getElementById("sta_" + num + "_" + message).innerText = desc;
			document.getElementById("dte_" + num + "_" + message).innerText = dte;
			
			//update the info in the counter object
			count[num] = count[num] + 1;
			if (count[num]<3){
				window.setTimeout('GetUpdate(' + num + ',' + message + ')',5000);
			}
			//hideDiv('divWait');
		} catch (ex) {
			alert("An error has occured: " + ex.message);
		}		
	}
	
	function cb_GetSMSCreditsUsed(response){
		var ds = checkResponse(response);
		var headers = new Array("SMS Credits Used","Sales","Date");
		var fields = new Array("SMSCreditsUsed","Sales","Date");
		if (ds != null){document.getElementById("smscredits").innerHTML = buildTable(ds,headers,fields,true,"SMS credits used in the last 14 days")}
		//toggleBox('divWait',0);		
		window.setTimeout('GetSMSCreditsUsed()',10000);
	}
	
	function cb_GetSMSMissingCallbacks(response){
		var ds = checkResponse(response);
		var headers = new Array("UserMessageRecipientId","Number","Charge","Date");
		var fields = new Array("UserMessageRecipientId","Recipient","GatewayUnits","CreateDate");
		if (ds != null){document.getElementById("callbacks").innerHTML = buildTable(ds,headers,fields,true,"SMS messages awaiting callbacks")}
		//toggleBox('divWait',0);		
		window.setTimeout('GetSMSMissingCallbacks()',10000);
	}
	
	function cb_GetRegistrations(response){
		var ds = checkResponse(response);
		var headers = new Array("UserId","Name","Country","Date");
		var fields = new Array("UserId","CustomerName","Country","CreateDate");
		if (ds != null){document.getElementById("registrations").innerHTML = buildTable(ds,headers,fields,true,"Registrations in the last 14 days")}
		//toggleBox('divWait',0);		
		window.setTimeout('GetRegistrations()',10000);
	}

	function pruef (textbox) {
		var anz=0, len=0,s;
		var msg = textbox.value.split("");

		for (var i=1; i<=textbox.value.length; i++) {
			s = msg[i-1];if (s=='€' || s=='~' || s=='^' || s=='\\' || s=='{' || s=='}' || s=='[' || s==']' || s=='|') 
			{
			if (anz>463) { break };anz+=2;
			} 
			else {
				if (anz>464) { break };
				anz++;
				}
				len = i;
				}
			if (textbox.value.length> len) {
				textbox.value = textbox.value.substring(0,len);
			}
			document.getElementById("ctl00_ContentPlaceHolder1_chars").value = 465-anz;
			if (navigator.appName =="Netscape" && parseInt(navigator.appVersion) <= 4){
				document.captureEvents(Event.KEYPRESS);textbox.onkeypress = pruef;
			}
			document.getElementById("ctl00_ContentPlaceHolder1_credits").value = Math.ceil(anz/155);
		}
	
		function show(id){
			switch (id){
				case 1:
					showInfo('Your details have been updated.');
					break;
				case 2:  
					showInfo('Your account details have been updated.');
					break
				default:
					showInfo('Thank you for taking the time to send us your comments. We are grateful for all customer opinion.');
					break;
			}
		}
	
	function setTab() {
        	var ULs, UL, LIs, LI, i=j= -1
		var url = cleanUrl(document.location.href);

		ULs = document.getElementsByTagName("ul"); 
		
        	while( UL=ULs[++i] ){/* stops when ULs[n]--> null */
                	if(UL.id=="tabMenu"){
                		j = -1;
                		LIs=UL.getElementsByTagName("li");
                		while( LI=LIs[++j] ){
					
							var tabUrl = cleanUrl(LI.childNodes.item(0).toString());
		                    if(tabUrl.toLowerCase()==url.toLowerCase()){// Change <li> background
		                        LI.childNodes.item(0).className="active";
		                        var tabText=LI.firstChild.firstChild.data;
	                            var newNode=document.createTextNode(tabText);
        	                    //LI.removeChild(LI.firstChild);
                	            //LI.appendChild(newNode);
                        	}
	                	}
                	}
        	} 
	}

	function cleanUrl(url){

		var tmpurl = url;
		if(tmpurl.indexOf("?")!=-1){
			tmpurl = tmpurl.split("?")[0];
		}
		if(tmpurl.indexOf("#")!=-1){
			tmpurl = tmpurl.split("#")[0];
		}
		return tmpurl;
	}
	window.onload = function(){setMainDiv();setTab();};
	window.onresize = function(){setMainDiv()};
	