/*  */
/* Author: Jeffrey Day */
/* Date  : August 2008 */
/*  */
/* Purpose: Tools for use with the alfa.com website built using Websmart */
/*  */

/* start parms */
var path = location.pathname.substr(0,4);
var isIE5=navigator.userAgent.toUpperCase().indexOf("MSIE 5")!=-1;
var targetElementID="container", targetElementStyleOffset=30;// 58 76 85 wrap container

/* functions */
function switchLanguage(newLang) /* change language */
{
  var lang = new String(newLang.options[newLang.selectedIndex].value);
  var page = location.pathname;
  var parm = location.search;
  var redir;
  var aPath = path + 'gs120w.pgm?wklang=' + lang;
  $.get(aPath);
  if((page.substr(0,1) == '/') && (page.substr(3,1) == '/'))
  {
    redir = '/' + lang + '/' + page.substr(4) + parm;
    parent.location = redir; 
  }
}

function adjustHeight() /* adjust height to fill screen vertically */
{
  if (document.getElementById) {
    var targetElement=document.getElementById(targetElementID),
        documentHeight, totalOffset;

    if (targetElement && document.documentElement.offsetHeight
        && targetElement.offsetHeight && targetElement.offsetTop) {
      documentHeight=document.documentElement.offsetHeight;
      if (targetElement.offsetHeight<documentHeight-targetElement.offsetTop) {
        if (isIE5)
          totalOffset=targetElement.offsetTop;
        else totalOffset=targetElement.offsetTop+targetElementStyleOffset;
        targetElement.style.height=String(documentHeight-totalOffset)+'px';
      }
    }
  }
}

function makeTabActive(defaultTabName) /* make a specific tab active */
{
  var tabName=getActiveTab();
  if((tabName == '') || (tabName == ' '))
  {
    tabName=defaultTabName;
  }
  document.getElementById(tabName).setAttribute('id','nav1');
  if(tabName == 'tab8')  // change color of titlebar
  {
    document.getElementById('titleBar').style.background='#7ABC3A';
  }
}

function setActiveTab(tabName) /* set the Active tab value in a cookie */
{
  var menu="menu";
  var days=365;
  return setCookie(menu,tabName,days)?false:true;
}

function getActiveTab() /* retrieve the active tab */
{
  var menu="menu";
  var tabName;
  tabName = getCookie(menu);
  return tabName;
}

function getCookie(c_name) /* read from a cookie */
{
  if (document.cookie.length>0)
  {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1)
    { 
      c_start=c_start + c_name.length+1; 
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return "";
}

function setCookie(NameOfCookie, value, expiredays) /* populate/create cookie path='/'  */
{
  var ExpireDate = new Date ();
  ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
  document.cookie = NameOfCookie + "=" + escape(value) +
    ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString())+
    "; path=/";
}

function formHandler(form) /* contact page select box to drop to correct section */
{
  var URL = document.form.site.options[document.form.site.selectedIndex].value;
  window.location.href = URL;
}

/*$(document).ready(function () {  $.get(aPath,  function(data) { $("#addressTable").replaceWith(data); }, "html");  }); prepend*/
function getAddressFields() /* add address fields to screen */
{
  var aPath = path + 'gs150w.pgm';
  $.get(aPath, function(data) { $("#addressTable").replaceWith(data); adjustHeight(); }, "html");
}

function checkLogon() /* determine if user is signed in, display name or diplay prompt fields */
{
  var unameVal = $("#uname").val();
  var upassVal = $("#upass").val();
  aPath = path + 'gs140w.pgm';
  $(document).ready(function () {  
    $.post(aPath,  { uname: unameVal, upass:  upassVal }, function(data) { 
    
    	// Create the JSON response as a javascript object
		var responseObj = eval('(' + data + ')');    	    	    	    	    	
    	
    	$("#userLogin").html(responseObj.html) 
    
    }, "html" );  
  });
}

function newLogon() /* determine if user is signed in, display name or diplay prompt fields */
{
  var unameVal = $("#uname").val();
  var upassVal = $("#upass").val();
  aPath = path + 'gs140w.pgm';
  /* aPath = 'https://' + location.hostname + path + 'gs140w.pgm'; */   /*  needs https - will be sending user / pwd */
  $.post(aPath,  { uname: unameVal, upass:  upassVal }, 
       function(data) { 
       // Create the JSON response as a javascript object
		var responseObj = eval('(' + data + ')');    	    	    	    	    	
    	
    	$("#userLogin").html(responseObj.html) 
        getAddressFields();
   }, "html" );
}

function newSession() /* signoff */
{
  var aPath = path + 'gs140w.pgm?task=new';
  $.get(aPath,  
    function(data) { 
    	// Create the JSON response as a javascript object
		var responseObj = eval('(' + data + ')');    	    	    	    	    	
    	
    	$("#userLogin").html(responseObj.html) 
                           getAddressFields();
  }, "html");
}

function buildSelectList() /* build work field of checkbox id's that are selected, this field is then read in, in Websmart */
{
  $("#wkCheckList").val("");
  for (i=0; i<document.getElementsByTagName('input').length; i++) {
    if (document.getElementsByTagName('input')[i].type == 'checkbox')
      if (document.getElementsByTagName('input')[i].checked == true)
        //if($("#wkCheckList").val() == '' )
          //$("#wkCheckList").val(document.getElementsByTagName('input')[i].id);
        //else
          $("#wkCheckList").val($("#wkCheckList").val() + document.getElementsByTagName('input')[i].id + ",");
  }
  //alert($("#wkCheckList").val());
}

function checkForm()
{
  formIsValid=true;

    if($("#vfirst").val()==''){
      formIsValid=false;
      $("#vfirst").addClass("error");
    }
    else $("#vfirst").removeClass("error");

    if($("#vlast").val()==''){
      formIsValid=false;
      $("#vlast").addClass("error");
    }
    else $("#vlast").removeClass("error");

    if($("#vcomp").val()==''){
      formIsValid=false;
      $("#vcomp").addClass("error");
    }
    else $("#vcomp").removeClass("error");

    if($("#vaddr1").val()==''){
      formIsValid=false;
      $("#vaddr1").addClass("error");
    }
    else $("#vaddr1").removeClass("error");

    if($("#vcity").val()==''){
      formIsValid=false;
      $("#vcity").addClass("error");
    }
    else $("#vcity").removeClass("error");

    if($("#vzip").val()==''){
      formIsValid=false;
      $("#vzip").addClass("error");
    }
    else $("#vzip").removeClass("error");

    if($("#vcountry").val()==''){
      formIsValid=false;
      $("#vcountry").addClass("error");
    }
    else $("#vcountry").removeClass("error");

    if($("#vphone").val()==''){
      formIsValid=false;
      $("#vphone").addClass("error");
    }
    else $("#vphone").removeClass("error");

    if($("#vemail").val()==''){
      formIsValid=false;
      $("#vemail").addClass("error");
    }
    else $("#vemail").removeClass("error");

  return formIsValid;
}

/*function togView()
{
 $(".artlink").toggleClass("hideit");
 adjustHeight();
 
 $(document).ready(function () {  $(".artlink").toggleClass("hideit"); adjustHeight();  });
}*/

checkLogon(); /* pull in login info */

//window.onresize=adjustHeight; /* buggy - bad browser compatibility */
$(window).resize(function() { adjustHeight(); }); /* assign adjustHeight to window.resize event */

//window.onload=adjustHeight;   /* buggy - bad browser compatibility */
$(window).load(function ()  { adjustHeight(); }); /* assign adjustHeight to window.onload event */
