/*
################################################################################
# $Id: browser.js,v 1.4 2005/09/28 11:36:09 atvision Exp $
# Copyright AT Veterinary Systems (2001-2004)
# Sniff out the big 2 browsers (couresty of developer.apple.com)
################################################################################

 2004-06-30 PJW  isNS7 and isFF added. Be warned that isNS6 should
                 really be named isNS6up
*/
var isNS4 = false;
var isIE4 = false;
var isIE5 = false;
var isNS6 = false;
var isNS7 = false;
var isFF  = false;
var isIE = false;
var isNS = false;
isNS4 = (document.layers) ? true : false;
isIE4 = (document.all && !document.getElementById) ? true : false;
isIE5 = (document.all && document.getElementById) ? true : false;
isNS6 = (!document.all && document.getElementById) ? true : false;
isIE = (isIE4 || isIE5);
isNS = (isNS4 || isNS6);
if (isNS) {
  var agt=navigator.userAgent.toLowerCase();
  isNS7 = (agt.indexOf('netscape/7')!=-1);
  isFF  = (agt.indexOf('firefox')!=-1);
}
/*----------------------------------------------------------------------------*/
function setAnchorStyle(anchor, style){
  anchor.className = style;
}

/*----------------------------------------------------------------------------*/
function showLayer(layerName){
  setLayerVisibility(layerName, 'visible');
}

/*----------------------------------------------------------------------------*/
function hideLayer(layerName){
  setLayerVisibility(layerName, 'hidden');
}

/*----------------------------------------------------------------------------*/
function setLayerVisibility(layerName, visibility){
  if (isNS4) {
    //alert("setlayerVisibility: "+layerName+" "+visibility);
    document.layers[layerName].visibility = visibility;
  } 
  else if (isIE4) {
    document.all[layerName].style.visibility = visibility;
  }
  else if (document.getElementById) { //IE5, NS6 & W3C compliants 
    document.getElementById(layerName).style.visibility = visibility;
  }
}

/*----------------------------------------------------------------------------*/
function setLayerHtml(layerName, html){
  if (isNS4) {
    var doc = document.layers[layerName].document;
    doc.open();
    doc.write(html);
    doc.close();	
  }
  else if (isIE4) {
    document.all[layerName].innerHTML = html;
  }
  else if (document.getElementById) {  //IE5, NS6 & W3C compliants
    document.getElementById(layerName).innerHTML = html;
  }
}

/*----------------------------------------------------------------------------*/
function getLayerForm(layerName, formName){
  var doc = (isNS4) ? document.layers[layerName].document : document;
  return doc.forms[formName];
}

/*----------------------------------------------------------------------------*/
function getLayerImage(layerName, imageName) {
  var doc = (isNS4) ? document.layers[layerName].document : document;
  return doc.images[imageName];
}


/*----------------------------------------------------------------------------*/
function openPdf(url) {
  openAlignedWindow(url, "_blank", "resizable,menubar,status", 791, 545, 'center', 'center');
}

/*----------------------------------------------------------------------------*/
function openAlignedWindow(html, name, options, width, height, align, valign) {
  var topBorder = 0;
  var leftBorder = 0;
  var rightBorder = 11;
  var bottomBorder = 57;
  var screenWidth = 800;  //default
  var screenHeight = 600; //default
  var x;
  var y;

  if (isIE) {
    screenWidth = screen.width;
    screenHeight = screen.height;
  }
  else if (isNS) {
    //screenWidth = window.outerWidth;
    //screenHeight = window.outerHeight;
    if (name == '') name = '_blank';
     
  }

  switch (align) {
    case 'left':   x = leftBorder;                           break;
    case 'right':  x = screenWidth - width - rightBorder;    break;
    default:       x = parseInt((screenWidth - width) / 2);  break;
  }
  switch (valign) {
    case 'top':    y = topBorder;                            break;
    case 'bottom': y = screenHeight - height - bottomBorder; break;
    default:       y = parseInt((screenHeight - height) / 2);break;
  }
  if (options != '')options += ',';
  if (x < 0) x = 0;
  if (y < 0) y = 0;
  options += 'width='+width+',height='+height+',screenX='+x+',screenY='+y+',top='+y+',left='+x+'';
  var newWindow = window.open(html, name ,options);
  /*if (name == 'mailWindow') {
    alert("openAlignedWindow: options=\""+options+"\" ("+screenWidth+","+screenHeight+")");
  }*/  
  newWindow.focus();
  return newWindow;
}

