// websterrific.js

/*global window, document, screen, escape, hex_md5, setTimeout, stayTopLeft, xmlHTTP, XMLHttpRequest, getObject, getObjectNN4, getURL, buildPOST, postForm, textResize, getWindowWidth, getWindowHeight, getWindowSize, getWindowScroll, createDiv, destroyDiv, centerDiv, floatDiv, JSFX, JSFX_StartEffects, makeLoginLogoutButton, atviirLogout, atviirRegistrationFormValidation, atviirLoginFormValidation, atviirPasswordResetFormValidation */

var xmlHTTP=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlHTTP = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) {
    try {
    xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
    xmlHTTP = false;
  }
 }
@end @*/
if (!xmlHTTP && typeof XMLHttpRequest!='undefined') {
  try {
    xmlHTTP=new XMLHttpRequest();
  } catch (e) {
    xmlHTTP=false;
  }
}
if (!xmlHTTP && window.createRequest) {
  try {
    xmlHTTP=window.createRequest();
  } catch (E) {
    xmlHTTP=false;
  }
}

function buildPOST(formName) {
  var e, name;
  var qs='';
  var theForm=document.forms[formName];
  for (e=0; e<theForm.elements.length; e++) {
    if (theForm.elements[e].name!=='') {
      name=theForm.elements[e].name;
      qs+=(qs==='')?'':'&';
//      qs+=name+'='+escape(theForm.elements[e].value);
      qs+=name+'='+theForm.elements[e].value;
    }
  }
  return qs;
}

function centerDiv(divId) {
  var px=document.layers ? '' : 'px';
  var divObject=new getObject(divId);
  var windowSize=getWindowSize();
  var windowScroll=getWindowScroll();
  var divLeft=Math.round(windowSize[0]/2)-Math.round(parseInt(divObject.style.width,10)/2)-15;
  var divTop=Math.round(windowSize[1]/2)-Math.round(parseInt(divObject.style.height,10)/2);
  divObject.style.left=divLeft+px;
  divObject.style.top=divTop+px;
  return[divLeft,divTop];
}

function contactValidation() {
var element, errorMessage;
  if (document.contactForm.contactName.value=='') {
    createDiv('errorDiv','300px','150px','#c0c0c0','#e0e0e0','#006633');
    errorMessage="<center><br /><h4 style='color: #000000;'>Please enter your name.<br /><br /><input type='button' value='OK' class='functionButton' onmouseover=\"this.className='functionButtonHover'\" onmouseout=\"this.className='functionButton'\" onclick=\"destroyDiv('errorDiv');\" /></h4></center>";
    element=document.getElementById('errorDiv');
    element.innerHTML=errorMessage;
    floatDiv('errorDiv');
    return;
  }
  if (document.contactForm.contactEmail.value=='') {
    createDiv('errorDiv','300px','150px','#c0c0c0','#e0e0e0','#006633');
    errorMessage="<center><br /><h4 style='color: #000000;'>Please enter your email address.<br /><br /><input type='button' value='OK' class='functionButton' onmouseover=\"this.className='functionButtonHover'\" onmouseout=\"this.className='functionButton'\" onclick=\"destroyDiv('errorDiv');\" /></h4></center>";
    element=document.getElementById('errorDiv');
    element.innerHTML=errorMessage;
    floatDiv('errorDiv');
    return;
  }
  if (document.contactForm.contactMessage.value=='') {
    createDiv('errorDiv','300px','150px','#c0c0c0','#e0e0e0','#006633');
    errorMessage="<center><br /><h4 style='color: #000000;'>Please enter a message.<br /><br /><input type='button' value='OK' class='functionButton' onmouseover=\"this.className='functionButtonHover'\" onmouseout=\"this.className='functionButton'\" onclick=\"destroyDiv('errorDiv');\" /></h4></center>";
    element=document.getElementById('errorDiv');
    element.innerHTML=errorMessage;
    floatDiv('errorDiv');
    return;
  }
  document.contactForm.submit();
}

function createDiv(id, width, height, backgroundColor, borderColor, textColor, html) {
  var newdiv=document.createElement('div');
  if (document.getElementById(id)) { destroyDiv(id); }
  newdiv.setAttribute('id', id);
  newdiv.style.position="absolute";
  if (width) { newdiv.style.width=width; }
  if (height) { newdiv.style.height=height; }
  if (backgroundColor) { newdiv.style.background=backgroundColor; }
  if (borderColor) { newdiv.style.border="4px solid "+borderColor; }
  if (textColor) { newdiv.style.color=textColor; }
  newdiv.style.overflow='auto';
  if (html) { newdiv.innerHTML=html; }
  document.body.appendChild(newdiv);
}

function destroyDiv(div, loadURL) {
  var divId=document.getElementById(div);
  document.body.removeChild(divId);
  if (loadURL!==undefined) { document.location.replace(loadURL); }
}

function eventAdd( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}

