function MM_findObj(n, d) { //v3.0
  var p,i,x;
  if(!d) d=document; 
  if((p=n.indexOf("?"))>0 && parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
  }
  if(!(x=d[n]) && d.all) 
    x=d.all[n]; 
  for (i=0; !x&&i<d.forms.length; i++) 
    x=d.forms[i][n];
  for(i=0; !x&&d.layers&&i<d.layers.length; i++) 
    x=MM_findObj(n,d.layers[i].document); 
  return x;
}

function tmt_findObj(n){
  var x,t; 
  if((n.indexOf("?"))>0 && parent.frames.length) {
    t=n.split("?");
    x=eval("parent.frames['"+t[1]+"'].document.getElementById('"+t[0]+"')");
  } else { 
    x=document.getElementById(n);
  }
  return x;
}

function findObj(obj) {
    if(document.getElementById){
        return tmt_findObj(obj);
    } else {
        return MM_findObj(obj);
    }
}

function objStyle(obj) {
    if(document.getElementById){
        return obj.style;
    } else {
        return obj;
    }
}

function findPosX(object) {
  var curleft = 0;
  if (object.offsetParent) {
    while (object.offsetParent) {
      curleft += object.offsetLeft
      object = object.offsetParent;
    }
  } else if (object.x)
    curleft += object.x;
  return curleft;
  
}

function findPosY(object) {
  var curtop = 0;
  if (object.offsetParent) {
    while (object.offsetParent) {
      curtop += object.offsetTop
      object = object.offsetParent;
    }
  } else if (object.y)
    curtop += object.y;
  return curtop;
}

function layerDisplay(object,display) {
  if(document.getElementById){
    obj=tmt_findObj(object);
    obj.style.display=display;
  } else {
    obj=MM_findObj(object);
    obj.display=display;
  }
}

function divisionDetails(division,display) {
  divisions = new Array("Office","Technical","Medical","Industrial");
  for (i=0;i<divisions.length;i++) {
    layerDisplay('divDetail'+divisions[i],(divisions[i]==division?display:'none'));
  }
}

function showDiv(div) {
  layerDisplay(div,'block');
}

function hideDiv(div) {
  layerDisplay(div,'none');
}

// Currency Validation 
function currencyVal(f) {
  var currencyFilter=/^\d*(\.\d+)?$/;
  if (!(currencyFilter.test(f.value))) {
    alert('Invalid Entry');
    f.focus();
    return false;
  }
  f.value=formatCurrency(f.value);
}
function formatCurrency(num) {
  num -= 0;
  if (num == 0) return "";
  num = (Math.round(num*100))/100;
  return (num == Math.floor(num)) ? num + '.00' 
              : ( (num*10 == Math.floor(num*10)) ? 
                       num + '0' : num);
}
function currencyOnlyPre(f,e,d) {
  var k;
  var kc;
  if (window.event)
     k = window.event.keyCode;
  else if (e)
     k = e.which;
  else
     return true;
  kc = String.fromCharCode(k);
  if ((k==null) || (k==0) || (k==8) ||
      (k==9) || (k==13) || (k==27) )
     return true;
  if ((("0123456789").indexOf(kc) > -1))
     return true;
  if (kc == ".") {
    if (f.value.indexOf(".") > -1) {
      return false;
    } else {
      return true;
    }
  }
  return false;
}
function currencyOnlyPost(f) {
  var clean = f.value.replace(/[^\d\.]/g,"");
  if (clean != f.value)
    f.value = clean;
}
// End Currency Validation

var uAgent = navigator.userAgent.toLowerCase();
var ua;

if (uAgent.indexOf("windows") > -1)
  os = "Win";
else
  os = "Mac";
  
if (document.layers)
  ua = "NN4";
else if (uAgent.indexOf("opera") > -1)
  ua = "Opera";
else if (uAgent.indexOf("msie") > -1)
  ua = "IE";
else if (uAgent.indexOf("safari") > -1)
  ua = "Safari";
else if (uAgent.indexOf("gecko") > -1)
  ua = "Gecko";

function embedSWF(content) {
  document.write(content);
}

var videoWindow = null;
function popUpVideo(file,width,height) {
  if (videoWindow && !videoWindow.closed) {
    videoWindow.close();
  }
  videoWindow = window.open("/staffing/video/popup-player.php?file="+file+"&width="+width+"&height="+height,'videoWindow','width='+width+',height='+(height+16)+',scrollbar=no,toolbar=no,status=no,location=no');
}

