﻿
//Vars

var iFrameObj;
var companyID;
var eCheckCode;
var arrIncludedFiles = new Array();

//end Vars


/*function InitialiseControls(regCode, CheckCode)
{
  if(!iFrameObj) iFrameObj = CreateFrameComponentHidden('CSCoreFrame');
  
  if(iFrameObj == null) return false;
  
  var iFrameDoc = GetFrameDocument(iFrameObj);
  
  if(iFrameDoc == null) return false;
  
  iFrameDoc.location.replace(aURL);
  
  return true;
}*/
function InitialiseControls(aCompanyID, aECheckCode)
{
    if(!companyID) companyID = aCompanyID;
    if(!eCheckCode) eCheckCode = aECheckCode;
}

function IncludeOnce(filename) 
{
    if (!InArray(filename, arrIncludedFiles)) {
        arrIncludedFiles[arrIncludedFiles.length] = filename;
        IncludeFile(filename);
    }
}

function InArray(findValue, checkArray) {
    for (var i = 0; i < checkArray.length; i++) 
    {
        if (checkArray[i] == findValue) {
            return true;
        }
    }
    return false;
}

function IncludeFile(filename) 
{
    var htmlObj;
    //Try and load the header object first failing that the body which should exist at least
    var htmlObjs = document.getElementsByTagName('head');
    
    //Get the first HEAD DOM object if none exists then just use the document body directly
    if (htmlObjs.length == 0) htmlObj = document.body;
    else htmlObj = htmlObjs.item(0);
    
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', filename);
    htmlObj.appendChild(js);
    
    return false;
}

function ValidateEmailAddress(email)
{
  if(!email) return false;

  var re = new RegExp("[A-Za-z0-9_\.-]+([A-Za-z0-9_-]*)?@([A-Za-z0-9_-]+\.)+[A-Za-z0-9_-]+");
  
  return email.match(re);
}

function GetFrameDocument(aFrameObj)
{
  if(aFrameObj == null) return null;
  
  var iFrameDoc;
  
  if (aFrameObj.contentDocument) iFrameDoc = aFrameObj.contentDocument; // For NS6
  else if (aFrameObj.contentWindow) iFrameDoc = aFrameObj.contentWindow.document; // For IE5.5 and IE6
  else if (aFrameObj.document) iFrameDoc = aFrameObj.document; // For IE5
  else return null;
  
  return iFrameDoc;
}

function BuildDefaultQueryString()
{
  return BuildQueryString(null, '');
}

function BuildQueryString(aFrameObj, formName, paramsEvent)
{
  if(!companyID || !eCheckCode)
  {
    alert('iDirres core controls have not been initialised.\nPlease call InitialiseControls before calling any methods.');
    return new String();
  }
  
  var qs = '?CompanyID=' + companyID + '&ECCode=' + encodeURIComponent(eCheckCode);
  
  if(paramsEvent)
  {
    try
    {
      var subParams = paramsEvent(aFrameObj);
      
      if(subParams != '')
      {
        qs += '&' + subParams;
      }
    }
    catch(exception)
    {
    }
  }
  
  if(formName == '') return qs;
  
  aForm = document.forms[formName];
  if(aForm)
  {
    //var qs = '';
    for (e = 0; e < aForm.elements.length; e++) 
    {
      if (aForm.elements[e].name != '') 
      {
        //Ignore the View state field incase a user has used VS to 
        //create the form instead of using standard HTML as this is not needed
        if(aForm.elements[e].name == '__VIEWSTATE') continue;
        
        qs += '&'; //qs += (qs == '') ? '?' : '&';
        qs += aForm.elements[e].name + '=' + escape(aForm.elements[e].value);
      }
    }
  }
  return qs
}

function CreateFrameComponentHidden(frameName)
{
  return CreateFrameComponent(frameName, document.body);
}

function CreateFrameComponent(frameName, parentNode)
{
    return CreateFrameComponentSize(frameName, parentNode, '0px', '0px');
}

