// Begin the code to animate the main navigation and validate the site search.

function cssNavHoverOn(tarDiv) {
  document.getElementById(tarDiv).style.cursor = "pointer";
  document.getElementById(tarDiv).style.backgroundPosition = "0px -38px";
}

function cssNavHoverOff(tarDiv) {
  setTimeout("cssNavHoverAnim('"+tarDiv+"',-38)",1);
}

//David Gilardi 12/11/2007:  Added cssNavTabFlip functions to flip between keyfacts navigation tabs
// Use this function to flip between "on" and "off" tab positions
// If the backgroundPosition = "0px - 30px" then flip to "0px 0px" and vice versa
// The onOff variable will refer to whether the tab icon is "on" or "off"
// with "on" being the currently selected tab
// NOTE:  I kind of overloaded the function with the addition of tabName.  This
// parameter is only relevant for the clicked button so it is only used from
// contect of the clicked button.  I did this to keep from having another 
// function call.
function cssNavTabFlip(tarDiv, onOff, tabName) {
  var position = document.getElementById(tarDiv).style.backgroundPosition;
  //alert(position);

  if (position == "0px 0px" && onOff == 0) {
    document.getElementById(tarDiv).style.cursor = "pointer";
    document.getElementById(tarDiv).style.backgroundPosition = "0px -30px";

  } else if (position == "0px -30px" & onOff == 1) {
    document.getElementById(tarDiv).style.cursor = "pointer";
    document.getElementById(tarDiv).style.backgroundPosition = "0px 0px";

    //Set the value of hub_keyfactsTabInfo based off of tabName
    document.getElementById('hub_keyfactsTabInfo').innerHTML = tabName;

  }

}
//End function add

function cssNavHoverAnim(tarDiv,yPos) {
  // alert(yPos);
  if (yPos == null) {
    yPos = -39;
  } else {
    if (yPos < 1) {
      yPos = yPos+8;
      document.getElementById(tarDiv).style.backgroundPosition = "0px "+yPos+"px";
      setTimeout("cssNavHoverAnim('"+tarDiv+"',"+yPos+")",1);
    } else {
      document.getElementById(tarDiv).style.backgroundPosition = "0px 0px";
    }
  }
}

function validNavSearch() {
  var form = document.navSearchForm;
  if ( form.query.value != null && form.query.value != "" ) {
   
      form.action="/sitesearch/search.jsp";
   
    return true;
  } else {
  
    alert("Please enter a search term.");
    return false;
    
  }
}

// End the code to animate the main navigation and validate the site search.



function wrapLink(targetDiv,setClass) {
  document.getElementById(targetDiv).className = setClass;
}

function validateResponseForm() {
  var alertString = "";
  
  if (document.response.FirstName.value == null || document.response.FirstName.value == "") {
    alertString = alertString + "- First Name\n";
  }
  
  if (document.response.LastName.value == null || document.response.LastName.value == "") {
    alertString = alertString + "- Last Name\n";
  }
  
  if (document.response.Email.value == null || document.response.Email.value == "") {
    alertString = alertString + "- E-mail Address\n";
  } else {
    var varValue = document.response.Email.value;
    var valResult = validRegExp("Email",varValue);
    if (valResult == 1) {
      alertString = alertString + "- Valid E-mail Address\n";
    }
  }
  
  if (document.response.phone.value != null && document.response.phone.value != "") {
	if(!document.response.phone.value.match(/.{0,1}\d{3}.{0,1}\d{3}.{0,1}\d{4}/)){
		alertString = alertString + "If entering a phone number it must be in (###)###-#### format\n";
	}
  }
  
  if (document.response.WhoAmI.value == null || document.response.WhoAmI.value == "") {
    alertString = alertString + "- I Am a\n";
  }
  
  if (document.response.sendTo.value == null || document.response.sendTo.value == "") {
    alertString = alertString + "- Subject\n";
  }
  
  if (document.response.Comments.value == null || document.response.Comments.value == "") {
    alertString = alertString + "- Comments or Questions\n";
  }
  
  if (alertString == null || alertString == "") {
    return true;
  } else {
    alert("Please include the required fields listed below:\n" + alertString)
    return false;
  }
}

function validRegExp(pattern,varValue) {
  var reglist=new Array();
  reglist["Phone"]="\\(\\d{3}\\)\\d{3}-\\d{4}";
  reglist["Email"]="\\w{2,}@(\\w|-){2,}\\.\\w{2,}";
  var x=varValue;
  var y=new RegExp(reglist[pattern]);
  if(y.test(x)) {
    var goodForm = 0;
  } else {
    var goodForm = 1;
  }
  return goodForm;
}

function toggleMessage(target,action) {
  var hidden = target;
  var shown = target + "_full";

  if (action == 0) {
    document.getElementById(hidden).className = "mycv_messageSchoolHolderShow";
    document.getElementById(shown).className = "mycv_messageHolderHide";
  } else if (action == 1) {
    document.getElementById(hidden).className = "mycv_messageSchoolHolderHide";
    document.getElementById(shown).className = "mycv_messageHolderShow";
  }
}

