//RMR to diff this with all the other territories common.js. This one has the code for 
//forgot password - popUp

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function extendOnClick(el, func) {
  var oldonclick = el.onclick;
  if (typeof el.onclick != 'function') {
    el.onclick = func;
  } else {
    el.onclick = function(evt) {
      oldonclick(evt, this);
      func(evt,this);
      return false;
    }
  }
}
addLoadEvent(popUpsAndTrackingFunctions);
//************************************
//functions to bring up external pop ups
//************************************
function popUpsAndTrackingFunctions()
{
  if (!document.getElementsByTagName) return false;
  var links = document.getElementsByTagName("a");
  for (var i=0; i<links.length; i++) 
  {
    var linkClassAttribute = links[i].className;
    
    if(linkClassAttribute) 
    {
        var parsedClassAttribute = linkClassAttribute.split(" ");
        for(var j=0; j<parsedClassAttribute.length; j++)
        {
            if ( parsedClassAttribute[j] == "promotion")
            { 
                links[i].onclick = getPromotionOmnitureVars;
            }

            if ( parsedClassAttribute[j] == "order")
            {
                links[i].onclick = getOrderOmnitureVars;
            }
        }       
    }
     
    var linkRelAttribute = links[i].getAttribute("rel");  
    if(linkRelAttribute)
    {
        var parsedLinkAttribute = linkRelAttribute.split(" ");
        for(var j=0; j<parsedLinkAttribute.length; j++)
        {
            if ( parsedLinkAttribute[j] == "player") 
            {
                extendOnClick(links[i],showPlayer);
             }
             else
             {
                if (parsedLinkAttribute[j] == "external") 
                {
                    extendOnClick(links[i],popUp);
                }
                else  
                {
                    if (parsedLinkAttribute[j] == "forgot_password") 
                   {
                       links[i].onclick = function() 
                       { 
                          displayWindow("http://www.YOUR_SERVER.com/spn/spn_forgot_password_screen.php","forgot_password",395,170,"directories=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0");
                          return false;
                       }
                    }
                    else
                    { if (parsedLinkAttribute[j] == "samewindow") 
                      {
                         if((linkClassAttribute.indexOf(" order") != -1) || 
                             (linkClassAttribute.indexOf(" promotion") != -1))
                         {
                               extendOnClick(links[i],stopDefault);
                         }
                      }
                    }
                }
             }
         }
     }      
  } 
}
//for links that are opening in the same window and requires tracking.
//The links opens the page before the tracking is completed. 
//Hence do chain actions - call promotion omniture tracking, stop default link behavior
// and then open the new link in the same window. 
function stopDefault(evt, link)
{
   return false;
}

function sameWindow(link)
{
    if(link == null)
      link = this;
    var winURL = link.getAttribute("href");
    window.location.href=winURL;
}

function openLinkInSameWindow(link)
{
    var linkClassAttribute = link.className;
    var linkRelAttribute = link.getAttribute("rel");  
    if (linkRelAttribute.indexOf("samewindow") != -1 && ((linkClassAttribute.indexOf(" order") != -1) || 
             (linkClassAttribute.indexOf(" promotion") != -1)))
         {
               sameWindow(link);
         }
}

function getPromotionOmnitureVars(evt, link)
{
    if (link == null)
          link = this;
          
    var url = link.getAttribute("href");
    var parsedClassAttribute = link.className.split(" ");
    var uniqueId = parsedClassAttribute[0];
    if (isOmnitureExists())
    {
        sCode.trackOutboundClick(url, uniqueId);  
    }
    openLinkInSameWindow(link);
}

function getOrderOmnitureVars(evt, link)
{
    if (link == null)
          link = this;
    
    var url = link.getAttribute("href");
    var parsedClassAttribute = link.className.split(" ");
    var uniqueId = parsedClassAttribute[0];
    if (isOmnitureExists())
    {
        sCode.trackOutboundClickToBuy(url,uniqueId);
    }
    openLinkInSameWindow(link);
}
 
