/*
Common functions used throughout Sprint sites
*/
var d = document;
actualVersion = ""; // for flash detection

/* general/utility */
function popUp(page, name, w, h, scroll) { // used for all pop up windows
	name = "foo";
	page = page.split(" ").join("%20");
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	//var resizable = false;
	winprop  = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=yes';
	win = window.open(page, name, winprop);
	win.focus();
}

function printThis() {
	var ua = navigator.userAgent.toLowerCase();
	var is_mac = ua.indexOf('mac') > 0;
	if (is_mac) { alert('To print:\n\nUse Command + P. on your keyboard\n') }
	else { print(); } 
}

function trim(s){ // remove extra whitespace before and after passed string
	toTrim = new Array(' ','\n','\r');
	for(x=0; x<toTrim.length; x++){
		while(s.substring(0,1) == toTrim[x]){
			s = s.substring(1,s.length);
		}
		while(s.substring(s.length-1,s.length) == toTrim[x]){
			s = s.substring(0,s.length-1);
		}
	}
	return s;
}

function getQueryVariable(variable) { // this function gets us the value of a passed variable name from the query string
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	} 
	return '';
}

function sprint_addLoadEvent(func) { // allows multiple window.onload's Thanks to Simon - http://simon.incutio.com/archive/2004/05/26/addLoadEvent
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
function sprint_addUnLoadEvent(func) { // copied from addLoadEvent, modified it for unload
  var oldonunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  } else {
    window.onunload = function() {
      oldonunload();
      func();
    }
  }
}

// finds the x,y position of an element thanks to PPK - http://www.quirksmode.org/js/findpos.html
function findPosX(obj){
	var curleft = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	return curtop;
}

/* manipulate layers */
function showHide(layerName){ // shows and hides a given layer **DON'T USE THIS VERSION**... here for legacy only
	if (d.getElementById){
		if(!layerName.nodeName) layerName = d.getElementById(layerName);
		layerName.style.display = layerName.style.display ? '' : 'none';
		return;
	}
}

function sprint_showHide(layerName){ // shows and hides a given layer
	if (d.getElementById){
		if(!layerName.nodeName) layerName = d.getElementById(layerName);
		
		curClass = " "+layerName.className + " ";
		if(curClass.indexOf(" seen ") != -1){
			hideLayer(layerName);
		} else {
			showLayer(layerName);
		}
		return;
	}
}

function showLayer(layerName){ // shows a given layer
	if (d.getElementById){
		if(!layerName.nodeName) layerName = d.getElementById(layerName);
		if(layerName){
			layerName.style.display = ''; // left for legacy hardcoded versions
			sprint_addClass(layerName,'seen');
			sprint_delClass(layerName,'hidden');
			return;
		}
	}
}

function hideLayer(layerName){ // hides a given layer
	if (d.getElementById){
		if(!layerName.nodeName) layerName = d.getElementById(layerName);
		sprint_delClass(layerName,'seen');
		sprint_addClass(layerName,'hidden');
		return;
	}
}

function sprint_delClass(curLayer,oldClass){
	if (d.getElementById){
		if(!curLayer.nodeName) curLayer = d.getElementById(curLayer);
		curClass = " "+curLayer.className + " ";
		if(curClass.indexOf(" "+ oldClass + " ") != -1){
			curLayer.className = trim(curClass.replace(" "+oldClass+" "," "));
		}
	}
}

function sprint_addClass(curLayer,newClass){
	if (d.getElementById){
		if(!curLayer.nodeName) curLayer = d.getElementById(curLayer);
		curClass = " "+curLayer.className + " ";
		if(curClass.indexOf(" "+ newClass + " ") == -1){
			curLayer.className = trim(curLayer.className + " " + newClass);
		}
	}
}

function sprint_toggleClass(curLayer,tog1,tog2){
	if (d.getElementById){
		if(!curLayer.nodeName) curLayer = d.getElementById(curLayer);
		curClass = " "+curLayer.className + " ";
		if(curClass.indexOf(" "+ tog1 + " ") != -1){
			sprint_delClass(curLayer,tog1);
			sprint_addClass(curLayer,tog2);
		} else if(curClass.indexOf(" "+ tog2 + " ") != -1){
			sprint_delClass(curLayer,tog2);
			sprint_addClass(curLayer,tog1);
		}
	}
}

function sprint_changeTabs(curTab){
	curTab = d.getElementById(curTab);
	if(curTab.className.indexOf('active') == "-1"){
		tabSet = curTab.parentNode;
		
		tabs = tabSet.getElementsByTagName('LI');
		for(f=0; f<tabs.length; f++){
			hideLayer(tabs[f].id+'Detail');
			sprint_delClass(tabs[f],'active');
		}
		
		showLayer(curTab.id+'Detail');
		sprint_addClass(curTab,'active');
		this.blur;
	}
}