function openWindow(theURL,winName,features) { 
	window.open(theURL,winName,features);
}

var adBaseShade = 0;
function showBaseShade(target) {
  if (adBaseShade != 1) {
    document.getElementById(target).style.visibility = "visible";
    adBaseShade = 1;
  } else {
    document.getElementById(target).style.visibility = "hidden";
    adBaseShade = 0;
  }
}

function tabFeaturedToggle(newActive) {
  for (var i=1; i<5; i++) {
    var newTarget = "section_header_tab_"+i;
    var newDisplay = "section_tab_"+i+"_content";

    if (newDisplay == newActive) {
      document.getElementById(newTarget).className = "section_header_tab_active";
      document.getElementById(newDisplay).style.display = "block";
    } else {
      document.getElementById(newTarget).className = "section_header_tab_inactive";
      document.getElementById(newDisplay).style.display = "none";
    }
  }
}

// This function is necessary to work arounnd the ID "Click to activate issue"
function loadFlashApp(path,width,height) {
  document.write("<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='"+width+"' height='"+height+"'>");
  document.write("  <param name='movie' value='"+path+"'>");
  document.write("  <param name='quality' value='high'>");
  document.write("  <embed src='"+path+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+width+"' height='"+height+"'></embed>");
  document.write("</object>");
}


function friendForm() {
  var mName = document.loungeEmailForm.name1.value;
  var mAddy = document.loungeEmailForm.address1.value;
  var fName = document.loungeEmailForm.name2.value;
  var fAddy = document.loungeEmailForm.address2.value;
  var comments = document.loungeEmailForm.comments.value;
  
  var alertMsg = "";
  
  if (!mName) {
    alertMsg = alertMsg+"- Please include your name\n";
    // highlightFormField("loungeEmailForm","name1");
  }
  if (!mAddy) {
    alertMsg = alertMsg+"- Please include your email Address\n";
  }
  if (!fName) {
    alertMsg = alertMsg+"- Please include your friend's name\n";
  }
  if (!fAddy) {
    alertMsg = alertMsg+"- Please include your friend's email address\n";
  }
  
  if (!alertMsg) {
    return true;
  } else {
    alert(alertMsg);
    return false;
  }
}

function highlightFormField(formName,targetField) {
  document.getElementById(targetField).style.border = "1px solid #ff0000";
  document.getElementById(targetField).style.backgroundColor = "#dddddd";
}

/* All functionality for the Article Rating Tool */

function rateArticlesHover(swapImg,targetImg) {
  var image = new Image();
  image.src = "/_img/"+swapImg;

  document[targetImg].src=image.src;
}

function rateArticles(prodAreaID,rate,articlePath) {
  document.getElementById("rankingHolder").innerHTML="<div class='rankings'><object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='150' height='20'><param name='movie' value='/_f/load_rating.swf' /><param name='quality' value='high' /><embed src='/_f/load_rating.swf' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='150' height='20'></embed></object></div>";
  var scoreArticle = getArticleCookie(prodAreaID,rate,articlePath);
  loadArticleXML("/_proc/articleRating.jsp?score="+rate+"&articlePath="+articlePath+"&scoreIt="+scoreArticle);
}

function loadArticleXML(url) {
	req = false;
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = function() { processRating(); }
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() { processRating(); }
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processRating() {
  if (req.readyState == 4) {
    document.getElementById("rankingHolder").innerHTML=req.responseText;
  }
}

function getArticleCookie(prodAreaID,rate,articlePath) {
	var nameEQ = "HOB100106ARTICLE=";
	var ca = document.cookie.split(';');
	for(var i=0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') {
      c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) {
        return updateArticleCookie(c.substring(nameEQ.length,c.length),prodAreaID,rate,articlePath);
      }
    }
	}
  if(rate > 0) {
    createArticleCookie(prodAreaID,articlePath);
  }
	return true;
}

function createArticleCookie(prodAreaID,articlePath) {
  var value = prodAreaID +"^"+ articlePath;
  setCookie(value);
}

function updateArticleCookie(currentVals,prodAreaID,rate,articlePath) {
  var value = "";
  var ar = true;
  var cVal = currentVals.split('|');
  for (var x=0; x<cVal.length; x++) {
    var cElem = cVal[x].split('^');
    if(prodAreaID == cElem[0] && articlePath == cElem[1]) {
      ar = false;
      break;
    }
  }
  if(ar && rate > 0) {
    value = currentVals+"|"+prodAreaID+"^"+articlePath;
    setCookie(value);
  }
  return ar;
}

function setCookie(value) {
  var date = new Date();
  date.setTime(date.getTime()+(30*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
	document.cookie = "HOB100106ARTICLE="+value+expires+";path=/";
}

function saveArticle() {
  var form = document.myGVArticle;
  form.articleUrl.value = location.pathname;
  form.submit();
}