function isOmnitureExists()
{
    //s.pageName is passed in through omniture code. This is assuming
    //that omniture code exists on every page. It is currently inject thru treatments
    //hence currently omniture code does exist in every page. 
    if (s != null && (s.pageName.length != 0))
        return true;
    else return false;
}
 
function popUp(evt, link) 
{
  if(link == null)
    link = this;
  winURL = link.getAttribute("href");
  window.open(winURL,'_blank');
  return false;
}

//*********************
function showPlayer(evt, link) {
//*********************
if(link == null)
    link = this;
pURL = link.getAttribute("href");
displayWindow(pURL,"Player",620,460,"directories=0,location=0,menubar=0,resizable=0,scrollbars=0,status=0,titlebar=0,toolbar=0");
return false;
}

//****************************************
//End of function
//****************************************


//*********************
function displayWindow(theURL,winName,width,height,features) { //v3.1
//*********************
  var window_width = width;
  var window_height = height;
  var newfeatures= features;
  var window_top = (screen.height-window_height)/2;
  var window_left = (screen.width-window_width)/2;
  newWindow=window.open(''+ theURL + '',''+ winName + '','width=' + window_width + ',height=' + window_height + ',top=' + window_top + ',left=' + window_left + ','+ newfeatures + '');
  newWindow.focus();
}

//*********************
function MM_openBrWindow(theURL,winName,features) { //v2.0
//*********************
  window.open(theURL,winName,features);
}

//*********************
function mm_openbrwindow(theURL,winName,features) { //v2.0
//*********************
  window.open(theURL,winName,features);
}

//***************************************************
function popFull(url,name){
//***************************************************
var w = 480, h = 340;

if (document.all || document.layers) {
   w = screen.availWidth;
   h = screen.availHeight;
}else{
    w = screen.width;
    h = screen.height;
}

var url;
var popW;
var popH;

window.open(url,name,'width=' + w + ',height=' + h + ',top=50,left=50');
}

function getCacheBuster()
{
    var cacheBuster = "?preventCache=" + new Date().valueOf();
    return cacheBuster;
}

function compressWhiteSpace(s)
{
  // Copyright 2001 by Mike Hall.
  // See http://www.brainjar.com for terms of use.

  // Condense white space.
  s = s.replace(/\s+/g, " ");
  s = s.replace(/^\s(.*)/, "$1");
  s = s.replace(/(.*)\s$/, "$1");

  // Remove uneccessary white space around operators, braces and parentices.
  s = s.replace(/\s([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])/g, "$1");
  s = s.replace(/([\x21\x25\x26\x28\x29\x2a\x2b\x2c\x2d\x2f\x3a\x3b\x3c\x3d\x3e\x3f\x5b\x5d\x5c\x7b\x7c\x7d\x7e])\s/g, "$1");

  return s;
};


