var vis=1;
var numberOfWords = 15;//for Community Forums
//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function cover(divname, faqurl, faqtitle) 
{ 

	var omtroverlay=document.getElementById('omtroverlay');
	var demoPopUp=document.getElementById(divname);
	
	if(faqurl)
	{
		document.getElementById('email_url').value = faqurl;
	}
	if(faqtitle)
	{
		document.getElementById('faq_title').value = faqtitle;
	}
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// set height of Overlay to take up whole page and show
	
	winH = (arrayPageSize[1] + 'px');
	winW = (arrayPageSize[0] + 'px');
	
	if (divname == 'demoPopUp')
	{
		adjWidth = 380;
		adjHeight = 235;
		leftpx = 200;
		toppx  = 100;
	}
	else if (divname == 'emailPopUp')
	{
		adjWidth = 200;
		adjHeight = 140;
		leftpx = 300;
		toppx  = 100;
	}
	
	if (leftpx < arrayPageSize[2]/2)
	{
		leftpx = (arrayPageSize[2]/2 - adjWidth);
	}
	if (toppx < arrayPageSize[3]/2)
	{
		toppx  = (arrayPageSize[3]/2 - adjHeight);
	}

	leftpx = leftpx + 'px';
	toppx  = toppx + 'px';

	if(vis)
	{
		vis=0;
		omtroverlay.style.display='block';   
		demoPopUp.style.display='block';
		demoPopUp.style.left=leftpx;
		demoPopUp.style.top=toppx;
		omtroverlay.style.height = winH;
		omtroverlay.style.width = winW;

	} 
	else 
	{   
		vis=1;
		omtroverlay.style.display='none'; 
		demoPopUp.style.display='none'; 
	}

}

function validate_required(field,alerttxt)
{
	var emailFilter=/^.+@.+\..{2,5}$/;
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
with (field)
  {
  if (value==null||value=="")
    {
    	alert("Enter an email address.");
    	return false;
    }
  else if ((!(emailFilter.test(value))) || (value.match(illegalChars))) 
    {
    	alert("Email is not valid.");
    	return false;
    }
  else
    {
    	return true;
    }
  }
}

function validate_form(thisform){
with (thisform)

  {
	  if (validate_required(EMAIL_FROM)==false || validate_required(EMAIL_TO)==false)
	  {
		return false;
	  }
	  else 
	  {
                cover('emailPopUp');
	  	return true;
	  }
  }

}

// modified rga_dd.js

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}

	function getElementsByClass(node,searchClass,tag) {
	var classElements = new Array();
	var els = node.getElementsByTagName(tag); // use "*" for all elements
	var elsLen = els.length;
	var pattern = new RegExp("\\b"+searchClass+"\\b");
		for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
				classElements[j] = els[i];
				j++;
			}
		}
		return classElements;
	}

	function rgadd_init() {
		var lnks = getElementsByClass(document,'rgadd','A');
		var divs = getElementsByClass(document,'rgadd','DIV');

		if (lnks.length==divs.length)
		{
			var i=0;
			for (i=0;i<=lnks.length-1;i++)
			{
				var lnk = lnks[i];
				var div = divs[i];

				lnk.custom = div;
				lnk.onclick = function() {
					rgadd_toggle(this,this.custom);
					return false;
				}

				lnk.href='javascript:void(0)';
				
				div.custom = lnk;
				div.onmouseout = function(event) {
					rgadd_hide(event,this,this.custom);
					return false;
				}

				var top = document.createElement('DIV');
				top.className='top';
				var bottom = document.createElement('DIV');
				bottom.className='bottom';
				var content = document.createElement('DIV');
				content.className='content';
				var pad = document.createElement('DIV');
				pad.className='pad';
				var org = div.innerHTML;

				div.innerHTML='';
				div.appendChild(top);
				div.appendChild(content);
				content.appendChild(pad);
				div.appendChild(bottom);
				pad.innerHTML=org;
			}
		}
png_init();

	}

	function rgadd_toggle(lnk,div) {
		var pos = findPos(lnk);
		var x = pos[0];
		var y = pos[1];
		var w = lnk.offsetWidth;
		var h = lnk.offsetHeight;
		var cls = lnk.className;

		h = h+9;
		y = y-12;

		if (cls.match('right'))
		{
			x = x-17;
		}
		else
		{
			x = x-(30-w);
		}

		if (div.style.left!=x+'px')
		{
			lnk.style.position='relative';
			lnk.style.zIndex='20';
			div.style.left=x+'px';
			div.style.top=y+'px';
			div.childNodes[1].style.paddingTop=h+'px';
		}
		else
		{
			div.style.left='-1000em';
			lnk.style.position='';
			lnk.style.zIndex='';
		}
	}

	function rgadd_hide(event,div,lnk) {
		if (!event) var event = window.event;
		var reltg = event.relatedTarget || event.toElement;
		
		if (reltg == lnk)
		{
			return;
		}

		while (reltg != div && reltg.nodeName != 'BODY' && reltg.nodeName != 'HTML')
		{
			reltg=reltg.parentNode
			if (reltg==div) return;
		}
		
		div.style.left='-1000em';
		lnk.style.position='';
		lnk.style.zIndex='';
	}