var lastWindow;

function sprint_slide(slideWhat,startFrom,dir,offsetX,offsetY,winX,winY,winW,winH){
	theBox = d.getElementById(slideWhat);
	if(slideWhat != lastWindow && theBox.className.indexOf('show') == "-1"){
		if(!startFrom.nodeName) startFrom = d.getElementById(startFrom);
		imgs = startFrom.getElementsByTagName('img'); // firefox bug with anchors/images
		if(imgs.length >="1")startFrom = imgs[0];
		if(lastWindow){
			hideLayer(lastWindow);
		}
		
		if(winW >= "1") theBox.style.width = winW+"px";
		if(winH >= "1") theBox.style.height = winH+"px";
		
		newLeft = findPosX(startFrom)+offsetX;
		if(!isNaN(winX))newLeft = winY+offsetX;
		if(winX == "left")newLeft = findPosX(startFrom)+offsetX;
		if(winX == "right")newLeft = findPosX(startFrom)+startFrom.offsetWidth+offsetX;

		newTop = findPosY(startFrom)-106+offsetY; // -106 to compensate for being in the bodyArea
		if(!isNaN(winY))newTop = winX-106+offsetY;
		if(winY == "top")newTop = findPosY(startFrom)-106+offsetY;
		if(winY == "bot")newTop = findPosY(startFrom)-106+startFrom.offsetHeight+offsetY;

		sprint_addClass(slideWhat,"seen"); // show layer
		
		if(dir == "left"){
			linkW = startFrom.offsetWidth;
			theW = theBox.offsetWidth;
			newLeft = newLeft-theW;
		}
		
		theBox.style.top = newTop+"px";
		theBox.style.left = newLeft+"px";
		
		if(dir == "right"){
			sprint_slideRight(theBox.id, 10, 10, 0, 0);
		}
		if(dir == "left"){
			sprint_slideLeft(theBox.id, 10, 10, theW, 0);
		}
		if(dir == "down"){
			sprint_slideDown(theBox.id, 0, 5, 0, 0);
		}
		lastWindow = slideWhat;
	}
}

function sprint_slideRight(layerName, xInt, yInt, xClip, yClip){
	var theLayer = d.getElementById(layerName);
	if (xClip < theLayer.offsetWidth || yClip < theLayer.offsetHeight){
		xClip += xInt;
		yClip += yInt;
		theLayer.style.clip = "rect(auto, "+xClip+"px, "+yClip+"px, auto)";
		slideagain = setTimeout("sprint_slideRight('"+layerName+"', "+xInt+", "+yInt+", "+xClip+", "+yClip+");", 0);
	}
}

function sprint_slideLeft(layerName, xInt, yInt, xClip, yClip){
	var theLayer = d.getElementById(layerName);
	if (xClip > 0 || yClip < theLayer.offsetHeight){
		xClip -= xInt;
		yClip += yInt;
		theLayer.style.clip = "rect(auto, "+theLayer.offsetWidth+"px, "+yClip+"px, "+xClip+"px)";
		slideagain = setTimeout("sprint_slideLeft('"+layerName+"', "+xInt+", "+yInt+", "+xClip+", "+yClip+");", 0);
	}
}

function sprint_slideDown(layerName, xInt, yInt, xClip, yClip){
	var theLayer = d.getElementById(layerName);
	if (yClip < theLayer.offsetHeight){
		yClip += yInt;
		theLayer.style.clip = "rect(auto, "+theLayer.offsetWidth+"px, "+yClip+"px, "+xClip+"px)";
		slideagain = setTimeout("sprint_slideDown('"+layerName+"', "+xInt+", "+yInt+", "+xClip+", "+yClip+");", 0);
	}
}

function sprint_slideClose(layerName){
	var theLayer = d.getElementById(layerName);
	hideLayer(layerName);
	lastWindow = "";
}

function sprint_destroyFlashLayer(layerName){
	var theLayer = d.getElementById(layerName);
	d.getElementById('bodyArea').removeChild(theLayer);
	lastWindow = "";
}

function sprint_makeFlashLayer(layerName,flashW,flashH,flashVars,flashPath,pageName){
	if(!d.getElementById(layerName)){
		newLayer = d.createElement('DIV');
		newLayer.id = layerName;
		newLayer.className = "floatingWindow hidden";
		
		newLayerGuts = '	<div class="shadowBL">';
		newLayerGuts += '		<div class="shadowTR">';
		newLayerGuts += '			<h3 class="title"><span><a href="#" onclick="sprint_destroyFlashLayer(\''+layerName+'\'); return false;">X Close</a></span></h3>';
		newLayerGuts += '			<div class="content">';
		if(actualVersion >= 6){
			newLayerGuts += sprint_makeFlash(flashW,flashH,flashVars,flashPath);
		} else {
			newLayerGuts += 'Sorry, but you don\'t have Flash which is required to view this item.';
		}
		newLayerGuts += '			</div>';
		newLayerGuts += '		</div>';
		newLayerGuts += '	</div>';
		
		newLayer.innerHTML = newLayerGuts;
		d.getElementById('bodyArea').appendChild(newLayer);
		if(pageName)sprint_runOmniture(pageName);
	}
}

