﻿//General Javascript Functions

function FormatMoney(n)
{
  var c=2;
  var d='.';
  var t=',';
	c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return '£'+s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

function Logout()
{
 LoginService.Logout(Logout2,OnTimeOut,OnError);
}

function Logout2(s)
{
 window.location = 'Default.aspx';
}

function Hide(c)
{
 var e = document.getElementById(c);
 if (e!=null)
 {
  e.style.display = 'none';
 }
}

function Hide2(c)
{
 var e = document.getElementById(c);
 if (e!=null)
 {
  e.style.visibility = 'hidden';
 }
}

function NewInput(s)
{
 if(!CtlExists(s))
 {
  var newI = document.createElement("INPUT");
  newI.setAttribute("type","hidden");
  newI.id = s;
  document.body.appendChild(newI);
 }
}
function NewInput2(s,v)
{
 NewInput(s);
 SetValue(s,v);
}

function CtlExists(c)
{
  if (document.getElementById(c) != null) 
  {
   return true;
  } else 
  {
   return false;
  }
}

function Show(c)
{
 document.getElementById(c).style.display = '';
}

function getTop (inputObj)
{		
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML'){
  		returnValue += inputObj.offsetTop;
  		if(document.all)returnValue+=inputObj.clientTop;
  	}
  } 
  return returnValue;
}


function getLeft (inputObj)
{	  
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if ((inputObj.tagName!='HTML') && (inputObj.tagName!='BODY'))
  	{
  		returnValue += inputObj.offsetLeft;
  		if(document.all)returnValue+=inputObj.clientLeft;
  	}
  }
  return returnValue;
}

function getRelTop (inputObj)
{		
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
  	if((inputObj.tagName!='HTML') && (inputObj.tagName!='DIV')){
  		returnValue += inputObj.offsetTop;
  		if(document.all)returnValue+=inputObj.clientTop;
  	}
  } 
  return returnValue;
}

function getRelLeft (inputObj)
{	  
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if ((inputObj.tagName!='HTML') && (inputObj.tagName!='BODY') && (inputObj.tagName!='DIV'))
  	{
  		returnValue += inputObj.offsetLeft;
  		if(document.all)returnValue+=inputObj.clientLeft;
  	}
  }
  return returnValue;
}

function urlParam(name)
{
  //Returns a parameter from the current URL
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]").toUpperCase();
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href.toUpperCase());
  if(results == null) return ""; else return results[1];
}

function isPage(s)
{
 return window.location.href.toUpperCase().indexOf(s)>=0;
}

function DDInx(c)
{
 //Returns selected index of a dropdown
 return document.getElementById(c).selectedIndex;
}

function DDVal(c)
{
 //Returns selected value for a dropdown
 return document.getElementById(c).options[DDInx(c)].value;
}

function DDTxt(c)
{
 //Returns selected text for a dropdown
 return document.getElementById(c).options[DDInx(c)].text;
}

function DDClr(c)
{
 document.getElementById(c).options.length=0;
}

function OnTimeOut(val) 
{
  OnError(val);
}

function OnError(val) 
{
 var i=val.get_statusCode();
 if ((i>=12001) && (i<=12156))
 {
  alert('There appears to be a problem with your internet connection. The most likely reason is your internet connection has dropped. Please try reconnecting to the internet and retrying.\r\nIf you continue to have problems (and are able to browse to other websites), contact CAP Support on 0113 2222007 and tell them you have received WinInet Error Code: '+i); 
 } else
 {
  alert('Error: ' + val.get_message() + '\r\n'+
        'Code: ' + i + '\r\n'+
        'Exception Type: ' + val.get_exceptionType() + '\r\n'+
        'Timeout: ' + val.get_timedOut()+"\r\n\r\n"+
        'Please close your browser and retry the section of the website which caused the problem.\r\nIf you continue to have problems, contact CAP Support on 0113 2222007 with the exact message shown here.');  
 }
}

function OnErrorLO(s) 
{
 window.location = 'Default.aspx';
}

function FixPNG()
{
  var arVersion = navigator.appVersion.split("MSIE")
  var version = parseFloat(arVersion[1])

  if ((version >= 5.5) && (document.body.filters)) 
  {
     for(var i=0; i<document.images.length; i++)
     {
        var img = document.images[i]
        var imgName = img.src.toUpperCase()
        if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
        {
           var imgID = (img.id) ? "id='" + img.id + "' " : ""
           var imgClass = (img.className) ? "class='" + img.className + "' " : ""
           var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
           var imgStyle = "display:inline-block;" + img.style.cssText 
           if (img.align == "left") imgStyle = "float:left;" + imgStyle
           if (img.align == "right") imgStyle = "float:right;" + imgStyle
           if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
           var strNewHTML = "<span " + imgID + imgClass + imgTitle
           + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
           + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
           + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
           img.outerHTML = strNewHTML
           i = i-1
        }
     }
  }
}

function ShowTopic(aTopicID)
{
 var aURL = 'Help.aspx';
 if (aTopicID != 0) aURL = aURL + '?id=' + aTopicID;
 window.open(aURL, 'caphelp', 'channelmode = 0, directories = 0, location = 0, menubar = 0, resizable = 1, scrollbars =1, status = 1, titlebar = 1, toolbar = 0, width = 550, height = 500');
}

function SetTxt(o,s)
{
 document.getElementById(o).innerText=s;
}
function SetHTML(o,s)
{
 document.getElementById(o).innerHTML=s;
}
function SetHTML2(o,s)
{
 if(CtlExists(o))
 {
  SetHTML(o,s)
 }
}
function SetValue(o,s)
{
 document.getElementById(o).value=s;
}
function GetValue(o)
{
 return document.getElementById(o).value;
}
function GetTxt(o)
{
 return document.getElementById(o).innerText;
}
function GetHTML(o)
{
 return document.getElementById(o).innerHTML;
}
function SetClass(o,s)
{
 if(CtlExists(o))
 {
  document.getElementById(o).className=s;
 }
}
function SetHref(o,s)
{
 var e = document.getElementById(o);
 if (e!=null)
 {
  e.href=s;
 }
}
function Disable(o)
{
 document.getElementById(o).disabled = true;
}
function SetTitle(o,t)
{
 document.getElementById(o).title = t;
}
function GetWidth(o)
{
 return document.getElementById(o).offsetWidth;
}
function SetWidth(c,w)
{
 if(CtlExists(c))
 {
   document.getElementById(c).style.width = w+'px';
 }
}