function print_url(url)
{
     location=url;
     print();
}


function toggleBox(szDivID, iState) // 1 visible, 0 hidden
{
    if(document.layers)	   //NN4+
    {
       document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.display = iState ? "block" : "none";
    }
    else if(document.all)	// IE 4
    {
        document.all[szDivID].style.display = iState ? "block" : "none";
    }
}



			function selectPhoneInit()
			{
				if (document.getElementById('console')) { document.getElementById('console').value=""; }
				setManufacturers();
			}

			function setManufacturers()
			{
				if (typeof(DL) != "undefined")
				
				{
					
					var str = "";
					var arr = DL.getParentArray("manufacturers", "name");
					var selobj =  (document.getElementById("sel_manufacturer")) ? document.getElementById("sel_manufacturer") : "";
	
					if (arr && selobj)
					{
						arr.sort();
						
						for (var i = 0; i < arr.length; i++)
						{
							
							var val = new String(arr[i]);
							str += val + ", ";
							
							if (val && val != "array") selobj.options[selobj.options.length] = new Option(val, val);
							

						}				
					}
				}
			}

			function showPhonesByManufacturer(manufacturer)
			{
				str = "";
				if (manufacturer)
				{
					manufacturer = U.unicodeEscape(manufacturer);
					var selobj =  (document.getElementById("sel_phones")) ? document.getElementById("sel_phones") : "";
					if (selobj)
					{
						selobj.options.length = 0;
						var hsh = DL.getChildArray("manufacturers", "name", manufacturer, "phones");
						var arr = new Array();						
						var sarr = new Array();
						for (var i = 0; i < hsh.length; i++)
						{
							if (hsh[i])
							{
								var name = hsh[i]["name"];
								var id = hsh[i]["id"];
								arr[id] = U.unicodeEscape(name);
								sarr[id] = name;
							}
						}


						selobj.options[selobj.options.length] = new Option("Model", "Model");
						sarr.sort();

						for (var id in sarr)
						{
							if (sarr[id]) 
							{
								var val = new String(sarr[id]);
								if (!val.match(/function/) && !val.match(/\[object Object\]/) && !val.match(/array/))
								{	
									val = U.unicodeEscape(val);
									phoneid = getPhoneIdFromName(val, arr);
									selobj.options[selobj.options.length] = new Option(val, phoneid);
								}
							}
						}				

						selobj.disabled = false;
					
					}					
				}			
			}
			
			function getPhoneIdFromName(name, idarr)
			{
				var str = "";
				
				for (var id in idarr)
				{
					if (name == idarr[id])
					{
						return id;
					}
					else
					{
						str += "trying";
					
					}
				
				}
				return name;			
			}

			function getPhoneObject(phoneid)
			{
				var arr = DL.getParentArray("manufacturers", "name");
				var selobj =  (document.getElementById("sel_manufacturer")) ? document.getElementById("sel_manufacturer") : "";
				var parr = new Array();
				var str = "";

				if (selobj)
				{
					for (var i = 0; i < arr.length; i++)
					{
						
						var val = new String(arr[i]);
						if (!val.match(/function/))
						{	
							var hsh = DL.getChildArray("manufacturers", "name", val, "phones");
							for (var j in hsh)
							{
								if (hsh[j])
								{
									var name = hsh[j]["name"];
									var id = hsh[j]["id"];
									if (id == phoneid) { return hsh[j] };
								}
							}
						}

					}				
				}
				return parr;			
			}


			function setSelectedPhoneImage()
			{
				var arr = getPhoneObject(_SELPHONEID);
				document.getElementById("selected_phone_img").innerHTML = "<img src=\"" + arr["filepathMini"] + "\">";
			
			}

			function setSelectedPhoneName()
			{
				var arr = getPhoneObject(_SELPHONEID);
				document.getElementById("selected_phone_name").innerHTML = arr["name"];
			}
			
			function chkForm(frm)
			{
				var manufacturer 	= (document.getElementById("sel_manufacturer")) ? 	document.getElementById("sel_manufacturer").value : "";
				var phoneid 			= (document.getElementById("sel_phones")) ? 	document.getElementById("sel_phones").value : "";
				if (phoneid)
				{
					_SELPHONEID = phoneid;
					frm.p.value = phoneid;
				}
				return (manufacturer && phoneid) ? true: false;
			}

			function dbg(string)
			{

				var elem = document.getElementById('console');

				if (string && elem)
				{
					elem.value+=string + "\n\n";
				
				}
			
			}


