/* -----------------------------------------------
Title: Common script
Author: Jarid Love
Modified: 31 Mar 2006
----------------------------------------------- */

// set path variables
isSSL = (location.protocol == "https:")?true:false;
isStage = (location.href.indexOf("stage") > 0) ? true : false;
isTest = (location.href.indexOf("test.quickcircu.com") > 0) ? true : false;
function getSecureUrl() {
	return (isTest)?"/":(isStage)?"/":"/";
}

s = (isTest)?"/":(isStage)?"/":"/";
ss = (isSSL)? getSecureUrl() : s;
n_domain = location.host; 
n_dirs = location.pathname.split("/");
n_root = (n_dirs.length>2)?n_dirs[1]:null;

// environment version detection variables
isDOM = (document.getElementById)?true:false; // all DOM based browsers including IE
isNS4 = (document.layers)?true:false;
isIE = (document.all)?true:false;
isMac = (navigator.appVersion.indexOf("Mac") != -1)?true:false;
isWin = (navigator.appVersion.indexOf("Win") != -1)?true:false;
usragent = navigator.userAgent.toLowerCase();
isOperaBadDom = (usragent.substr(usragent.indexOf("opera")+6,1)<=6)?true:false; // this detects opera 6 or below which have crappy DOM support
isOperaGoodDom = (usragent.substr(usragent.indexOf("opera")+6,1)>6)?true:false;
isKonqueror = (usragent.indexOf("konqueror")>=0)?true:false;
langCompatible = (isDOM && n_domain.indexOf("quickcircu.com") >= 0);

// script for localization
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 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 "iso-8859-1";
}

n_lang = readCookie("language");
n_country = readCookie("country");
n_enc = getPageCharset();

// language redirection
function siteSupportsLang() {
	return false;
}

function isLangDir(dir) {
	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"];
	for(var i = langDirs.length-1; i > -1; i--) if(langDirs[i] == dir) return true;
	return false;
}

function isFilteredDir(dir) {
	switch(dir) {
		case "cgi-bin" :
		case "home" :
		case "servlet" :
		case "global" :
		case "ICSLogin" :
		case "ICHAINLogout" :
			return true;
		default :
			return false;
	}
}

function setLang(lang) {
	var durration = 31536000000; // (365 days * 24 hours * 60 min * 60 s * 1000 ms)
	document.cookie = "language="+lang+";expires="+(new Date(new Date().getTime()+durration)).toGMTString()+";path=/;domain=.quickcircu.com";
}


// include menu generating script if compatible and layout css
if(isDOM) { 
	if(window.name == "printer_friendly") { document.write('<link rel="stylesheet" type="text/css" href="/common/inc/printerfriendly.css" title="Printer Friendly" />'); var s_prop15=location.href; }
	document.write('<scr'+'ipt type="text/javascript" src="/common/inc/nav/main.js"><\/scr'+'ipt>');
	var hasHdrNav = true; 
}

function showPrinterFriendly() {
	if(document.getElementById("contentcontainer")) {
		var pfwindow = window.open(location.href,"printer_friendly");
		pfwindow.focus();
	}
	else 
	  return false; 
}

// include text for header/footer
function loadHdrTransltion(suffix) {
	if(suffix == 'en-us' || n_lang == null) suffix = "";
	else suffix = '_' + suffix;
	document.write('<scr'+'ipt type="text/javascript" src="/common/inc/local/hdr_txt'+suffix+'.js"><\/scr'+'ipt>');
}
if(n_domain.indexOf("quickcircu.de") >= 0) loadHdrTransltion("de-de");
else if(n_domain.indexOf("quickcircu.co.jp") >= 0) loadHdrTransltion("ja-jp");
else loadHdrTransltion((typeof navlang!="undefined") ? navlang : n_lang);

// random int method
function getRandomInt(min, max) {
	return (min >= max) ? max : Math.floor(((max+1 - min)*Math.random())+min);
}

// fn to open a new window when linking to an external site
function openExternal(URL, w, h, s, n, r) {	// 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);
	var winExt = (!n) ? window.open(URL,'_blank',attr) : window.open(URL,n,attr);
	if(winExt && winExt.blur) winExt.focus();
}

// temp function for home page
function changeBackground(n){ 
	document.body.style.backgroundImage = "url(/img/hm/"+n+")";
	document.getElementById("style").style.background = "none";
	document.getElementById("hdr").style.background = "none";
	document.getElementById("hdr-lnks1-home").style.backgroundColor = "transparent";
	document.getElementById("hdr-lnks2-login").style.backgroundColor = "transparent";
	document.getElementById("hdr-lnks2-login").getElementsByTagName("a")[0].style.backgroundColor = "transparent";
	document.getElementById("contentcontainer").style.backgroundColor = "transparent";
}
