﻿// JScript File

var deg2rad = Math.PI / 180;
var rad2deg = 180.0 / Math.PI;
var pi = Math.PI;

function NEtoLL(east, north) 
{
  // converts NGR easting and nothing to lat, lon.
  // input metres, output radians
  var nX = Number(north);
  var eX = Number(east);
  a = 6377563.396;       // OSI semi-major
  b = 6356256.91;        // OSI semi-minor
  e0 = 400000;           // easting of false origin
  n0 = -100000;          // northing of false origin
  f0 = 0.9996012717;     // OSI scale factor on central meridian
  e2 = 0.0066705397616;  // OSI eccentricity squared
  lam0 = -0.034906585039886591;  // OSI false east
  phi0 = 0.85521133347722145;    // OSI false north
  var af0 = a * f0;
  var bf0 = b * f0;
  var n = (af0 - bf0) / (af0 + bf0);
  var Et = east - e0;
  var phid = InitialLat(north, n0, af0, phi0, n, bf0);
  var nu = af0 / (Math.sqrt(1 - (e2 * (Math.sin(phid) * Math.sin(phid)))));
  var rho = (nu * (1 - e2)) / (1 - (e2 * (Math.sin(phid)) * (Math.sin(phid))));
  var eta2 = (nu / rho) - 1;
  var tlat2 = Math.tan(phid) * Math.tan(phid);
  var tlat4 = Math.pow(Math.tan(phid), 4);
  var tlat6 = Math.pow(Math.tan(phid), 6);
  var clatm1 = Math.pow(Math.cos(phid), -1);
  var VII = Math.tan(phid) / (2 * rho * nu);
  var VIII = (Math.tan(phid) / (24 * rho * (nu * nu * nu))) * (5 + (3 * tlat2) + eta2 - (9 * eta2 * tlat2));
  var IX = ((Math.tan(phid)) / (720 * rho * Math.pow(nu, 5))) * (61 + (90 * tlat2) + (45 * Math.pow(Math.tan(phid), 4) ));
  var phip = (phid - ((Et * Et) * VII) + (Math.pow(Et, 4) * VIII) - (Math.pow(Et, 6) * IX)); 
  var X = Math.pow(Math.cos(phid), -1) / nu;
  var XI = (clatm1 / (6 * (nu * nu * nu))) * ((nu / rho) + (2 * (tlat2)));
  var XII = (clatm1 / (120 * Math.pow(nu, 5))) * (5 + (28 * tlat2) + (24 * tlat4));
  var XIIA = clatm1 / (5040 * Math.pow(nu, 7)) * (61 + (662 * tlat2) + (1320 * tlat4) + (720 * tlat6));
  var lambdap = (lam0 + (Et * X) - ((Et * Et * Et) * XI) + (Math.pow(Et, 5) * XII) - (Math.pow(Et, 7) * XIIA));
  var geo = { latitude: phip, longitude: lambdap };
  return(geo);
} 

function Marc(bf0, n, phi0, phi)
{
  var Marc = bf0 * (((1 + n + ((5 / 4) * (n * n)) + ((5 / 4) * (n * n * n))) * (phi - phi0))
    - (((3 * n) + (3 * (n * n)) + ((21 / 8) * (n * n * n))) * (Math.sin(phi - phi0)) * (Math.cos(phi + phi0)))
    + ((((15 / 8) * (n * n)) + ((15 / 8) * (n * n * n))) * (Math.sin(2 * (phi - phi0))) * (Math.cos(2 * (phi + phi0))))
    - (((35 / 24) * (n * n * n)) * (Math.sin(3 * (phi - phi0))) * (Math.cos(3 * (phi + phi0)))));
  return(Marc);
}

function InitialLat(north, n0, af0, phi0, n, bf0)
{
  var phi1 = ((north - n0) / af0) + phi0;
  var M = Marc(bf0, n, phi0, phi1);
  var phi2 = ((north - n0 - M) / af0) + phi1;
  var ind = 0;
  while ((Math.abs(north - n0 - M) > 0.00001) && (ind < 20))  // max 20 iterations in case of error
  {  
	ind = ind + 1;
	phi2 = ((north - n0 - M) / af0) + phi1;
    M = Marc(bf0, n, phi0, phi2);
    phi1 = phi2;
  }
  return(phi2);  
}


function CountChars(txt,lbl)
{

    document.getElementById(lbl).innerHTML = 999 - document.getElementById(txt).value.length + ' characters remaining';
}

function PleaseWait()
{
    document.getElementById("tblMain").style.display='none'
    document.getElementById("tblPleaseWait").style.display='block'
    
    setTimeout("UpdateImg('imgPleaseWait','../images/please_wait.gif');",50)   
}
function UpdateImg(ctrl,imgsrc)
{
    var img = document.getElementById(ctrl);
    img.src = imgsrc;
}

function OpenLargeImage(url)
{
    window.open(url, 'LargeImage', 'toolbar=0,location=0,statusbar=0,menubar=0,width=800,height=600')
}