/* misc */
function toggleText(foo,tog1,tog2){ // toggles text that is within a span below the passed element
	if(!foo.nodeName) foo = d.getElementById(foo);
	s = foo.getElementsByTagName('span');
	for (var i=0; i<s.length; i++) {
		if(s[i].innerHTML == tog1){
			s[i].innerHTML = tog2;
		} else if(s[i].innerHTML == tog2){
			s[i].innerHTML = tog1;
		}
	}
}

function rolloverTo(picName,imgName) { // mouseover image
	d[picName].src = eval(imgName + ".src"); 
}


function sprint_runOmniture(pageName){
	s_pageName=pageName;
	void(setTimeout(s_gs(s_account),0));
}

// Omniture variables - setup all first, rewrite per page on page
var s_server=window.location.host;
var s_pageType="regular";
var s_pageName= s_channel= s_prop1= s_prop2= s_prop3= s_prop4= s_prop5= s_prop6= s_prop7= s_prop8= s_prop9= s_prop10="";
/* E-commerce Variables */
var s_campaign= s_state= s_zip= s_events= s_products= s_purchaseID= s_eVar1= s_eVar2= s_eVar3= s_eVar4= s_eVar5= s_eVar6= s_eVar7= s_eVar8= s_eVar9= s_eVar10="";


// flash detection
function sprint_flashDetect(reqVersion){ // detect users version of flash, and if it's high enough
	var maxFlashVersion = 9;
	if(actualVersion == ""){
		var gotIt = got2 = got3 = got4 = got5 = got6 = got7 = got8 = got9 = false;
		if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 && (navigator.appVersion.toLowerCase().indexOf("win") != -1)) {
			vbCode = '<scr'+'ipt language="VBScript"\> \n';
			vbCode += 'on error resume next \n';
			for(x=2; x<=maxFlashVersion; x++){
				vbCode += 'got'+x+' = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+x+'"))) \n';
			}
			vbCode += '</scr'+'ipt\> \n';
			d.write(vbCode);
			for (var i = 2; i <= maxFlashVersion; i++) {  
				if (eval("got" + i) == true) actualVersion = i;
			}
		} else {
			var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
			if (plugin) {
				actualVersion = parseInt(plugin.description.substring(plugin.description.indexOf(".")-1));
			}
		}
	}
	useFlashVer = getQueryVariable('useFlashVer');
	if(useFlashVer) actualVersion = useFlashVer;
	if(actualVersion >= reqVersion) gotIt = true;
	return gotIt;
}
sprint_flashDetect(6); // go ahead and run now for any future use

function sprint_makeFlash(w,h,vars,fileName){ // generate flash code based on passed vars
	swf  = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="'+window.location.protocol+'//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="'+w+'" height="'+h+'" class="flashTitle">\n';
	swf += '	<param name="movie" value="'+fileName+'" />\n';
	swf += '	<param name="wmode" value="transparent" />\n';
	swf += '	<param name="flashvars" value="'+vars+'" />\n';
	swf += '	<embed src="'+fileName+'" flashvars="'+vars+'" width="'+w+'" height="'+h+'" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer" class="flashTitle" wmode="transparent"><'+'/embed>\n';
	swf += '<'+'/object>\n';
	return swf;
}

function sprint_flashMessaging(reqVer,layerName,message){ // generate flash code based on passed vars
	if(sprint_flashDetect(reqVer)){
		showLayer(layerName);
	}else{
		document.write('<div class="noticeBox"><p>'+message+'</p></div>');
	}
}

//var detect = navigator.userAgent.toLowerCase();
//if(detect.indexOf('msie') != "-1" && detect.indexOf('mac')=="-1")sprint_addLoadEvent(wFix);


function createKatrina(){
	if(window.location.protocol.indexOf("s")!= "-1"){ // check for secure or not. will be appended to image src attribute 
		pCol = "https://";
	}else {
		pCol = "http://";
	}
	var bodyElement = document.getElementsByTagName('body')[0];
	var theDiv = document.createElement('div');
	theDiv.style.position = 'absolute';
	theDiv.id = 'katrina';
	theDiv.style.left = '492px';
	theDiv.style.top = '16px';
	theDiv.style.zIndex = '2';
	var theDivsLink = document.createElement('a');
	theDivsLink.href = 'http://www.sprint.com/hurricaneinfo'
	theDivsLink.target = '_blank'
	var theDivsLinksContent = document.createElement('img');
	theDivsLinksContent.src = pCol + 'www.sprint.com/assets/images/common/entry/prms/buk_hurricane.gif';
	theDivsLinksContent.style.border = '0';
	theDivsLink.appendChild(theDivsLinksContent);
	theDiv.appendChild(theDivsLink);
	
	//check for frames...
	if (window.top!=window.self)
		return;

	if (bodyElement.id != 'pop') {  
		bodyElement.appendChild(theDiv);
	}
}

