/* -----------------------------------------------
Title: Common script (used for non novell.com sites in template)
Author: Jarid Love
Modified: 30 Aug 2010 by Jarid Love
Comment: Added RAC popup
----------------------------------------------- */

/* Environment Variables
---------------------------------------------------- */
var isExternalHdr = true;
var isSSL = (location.protocol == "https:")?true:false;
var isStage = (location.href.indexOf("stage") > 0 || location.href.indexOf("usability") > 0) ? true : false;
var isTest = (location.href.indexOf("wwwtest.provo.novell.com") > 0  || location.href.indexOf("wwwqatest.provo.novell.com") > 0) ? true : false;
var isDev = (location.href.indexOf("wwwdev.provo.novell.com") > 0) ? true : false;
var allowRedirect = (location.href.indexOf("forceRedirect=false") > 0) ? false: true;

function getSecureUrl() {
	return (isDev)?"https://secure-wwwdev.provo.novell.com":(isTest)?"https://secure-wwwtest.provo.novell.com":(isStage)?((location.href.indexOf("usability") > 0)?"http://usabilitytest.novell.com":"https://secure-wwwstage.provo.novell.com"):"https://secure-www.novell.com";
}

var s = (isDev)?"http://wwwdev.provo.novell.com":(isTest)?"http://wwwtest.provo.novell.com":(isStage)?((location.href.indexOf("usability") > 0)?"http://usabilitytest.novell.com":"http://wwwstage.provo.novell.com"):"http://www.novell.com";
var n_server = s;
var ss = (isSSL)? getSecureUrl() : s;
var n_domain = location.host; 
var n_dirs = location.pathname.split("/");
var n_root = (n_dirs.length>=2)?n_dirs[1]:null;
var n_root2 = (n_dirs.length>=3)?n_dirs[2]:null;
var n_root3 = (n_dirs.length>=4)?n_dirs[3]:null;
var n_root4 = (n_dirs.length>=5)?n_dirs[4]:null;

var isDOM = document.getElementById?true:false; // all DOM based browsers including IE
var isNS4 = document.layers?true:false;
var isIE = document.all?true:false;
var isMac = (navigator.appVersion.indexOf("Mac") != -1)?true:false;
var isWin = (navigator.appVersion.indexOf("Win") != -1)?true:false;
var isLinux = (navigator.platform.indexOf("Linux") != -1)?true:false;
var usragent = navigator.userAgent.toLowerCase();
var isOperaBadDom = (usragent.substr(usragent.indexOf("opera")+6,1)<=6)?true:false; // this detects opera 6 or below which have crappy DOM support
var isOperaGoodDom = (usragent.substr(usragent.indexOf("opera")+6,1)>6)?true:false;
var isKonqueror = (usragent.indexOf("konqueror")>=0)?true:false;
var isPlone = (typeof is_plone != 'undefined') ? is_plone : false;
var is404Page = (typeof is404Page != "undefined") ? is404Page : false;

/* Cookie Functions
---------------------------------------------------- */
function readCookie(name) {
	var cks = document.cookie.split(';');
	for(var i=cks.length-1; i >= 0; i--) {
		var c = cks[i];
		while(c.charAt(0)===' ') {
			c = c.substring(1,c.length);
		}
		if(c.indexOf(name+"=")===0) {
			return c.substring(name.length+1,c.length);
		}
	}
	return null;
}