function eventRemove( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

function floatDiv(divId) {
  var ftlObj;
  var px=document.layers ? '' : 'px';
  var divPosition=centerDiv(divId);
  var startX=divPosition[0], startY=divPosition[1];
  function ml() {
    var divObject=document.getElementById(divId);
    if (document.layers) { divObject.style=divObject; }
    divObject.sP=function(x,y){
      this.style.left=x.toString()+px;
      this.style.top=y.toString()+px;
    };
    divObject.x=startX; divObject.y=startY;
    return divObject;
  }
  window.stayTopLeft=function() {
    var windowScroll=getWindowScroll();
    var pY=windowScroll[1];
    ftlObj.y += (pY + startY - ftlObj.y)/8;
    ftlObj.sP(ftlObj.x, ftlObj.y);
    setTimeout(stayTopLeft, 40);
  };
  ftlObj=ml();
  stayTopLeft();
}

function getObject(name) {
  if (document.getElementById) {
    this.obj=document.getElementById(name);
    this.style=document.getElementById(name).style;
  }else if (document.all) {
    this.obj=document.all[name];
    this.style=document.all[name].style;
  }else if (document.layers) {
    this.obj=getObjectNN4(document,name);
    this.style=this.obj;
  }
}

function getObjectNN4(obj,name) {
  var x=obj.layers;
  var foundLayer;
  for (var i=0;i<x.length;i++) {
    if (x[i].id===name) { foundLayer=x[i]; }
    else if (x[i].layers.length) { var tmp=getObjectNN4(x[i],name); }
    if (tmp) { foundLayer=tmp; }
  }
  return foundLayer;
}

function getScrollHeight() {
  var scrollVertical=0;
  if (typeof window.pageYOffset==='number') {
    //***** Netscape compliant *****
    scrollVertical=window.pageYOffset;
  }else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
    //***** DOM compliant *****
    scrollVertical=document.body.scrollTop;
  }else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
    //***** IE6 standards compliant mode *****
    scrollVertical=document.documentElement.scrollTop;
  }
  return scrollVertical;
}

function getScrollWidth() {
  var scrollHorizontal=0;
  if (typeof window.pageYOffset==='number') {
    //***** Netscape compliant *****
    scrollHorizontal=window.pageXOffset;
  }else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
    //***** DOM compliant *****
    scrollHorizontal=document.body.scrollLeft;
  }else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
    //***** IE6 standards compliant mode *****
    scrollHorizontal=document.documentElement.scrollLeft;
  }
  return scrollHorizontal;
}

function getWindowHeight() {
  var windowHeight=0;
  if (typeof window.innerHeight==='number') {
    /***** Non-IE *****/    windowHeight=window.innerHeight;
  }else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    /***** IE 6+ in 'standards compliant mode' *****/    windowHeight=document.documentElement.clientHeight;
  }else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    /***** IE 4 compatible *****/    windowHeight=document.body.clientHeight;
  }
  return windowHeight;
}

function getWindowScroll() {
  var scrollHorizontal=getScrollWidth();
  var scrollVertical=getScrollHeight();
  return[scrollHorizontal,scrollVertical];
}

function getWindowSize() {
  var windowWidth=getWindowWidth();
  var windowHeight=getWindowHeight();
  return[windowWidth, windowHeight];
}

function getWindowWidth() {
  var windowWidth=0;
  if (typeof window.innerWidth==='number') {
    /***** Non-IE *****/    windowWidth=window.innerWidth;
  }else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
    /***** IE 6+ in 'standards compliant mode' *****/    windowWidth=document.documentElement.clientWidth;
  }else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
    /***** IE 4 compatible *****/    windowWidth=document.body.clientWidth;
  }
  return windowWidth;
}

function getURL(httpURL, responseDiv) {
var element;
  if (responseDiv!=='') {
    element=document.getElementById(responseDiv);
    element.innerHTML="<h4 style='text-align: left; color: #006633'>&nbsp;&nbsp;loading...</h4>";
    xmlHTTP.open("GET", httpURL);
    xmlHTTP.onreadystatechange=function() {
      if (xmlHTTP.readyState===4 && xmlHTTP.status===200) {
        element.innerHTML=xmlHTTP.responseText;
//        element.scrollTop=element.scrollHeight; /* scroll div to bottom */
      }
    };
    xmlHTTP.send(null);
  }else{
    xmlHTTP.open("GET", httpURL);
    xmlHTTP.onreadystatechange=function() {};
    xmlHTTP.send(null);
  }
}

function JSFX_StartEffects() {
  JSFX.Butterfly(1);
}

function makeLoginLogoutButton(memberName) {
  if (memberName==='') {
    document.mainMenuForm.atviirLoginLogoutButton.value='Login';
  }else{
    document.mainMenuForm.atviirLoginLogoutButton.value='Logout['+memberName+']';
    document.mainMenuForm.atviirLoginLogoutButton.onclick=atviirLogout;
  }
}

function postForm(formName, httpURL, responseDiv) {
var xmlMessage=buildPOST(formName);
var element=document.getElementById(responseDiv);
    element.innerHTML="<h4 style='text-align: left; color: #006633'>&nbsp;&nbsp;processing...</h4>";
    xmlHTTP.open("POST", httpURL, false);
    // for ie compatability
    xmlHTTP.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHTTP.send(xmlMessage);
    element.innerHTML = xmlHTTP.responseText;
}

function setColumnHeight() {
var column1=document.getElementById('column1Div');
var column2=document.getElementById('iframe');
var columnHeight=getWindowHeight()-150;
var menuDiv=document.getElementById('menuDiv');
var menuHeight=getWindowHeight()-250;
    column1.style.height=columnHeight+'px';
    column2.style.height=columnHeight+'px';
    menuDiv.style.height=menuHeight+'px';
}

function textResize() {
  var screenWidth=screen.width;
  var screenHeight=screen.height;
  document.body.style.fontSize=screenWidth/1280*100+'%';
}