function qtVideo(file,width,height) {
  document.write(generateVideoEmbedHTML("qt",file,width,height));
}

function retroTV(file,length) {
  var hasFlash = DetectFlashVer(7, 0, 0);
  if (!hasFlash) {
    if (confirm("Flash Player 7 or higher is required to play this video.\n\nPress ok to automatically be taken to the plug-in download page to upgrade your player.")) {
      document.location='http://www.macromedia.com/go/getflashplayer';
    }
    return false;
  }
  var div = document.getElementById("tvVideoPlaceholder");
  if (!this.playing) {
    this.oldHTML = div.innerHTML;
  }
  div.innerHTML = generateVideoEmbedHTML("flv",file,331,310,length,"retro");
  videoDone = function () { 
    var div = document.getElementById("tvVideoPlaceholder");
    div.innerHTML = oldHTML;
    playing = false;
  };
  this.playing = true;
}


function generateVideoEmbedHTML(type,file,width,height,length,skin) {
  if (type == "flv") {
    return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+width+'" height="'+height+'" id="player" align="middle">' +
    '<param name="allowScriptAccess" value="sameDomain" />' +
    '<param name="movie" value="/staffing/swf/'+skin+'Video.swf?skin='+skin+'&file=/staffing/video/'+file+'.flv&length='+length+'" />' +
    '<param name="quality" value="high" />' +
    '<param name="bgcolor" value="#ffffff" />' +
    '<param name="wmode" value="transparent" />' +
    '<embed wmode="transparent" src="/staffing/swf/'+skin+'Video.swf?skin='+skin+'&file=/staffing/video/'+file+'.flv&length='+length+'" quality="high" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="player" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
    '</object>';
  }
  if (type == "qt") {
    height += 16;
    return '<object width="'+width+'" height="'+height+'"' +
    'classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"' +
    'codebase="https://www.apple.com/qtactivex/qtplugin.cab">' +
    '<param name="src" value="/staffing/video/'+file+'.mov">' +
    '<param name="autoplay" value="true">' +
    '<param name="controller" value="true"><embed src="/staffing/video/'+file+'.mov" width="'+width+'" height="'+height+'"' +
    'autoplay="true" controller="true"' +
    'pluginspage="https://www.apple.com/quicktime/download/">' +
    '</embed></object>';
  }
}



// audio player functions begin

var audioCurrentlyPlaying;

if (document.images) {
	playImage = new Image();
	playImage.src = "/staffing/images/play-over.gif";
	stopImage = new Image();
	stopImage.src = "/staffing/images/stop-over.gif";
}
	
function simpleAudioPlayerStopped() {
  toggleAudio(audioCurrentlyPlaying);
}

function toggleAudio(id,file) {
  var rand = Math.random();
  var div = document.getElementById("audioDiv");
  var playStatus = document.getElementById(id+"PlayStatus");
  if (audioCurrentlyPlaying == id) {
    // stop
    div.innerHTML = "";
    if (element = document.getElementById(audioCurrentlyPlaying+"PlayStatus")) {
      element.src = element.src.replace("stop","play");
    }
    audioCurrentlyPlaying = "";
  } else {
    if (audioCurrentlyPlaying) {
      if (element = document.getElementById(audioCurrentlyPlaying+"PlayStatus")) {
        element.src = element.src.replace("stop","play");
      }
    }
    // play
    div.innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="https://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="1" height="1" id="HiddenSimplePlayer">' +
    '<param name="wmode" value="transparent" />' +
    '<param name="allowScriptAccess" value="sameDomain" />' +
    '<param name="movie" value="/swf/HiddenSimplePlayer.swf?file=/audio/'+file+'.mp3&rand='+rand+'" />' +
    '<param name="quality" value="high" />' +
    '<param name="bgcolor" value="#ffffff" />' +
    '<embed src="/swf/HiddenSimplePlayer.swf?file=/audio/'+file+'.mp3&rand='+rand+'" wmode="transparent" quality="high" bgcolor="#ffffff" width="1" height="1" name="HiddenSimplePlayer" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="https://www.macromedia.com/go/getflashplayer" />' +
    '</object>';
    audioCurrentlyPlaying = id;
    if (element = document.getElementById(audioCurrentlyPlaying+"PlayStatus")) {
      element.src = element.src.replace("play","stop");
    }
  }
}

function hoverAudio(id) {
  if (element = document.getElementById(id+"PlayStatus")) {
    element.src = element.src.indexOf("-over.") > 0 ? element.src = element.src.replace("-over.gif",".gif") : element.src = element.src.replace(".gif","-over.gif");
  }
}