function deleteCookie(name,path,domain) {
	if(readCookie(name)) { document.cookie = name + "=" +
		((path) ? ";path=" + path : ";path=/") +
		((domain) ? ";domain=" + domain : ";domain=.novell.com") +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

function createCookie(name,value,days,path,domain) {
	var expires = "";
	if(days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	document.cookie = name+"="+value+expires +
		((path) ? ";path=" + path : ";path=/") +
		((domain) ? ";domain=" + domain : ";domain=.novell.com");
}

/* Localization Functions
---------------------------------------------------- */
function getPageCharset() {
	var mt = document.getElementsByTagName('meta');
	for(var i=0; i<mt.length; i++) {
		var ct = mt[i].getAttribute('content');
		if(ct !== null && ct.toLowerCase().indexOf('charset=') != -1) {
			return ct.substring(ct.toLowerCase().indexOf('charset=')+8);
		}
	}
	return "UTF-8";
}

var n_lang = readCookie("novell_language");
var n_country = readCookie("novell_country");
var n_cnt=(n_country!==null && n_country.length>3)?n_country.substring(0,2):"US";
var n_cntName= (n_country!==null && n_country.length>3)?n_country.substring(3):"United States";
var n_enc = getPageCharset();

function siteSupportsLang() {
	if(isDOM) {
		var n_lang_sites = ["supportstage.provo.novell.com","support.novell.com","downloadstage.provo.novell.com","download.novell.com",s.substring(7,s.length),ss.substring(8,ss.length)]; // sites that support localization
		for(var i = n_lang_sites.length-1;i > -1; i--) {
			if(n_domain == n_lang_sites[i]) {
				return true;
			}
		}
	}
	return false;
}
var isLangCompatible = siteSupportsLang();
var langCompatible = isLangCompatible; // for legacy support

function isNonEnglishDir(dir) {
	if(dir === null) {
		return false;
	}
	var langDirs = ["de-de","es-es","fr-fr","it-it","nl-nl","cs-cz","zh-cn","zh-tw","ja-jp","hu-hu","pl-pl","pt-br","ru-ru","sv-se","fi-fi","da-dk","no-no","tr-tr"];
	for(var i = langDirs.length-1; i > -1; i--) {
		if(langDirs[i] == dir) {
			return true;
		}
	}
	return false;
}

function isCountryDir(dir) {
	if(dir === null) {
		return false;
	}
	var coDirRegex = new RegExp("^[A-Z][A-Z]$");
	if(coDirRegex.test(dir)) {
		return true;
	}
	return false;
}

function isFilteredRootDir(dir) {
	switch(dir) {
		case "communities" :
		case "rc" :
		case "cgi-bin" :
		case "servlet" :
		case "global" :
		case "ICSLogin" :
		case "ICHAINLogout" :
			return true;
		default :
			return false;
	}
}

function setLang(lang) {
	createCookie("novell_language",lang,365);
	n_lang = lang;
}

function setCountry(co,coname) {
	createCookie("novell_country",co+"|"+coname,(co === "") ? 0 : 365);
	n_cnt = co;
	n_cntName = coname;
	n_country = readCookie("novell_country");
}

function setDefaultCountryForLang(lang) {
	switch(lang) {
		case "it-it" : 
			setCountry("IT","Italia");
			break;
		case "ja-jp" : 
			setCountry("JP","Japan");
			break;
		case "zh-cn" : 
			setCountry("CN","&#20013;&#22269;");
			break;
		case "sv-se" : 
			setCountry("SE","Sverige");
			break;
		case "pl-pl" : 
			setCountry("PL","Polska");
			break;
		case "no-no" : 
			setCountry("NO","Norge");
			break;
		case "cs-cz" : 
			setCountry("CZ","&#268;esk&#225; republika");
			break;
		case "hu-hu" : 
			setCountry("HU","Magyarorsz&#225;g");
			break;
		case "fi-fi" :
			setCountry("FI","Suomi");
			break;
		case "ru-ru" :
			setCountry("RU","Rossiya");
			break;
		case "tr-tr" :
			setCountry("TR", "Turkey");
			break;
	}
}

function getNovellTranslation(arry) {
	var txt = arry[n_lang];
	if(!txt) {
		txt = arry["en-us"];
	}
	return txt;
}

/* Localization Redirect
---------------------------------------------------- */
// we're on external page, so don't do redirect
if(n_lang===null) {
	n_lang = "en-us";
}

/* Include menu script, css, and local phone number
---------------------------------------------------- */
document.write('<script type="text/javascript" src="'+ss+'/common/util/geophone.php?co='+n_cnt+'"><\/script>');

if(n_lang == "ja-jp"){
	document.write('<link rel="stylesheet" type="text/css" href="'+ss+'/common/inc/css/fonts_ja.css" />');
}

if(typeof $ == 'undefined') {
	// only include jquery if page doesn't use prototype
	document.write('<script type="text/javascript" src="'+ss+'/common/inc/jquery/jquery.min.js"></script>');
	document.write('<script type="text/javascript" src="'+ss+'/common/inc/jquery/plugins/novell.common.js"></script>');
	document.write('<script type="text/javascript" src="'+ss+'/common/inc/jquery/plugins/scroll/jScrollPane.js"></script><link rel="stylesheet" href="'+ss+'/common/inc/jquery/plugins/scroll/jScrollPane.css" type="text/css" media="screen" />');
	document.write('<script type="text/javascript" src="'+ss+'/common/inc/jquery/plugins/scroll/jquery.mousewheel.js"></script>');
	document.write('<script type="text/javascript" src="'+ss+'/common/inc/jquery/plugins/jquery.prettyPhoto.js"></script><link rel="stylesheet" href="'+ss+'/common/inc/css/jquery.prettyPhoto.css" type="text/css" media="all" />');
}

document.write('<link rel="stylesheet" href="'+ss+'/common/inc/css/main_2010.css?pfat=sturgis16" type="text/css" media="all" />');
document.write('<link rel="stylesheet" type="text/css" href="'+ss+'/common/inc/css/tier4_2011.css" type="text/css" media="all" />');

if(isDOM) { 
	if(window.name == "printer_friendly") {
		document.write('<link rel="stylesheet" type="text/css" href="'+ss+'/common/inc/printerfriendly.css" title="Printer Friendly" />');
		var s_prop15=location.href;
	}
	document.write('<script type="text/javascript" src="'+ss+'/common/inc/nav/main.js"><\/script>');
	var hasHdrNav = true; 
}

function showPrinterFriendly() {
	if(document.getElementById("contentcontainer") || document.getElementById("pageContent") || (typeof jQuery != 'undefined' && ($('.pageContainer') || $('.contentContainer')))) {
		var pfwindow = window.open(location.href,"printer_friendly");
		pfwindow.focus();
	}
}

/* login/out functions
---------------------------------------------------- */
function setLoginStatus() {
	var cks = document.cookie.split(';');
	for(var i=0; i < cks.length; i++) {
		var c = cks[i];
		while(c.charAt(0)===' ') {
			c = c.substring(1,c.length);
		}
		if(c.indexOf("IPC")===0 && c.indexOf("=")==16) {
			document.write('<script type="text/javascript" src="'+ss+'/inc/util/secure/userInfo.php"><\/script>');
		}
	}
}
var isLoggedIn = false;
setLoginStatus();

/* Misc get functions
---------------------------------------------------- */
function getQueryParm(name) {
	var params = document.params;
	if(!params) {
		document.params = (location.search.substring(1)).split("&");
		params = document.params;
	}
	for(var i = 0; i < params.length; i++) {
		var param = params[i].split("=");
		if(param[0] == name) {
			return param[1];
		}
	}
	return null;
}

function getRandomInt(min, max) {
	return (min >= max) ? max : Math.floor(((max+1 - min)*Math.random())+min);
}

function getItemWidth(obj) {
	var width = obj.clientWidth;
	return (width === 0) ? obj.offsetWidth : width;
}

function getItemHeight(obj) {
	var height = obj.clientHeight;
	return (height === 0) ? obj.offsetHeight : height;
}

function getXY(obj) {
	var intX = 0;
	var intY = 0;
	try {
		do { // Walk up our document tree until we find the body and add the distance from the parent to our counter.
			intX += obj.offsetLeft;
			intY += obj.offsetTop;
			obj = obj.offsetParent;
		} while(obj.tagName != "BODY" && obj.tagName != "HTML");
	}
	finally {
		return [intX,intY];
	}
}

/* RAC
---------------------------------------------------- */
var inPageRAC = false;
function closeRAC() {
	if(inPageRAC) {
		$("#requestcall").remove().appendTo("#requestcallplaceholder").unwrap().removeClass("overlay");
		$("#racoverlaycontainer").remove();
		hideRACCanopy();
	}
	else {
		$("#racpopup").fadeOut("fast", function() {
			hideRACCanopy();
		});
	}
}

function displayRACPopup() {
	$("#racpopup").css({  
		"display": "block",
		"visibility": "hidden"
	});
	$("#racpopup").css({  
		"top": Math.max(($(window).height()-$("#racpopup").height())/2+$(window).scrollTop(),10),  
		"left": Math.max(($(window).width()-$("#racpopup").width())/2+$(window).scrollLeft(),10)
	});
	$("#racpopup").css({  
		"display": "none",
		"visibility": "visible"
	});
	$("#racpopup").fadeIn("fast");
	$("#raccanopy img").hide();
}

function addRACCanopy() {
	if($("#raccanopy").size() == 0) {
		$("body").append('<div id="raccanopy"><img src="'+n_server+'/common/img/racloader.gif" /></div>');
		$("#raccanopy").css("opacity","0.8").click(function() {
			closeRAC();
		});
	}
}

function showRACCanopy() {
	$("#raccanopy img").show();
	hidePlugins(true)
	$("#raccanopy").fadeIn("fast");
	if(isIE && parseInt($.browser.version) < 7) {
		$("#raccanopy").css("position", "absolute").width($(window).width()).height($(document).height());
	}
}

function hideRACCanopy() {
	$("#raccanopy").fadeOut("fast");
	showPlugins(true);
}

function addRACOverlay() {
	addRACCanopy();
	if($("#racpopup").size() == 0) {
		$("body").append('<div id="racpopup"><div id="racpopup_content"></div><div id="racpopup_ftr"><p><a href="#" onclick="closeRAC();return false;"><strong>Close</strong></a></p></div></div>');
	}
}

function highlightInpageRAC() {
	if(isIE && parseInt($.browser.version) < 9) {
		$("#requestcall").css("position","relative");
		$("#requestcall").queue("fx");
		$("#requestcall").animate({left:'+=10'},50);
		$("#requestcall").animate({left:'-=20'},100);
		$("#requestcall").animate({left:'+=20'},100);
		$("#requestcall").animate({left:'-=20'},100);
		$("#requestcall").animate({left:'+=10'},50);
	}
	else {
		$('#requestcall').animate({shadow: '0 0 25px #000'});
	}
}

function requestCall(url) {
	if($("#racpopup").size() > 0) {
		showRACCanopy();
		displayRACPopup();
	}
	else if($("#requestcall").size() > 0) {
		// rac form in page
		inPageRAC = true;
		$.scrollTo("#requestcall",500,{ onAfter:highlightInpageRAC, offset: {top:-20, left:0}, axis:'y' });
	}
	else {
		// rac form not in page so show overlay
		inPageRAC = false;
		addRACOverlay();
		showRACCanopy();
		$.get(n_server+"/"+n_lang+"/common/inc/requestcall_smallform.html", function(data){
			$("#racpopup_content").html(data);
			displayRACPopup();
		},"html");
	}
}

function switchToNotice(noticeUrl) {
	if($("#racpopup").size() > 0) { // form is in popup
		$("#raccanopy img").show();
		$("#racpopup").fadeOut("fast", function() {
			$("#requestcall").css({ 
				"visibility": "hidden",
				"height": "1px",
				"width" : "1px",
				"position" : "absolute"
			});
			$("#racpopup").css("visibility","hidden");
	
			$.get(noticeUrl, function(data){
				$("#racpopup_content").append('<div class="home_tip">'+data+'</div>');
				displayRACPopup();
			},"html");
		});
	}
	else { // form is in page
		addRACOverlay();
		showRACCanopy();
		$.get(noticeUrl, function(data){
			$("#racpopup_content").html('<div class="home_tip">'+data+'</div>');
			displayRACPopup();
		},"html");
	}
	return false;
}

function showRACHome() {
	switchToNotice(n_server+"/"+n_lang+"/common/inc/requestcall_smallform_home_content.html");
	return false;
}

function showRACTech() {
	switchToNotice(n_server+"/"+n_lang+"/common/inc/requestcall_smallform_tech_content.html");
	return false;
}

function homeClick() {
	document.getElementById('salescallrequest').submit();
}

/* Popup functions
---------------------------------------------------- */
function openExternal(URL, w, h, s, n, r) { // open link in new window (for external links)
	// It is possible to bypass w and h by passing in 'noW', 'noH'
	var attr = "";
	if(typeof w != "undefined" && w!= 'noW') {
		attr += "width=" + w;
	}
	if(typeof h != "undefined" && h != 'noH') {
		attr += ",height=" + h;
	}
	if(attr.length > 0) { // when no width or height is specified, use window core defaults, do not add any other values
		attr += (typeof s != "undefined" && s === true) ? ',scrollbars=yes' : ',scrollbars=no'; // default on sized window = no scrollbars
		attr += (typeof r != "undefined" && r === false) ? ',resizable=no' : ',resizable=yes'; // default on sized window = resizable
	}
	if(attr.charAt(0)==",") {
		attr = attr.substring(1);
	}
	if(URL.indexOf("play.php") != -1){
		assetClick(URL, false);
	}
	var winExt = (!n) ? window.open(URL,'_blank',attr) : window.open(URL,n,attr);
	if(winExt && winExt.blur) {
		winExt.focus();
	}
}

/* Functions to hide/show plugins (ie. flash)
---------------------------------------------------- */
function hideFlash(hideall) {
	$("object, embed").each(function(){
		var y = $(this).offset().top;
		if ((y > 0 && y < 500) || (typeof hideall != 'undefined' && hideall == 'all')) {
			$(this).css("visibility", "hidden");
		}
	});
}

function hidePlugins(hideall) {
	// hide select boxes in IE versions > 7 because they show through menus
	var agt=navigator.userAgent.toLowerCase();
	if(isIE && parseInt($.browser.version) < 7) {
		$("select").css("visibility","hidden");
	}
	// hide flash content because it shows through navigation
	if(typeof flash != "undefined") {
		// if flash.forcehide, hide property has been manually set
		// if linux, wmode not supported, so hide flash
		// if flash version is less than 6,0,79,0 hide flash because wmode not supported widely
		if(flash.forcehide || isLinux || (flash.version <= 6 && flash.rev < 79) || isOperaGoodDom) {
			hideFlash(hideall);
		}
	}
	if(typeof SWFObject != "undefined") {
		var swfObj = new SWFObject();
		if((swfObj.installedVer.major < 6 && swfObj.installedVer.rev < 79) || isLinux || isOperaGoodDom){
			hideFlash(hideall);
		}
	}
	if(typeof frames != "undefined") {
		$("iframe[src*=http://www.novell.com/huddle/event/index.php]").each(function(){
			var getHeight = $(this).height();
			$("#videoembed").height(getHeight);
			$("#videoembed").css({"background-color":"#333"});
			$(this).css({"display":"none"});
		});
	}
	hiddenElmnts = true;
}

function showFlash(hideall) {
	$("object, embed").each(function(){
		var y = $(this).offset().top;
		if ((y > 0 && y < 500) || (typeof hideall != 'undefined' && hideall == 'all')) {
			$(this).css("visibility", "visible");
		}
	});
}

function showPlugins(hideall) {
	// show select boxes in IE versions > 7 that were hidden because they show through menus
	var agt=navigator.userAgent.toLowerCase();
	if(isIE && parseInt($.browser.version) < 7) {
		$("select").css("visibility","inherit");
	}
	// show flash content that was hidden because it shows through navigation
	if(typeof flash != "undefined") {
		if(flash.forcehide || isLinux || (flash.version <= 6 && flash.rev < 79) || isOperaGoodDom) {
			showFlash(hideall);
		}
	}
	if(typeof SWFObject != "undefined") {
		var swfObj = new SWFObject();
		if((swfObj.installedVer.major < 6 && swfObj.installedVer.rev < 79) || isLinux || isOperaGoodDom){
			showFlash(hideall);
		}
	}
	if(typeof frames != "undefined") {
		$("iframe[src*=http://www.novell.com/huddle/event/index.php]").each(function(){
			$("#videoembed").css({"background":"transparent"});
			$(this).css({"display":"block"});
		});
	}
	hiddenElmnts = false;
}