corefunctions = {
  init: function () 
  {
    this.loadevent(this.externallinks);
  },
  loadevent: function (func) 
  {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
      window.onload = func;
    } else {
      window.onload = function() {
        if (oldonload) {
          oldonload();
        }
        func();
      }
    }
  },
  externallinks: function () 
  {
    if ( document.getElementsByTagName ) {
       var links = document.getElementsByTagName ( 'a' );
       for (var i = 0; i < links.length; i++ ) {
		 	if(links[i].getAttribute("rel") && links[i].getAttribute("href")) {
				links[i].onclick = function() {
					var theURL = this.getAttribute("href");
					var theREL = this.getAttribute("rel");
					var relArr = theREL.split('|');
					if (relArr[0] == 'external'){
						this.target = '_blank';
					}
					if (relArr[0] == 'externalpop'){
						return corefunctions.popup(theURL,theREL);
					}
			  	}
			}
       }
	   
       var links = document.getElementsByTagName ( 'area' );
       for (var i = 0; i < links.length; i++ ) {
         var link = links[i];
         if ( ( link.getAttribute ( 'href' ) ) && ( link.getAttribute ( 'rel' ) == 'external' ) ) {
           link.target = '_blank';
         }
       }
	   
    }//end outer if to get tag name
  },popup: function(theURL,theREL){
				var totalNumProps = 11;
				var reqNumProps = 4;
				var relArr = theREL.split(" ");
				
				//Check for syntax of REL string value and determine value for position. If syntax is incorrect, do not open a window.
				if(relArr.length > 2){
					//alert('INCORRECT! Too many spaces in REL tag associated with this link: '+ relArr);
					return false;
				}else if( (relArr.length == 2) && (relArr[1] != "") ){	
					var position = relArr[1];
					
				}else{
					var position = "default";	
				}
			
				//Check for syntax of remaining REL string values and set properties. If syntax is incorrect, do not open a window. 
				relArr = relArr[0];
				relArr = relArr.split("|");
				if ( (relArr.length < reqNumProps) || (relArr[relArr.length-1] == "") || (this.myParseInt(relArr[2], 0) == 0) || (this.myParseInt(relArr[3], 0) == 0) ){
					//alert('INCORRECT! Must have at least 4 initial values or there is an extra | separator or width/height is not a number!');
					return false;
				}
				
				var newArr = new Array(totalNumProps-relArr.length);
				for ( var i=0; i < newArr.length; i++) {
						newArr[i] = "0";
				}
				relArr = relArr.concat(newArr);
				var func = relArr[0]; 
				var target = relArr[1];
				var width = relArr[2];
				var height = relArr[3];			
				var toolbar = relArr[4];
				var scrollbars = relArr[5];
				var menubar = relArr[6];
				var resizable = relArr[7];
				var status = relArr[8];
				var location = relArr[9];
				var directory = relArr[10];
				
				for(var i=0; i < relArr.length; i++){
					var currProp = relArr[i];
					switch (currProp){
						case "t":
							toolbar = "1";
							break;
						case "s":
							scrollbars = "1";
							break;
						case "m":
							menubar = "1";
							break;
						case "r":
							resizable = "1";
							break;
						case "st":
							status = "1"
							break;
						case "l":
							location = "1"
							break;
						case "d":
							directory = "1";
							break;
						default: 
							break;
					}	
				}
				
				//Debug info for total number of REL tag properties
				/*var arrOutput = "There are "+relArr.length+" properties:\n";
				for ( var i=0; i < totalNumProps; i++) {
					arrOutput += relArr[i] + "\n"; 
				}
				alert(arrOutput);*/
				
				if(position != "default"){
					var commaArr = position.split(",");
					
					if((commaArr.length > 2) || ((commaArr.length == 2) && ( (this.myParseInt(commaArr[0], 0) == 0) || (this.myParseInt(commaArr[1], 0) == 0)))) {	
						//alert('To many commas or coordinate values are not numbers for position value: '+commaArr);
						return false;
					}else if (commaArr.length == 2){	
						var left = commaArr[0];
						var top = commaArr[1];
						//alert('Popup is being placed at coordinate: '+left+','+top);
					}else{
						if(position == "center"){
							//Center the popup window
							var winLeft = (screen.width - width) / 2;
     						var winTop = (screen.height - height) / 2;
							var left = winLeft.toString();
							var top = winTop.toString();
							//alert('Popup is centered at '+left+','+top);
						}
						if(position == "full"){
							//Calculate fullscreen of popup window
							var width = screen.width;
     						var height = screen.height;
							var left = "0";
							var top = "0";
							//alert('Popup is full size with a w='+width+' height='+height);
						}
					}
				}else{
					var left = "200";
					var top = "200";
					//alert('Popup is full size at w='+width+' height='+height+' at coordinate '+left+','+top)
				}

				window.open(theURL,'_new',"width="+width+",height="+height+",toolbar="+toolbar+",scrollbars="+scrollbars+",menubar="+menubar+",resizable="+resizable+",status="+status+",location="+location+",directory="+directory+",left="+left+",top="+top+"'");
				return false;	
},myParseInt: function(str,defaultValue){
    var retValue = defaultValue;
    if(str!=null){
        if(str.length>0){
            if (!isNaN(str)){
                retValue = parseInt(str);
            }
        }
    }
    return retValue;
}//end myParseInt

}//end corefunctions

function externalpop(){
	corefunctions.externallinks();
}
addLoadEvent(externalpop);