var myurl = location.href;
var mySub1 = "/";
var mySub2 = ".";
var filename = leftFrom(rightFrom(myurl, mySub1), mySub2);
if (filename == "") filename = "index";

function getFilename() {
  return filename;
}

function LoadScript(url) {
  document.write('<script src="', url, '" type="text/JavaScript"></script>');
}

function calcHeight()
{
  //find the height of the internal page
  var the_height = document.getElementById('content').contentWindow.document.body.scrollHeight;

  //change the height of the iframe
  document.getElementById('content').height = the_height;
}

function validate_form (form) {
  var fld = document.myForm.Name;
  if (trim(fld.value) == "") {
    fld.style.background = 'Yellow';
    fld.focus();
    alert ( "Please anter your fullname" );
    return false;
  }
  fld.style.background = 'White';

  fld = document.myForm.Phone;
  if (trim(fld.value) == "") {
    fld.style.background = 'Yellow';
    fld.focus();
    alert ( "Please enter a valid phone number starting with area code" );
    return false;
  }
  fld.style.background = 'White';

  fld = document.myForm.Email;
  if ( !validateEmail (document.myForm.Email)) {
    fld.style.background = 'Yellow';
    fld.focus();
    alert ( "Please enter a valid email address" );
    return false;
  }
  fld.style.background = 'White';

  if (form == "orderForm") {
    fld = document.myForm.Stuck;
    if (trim(fld.value) == "") {
      fld.style.background = 'Yellow';
      fld.focus();
      alert ( "Please specify the stuck number" );
      return false;
    }
    fld.style.background = 'White';
  }
  return true;
}

//Returns everything right of the last instance of the subString to the end of the fullString
function rightFrom(fullString, subString) {
  if (fullString.lastIndexOf(subString) == -1) {
    return "";
  } else {
    return fullString.substring(fullString.lastIndexOf(subString)+1, fullString.length);
  }
}

//Returns everything left of the last instance of the subString to the start of the fullString
function leftFrom(fullString, subString) {
  if (fullString.lastIndexOf(subString) == -1) {
    return "";
  } else {
    return fullString.substring(0, fullString.lastIndexOf(subString));
  }
}

/* --------------------------------------------

function preload() {
  var brochure = new Array();
  var pics = preload.arguments;
  var dir = '../images/Smog_' // path to images
  for (i=0;i<pics.length;i++) {
    brochure[i] = new Image();
    brochure[i].src = dir + pics[i] + '.gif'; // change file extension, if necessary.
    // uncomment next line for testing; click OK to continue loop and cancel to break
    // if(!confirm(brochure[i].src)) break;
  }
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(function() {
 	preload('03','04','05','06','07','08','09','10',
 	        '11','12','13','14','15','16','17','18','19','20',
 	        '21','22','23','24','25','26','27','28','29','30',
 	        '31','32','33','34','35','36','37','38','39','40',
 	        '41','42','43','44','45','46','47','48','49','50',
 	        '51','52','53','54','55');
});
------------------------------------------------ */


function blockKeys(event){
  if (event.keyCode < 48 || event.keyCode > 57) 
    return false;
  return true;
}

function formatPhone(textfield) {
  val = textfield.value;
  if(val.length == 3) {
    var newVal = val;
    if(val.indexOf("(") == -1)
      newVal = "(" + newVal;
    textfield.value = newVal;
  }
  val = textfield.value;
  if(val.length == 4) {
    var newVal = val;
    if(val.indexOf(")") == -1)
      newVal = newVal + ")";
    textfield.value = newVal;
  }
  val = textfield.value;
  if(val.length == 8)
    textfield.value = val + "-";
}
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        return false;
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        return false;
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        return false;
    } else {
        fld.style.background = 'White';
    }
    return true;
}

