////////////////////////////////////////////////////
// qmCore.js
// All Javascript functions used common in all pages
// Copyright QuestionMark Computing 2006 - 2008
// Build No		: 5.2.10.28
//

var isDeliveryPage = false;

// Call this to establish whether the viewing browswer is SB
function SB_IsPresent()
{
   return ((window.external != null) && (window.external.SB_GetVersion != null));
}

var bSB = SB_IsPresent();

// Call this to establish the version of the viewing SB
function SB_GetVersion()
{
   if (bSB) {
      return window.external.SB_GetVersion();
   } else {
      return "";
   }
}

var bQS = (SB_GetVersion().indexOf("Perception Secure Browser") == -1);

/***** upto this moved from SBAPI *******/


var bIsIE=((navigator.userAgent.indexOf('MSIE')!=-1)&&!(navigator.userAgent.indexOf('Opera')!=-1));// IE check
// also, another try is:  (navigator.appName == "Microsoft Internet Explorer" && window.ActiveXObject);

// To open popup window
function OpenWindow(sURL, sName, sFeatures) {
	if (bSB) {
		var sParams = "href=" + sURL + " " + sFeatures;
		SB_PopUp(sParams);
	} else {
		window.open(sURL, sName, sFeatures).focus();
	}
}

// replace the current document with a defined URL and replace the document in the browser history
function replaceURL(sURL){
	document.location.replace(sURL);
}

/* The below functionality is used to hide back button in certain pages
   when accessed via secure browser. Example: in ChooseLang & TestCenter page  */
function controlBackButton() {
	var objBack = document.getElementById('bSB_HideButton');
	if (bSB && objBack) {
		objBack.style.display = "none";
	} 
}


/*  The below functions are to hanndle shortcut keys from being used upon conditions.
    ALT and CTRL whether used individually or in combination with other keys. And allowing Backspace only in areas 
	like text input, textarea etc to delete typing contents. Otherwise block it out  */
	
var sShortCutDisabledWarningText;
var sAltDisabledWarningText;
var sCtrlDisabledWarningText;
var element;
// default conditions
var sCtrl = false; 
var sAlt = false;
var sBackspace = false;


function handleDownKeys(e) {

    var char;
	char = (e) ? e.which : window.event.keyCode;
	evt = (bIsIE) ? window.event : e;

	if (!e) {
		var wEvt = window.event;
		element = wEvt.srcElement;
	} else if(e.target) {
		 element=e.target;
	}	
	if(element.nodeType==3) element=element.parentNode; // safari
	
	if (char == 18) {
		sAlt = true;
	}
	
	if(char == 8 && !(element.tagName == 'TEXTAREA'  || element.tagName == 'INPUT')) {
		sBackspace  = true;
	} 
	
	if((element.tagName == 'TEXTAREA'  || element.tagName == 'INPUT') && (char == 67 || char == 65  || char == 86) && evt.ctrlKey == true) {
		sCtrl = false;
	} else if (char != 17 && evt.ctrlKey == true) {
		sCtrl = true;
	}
	
	
	if (sAlt == true || sCtrl == true || sBackspace == true) {

		//alert('in' + sAlt + "_"+ sCtrl + "_"+ sBackspace); 		
		if (navigator.userAgent.indexOf('Firefox')  == -1) { // For non-FF browsers, alert reqd for both Alt and Ctrl conditions
			if(sAlt) alert(sAltDisabledWarningText);
			else if (sCtrl) alert(sCtrlDisabledWarningText);
		}
	
		if(e) { 
			if (e.cancelBubble) e.cancelBubble = true;
			if (e.returnValue) e.returnValue = false;
			
			if (e.stopPropagation) {	// Firefox.		
				e.stopPropagation();
				e.preventDefault();	
				if (navigator.userAgent.indexOf('Firefox')  >= 0) {  
					if(sAlt) alert(sAltDisabledWarningText);// For Firefox, alert only for Alt key
				}								
			}
		} else {			
				if ((window.event.keyCode != 17) && window.event.keyCode != 16) { // to avoid IE error, reason unknown
					window.event.keyCode = 0;
				}
				window.event.cancelBubble = true;
				window.event.returnValue = false;					
		} 
		
		sAlt = false; sBackspace = false; sCtrl = false; /* Values are reset to false, though it is not an ideal solution and have drawbacks */
	}
	return false;		
}

/* end of shortcut key handling */


/* code for style control */
var selectedFontStyle=getCookie("qmFontStyle");
var selectedContrastStyle=getCookie("qmContrastStyle");

if (selectedFontStyle!=null) { //load user chosen font style sheet from cookie if there is one stored	
	setFontStyle(selectedFontStyle);
}
else {
	setFontStyle('none');
}

if (selectedContrastStyle!=null) { //load user chosen contrast style sheet from cookie if there is one stored	
	setContrastStyle(selectedContrastStyle);
}
else {
	setContrastStyle('none');
}

