function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}



function validateFormOnSubmit(theForm) {

document.getElementById('inprogress').style.visibility='visible';
var reason = "";

  
  reason += validateEmpty(theForm.FirstName, "First name is missing.");
    reason += validateEmpty(theForm.LastName, "Last name is missing.");
	  reason += validateEmpty(theForm.Company, "Company is missing.");
	    reason += validateEmpty(theForm.Title,  "Occupation is missing.");
  reason += validateEmail(theForm.Email);
  reason += validatePhone(theForm.Phone);
  reason += validateChecked(theForm,"Please select at least one checkbox for additional information.");
     
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
document.getElementById('inprogress').style.visibility='hidden';
    return false;
  }
 
theForm.etrid.value = getCookieValue("poweredby",0);


if (gup('etrnm').length > 1){
//theForm.accountname.value = gup('etrnm');
}
else
{
//theForm.accountname.value = getCookieValue("poweredby",1);
}


document.cookie = 'poweredby=delete; expires=Fri, 27 Jul 2001 02:47:11 UTC; path=/'

//alert("a:" + theForm.etrid.value);
  return true;
//return false;
 }



function getCookieValue(NameOfCookie,pos){
if (document.cookie.length > 0) {

				begin = document.cookie.indexOf(NameOfCookie + "=");
				if (begin != -1) {

					begin += NameOfCookie.length + 1;
					end = document.cookie.indexOf(";", begin);
	
						if (end == -1) {

								end = document.cookie.length;
								cval = unescape(document.cookie.substring(begin, end));
								var values = cval.split(",");
								return values[pos];
						  }  
			      }  
					
	     }  
					
}


function validateEmpty(fld,errorMessage) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#fff0c7'; 
        error = errorMessage + "\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}


function validateChecked(theForm,errorMessage) {
    var error = "";
 
    if (theForm.Demo.checked == 0 && theForm.Insight.checked == 0 && theForm.EmailFieldGuide.checked == 0) {
        
        error = errorMessage + "\n"
    } 
    return error;  
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = '#fff0c7';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#fff0c7';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#fff0c7';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = '#fff0c7';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = '#fff0c7';
    } 
    return error;
}

function validateZip(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Zip code is missing.\n";
        fld.style.background = '#fff0c7';
    } else if (isNaN(parseInt(stripped))) {
        error = "The zip code contains illegal characters.\n";
        fld.style.background = '#fff0c7';
    } else if (!(stripped.length == 5)) {
        error = "The zip code is the wrong length. Make sure you included a 5 digit zip.\n";
        fld.style.background = '#fff0c7';
    }
    return error;
}





function applyCascadingDropdown(sourceId, targetId) {
    var source = document.getElementById(sourceId);
    var target = document.getElementById(targetId);
    if (source && target) {
        source.onchange = function() {
            displayOptionItemsByClass(target, source.value);
        }
        displayOptionItemsByClass(target, source.value);
    }
}

//Displays a subset of a dropdown's options
function displayOptionItemsByClass(selectElement, className) {
    if (!selectElement.backup) {
        selectElement.backup = selectElement.cloneNode(true);
    }
    var options = selectElement.getElementsByTagName("option");
    for(var i=0, length=options.length; i<length; i++) {
        selectElement.removeChild(options[0]);
    }
    var options = selectElement.backup.getElementsByTagName("option");
    for(var i=0, length=options.length; i<length; i++) {
        if (options[i].className==className)
            selectElement.appendChild(options[i].cloneNode(true));
    }
}

//Binds dropdowns
function applyCascadingDropdowns() {
    applyCascadingDropdown("country", "state");
    //We could even bind items to another dropdown
    //applyCascadingDropdown("items", "foo");
}

//execute when the page is ready
window.onload=applyCascadingDropdowns;



/***********************************************
* Cool DHTML tooltip script II- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var offsetfromcursorX=12 //Customize x offset of tooltip
var offsetfromcursorY=10 //Customize y offset of tooltip

var offsetdivfrompointerX=10 //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=14 //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

document.write('<div id="dhtmltooltip"></div>') //write out tooltip DIV
document.write('<img id="dhtmlpointer" src="http://image.exct.net/lib/fef9117476660c/m/1/arrow2.gif">') //write out pointer image

var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

var pointerobj=document.all? document.all["dhtmlpointer"] : document.getElementById? document.getElementById("dhtmlpointer") : ""

function ietruebody(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thewidth, thecolor){
if (ns6||ie){
if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
tipobj.innerHTML=thetext
enabletip=true
return false
}
}

function positiontip(e){
if (enabletip){
var nondefaultpos=false
var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
//Find out how close the mouse is to the corner of the window
var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20
var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20

var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX
var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY

var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000

//if the horizontal distance isn't enough to accomodate the width of the context menu
if (rightedge<tipobj.offsetWidth){
//move the horizontal position of the menu to the left by it's width
tipobj.style.left=curX-tipobj.offsetWidth+"px"
nondefaultpos=true
}
else if (curX<leftedge)
tipobj.style.left="5px"
else{
//position the horizontal position of the menu where the mouse is positioned
tipobj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px"
pointerobj.style.left=curX+offsetfromcursorX+"px"
}

//same concept with the vertical position
if (bottomedge<tipobj.offsetHeight){
tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
nondefaultpos=true
}
else{
tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px"
pointerobj.style.top=curY+offsetfromcursorY+"px"
}
tipobj.style.visibility="visible"
if (!nondefaultpos)
pointerobj.style.visibility="visible"
else
pointerobj.style.visibility="hidden"
}
}

function hideddrivetip(){
if (ns6||ie){
enabletip=false
tipobj.style.visibility="hidden"
pointerobj.style.visibility="hidden"
tipobj.style.left="-1000px"
tipobj.style.backgroundColor=''
tipobj.style.width=''
}
}

document.onmousemove=positiontip