/*
common.js is for common functions used throughout Embarq sites
*/

var d = document;
actualVersion = ""; // for flash detection

/* floodlight tag tracking onclick */

function dblClck_btnClick() {
	/* Start of DoubleClick Floodlight Tag: Please do not remove  Activity name of this tag: Embarq CL Universal Check Availability Button. Creation Date: 12/15/2009 */
	var tag_url="https://fls.doubleclick.net/activityi;src=2417582;type=cavail;cat=embar703;ord=1;num="+Math.floor(Math.random()*999999);
	if(document.getElementById("DCLK_FLDiv")){var flDiv=document.getElementById("DCLK_FLDiv");}
	else{var flDiv=document.body.appendChild(document.createElement("div"));flDiv.id="DCLK_FLDiv";flDiv.style.display="none";}
	var DCLK_FLIframe=document.createElement("iframe");
	DCLK_FLIframe.id="DCLK_FLIframe_"+Math.floor(Math.random()*999999);
	DCLK_FLIframe.src=tag_url;
	flDiv.appendChild(DCLK_FLIframe);
 }
 
/* general/utility */

function addBodyEvent(event,action) {
	var onEvent="on"+event;
	if (window.addEventListener) {
		window.addEventListener(event, action, false);
	} else if (document.addEventListener) {
		document.addEventListener(event, action, false);
	} else if (window.attachEvent) {
		window.attachEvent(onEvent, action);
	} else if (document.getElementById) {
		document.onEvent=action;
	}
}

function displayHelpWindow1(code){	//Chat Functionality
    /*--SALES TEMPLATE USES BUSINESS APP AND CALLROUTCODE 15. TEMPLATE NAME IS EMBARQSALES--*/
    var dt = new Date();
    var gURL="https://ecare.centurylink.com/netagent/scripts/srvgate.dll?Action=1000&RouteIdent=Embarqsales&EMAGIC=on";
    var Width = 600; //var Width = (screen.width * .4);
    var Height = 450; //var Height = (screen.height * .80);
    var winLeft = (screen.width  - Width)/2; //var winLeft = 0;
    var winTop  = 0;
    var feature = "toolbar=no,location=no,status=no,menubar=no,scrollbars=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(gURL + "&t=" + dt.getTime(),"WebInteractionHelp",feature);
} 

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 '';
}

 /* login functionality */
 
function getCookie(c) {
	var dc=document.cookie;
	return (dc.indexOf(c)!=-1)?(dc.split(c)[1].split(';')[0].charAt(0)=="=")?dc.split(c+'=')[1].split(';')[0]:dc.split(c)[1].split(';')[0]:"null";
}	

function setCookie(c_name,value) {
	document.cookie=c_name+"="+value+"; path=/";
}

function setLoginCookie(c_name,value) {
	var c_val=getCookie(c_name);
	if(c_val!=value)document.cookie=c_name+"="+value+"; path=/";
}

/* under evaluation */

// finds the x,y position of an element
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){
    var layer=document.getElementById(layerName); if(layer==null)return false;
    (layer.style.display=='none'||layer.style.display=='')?layer.style.display='block':layer.style.display='none';
}
//Business T&C fix
try{if(custClassBus()){sprint_showHide=function(div){eval('$("div[id*='+div+']").toggle()')}}}catch(e){}

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;
	}
}

function innerTabs(section,tablist,tab,box){ // 
	if(!tablist.nodeName) tablist = d.getElementById(tablist);
	s = tablist.getElementsByTagName('li');
	for (var i=0; i<s.length; i++) {
		if(s[i].id == tab){
			sprint_addClass(s[i].id,'active');
		} else {
		sprint_delClass(s[i].id,'active');
		}
	}
	if(!section.nodeName) section = d.getElementById(section);
	f = section.getElementsByTagName('div');
	for (var i=0; i<f.length; i++) {
		if(f[i].id == box){
			//alert('show '+f[i].id);
			showLayer(f[i].id);
		} else if(f[i].id != ''){
			//alert('hide '+f[i].id);
			hideLayer(f[i].id);
		}
	}
}

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;
		}
	}
}

// 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>');
	}
}


//PopUp Window

function openWindow(url) {
  popupWin = window.open(url, name, 
'scrollbars,resizable,width=360,height=400,left=20,top=20')
}

//Footer PopUp Window
function openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}