/*
  jsfunctions.js
*/

// set variables // ======================================================================================

// get cookie settings
var userfont  = GetCookie ('userfontc');

// validate cookie setting and apply defaults if necessary
if (userfont>1) {} else {userfont = 11;}

var fontModifier  =  1; // how much to increase/decrease font size each time (in pixels)
var curFontSize   = 11; // curFontSize needs to be the same as the font size for paragraphs, as set in the css (in pixels)
var curLineHeight = 16; // curLineHeight needs to be the same as the line height for paragraphs, as set in the css (in pixels) 

// detect browser support for getElementById and createElement
var canGetElem = false;
var canCreateElem = false;
if (document.getElementById) {
    canGetElem = true;
    if (document.createElement) {
        canCreateElem = true;
    }
}

// functions // ============================================================================================
function imgXY(imgID) {
 /* This function returns the upperleft x,y coordinates of the specified image */
 var XY = new Object();
 var imgObj = document.images[imgID];
 if (typeof(imgObj)=='undefined') {
    XY.x = 0;
    XY.y = 0;
 } else {
    if (document.layers) {
       XY.x = eval(imgObj).x;
       XY.y = eval(imgObj).y;
    } else {
       var x,y, tempEl;
       x = eval(imgObj).offsetLeft;
       y = eval(imgObj).offsetTop;
       tempEl = eval(imgObj).offsetParent;
       while (tempEl != null) {
          x += tempEl.offsetLeft;
          y += tempEl.offsetTop;
          tempEl = tempEl.offsetParent;
       }
       XY.x = x;  // minor adjustments can be made here
       XY.y = y;
    }
 }         
 return XY;
}
function IsValidEmail(EmailAddr) {
    return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(EmailAddr))
}
function Trim(str) { // trim leading and trailng whitespace
    if (!str) return "";
    return str.replace(/^\s+/, "").replace(/\s+$/, "");
}
function SetPass() {
    var cPass = prompt("Well?",'');
    document.cookie="fg=" + escape(cPass) + ";path=/;";
    history.go(); 
} 
function IsPhone(strString) {      //  check for valid numeric phone strings   

    var strValidChars = "0123456789";
    var strChar;
    var blnResult = true;
    
    strString = strString.replace(/ /g, ""); // remove spaces
    strString = strString.replace(/\-/g, ""); // remove dashes
    strString = strString.replace(/\)/g, ""); // remove right bracket
    strString = strString.replace(/\(/g, ""); // remove left bracket
    if (strString.length == 0) return false;
    
    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) blnResult = false;
    }
    return blnResult;
}
function redirect(Url) {
     window.location.url=Url;
}

function swap(vImage, vSrc) { 
   var objStr,obj; 
   if (document.images) { 
      if (typeof(vImage) == 'string') { 
         objStr = 'document.' + vImage; 
         obj = eval(objStr); obj.src = vSrc; 
      } else if ((typeof(vImage) == 'object') && vImage && vImage.src) vImage.src = vSrc; 
   } 
}

function displaySection(IDvalue) {
   if (document.all(IDvalue).style.display == "block") { 
      document.all(IDvalue).style.display = "none"; 
      //document.cookie = IDvalue + "=0";
      //alert(IDvalue+"=0");
   } else if (document.all(IDvalue).style.display == "none") { 
      document.all(IDvalue).style.display = "block"; 
      //document.cookie = IDvalue + "=1";
      //alert(IDvalue+"=1");
   }
} 

// font setting code // =================================================================

function SetFontFromCookie() {
    ufd = ((userfont - curFontSize)/2);
    if (userfont >= 11) {
        for (i=0;i<ufd;i++) {
            fontSize(1);
        }    
    } else {
        fontSize(0);
    }    
}

function fontSize(act) {
   if (canGetElem) {
      tmpContent = document.getElementById("ContentMain");
      if (act == 1) {
         curFontSize += fontModifier;
         curFontSize  = Math.min(curFontSize, 41);
      }
      else if (act == 0) {
         curFontSize -= fontModifier;
         curFontSize  = Math.max(curFontSize, 6);
      }

      curLineHeight = Math.round(curFontSize * 1.5);
      
      tmpContent.style.fontSize = curFontSize + "px";
      
//document.getElementById("ContentMain").style.fontsize = curFontSize + "px";
      tmpContent.style.lineHeight = curLineHeight + "px";
      for (v = 0; v < tmpContent.getElementsByTagName("br").length; v++) {
         tmpContent.getElementsByTagName("br")[v].style.fontSize   = curFontSize   + "px";
         tmpContent.getElementsByTagName("br")[v].style.lineHeight = curLineHeight + "px";
      }
   }
//document.getElementById("abc").innerHTML=curFontSize; // for debugging
//document.getElementById("def").innerHTML=curLineHeight; // for debugging

    // set cookie with font size
    var expdate = new Date();
    FixCookieDate (expdate);
    expdate.setTime (expdate.getTime() + (672*60*60*1000)); // 4 weeks
    SetCookie("userfontc",curFontSize,expdate);        
}

// cookies code // ========================================================================

function getCookieVal(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function FixCookieDate(date) {
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0) 
        date.setTime (date.getTime() - skew);
}

function GetCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i=0;
    while (i < clen) {    
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
        return getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
        }
    return null;
}

function SetCookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

// bookmark the page
// urlAddress = "http://www.forsythes.com.au/fa.asp";
// pageName = "Forsythes Forensic Accounting Home Page";

function addToFavorites(urlAddress,pageName) {
   if (window.external) {
      window.external.AddFavorite(urlAddress,pageName)
   } else { 
      alert("Sorry! Your browser doesn't support this function.");
   }
}

function ShowPage(sFileName,sDimensions, sNote) {
   var pop_win = window.open(sFileName, "win2", sDimensions);
   pop_win.focus();
}

function ShowPic(sFileName,sDimensions, sNote) {
   var pop_win = window.open("", "win2", sDimensions);
   pop_win.document.open("text/html", "replace");
   pop_win.document.write("<HTML><HEAD><title>" + sFileName + "</title></HEAD>");
   pop_win.document.write("<body leftmargin=0 topmargin=0 rightmargin=0 bottommargin=0 marginwidth=0 marginheight=0>");
   pop_win.document.write("<a href='javascript:self.close();'><img src='" + sFileName + "' alt='Click to close' border=0></a>");
   pop_win.document.write("<br><center><font face=verdana size=1>" + sNote + "</font></center>");
   pop_win.document.write("</body></HTML>");
   pop_win.focus();
}

function logon_win(gid) { // used on accounting lhs column
    window2=window.open("http://www.praemium.biz/pr_redirector/redirect.aspx?gid="+gid, gid+"_main" , "fullsceen=yes, toolbar=no, menubar=no, scrollbars=yes, status=yes, resizable=yes");window2.focus(); 
}
// eof : jsfunctions.js
//-----------------------