function CreateFrameComponentSize(frameName, parentNode, aWidth, aHeight)
{
  //If the browser does not support dynamic elemets then return null
  if(!document.createElement) return null;
  
  //If no parent node assigend then add to the body
  if(!parentNode) parentNode = document.body;
  
  var aFrameObj;
  if (!aFrameObj && document.createElement) 
  {
    try 
    {
      var tempIFrame = document.createElement('iframe');
      tempIFrame.setAttribute('id', frameName);
      tempIFrame.setAttribute('name', frameName);
      tempIFrame.setAttribute('frameborder', 0);
      //tempIFrame.setAttribute('scrolling', 'no');
      tempIFrame.style.border = '0px';
      tempIFrame.style.width = aWidth;
      tempIFrame.style.height = aHeight;
      aFrameObj = parentNode.appendChild(tempIFrame);
      
      //If document.frames is supported get the reference here as some browsers don't
      //return an object from appendChild so aFrameObj might be null or unusable
      if (document.frames) aFrameObj = document.frames[frameName];
      aFrameObj = tempIFrame;
    } 
    catch(exception) 
    {
      //Some browser still don't support dynamic html so append the hidden iFrame at the end of the body
      iframeHTML = '\<iframe id="' + frameName + '" frameborder="0" style="border:0px;width:'+ aWidth + ';height:' + aheiaHeight + '"><\/iframe>';
      parentNode.innerHTML += iframeHTML;
      aFrameObj = new Object();
      aFrameObj.document = new Object();
      aFrameObj.document.location = new Object();
      aFrameObj.document.location.iframe = document.getElementById(frameName);
      //create our own function to support replace
      aFrameObj.document.location.replace = function(location) {this.iframe.src = location;};
    }
  }

  return aFrameObj
}

function SetFrameSize(aFrameObj, aWidth, aHeight)
{
  if (!aFrameObj) return;
  
  aFrameObj.style.height = aHeight;
  aFrameObj.style.width = aWidth;
}

function AutoSetFrameByName(aFrameName, autoHeight, autoWidth) 
{
    if (aFrameName == '') return;

    if (autoHeight == null) autoHeight = false;
    if (autoWidth == null) autoWidth = false;

    //If both with and height are false then nothing to change
    if (!autoWidth && !autoHeight) return;
    
    var aFrameObj = document.getElementById(aFrameName);
    if (!aFrameObj) return;
    
    var aFrameDoc = GetFrameDocument(aFrameObj);

    //Always set the width last as the vertical srcrollbar will affect the width of the
    //content if its allowed to be 100% so avoid reducing this first
    //The addition of 6 on the hight is to stop the disabled scrollbar fro being displayed
    //setting scrolling to 'no' has no affect of hiding it
    if (autoHeight) aFrameObj.style.height = aFrameDoc.body.scrollHeight + 6;
    if (autoWidth) aFrameObj.style.width = aFrameDoc.body.scrollWidth;

    //If the height of the body is less than or the same as the offset then no scrolling is required
    //if (tmpHeight <= aFrameDoc.body.offsetHeight) aFrameObj.scrolling = 'no';
}

function GetCommerceServer()
{
  var strComp = companyID.toString();
  
  if(strComp.length < 6)
  {
    strComp = '00000'.substr(0, 6 - strComp.length) + strComp;
  }
  
  return 'http://drc' + strComp + '.dirres.net';
  //return 'http://localhost:57743'; //Web Contact Resource Server
  //return 'http://localhost:57742'; //Web Commerce Server
}

function GetSupportServer()
{
  var strComp = companyID.toString();
  
  if(strComp.length < 6)
  {
    strComp = '00000'.substr(0, 6 - strComp.length) + strComp;
  }
  
  return 'http://drs' + strComp + '.dirres.net';
}

function CallServer(aFrameObj, serverURL, formName)
{
  return CallServerCallBack(aFrameObj, serverURL, formName, null, null);
}
function CallServerParams(aFrameObj, serverURL, formName, paramEvent)
{
  return CallServerCallBack(aFrameObj, serverURL, formName, paramEvent, null);
}
function CallServerCallBack(aFrameObj, serverURL, formName, paramEvent, callBack)
{
  if(aFrameObj == null) return false;
  
  var iFrameDoc = GetFrameDocument(aFrameObj);
  
  if(iFrameDoc == null) return false;
  /*if (aFrameObj.contentDocument) iFrameDoc = aFrameObj.contentDocument; // For NS6
  else if (aFrameObj.contentWindow) iFrameDoc = aFrameObj.contentWindow.document; // For IE5.5 and IE6
  else if (aFrameObj.document) iFrameDoc = aFrameObj.document; // For IE5
  else return false;*/
  
  var qs = BuildQueryString(aFrameObj, formName, paramEvent);
  if(callBack) qs += '&callback=' + callBack;
  var aURL = serverURL + qs;
  
  iFrameDoc.location.replace(aURL);
  
  return true;
}