function FillHiddenLocation(sel,txt)
{
    var selectedValue = sel.options[sel.selectedIndex].value;
    document.forms[0].elements[txt].value = selectedValue;
}

function LocationLookup(parentId, value, name)
{
    this.parentId = parentId;
    this.value = value;
    this.name = name;
}

function BusinessTypeLookup(parentId, value, name)
{
    this.parentId = parentId;
    this.value = value;
    this.name = name;
}

function changedLocation(sel,target)
{
    var second = document.forms[0].elements[target];
    if (!second)
    {
        return; //error
    }
    
    var selectedValue = sel.options[sel.selectedIndex].value;
    second.options.length = 0;
    
    for (var i = 0; i < LocationLookups.length; ++i)
    {
        var LocationLookup = LocationLookups[i];
        if (LocationLookup.parentId == selectedValue)
        {
            var option = document.createElement('OPTION');
            option.value = LocationLookup.value;
            option.innerHTML = LocationLookup.name;
            second.appendChild(option);
        }
    }
}

function changedBusinessType(sel,target)
{
    var second = document.forms[0].elements[target];
    if (!second)
    {
        return; //error
    }
    
    var selectedValue = sel.options[sel.selectedIndex].value;
    second.options.length = 0;
    
    for (var i = 0; i < BusinessTypeLookups.length; ++i)
    {
        var BusinessTypeLookup = BusinessTypeLookups[i];
        if (BusinessTypeLookup.parentId == selectedValue)
        {
            var option = document.createElement('OPTION');
            option.value = BusinessTypeLookup.value;
            option.innerHTML = BusinessTypeLookup.name;
            second.appendChild(option);
        }
    }
}


function tvwOneSelected(event) 
{
    var obj = event.srcElement || event.target ; 
    var treeNodeFound = false; 
    var checkedState
    
    if (obj.tagName == "INPUT" && obj.type == "checkbox") 
    { 
        var treeNode = obj; 
        checkedState = treeNode.checked;
                
        //untick all checkboxes
        var elem = document.forms[0].elements;
        var str = ""
        for(var i = 0; i < elem.length; i++)
        {
            if (elem[i].type == 'checkbox')
            {
                elem[i].checked = false;
            }
        }
        obj.checked = true
    }
}

function checkTerms(sender, args)
{
    if (document.all.ctl00_cph1_wiz_radAgree.checked)
    {
        args.IsValid = true;      
    }
    else
    {
        args.IsValid = false;
        return;      
    }
}


function StartTimer(timer_box)
{
    if (document.getElementById(timer_box).value == '')
    {
        d = new Date()
        document.getElementById(timer_box).value = d
    }
}
function StopTimer(timer_box)
{
    d = new Date()
    document.getElementById(timer_box).value = d
}


function DisableEnter()
{

    if (window.event.keyCode == 13) 
    {
        event.returnValue=false; 
        event.cancel = true;
    }
}

function noPostBack(sNewFormAction,sTarget)
{
    if (sTarget != 'undefined')
    {
        document.forms[0].target = sTarget;
    }
    
    document.forms[0].action = sNewFormAction;
    document.forms[0].__VIEWSTATE.name = 'NOVIEWSTATE';
}


function sstchur_SmartScroller_GetCoords()
  {
    var scrollX, scrollY;
    if (document.all)
    {
      if (!document.documentElement.scrollLeft)
        scrollX = document.body.scrollLeft;
      else
        scrollX = document.documentElement.scrollLeft;

      if (!document.documentElement.scrollTop)
        scrollY = document.body.scrollTop;
      else
        scrollY = document.documentElement.scrollTop;
    }
    else
    {
      scrollX = window.pageXOffset;
      scrollY = window.pageYOffset;
    }
    document.forms[0].ctl00_scrollLeft.value = scrollX;
    document.forms[0].ctl00_scrollTop.value = scrollY;
  }


  function sstchur_SmartScroller_Scroll()
  {
  
    var x = document.forms[0].ctl00_scrollLeft.value;
    var y = document.forms[0].ctl00_scrollTop.value;
    window.scrollTo(x, y);
  }

  
function OpenModal(url,width,height)
{
    var WinSettings = 'center:yes;status:no;toolbar:no;location:no;menubar:no;resizable:yes;dialogWidth:' + width + 'px;dialogHeight:' + height + 'px'
	var return_value = window.showModalDialog(url, '', WinSettings)
	//var return_value = window.open(url, '', WinSettings)
}

function OpenWin(url,width,height,name)
{


    var WinSettings = 'toolbar=0,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=' + width + ',height=' + height
	var return_value = window.open(url, name, WinSettings)
	//var return_value = window.open(url, '', WinSettings)
}
  
  window.onload = sstchur_SmartScroller_Scroll;
  window.onscroll = sstchur_SmartScroller_GetCoords;
  window.onclick = sstchur_SmartScroller_GetCoords;
  window.onkeypress = sstchur_SmartScroller_GetCoords;