//click to chat function
function displayHelpWindow1()
{
	suppressInq();
	var dt = new Date();
	var gURL1="https://www.nextel-chat.com/wi/servlet/HelpRequest?WI_TemplateName=/sprint";
	var gURL2=gURL1 + "&WI_StartUrl="+document.location.href+"&WI_BrowserShareWindowName="+top.name;
	var gURL= gURL2 + "&WI_OrigianlHost="+document.location.host;
	
	var Width = (screen.width * .4);
	var Height = (screen.height * .86);
	var winLeft = (screen.width - Width) /1;
	var winTop = 0;
	var feature = "toolbar=no,location=no,status=no,menubar=no,scroolbars=yes,resizable=yes,width=" + Width +",height=" + Height + ",top=" + winTop + ',left=' + winLeft;
	var win = window.open("","WebInteractionHelp",feature);
	if(top.name==null || top.name=="")top.name="WIBrowserShare"+(new Date()).getTime();
	if(win != null && win.bHelpInProgress != null) return;
	win=window.open("/explore/ctc_proxy.jsp?nextPage=" + escape(gURL + "&t=" + dt.getTime()),"WebInteractionHelp",feature);
	window.parent.resizeTo((screen.width*.6),(screen.height*.90));
	window.parent.moveTo(0,0);
}
//sprint_addLoadEvent(createKatrina) 

/* Added 1/05/2006 by ccs5025 */	
function displayCopyYear () {
		currentYear = new Date();
		document.write (currentYear.getFullYear());
}

/* Added 1/25/2006 by asm0686: used to suppress/unsuppress inQ Chat window */
function suppressInq() {
	if (typeof(inqshowchatTimer) != 'undefined') {
		clearInterval(inqshowchatTimer);
		hideMe();
	}
}
function unsuppressInq() {
	if (typeof(inqshowchatTimer) != 'undefined') {
		showMe();
	}
}
/* End - Added 1/25/2006 by asm0686: used to suppress/unsuppress inQ Chat window */

/* Added 2/10/2006  to show/hide click to chat */
function hideCtc()
{
	var elem = document.getElementById("click_to_chat");
	if(elem)
		elem.style.visibility = "hidden";

	elem = document.getElementById("click_to_chat2");
	if(elem)
		elem.style.visibility = "hidden";
}
function showCtc()
{
	var elem = document.getElementById("click_to_chat");
	if(elem)
		elem.style.visibility = "visible";

	elem = document.getElementById("click_to_chat2");
	if(elem)
		elem.style.visibility = "visible";
}
/* End - Added 2/10/2006 to show/hide click to chat */


// Added 8/24/2006 by JCL for Talisma project
// click to chat - Talisma
function displayHelpWindow2(pageId)
{
	suppressInq();

	var url = "https://sprint.cnxchat.com/narouter/nadispatch.aspx?RouteIdent=";
	url += "CDMA_" + pageId;
	url += "&questid=5EE2A889-DC2A-45E2-B29D-6C445295E79A&portid=60530401-F9E3-45D0-9D0B-4E70F4BEB336&nareferer=" + escape(document.location);
	url += "&id1=Sprint&id2=Reactive_Chat;" + pageId;

	var Width = (screen.width * .4);
	var Height = (screen.height * .86);
	var winLeft = (screen.width - Width) /1;
	var winTop = 0;
	var feature = "toolbar=no,location=no,status=no,menubar=no,scroolbars=yes,resizable=yes,width=" + Width +",height=" + Height + ",top=" + winTop + ',left=' + winLeft;

	var win = window.open("","WebInteractionHelp",feature);
	if(top.name==null || top.name=="")top.name="WIBrowserShare"+(new Date()).getTime();
	if(win != null && win.bHelpInProgress != null) return;

	//win=window.open(url,"WebInteractionHelp",feature);
	win=window.open("/explore/ctc_proxy.jsp?nextPage=" + escape(url),"WebInteractionHelp",feature);
	
	window.parent.resizeTo((screen.width*.6),(screen.height*.90));
	window.parent.moveTo(0,0);
}

function selectCtc2()
{
	var elem = document.getElementById('click_to_chat');
	if(elem)
		elem.parentNode.removeChild(elem);
}

