﻿// *****************************************************************************
//  FORMATTING FUNCTIONS
// *****************************************************************************

    function LeftTrim(text)
    {
        return text.replace(/^\s+/,"");
    }		
// *************************************************************************	
    function RightTrim(text)
    {
        return text.replace(/\s+$/,"");		
    }
// *************************************************************************	
    function Trim(text)
    {
        return LeftTrim(RightTrim(text));		
    }	
// *************************************************************************			
    function RemoveWhiteSpace(text)
    {
    //	Remove white space characters.
        return text.replace(/[\f\n\r\t\v]+/g,"");
    }

// *****************************************************************************
//  OTHER FUNCTIONS
// *****************************************************************************
            
        function GetIEVersion()
        {
        //	--------------------------------------------------------------------        
    //	Purpose: to determine the Internet Explorer version number
        //	--------------------------------------------------------------------

        //	Declare variables
            var strUserAgent = navigator.userAgent.toLowerCase();
            var iMSIEIndex = strUserAgent.indexOf("msie");
            var iIEVersion = new Number(0);
            var bIsIE, bIsOpera;
                    
        //	Determine whether browser is Internet Explorer
            bIsOpera = (strUserAgent.indexOf("opera") != -1);
            bIsIE = (strUserAgent.indexOf("win") >= 0 && strUserAgent.indexOf("msie")
             >= 0 && !bIsOpera)

        //	Evaluate IE version.  Return zero if not IE			 
            if (bIsIE)
                iIEVersion = Number(strUserAgent.substring((iMSIEIndex + 5),(iMSIEIndex 
                 + 8)));
            else
                iIEVersion = 0;
                
        //	Return version number
            return iIEVersion;
        }
//  ****************************************************************************

        function OpenWindow(istrUrl, width, height)
        {
        //	--------------------------------------------------------------------        
        //	Purpose: to open a new URL in an undecorated window
        //	--------------------------------------------------------------------	
        
        //  Declare variables
            var objWin;
            //  reference to new window
            var strToolbars;
            //  toolbars to display
            var strOptions;
            //  other window properties
            var lngX;
            //  X position of new window in pixels
            var lngY;
            //  Y position of new window in pixels
            
        //  Define toolbars
            strToolbars = "" +
            "directories=no,location=no,menubar=no,status=no," +
            "titlebar=no,toolbar=no";
            
        //  Get Left/Top positions in order to centre window
            lngX = (screen.availWidth - width) / 2;
            lngY = (screen.availHeight - height) /2;
                
        //  Define options
            strOptions = "" +
            "scrollbars=yes,dependent=yes,left=" + lngX + ",top=" + lngY + "," +
            "width=" + width + ",height=" + height;
            
        //  Open window
            objWin = open(istrUrl, 'Demo', strToolbars + "," + strOptions);    
        }   		
//  ****************************************************************************
        
        function ShowImage()
        {
        //	--------------------------------------------------------------------        
        //	Purpose: to update the SRC attribute of an IMG called 'imgMain'
        //  Assumptions: page contains IMG element with ID: 'imgMain' and that
        //  page will be pass URL in the form: ShowImage.htm?imgurl=[image URL]
        //	--------------------------------------------------------------------

        //	Declare variables
            var iCharPos = new Number(-1);
            
            iCharPos = document.location.href.indexOf("imgurl");
            if (iCharPos >= 0)
                document.getElementById("imgMain").src = 
                document.location.href.substr(iCharPos + 7);
        }		
//  ****************************************************************************

    function $(id)
    {
        //	--------------------------------------------------------------------        
    //	Purpose: to provide a shorthand way of addressing DOM element
        //	--------------------------------------------------------------------	
            
            if (document.getElementById(id) != null)
                return document.getElementById(id);
            else
            return GetFormElemByName(id);

    }
//  ****************************************************************************    
      
      function GetFormElemByName(name)
      {
          // Get reference to element by source ID attribute. ID is 
          // obfuscated by ASP.NET when merging content with master 
          // page
        var form = document.getElementById("aspnetForm");
          for (var i = 0; i < form.elements.length; i++)
          {
              elem = form.elements[i];
              if (elem.id.indexOf(name) >= 0)
                  return elem;
          }	
      }    
//  ****************************************************************************
            
        function IsValidEnquiry()
        {
                // Declare variables
            var strValidationMsg = "";
            
            // Check that name field is not blank
            if (Trim($("tbxName").value) == "")
                strValidationMsg = "The Name field cannot be blank";

        
            // Check that one of the Contact fields has been completed
            if (strValidationMsg == "")
            {
                if (Trim($("tbxTelephone").value) == "" && Trim($("tbxEMail").value) 
                  == "")
                    strValidationMsg = "Please provide contact details by completing "
                    + "either the Telephone or E-Mail field";   
            }
            // Check that Enquiry textbox is not blank
            if (strValidationMsg == "")
            {
                if (Trim($("tbxEnquiry").value) == "")
                    strValidationMsg = "Please complete the Enquiry details field.";
            }
            
            // Return confirmation.  Display validation message if form incomplete
            if (strValidationMsg != "")
            {
                alert(strValidationMsg);
                return false;
            }
            else 
                return true;
        
            
            
        }
//  ****************************************************************************
            
        function IsValidSupportRequest()
        {
                // Declare variables
            var strValidationMsg = "";
            
            // Check that name field is not blank
            if (Trim($("tbxName").value) == "")
                strValidationMsg = "The Name field cannot be blank";

        
            // Check that E-Mail address has been completed
            if (strValidationMsg == "")
            {
                if (Trim($("tbxEMail").value)== "")
                    strValidationMsg = "Please E-Mail field cannot be blank"
            }
            // Check that Support Enquiry textbox is not blank
            if (strValidationMsg == "")
            {
                if (Trim($("tbxEnquiry").value) == "")
                    strValidationMsg = "Please complete the Support Request details field.";
            }
            // Return confirmation.  Display validation message if form incomplete
            if (strValidationMsg != "")
            {
                alert(strValidationMsg);
                return false;
            }
            else 
                return true;		
                
        }        
//  ****************************************************************************
        
        function GetMessageLength()
        {
            // Get length of elements that will form part of message
            var iNameLen = new String("Usr:" + $("tbxName").value + "\n").length;
            var iTelephoneLen = new String("Tel:" + $("tbxTelephone").value + "\n").length;
            var iSubjectLen = new String("Sub:" + $("tbxSubject").value + "\n").length;
            var iEnquiryLen = $("tbxEnquiry").value.length;
            
            // Add up characters used, and allow for SMS concatenation bytes
            var iMsgLen = iNameLen + iTelephoneLen + iSubjectLen + iEnquiryLen;
            if (iMsgLen >= 153 && iMsgLen < 314)
                iMsgLen += 7;
            else if (iMsgLen >= 314)
                iMsgLen += 14;
                
            // Return length of message
            return iMsgLen;
            
        }
        function UpdateCharCount()
        {
            $("spnAvailableChars").innerText = (480 - GetMessageLength()) + "(" + GetMessageLength() + ")";
        }