var scrollSpeed = 1; // number of pixels to change every frame
var scrollHeight = 0; // this will hold the height of your content
var scrollDelay = 60; // delay between movements.
var scrollInitialDelay = 1000; // Initial scroll delay
var scrollMove = scrollSpeed; // for stop & start of scroll

window.onload = initPage;

function initPage() {
  external_links();
  /*if(document.getElementById('map_div')) {
    setTimeout("switch_contact(999)", 1000);
    setTimeout("initMap()", 1100);
    //initMap();    
  }*/
  if(document.getElementById('qcontact_name')) {
    document.getElementById('qcontact_name').onfocus = clearfield;
    document.getElementById('qcontact_name').onblur = reinstatefield;
  }
  if(document.getElementById('qcontact_telephone')) {
    document.getElementById('qcontact_telephone').onfocus = clearfield;
    document.getElementById('qcontact_telephone').onblur = reinstatefield;
  }
  if(document.getElementById('qcontact_email')) {
    document.getElementById('qcontact_email').onfocus = clearfield;
    document.getElementById('qcontact_email').onblur = reinstatefield;
  }
  if(document.getElementById('qcontact_company')) {
    document.getElementById('qcontact_company').onfocus = clearfield;
    document.getElementById('qcontact_company').onblur = reinstatefield;
  }
  if(document.getElementById('qcontact_enquiry')) {
    document.getElementById('qcontact_enquiry').onfocus = clearfield;
    document.getElementById('qcontact_enquiry').onblur = reinstatefield;
  }
  if(document.getElementById('qc-form-form')) {
    document.getElementById('qc-form-form').style.display = 'none';
    document.getElementById('qc-form-message').style.display = 'block';
    document.getElementById('qc-form-header').style.cursor = 'pointer';
    document.getElementById('qc-form-header').onclick = openCloseQC;
  }
  if(document.getElementById('scrollTxt') && document.getElementById('scrollBox')) {
    getHeight();
    document.getElementById('scrollTxt2').style.top = (scrollHeight) + 'px';
    
    setTimeout('scrollBox();', scrollInitialDelay);
    document.getElementById('scrollBox').onmouseover = scrollStop;
    document.getElementById('scrollBox').onmouseout = scrollResume;
  }
}

function clearfield() {
  switch(this.id) {
    case 'qcontact_enquiry':
      trimValue = trim(this.value);
      if(trimValue == 'Enquiry') {
        this.value = '';
      }
    break;
    case 'qcontact_name':
      trimValue = trim(this.value);
      if(trimValue == 'Name') {
        this.value = '';
      }
    break;
    case 'qcontact_telephone':
      trimValue = trim(this.value);
      if(trimValue == 'Tel') {
        this.value = '';
      }
    break;
    case 'qcontact_company':
      trimValue = trim(this.value);
      if(trimValue == 'Company') {
        this.value = '';
      }
    break;
    case 'qcontact_email':
      trimValue = trim(this.value);
      if(trimValue == 'Email') {
        this.value = '';
      }
    break;
  }
}

function reinstatefield() {
  switch(this.id) {
    case 'qcontact_enquiry':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Enquiry';
      }
    break;
    case 'qcontact_name':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Name';
      }
    break;
    case 'qcontact_telephone':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Tel';
      }
    break;
    case 'qcontact_company':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Company';
      }
    break;
    case 'qcontact_email':
      trimValue = trim(this.value);
      if(trimValue == '') {
        this.value = 'Email';
      }
    break;
  }
}

function trim(str, chars) {
  return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
  chars = chars || "\\s";
  return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function scrollBox() {
  var currentPosition = document.getElementById('scrollTxt').style.top.substring(0, document.getElementById('scrollTxt').style.top.length - 2)
  var currentPosition2 = document.getElementById('scrollTxt2').style.top.substring(0, document.getElementById('scrollTxt2').style.top.length - 2)
  if(currentPosition == "") {
    currentPosition = 0;
  }
  if(currentPosition2 == "") {
    currentPosition2 = 0;
  }
  if(currentPosition < (0 - scrollHeight)) {
    currentPosition = scrollHeight;
  }
  if(currentPosition2 < (0 - scrollHeight)) {
    currentPosition2 = scrollHeight;
  }
  document.getElementById('scrollTxt').style.top = (currentPosition - scrollMove) + 'px';
  document.getElementById('scrollTxt2').style.top = (currentPosition2 - scrollMove) + 'px';
  
  setTimeout('scrollBox();', scrollDelay);
}

function getHeight() {
  scrollHeight = document.getElementById('scrollTxt').offsetHeight;
}

function scrollResume() {
  scrollMove = scrollSpeed;
}

function scrollStop() {
  scrollMove = 0;
}

function insert_flash_object(objID, flashLocation, width, height, flashString) {
  var flashObject;
  flashObject = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='" + width + "' height='" + height + "'>";
  flashObject = flashObject + "<param name='wmode' value='transparent' />";
  flashObject = flashObject + "<param name='movie' value='" + flashLocation + "' />";
  flashObject = flashObject + "<param name='quality' value='high' />";
  if(typeof(flashString) == "undefined") {
    flashObject = flashObject + "<embed src='" + flashLocation + "' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='" + width + "' height='" + height + "' wmode='transparent'>";
  } else {
    flashObject = flashObject + "<param name='FlashVars' value='" + flashString + "' />";
    flashObject = flashObject + "<embed src='" + flashLocation + "' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='" + width + "' height='" + height + "' wmode='transparent' ";
    flashObject = flashObject + "FlashVars='" + flashString + "'>";
  }
  flashObject = flashObject + "</embed></object>";
  if(document.getElementById(objID)) {
    document.getElementById(objID).innerHTML = flashObject;
  }
}
function external_links() {
  if(!document.getElementsByTagName) {
    return;
  } else {
    var anchors = document.getElementsByTagName("a"); 
    for(var i=0; i<anchors.length; i++) {
      var anchor = anchors[i]; 
      if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external" || anchor.getAttribute("rel") == "external nofollow") {
        anchor.target = "_blank";
      }
    }
  } 
}
function confirmFileDelete(fileId) { 
  if(confirm("Are you sure you want to delete this file?")) {
    window.location = 'action.php?view=my-files&action=delete&fileid=' + fileId;
    return true; 
  } else { 
    return false; 
  } 
}

function regenerateCaptcha(imageID, viewName) {
  xmlhttp.open("GET", "logicajax/generateCaptcha.php?imageid=" + imageID + "&viewname=" + viewName);
  xmlhttp.onreadystatechange = function() {
    if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
      document.getElementById(imageID).src = xmlhttp.responseText;
    }
  }  
  xmlhttp.send(null);
}


function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=878,height=388,left = 201,top = 318');");
}

function openCloseQC(){
  form = document.getElementById('qc-form-form');
  message = document.getElementById('qc-form-message');
  if(form.style.display == 'none'){
    form.style.display = 'block';
    message.style.display = 'none';
  }else if(form.style.display == 'block'){
    form.style.display = 'none';
    message.style.display = 'block';
  }else{
    form.style.display = 'block';
  }
}