// audio player functions end

function popupMap(title, address, city, state, zip, phone, note, lat, lon) {
  window.open("/driving-directions/map.php?title="+escape(title)+"&address="+escape(address)+"&city="+escape(city)+"&state="+escape(state)+"&zip="+escape(zip)+"&phone="+escape(phone)+"&note="+escape(note)+(typeof lat != "undefined" && typeof lon != "undefined" ? "&lat="+lat+"&lon="+lon : ""), "mapWin", "scrollbars=yes,toolbar=no,location=no,directories=no,status=no,width=640,height=600");
}

function writeEmail(host,username,text,extra) {
  document.write("<a href=\"mailto:"+username+"@"+host+"\" "+extra+">");
  if (text) {
    document.write(text);
  } else {
    document.write(username+"@"+host);
  }
  document.write("</a>");
}

function validateEmail(email) {
  var emailFilter = /^[\w\.\-\+]+@[\w\.\-\+]+\.\w{2,6}$/;
  return emailFilter.test(email);
}

function restrictCharacters(e, restriction) {
  var restrictions = {
  'digits':/[1234567890]/g,
  'decimal':/[0-9\.]/g,
  'alpha':/[A-Z]/g,
  'phone':/[\(\)0-9\- ]/g,
  'email':/[^\s\<\>"']/g
  };
  if (!e) var e = window.event;
  if (e.keyCode) code = e.keyCode;
  else if (e.which) code = e.which;
  var character = String.fromCharCode(code);
  if (code==27) { this.blur(); return false; }
  if (code==13) { return true; }
  if (!e.ctrlKey && code!=9 && code!=8 && code!=36 && code!=37 && code!=38 && (code!=39 || (code==39 && character=="'")) && code!=40) {
    if (character.match(restrictions[restriction])) {
      return true;
    } else {
      return false;
    }
  }
}

function formatPhoneNumber(field) {
  var phoneNumber = field.value;
  phoneNumber = phoneNumber.replace(/[^\d]/g,'');
  if (phoneNumber.indexOf('1') == 0) phoneNumber = phoneNumber.substr(1,phoneNumber.length);
  if (phoneNumber.length == 7) phoneNumber = '808'+phoneNumber;
  phoneNumber = phoneNumber.replace(new RegExp ('^(\\d{3})(\\d{3})(\\d{4})$'),'($1) $2-$3',phoneNumber);
  field.value=phoneNumber;
}

altres = function() {};

altres.ajax = function() {};

altres.ajax.HTTPClient = function () {
  this.http = null;
  this.asynchronous = true;
  this.callback = function() { return null; };
  /**
   * processes an ajax call and handles the response
   * @param {String} type Text or XML
   * @param {String} method POST, GET, or POSTRAW
   * @param {String} url
   * @param {String} data POST, GET or POSTRAW data
   * @param {Function} [postFunction] callback function to post response to
   * @param {Object} [carryObject] object to pass back to the callback function the second paramter
   */
  this.makeRequest = function(type,method,url,data,postFunction,carryObject) {
    var url;
    var data;
    this.type = type;
    this.postFunction = postFunction;
    this.carryObject = carryObject;
    if (window.XMLHttpRequest) {
      this.http = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      this.http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var self = this;
    this.http.onreadystatechange = function() {
      if (self.http.readyState == 4) {
        var status = "";
        var statusText = "";
        var success = 0;
        // necessary to catch unexplainable error in Firefox
        try {
          status = self.http.status;
          statusText = self.http.statusText;
          success = 1;
        } catch (error) { }
        if (success) {
          if (status == 200) {
            if (self.postFunction) {
              if (self.type == "Text") {
                eval('self.postFunction(self.http.responseText'+(self.carryObject?',self.carryObject':'')+')');
              } else if (self.type == "XML") {
                eval('self.postFunction(self.http.responseXML.documentElement'+(self.carryObject?',self.carryObject':'')+')');
              }
            }
          } else {
            alert("There was a problem retrieving the data:\\n" + statusText);
          }
        }
      }
      self.callback(self);
    }
    this.http.open(method=="POSTRAW"?"POST":method, url, (this.asynchronous?true:false));
    if (method == "POST") {
      this.http.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');
    }
    this.http.send(data);
  };
};

altres.util = function() {};

altres.util.escapeForURI = function(string) {
  return encodeURIComponent(string);
};