function getCookie(Name) { 
	var re=new RegExp(Name+"=[^;]+", "i"); //construct RE to search for target name/value pair
	if (document.cookie.match(re)) { //if cookie found
		return document.cookie.match(re)[0].split("=")[1] //return its value
	}
	return null
}

// to set the approriate font style
function setFontStyle(title) {

	var i, cacheFontObj;		
	for(i=0; (cacheFontObj=document.getElementsByTagName("link")[i]); i++) {
		if(cacheFontObj.getAttribute("name").toLowerCase()=="font_css" && cacheFontObj.getAttribute("title")) { //if this is an alternate stylesheet with title	
			cacheFontObj.disabled = true;
			if(cacheFontObj.getAttribute("title") == title) { //enable alternate stylesheet with title that matches parameter
				cacheFontObj.disabled = false //enable chosen style sheet
			}
		}
	}
} 

// to set approriate contrast style
function setContrastStyle(title) {
	var i, cacheContrastObj;		
	for(i=0; cacheContrastObj=document.getElementsByTagName("link")[i]; i++) {
		if(cacheContrastObj.getAttribute("name").toLowerCase()=="contrast_css" && cacheContrastObj.getAttribute("title")) { //if this is an alternate stylesheet with title	
			cacheContrastObj.disabled = true;
			if(cacheContrastObj.getAttribute("title") == title) { //enable alternate stylesheet with title that matches parameter
				cacheContrastObj.disabled = false //enable chosen style sheet
			}
		}
	}
}

var fontStyleSheets = [];
fontStyleSheets[0] = 'none'; // default with no fontsize style applied
var contrastStyleSheets = []; 
contrastStyleSheets[0] = 'none';// default with no contrast style applied
 
function chooseFontStyle(styletitle){ //Interface function to switch fontsize style sheets 
		
	var i, cacheFontObj; 
	var j = 1;
	for(i=0; cacheFontObj=document.getElementsByTagName("link")[i]; i++) {
		if(cacheFontObj.getAttribute("name").toLowerCase()=="font_css" && cacheFontObj.getAttribute("title")) {
			fontStyleSheets[j++] = cacheFontObj.getAttribute("title");
		}		
	}
	var nStyle = 0;                     // referencing array number. Set to default.
	var existingFontStyle = 'none';
	
	// Use recorded font style if stored
	var storedFontStyle = getCookie("qmFontStyle");
	if (storedFontStyle != null) {
        existingFontStyle = storedFontStyle;
	}
	
	for(z=0; z<fontStyleSheets.length; z++) {		
		if(fontStyleSheets[z] == existingFontStyle && z != fontStyleSheets.length - 1) {
			nStyle = z + 1;					
			break;
		}
	}
	styletitle = fontStyleSheets[nStyle];				
	setFontStyle(styletitle);
	
	if(isDeliveryPage && isQxQPage && bAutoResize > 1) {
		setOriginalText(); // To re-calculate button widths with new size
		setAllowedScreenWidths();
		autoResize();
	}
	var expireDate = new Date();
	var expstring = expireDate.setDate(expireDate.getDate() + parseInt(20));
	document.cookie = "qmFontStyle="+styletitle+"; expires="+expireDate.toGMTString()+"; path=/";
		
}

function chooseContrastStyle(styletitle){ //Interface function to switch contrast style sheets 

	var i, cacheContrastObj; 
	var j = 1;
	for(i=0; cacheContrastObj=document.getElementsByTagName("link")[i]; i++) {
		if(cacheContrastObj.getAttribute("name").toLowerCase()=="contrast_css" && cacheContrastObj.getAttribute("title")) {
			contrastStyleSheets[j++] = cacheContrastObj.getAttribute("title");
		}		
	}
	var nStyle = 0;                         // referencing array number. Set to default.
	var existingContrastStyle = 'none';
	
	var storedContrastStyle = getCookie("qmContrastStyle");
	if (storedContrastStyle != null) {
	    existingContrastStyle = storedContrastStyle;
	}
	
	for(z=0; z<contrastStyleSheets.length; z++) {		
		if(contrastStyleSheets[z] == existingContrastStyle && z != contrastStyleSheets.length - 1) {
			nStyle = z + 1;					
			break;
		}
	}
	styletitle = contrastStyleSheets[nStyle];
	setContrastStyle(styletitle);				
	var expireDate = new Date();
	var expstring= expireDate.setDate(expireDate.getDate()+parseInt(20));
	document.cookie = "qmContrastStyle="+styletitle+"; expires="+expireDate.toGMTString()+"; path=/";
}

/* end of style control code*/
	
// to set focus to certain element for better accessibility
function SetFocus(elementID)
{
    var eFocus = document.getElementById(elementID);
    if (eFocus != null) {
        eFocus.focus();
	}
}

