/**
 * Get cookie values, then check for country and language
 **/
function GetCookie(name) {
    var value = "";
    if ((begin = document.cookie.indexOf(name + "=")) >= 0) {
        if ((end = document.cookie.indexOf(";",begin)) >= 0) {
            value = document.cookie.substring(begin + name.length + 1,end);
        } else {
            value = document.cookie.substr(begin + name.length + 1);
            }
        }
    return(unescape(value));
}
// 20070601 aathavale pilot: used to create country/time zone/lang cookies using GET variables in URL
function createCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 var ck = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString()+ ";path=/;domain=.ptc.com;";
 document.cookie = ck;
}

// 20070601 aathavale pilot: function to extract GET variables from url
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];
}


/**
 * Global navigation drop down code
**/
sfHover = function() {
	var sfEls = document.getElementById("globalNav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

s1Hover = function() {
        var menuels = document.getElementById("s1select");
		if (menuels) {
			menuels = document.getElementById("s1select").getElementsByTagName("LI");
		
        for (var i=0; i<menuels.length; i++) {
                menuels[i].onmouseover=function() {
                        this.className+=" s1hover";
                }
                menuels[i].onmouseout=function() {
                        this.className=this.className.replace(new RegExp(" s1hover\\b"), "");
                }
        	}
		}
}
if (window.attachEvent) window.attachEvent("onload", s1Hover);

/**
 * Alternate style sheet swap code (deprecated - aa 05232007)
**/
function setActiveStyleSheet (title) {
   var i, a, main;
   for (i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
       a.disabled = true;
       if (a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

/**
 * Image functions for swapping, preloading, etc.
**/
function MM_preloadImages () {//v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_openBrWindow (theURL,winName,features) { //v2.0
    var newWindow = window.open(theURL,winName,features);
    newWindow.focus();
}

function GoUrl (s) {
	var d = s.options[s.selectedIndex].value;
	window.top.location.href = d;
	s.selectedIndex = 0;
}

/**
 * 20050920 ekristoff  Make sure people don't double-click submits. (#861801)
**/
var submitLocked = false;
function canSubmitForm () { 
	if ( submitLocked ) {  
		return false; 
	} else {
		submitLocked = true;  
		return true;
	}
}

/**
 * Clear search text input on focus.
**/
function clearInput (obj, theText) {
	if (obj.value == "") {
		obj.value = theText;
	} else {
		obj.value = "";
	}
}

/**
 * 20061101 bpeterson	Handle general popups (adapted from the WCMS-specific one).
**/
var popupOrigTops	= new Array();
function popupOff (id) {
	document.getElementById(id).className = 'popupOff';
	return false;
}
function popupOn (id) {
	if (popupOrigTops[id]) {
		document.getElementById(id).style.top = popupOrigTops[id];
	} else {
		popupOrigTops[id] = document.getElementById(id).style.top;
	}
	document.getElementById(id).className = 'popupOn';
	var helpHeight			= document.getElementById(id).offsetHeight;	// height of the pop-up box
	var windowHeight		= window.innerHeight;						// height of the browser window
	var aboveElementHeight	= window.event.clientY;						// distance between the top of the pop-up box and the top of the browser window
	if (!windowHeight || windowHeight<0) {// IE
		windowHeight = document.documentElement.clientHeight;
	}
	if (!windowHeight || windowHeight<0) {// FF?
		windowHeight = document.body.clientHeight;
	}
	// Reposition vertically if the popup is scrolling off of the bottom
	if (aboveElementHeight + helpHeight > windowHeight - 10) {		
		var moveUp = windowHeight - aboveElementHeight - helpHeight - 10;
		if (helpHeight > windowHeight - 10) {
			moveUp = 10 - aboveElementHeight;
		}
		document.getElementById(id).style.top = moveUp + "px";
	}
	return false;
}

/**
 * 20061101 bpeterson	Enable window.event for non-IE browsers (adapted from the WCMS-specific one).
**/
function PTC_getCursorHandler (e) {
	window.event.clientX	= e.clientX;
	window.event.clientY	= e.clientY;
	window.event.x			= e.layerX;
	window.event.y			= e.layerY;
	window.event.screenX	= e.screenX;
	window.event.screenY	= e.screenY;
	// route the event back to the intended function
	return routeEvent(e);
}
if (!window.event && window.captureEvents) {	
	// create an object to store the event properties 
	window.event = new Object;
	// set up event capturing and window event handlers (add or subtract as desired)
	window.captureEvents(Event.MOUSEOVER|Event.MOUSEOUT|Event.CLICK|Event.DBLCLICK);
	window.onclick		= PTC_getCursorHandler;
	window.ondblclick	= PTC_getCursorHandler;
	window.onmouseout	= PTC_getCursorHandler;
	window.onmouseover	= PTC_getCursorHandler;
}

/**
	*05/18/07 XHTML compliant target="_blank" code (replace target="_blank" with rel="external")
**/
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
/**
 * 
 * 20070723 aathavale use the following func to add onLoad events
 */
function addLoadEvent(func) {
  var test = func;
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      test();
    }
  }
}
addLoadEvent(externalLinks);