/*----------------------------------------------------------------------------*/
function getParameter(name, defaultValue){
  url = location.href;
  i = url.indexOf(name+"=",0);
  if(i != -1){
    value = url.substr(i+name.length+1,256);
    i = value.indexOf("&",0);
    if(i != -1){
      value = value.substr(0,i);
    }
    value = unescape(value);
  }
  else {
    value = defaultValue;
  }
  //alert("getParameter: "+name+"="+value);
  return value;
}

/*----------------------------------------------------------------------------*/
function lengthMatching(s1,s2) {
  var lengthMatching = 0;
  for (var i=0; i < s1.length; i++) {
    if (s1.charAt(i).toLowerCase() == s2.charAt(i).toLowerCase()) {
      lengthMatching = i+1;
    }
    else {
      break;
    }
  }
  //alert("lengthMatching: "+lengthMatching + " "+s1+" "+s2);
  return lengthMatching;
}
/*----------------------------------------------------------------------------*/
function isInteger(value){
  //not quite right - can have embedded minus signs
  var result = isStringValid(value, false,true,'-');
  return result;
}
/*----------------------------------------------------------------------------*/
function isNumeric(value){
  //not quite right - can have embedded minus signs and multiple decimal points
  var result = isStringValid(value, false,true,'-.');
  return result;
}
/*----------------------------------------------------------------------------*/
function isStringValid(value, allowAlpha, allowNumeric, specials){
  var alphas = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  var numerics = "0123456789";
  var validChars = '';
  var c;

  if (allowAlpha) validChars = alphas;
  if (allowNumeric) validChars += numerics;
  validChars += specials;

  for (var i=0; i< value.length; i++) {
    c = "" + value.substring(i, i+1);

    if (validChars.indexOf(c) == "-1")return false;
  }
  return true;
}
/*----------------------------------------------------------------------------*/
function isEmailValid($email){
  var $i = $email.indexOf('@');
  //alert($i+' '+$email);
  $result = ($i < 1 || ($i+1) == $email.length) ? false : true;
  return $result;
}

/*----------------------------------------------------------------------------*/
function getEmailErrorMessage($email){
  var $i = $email.indexOf('@');
  $result = "Email address is not valid";
  //alert('i='+$i+' '+$email);
  if ($i < 0) {
    $result = "Email address must contain an @";
  }
  else if ($i == 0) {
    $result = "Email address cannot start with an @";
  }
  else if ($i+1 == $email.length) {
    $result = "Email address cannot end with an @";
  }
  return $result;
}

/*----------------------------------------------------------------------------*/
function getFilename(path){
  var i = path.lastIndexOf('/');
  if (i >= 0) {
    var file = path.substr(i+1, 250);
  }
  else {
    var file = path;
  }
  return file;
}
    
/*----------------------------------------------------------------------------*/
function noInternetAccess() {
  alert("Your user profile does not currently allow internet access");
}

/*----------------------------------------------------------------------------*/
function noEmail() {
  alert("Your user profile does not currently allow email");
}

/*----------------------------------------------------------------------------*/
function noUploading() {
  alert("Your user profile does not currently allow Central Filing changes");
}

/*----------------------------------------------------------------------------*/
function noSms() {
  alert("Your user profile does not currently allow texting");
}

/*----------------------------------------------------------------------------*/
function noFaxing() {
  alert("Your user profile does not currently allow file uploads");
}

/*----------------------------------------------------------------------------*/
function displayThawteCertificate() {
  openAlignedWindow("https://www.thawte.com/cgi/server/certdetails.exe?referer=http://www.visionline.co.uk","","status,sizable", 500, 565, 'center', 'center');
}

/*----------------------------------------------------------------------------*/
function visionPageLoaded() {
  if (page$showTree) drawTree();
  startAdvertTimer();
}

/*----------------------------------------------------------------------------*/
function lovliam(r,d,s) {
  a = '\074\141\040\150\162\145\146\075\047\155\141\151\154\164\157\072'+r+'\100';
  if (d) {
    a += d;
  }
  else {
    a+= '\166\151\163\151\157\156\154\151\156\145\056\143\157\056\165\153';
  }  
  if (s) {
    a+='\077\163\165\142\152\145\143\164\075'+escape(s);
  }
  a += '\047\076';
  return(a);
}