function MM_preloadImages() { //v3.0
  var d=document;
  if(d.images){
    if(!d.MM_p) d.MM_p=new Array();
      var i,j=d.MM_p.length,a=MM_preloadImages.arguments;
      for(i=0; i<a.length; i++)
        if (a[i].indexOf("#")!=0){
          d.MM_p[j]=new Image;
          d.MM_p[j++].src=a[i];
        }
  }
}
/*
function getWebUrl() {
  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async = "false";
  xmlDoc.load("../config/web.xml");

// Menu
  var webName = xmlDoc.getElementsByTagName('webName');
  var siteName = webName[0].text;

  if (siteName == "pacific") {
      var WebUrls = xmlDoc.getElementsByTagName('menu');
 
      var oneMenuUrl = WebUrls[0].firstChild.nodeValue;
      document.getElementById("oneMenuUrl").href = oneMenuUrl;

      var twoMenuUrl = WebUrls[1].firstChild.nodeValue;
      document.getElementById("twoMenuUrl").href = twoMenuUrl;

      var threeMenuUrl = WebUrls[2].firstChild.nodeValue;
      document.getElementById("threeMenuUrl").href = threeMenuUrl;

      var fourMenuUrl = WebUrls[3].firstChild.nodeValue;
      document.getElementById("fourMenuUrl").href = fourMenuUrl;

      var fiveMenuUrl = WebUrls[4].firstChild.nodeValue;
      document.getElementById("fiveMenuUrl").href = fiveMenuUrl;

      var sixMenuUrl = WebUrls[5].firstChild.nodeValue;
      document.getElementById("sixMenuUrl").href = sixMenuUrl;

      var sevenMenuUrl = WebUrls[6].firstChild.nodeValue;
      document.getElementById("sevenMenuUrl").href = sevenMenuUrl;
  }
  else
    if (siteName == "midland") {
      var WebUrls = xmlDoc.getElementsByTagName('menu');
 
      var oneMenuUrl = WebUrls[0].firstChild.nodeValue;
      document.getElementById("oneMenuUrl").href = oneMenuUrl;
 
      var twoMenuUrl = WebUrls[1].firstChild.nodeValue;
      document.getElementById("twoMenuUrl").href = twoMenuUrl;
  
      var threeMenuUrl = WebUrls[2].firstChild.nodeValue;
      document.getElementById("threeMenuUrl").href = threeMenuUrl;

      var fourMenuUrl = WebUrls[3].firstChild.nodeValue;
      document.getElementById("fourMenuUrl").href = fourMenuUrl;
     // Submenu 1
      var WebUrls = xmlDoc.getElementsByTagName('submenu1');

      var oneSubmenu1Url = WebUrls[0].firstChild.nodeValue;
      document.getElementById("oneSubmenu1Url").href = oneSubmenu1Url;

      var twoSubmenu1Url = WebUrls[1].firstChild.nodeValue;
      document.getElementById("twoSubmenu1Url").href = twoSubmenu1Url;
  
      var threeSubmenu1Url = WebUrls[2].firstChild.nodeValue;
      document.getElementById("threeSubmenu1Url").href = threeSubmenu1Url;

      var fourSubmenu1Url = WebUrls[3].firstChild.nodeValue;
      document.getElementById("fourSubmenu1Url").href = fourSubmenu1Url;

      var fiveSubmenu1Url = WebUrls[4].firstChild.nodeValue;
      document.getElementById("fiveSubmenu1Url").href = fiveSubmenu1Url;

     // Submenu 2
      var WebUrls = xmlDoc.getElementsByTagName('submenu2');

      var oneSubmenu2Url = WebUrls[0].firstChild.nodeValue;
      document.getElementById("oneSubmenu2Url").href = oneSubmenu2Url;

      var twoSubmenu2Url = WebUrls[1].firstChild.nodeValue;
      document.getElementById("twoSubmenu2Url").href = twoSubmenu2Url;
   
      var threeSubmenu2Url = WebUrls[2].firstChild.nodeValue;
      document.getElementById("threeSubmenu2Url").href = threeSubmenu2Url;

      var fourSubmenu2Url = WebUrls[3].firstChild.nodeValue;
      document.getElementById("fourSubmenu2Url").href = fourSubmenu2Url;
    }
    else
      alert("Config file is damaged");
}
*/