function imagePreloader() {
  // function to preload images 
  var i = 0;
  var imageObj = new Image();
  var images = new Array();
  images[0]="images/bg_nav.jpg";
  images[1]="images/bg_nav_highlight.jpg";
  images[2]="images/navbar_icons/Icon-AboutUs.jpg";
  images[3]="images/navbar_icons/Icon-Clients.jpg";
  images[4]="images/navbar_icons/Icon-Contact.jpg";
  images[5]="images/navbar_icons/Icon-Home.jpg";
  images[6]="images/navbar_icons/Icon-News.jpg";
  images[7]="images/navbar_icons/Icon-RequestQuote.jpg";
  images[8]="images/navbar_icons/Icon-Services.jpg";
  images[9]="images/exclamation1.png";
  images[10]="images/tick1.png";
  for(i=0; i<=10; i++) {
    imageObj.src=images[i];
  }
} 

function goToPage(page) {
  // function to navigate to a given link which is different from the currently loaded page 
  if (currentPage != page.toUpperCase()) {
    window.location.href = page;
  }  
}

function loginFormSubmit() {
  // Function to submit login form if validation succesful
  if (document.getElementById("username").value == '') {
    focusControl = 'username';
    showMessageBox(':: Authentication Error ::', 'Please provide a Login ID', 'username', 'images/exclamation1.gif');
  } 
  else if (document.getElementById("password").value == '') {    
    focusControl = 'password';
    showMessageBox(':: Authentication Error ::', 'Please provide a password', 'password', 'images/exclamation1.gif');
  }
  else {

    var currentPage1 = window.location.href.toUpperCase();
    if (currentPage1.match(/^.*\.asp$/i)) {		
			currentPage1 = currentPage1.substring(currentPage1.lastIndexOf('/') + 1);			
		}
		else {
			//default to index.asp
			currentPage1 = 'INDEX.ASP';
		}
				
										
    document.getElementById('currentURL').value = currentPage1;  
    document.getElementById('frmLogin').submit();
  }
}

function showMessageBox(msgTitle, msg, msgFocus, msgIcon) {
  document.getElementById("messageBoxTitle").innerHTML = msgTitle;
  document.getElementById("Message").innerHTML = msg;  
  document.getElementById("messageBox").style.position = "absolute";
  document.getElementById("messageBox").style.visibility = "visible";
  document.getElementById("messageBox").style.textAlign = "center";
  document.getElementById("messageBox").style.left = "50%";
  document.getElementById("messageBox").style.top = "50%";
  document.getElementById("messageBox").style.width = "400px";
  document.getElementById("messageBox").style.height = "150px";
  document.getElementById("messageBox").style.margin = "-75px 0 0 -200px";
  document.getElementById("messageBox").style.zIndex=30;
  //document.getElementById("click4_it").style.visibility = "hidden";
  showBackPageCover(1);
  document.getElementById("mBoxIcon").style.background ='url(' + msgIcon + ') no-repeat';  
  focusControl = msgFocus;
}

function showBackPageCover(status) {
  var bgCover;
  bgCover = document.getElementById("backgroundPage");
  if (status == 1) {    
    bgCover.style.zIndex = 25;
    bgCover.style.top = '0px';
    bgCover.style.right = '0px';
    bgCover.style.width = screen.width;
    bgCover.style.height = document.getElementById("EOP").offsetTop;
    setOpacity("backgroundPage", 70);
    bgCover.style.visibility = "visible";
  }
  else {
    bgCover.style.zIndex = 20;
    bgCover.style.visibility = "hidden";
  }
}

function hideMessageBox(fControl) {
  document.getElementById("messageBox").style.visibility = "hidden";
  //document.getElementById("click4_it").style.visibility = "visible";
  showBackPageCover(0);
  if (fControl != '') {
    document.getElementById(fControl).focus(); 
  }
  if (focusControl != '') {
    document.getElementById(focusControl).focus(); 
  }
}

function tick() {
  document.getElementById("loginUnavailableBox").style.zIndex = 5;
  document.getElementById('loginTimer').innerHTML = countDown + ' seconds...';
  countDown -= 1;
  if (countDown > -1) {
    timerID = setTimeout("doTimer()",1000);
  }
  else {
    // stop timer
    stopTimer();
  }
}

function doTimer() {
  isTicking = 1;
  tick();
}

function stopTimer() {
  clearTimeout(timerID);
  isTicking = 0;
  document.getElementById("loginUnavailableBox").style.zIndex = -1;
}

function validate_email(field) {
  with (field) {
    apos=value.indexOf("@");
    dotpos=value.lastIndexOf(".");
    if (apos < 1 || dotpos-apos < 2) {      
      field.focus();
      return false;
    }
    else {
      return true;
    }
  }
}

function validate_required(field) {
  with (field) {
    if (value==null || value=="") {      
      field.focus();
      return false;
    }
    else {
      return true;
    }
  }
}

window.size = function() {
  var w = 0;
  var h = 0;
  //IE
  if(!window.innerWidth) {    
    if(!(document.documentElement.clientWidth == 0)) {
      //strict mode
      w = document.documentElement.clientWidth;
      h = document.documentElement.clientHeight;
    }    
    else {
      //quirks mode
      w = document.body.clientWidth;
      h = document.body.clientHeight;
    }
  }
  else {
    //w3c
    w = window.innerWidth;
    h = window.innerHeight;
  }
  return {width:w,height:h};
}

window.center = function() {
  var hWnd = (arguments[0] != null) ? arguments[0] : {width:0,height:0};
  var _x = 0;
  var _y = 0;
  var offsetX = 0;
  var offsetY = 0;
  //IE
  if(!window.pageYOffset) {
    //strict mode
    if(!(document.documentElement.scrollTop == 0)) {
      offsetY = document.documentElement.scrollTop;
      offsetX = document.documentElement.scrollLeft;
    }    
    else {
      //quirks mode
      offsetY = document.body.scrollTop;
      offsetX = document.body.scrollLeft;
    }
  }
  else {
    //w3c
    offsetX = window.pageXOffset;
    offsetY = window.pageYOffset;
  }
  
  _x = ((this.size().width-hWnd.width)/2)+offsetX;
  _y = ((this.size().height-hWnd.height)/2)+offsetY;
  return{x:_x,y:_y};
}

function popupImage(imgPath, w, h) {
  var point = window.center({width:w,height:h});
  var iV = document.getElementById("imageViewer");
  var fI = document.getElementById("frameImage");
  iV.style.position = "absolute";
  iV.style.top = point.y + "px";
  iV.style.left = point.x - (w / 4) + "px";
  iV.style.width = w + "px";
  iV.style.height = h + 30 + "px";
  iV.style.zIndex = 22;
  
  fI.width = w + "px";
  fI.height = (h + 2) + "px";
  
  fI.src = "pecs_movie.asp";
  
  var w = (fI.contentWindow || fI.contentDocument);
  var d;
  if (w.document) {
    d = w.document;
  }

  iV.style.display = "block";
}

function closePopup() {
  var iV = document.getElementById("imageViewer");
  var fI = document.getElementById("frameImage");
  iV.style.display = 'none';
  iV.style.width =  "10px";
  iV.style.height = "10px";

  var w = (fI.contentWindow || fI.contentDocument);
  var d;
  if (w.document) {
    d = w.document;
  }
  
  fI.src = '';
}