function png_init() {

	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])

	if ((version >= 5.5) && (version < 7) && (document.body.filters)) 

	{
	   for(var i=0; i<document.images.length; i++)
	   {
			  var img = document.images[i]
			  var imgName = img.src.toUpperCase()
			  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			  {
					 var imgID = (img.id) ? "id='" + img.id + "' " : ""
					 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
					 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
					 var imgStyle = "display:inline-block" + img.style.cssText 
					 if (img.align == "left") imgStyle = "float:left;" + imgStyle
					 if (img.align == "right") imgStyle = "float:right;" + imgStyle
					 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
					 var strNewHTML = "<span " + imgID + imgClass + imgTitle
					 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
					 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
					 + "(src=\'" + img.src + "\', sizingMethod='image');\"></span>" 
					 img.outerHTML = strNewHTML
					 i = i-1
			  }
	   }
	}
}



			function initCommunity()
			{

				triggerJSON("http://community.vzw.com/restapi/vc/categories/id/mobile_devices/kudos/leaderboard/top-messages?xslt=json.xsl&max_age=one_week&page_size=1&restapi.response_style=view&restapi.format_detail=full_list_element","ACTIVECAST-FT1");
				triggerJSON("http://community.vzw.com/restapi/vc/categories/id/Wireless_Plans/kudos/leaderboard/top-messages?xslt=json.xsl&max_age=one_week&page_size=1&restapi.response_style=view&restapi.format_detail=full_list_element","ACTIVECAST-FT2");
			}

			function triggerJSON(url, contentid)
			{
				var request = new Json.Remote(url, {
						onComplete: function(jsonObj) {
							dataobj = jsonObj.response;
							initializeData(dataobj, contentid);
						}
					}).send();
			}

			function initializeData(jsonObj, contentid)
			{
	
				var msgdata = getKudoedMessage(jsonObj);
					
					_MSG = formatMessage(msgdata);
					
					if (document.getElementById(contentid))
					{
						document.getElementById(contentid).innerHTML = _MSG;
					}
			}


			function formatMessage(msgdata)
			{
					var str = "<p><a href=\"" + msgdata["href"] +  "\" target=\"self\">" + msgdata["subject"] + "<\/a><br \/>";  
					str += msgdata["body"] + "<\p>";  
					return str;
			}

			function getKudoedMessage(dataobj)
			{
				var msg = dataobj["kudoedmessages"]["kudoedmessage"]["message"];
				var data = new Array();

				if (msg)
				{
					data["body"] = msg["body"]["$"];
					data["subject"] = msg["subject"]["$"];
					data["href"] = msg["view_href"]["$"];
					data["body"] = removeHTML(data["body"]);
					data["body"] = truncateBodyText(data["body"], numberOfWords);
				}

				return data;

			}

			function removeHTML(string)
			{
				var newLineToWhiteSpaceRegExp=/<([^<\b\r>]*)>/g;
				newLineToWhiteSpace=string.replace(newLineToWhiteSpaceRegExp,"");
				newLineToWhiteSpace=newLineToWhiteSpace.replace(/\n/," ");
				newLineToWhiteSpace=newLineToWhiteSpace.replace(/\r/," ");
				//newLineToWhiteSpace=newLineToWhiteSpace.replace(/\&#192;/is,"BBB");
				return newLineToWhiteSpace.replace(/<([^<>]*)>/g,"");
			}

			function truncateBodyText(string,numberOfWords)
			{
				var truncatedString="";
				if(typeof string!='undefined'&&string!=null)
				{
					if(numberOfWords>-1)
					{
						string=string.toString();
						splits=string.split(" ",numberOfWords);
						truncatedString=splits.join(" ");
						truncatedString += "...";
					}
					else
					{
						truncatedString=string;
					}
				}
				return truncatedString;
			}


function trim(str){
	return str.replace(/^\s+|\s+$/g, '') ;
}

function search_go(frm){
	var strTerm=trim(frm.q.value);
	var defaultQ="Example: How do I download VZ Navigator?";
	if(strTerm=="" || strTerm==defaultQ){
		alert("Please enter a search phrase or keyword.");
		frm.q.focus();
		return false;
	}
        _hbLink("search_go");
	return true;
}
