
	/**
	* @package CommonF
	* contains common functions and constants
	**/
	
	var CommonF = {};
	
	/***************************** global variables *********************************************/
	
	CommonF.loadMask;
	CommonF.Countries = [];
	
	/***************************** global constants *********************************************/
	
	CommonF.myGameStyle = 0;
	CommonF.myLanguage = "sk";
	CommonF.minContentWidth = 848;
	CommonF.minContentHeight = 497;
	
	CommonF.FIDE_CARD_PLAYER_IMG_PREFIX = 'http://ratings.fide.com/card.php?code=';
	
	/***************************** global functions *********************************************/
	
	CommonF.getFullImagePath = function(p_imagePath){
		if (!p_imagePath) return '';
		return window.imageServerStorageUrl + p_imagePath;
	}
	
	CommonF.callForum = function(p_param){
		window.open(this.getForumLink(p_param), '_blank');
	}
	
	CommonF.getForumLink = function(p_param){
		if (!p_param) p_param = '';
		return (window.FORUM_LINK_PREFIX + p_param);
	}
	
	CommonF.applyCorrections = function(){
		this.createExtCorrectedTabPanel();
	}
	
	CommonF.isFlash = function(){
		if (this.isFlash == null){
			var flashPlayerVersion = swfobject.getFlashPlayerVersion(); 
			this.isFlash = flashPlayerVersion.major >= 9;
		}
		
		return this.isFlash;
	}
	
	CommonF.getActualTime = function(){
        return new Date();
	}
	
	CommonF.getActualTimeInMillis = function(){
        return CommonF.getActualTime().getTime();
	}
	
	CommonF.getDayInMillis = function(){
		return 1000*60*60*24;
	}
	
	CommonF.getNextDay = function(p_time){
		var day = this.getDayInMillis();
		
		return p_time + day;
	}
	
	CommonF.isToday = function(p_time){
		var now = new Date();
		
		return ((now.getFullYear() === p_time.getFullYear()) && 
			   	(now.getMonth() === p_time.getMonth()) && 
			    (now.getDate() === p_time.getDate()));
	}
	
	CommonF.getContentScrollParams = function(){
		var contentDIV = document.getElementById(CommonInterface.contentID);
		var scrollEl = contentDIV.parentNode;
		var values = {};
		values.left = scrollEl.scrollLeft;
		values.top = scrollEl.scrollTop;
		return values;
	}
	
	CommonF.getBodyHeight = function(){
		var bodyHeight;
		if (self.innerHeight) // all except Explorer
		{
			bodyHeight = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
			// Explorer 6 Strict Mode
		{
			bodyHeight = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			bodyHeight = document.body.clientHeight;
		}
		
		return bodyHeight;
	}
	
	CommonF.getCenterBodyHeight = function(){
        var bodyHeight = this.getBodyHeight();
		
		return (bodyHeight - this.getHeaderHeight() - 1);
	}
	
	CommonF.getHeaderHeight = function(){
		var headerDIV = document.getElementById("id_header");
		return headerDIV.offsetHeight;
	}

	CommonF.isKnownCountry = function(p_countryID) {
		return ((p_countryID) && (p_countryID.toLowerCase() != 'zz') && (p_countryID.toLowerCase() != 'o1'));	
	}
	
	CommonF.getCountryFlag = function(p_countryID){
        if (!p_countryID) p_countryID = 'zz';
	
		return "images/common/flags/"+p_countryID.toLowerCase()+".gif";
	}
	
	CommonF.getUnknownCountry = function(){
		return {cntry3:'zz', flag:155, id:'zz', name: CFStr.common_up_unknown, state:0};
	}
	
	CommonF.getAllCountry = function(){
		return {cntry3:'aa', flag:155, id:'aa', name: CFStr.common_lower_all_2, state:0};
	}
	
	// sort
	CommonF.getCountry = function(p_countryID){
		//	returns default country
		if (!p_countryID) p_countryID = 'zz';
		
		if (!CommonF.Countries || (CommonF.Countries.length < 1)) return this.getUnknownCountry();
		
        for (var i=0; i<CommonF.Countries.length; i++)
            if (CommonF.Countries[i].id.toLowerCase() === p_countryID.toLowerCase()) return CommonF.Countries[i];
            
        return this.getCountry('zz');
	}
	
	// sort
	CommonF.getCountryByTag3 = function(p_cntry3){
		//	returns default country
		if (!p_cntry3) return CommonF.getCountry('zz');
		
        for (var i=0; i<CommonF.Countries.length; i++)
            if (CommonF.Countries[i].cntry3.toLowerCase() === p_cntry3.toLowerCase()) return CommonF.Countries[i];
            
        return this.getCountry('zz');
	}
	
	CommonF.getCountryOptionList = function(p_selected){
		var resultStr = '<option value="" '+(p_selected ? '' : 'selected')+'> '+CFStr.sentence_43+' ...</option>';
		
		for(var i=0; i<CommonF.Countries.length; i++){
			resultStr+='<option value="'+CommonF.Countries[i].id.toUpperCase()+'" '+(p_selected!=null && p_selected.toLowerCase() === CommonF.Countries[i].id.toLowerCase() ? 'selected':'')+'> '+
						CommonF.Countries[i].name+'</option>';
		}
		return resultStr;
	}
	
	CommonF.getEloType = function(p_isOnlineGame){     
        switch (p_isOnlineGame){
            case true: return 0;   // online game 
            case false: return 1;   // offline game
        }
    }

	CommonF.showLoadMask = function(p_text){
		if (this.loadMask)
			this.loadMask.hide();
		this.loadMask = new Ext.LoadMask(document.body, {msg: p_text});
		this.loadMask.show('...', true);
	}	
	
	CommonF.hideLoadMask = function(){
		if (this.loadMask)
			this.loadMask.hide();
	}
	
	CommonF.IsNumeric = function(sText){
	    var ValidChars = "0123456789";
	    var IsNumber=true;
	    var Char;
	
	    for (i = 0; i < sText.length && IsNumber == true; i++) { 
	        Char = sText.charAt(i); 
	        if (ValidChars.indexOf(Char) == -1) {
	            IsNumber = false;
	        }
	    }
	    return IsNumber;
	}
	
	CommonF.getUniqueID = function(){
		if (!this.uniqueID) this.uniqueID = 0;
    	return this.uniqueID++;
	}
	
	CommonF.createSpotLight = function(p_divID){
		
		var DIV = document.getElementById(p_divID);
		var style = 'width: '+(DIV.offsetWidth+CFTemplates.globalConfig.spotlight.width)+'px; height: '+(DIV.offsetHeight+CFTemplates.globalConfig.spotlight.height)+'px; top:'+CFTemplates.globalConfig.spotlight.top+'px;left:'+CFTemplates.globalConfig.spotlight.left+'px;';
		var spotDIV = document.createElement('div');
		spotDIV.className = "spotlight";
		spotDIV.style.cssText = style;
		
		var id = 'id_spotlight_'+CommonF.getUniqueID();
		spotDIV.setAttribute('id', id);
		DIV.parentNode.appendChild(spotDIV);
		return id;
	}
	
	CommonF.setSelectValue = function(p_value, p_selectID){
		
		var select = document.getElementById(p_selectID);
		
		for (var i=0; i<select.options.length; i++){  	 
			if (select.options[i].value == p_value)
				if (Ext.isIE6)
					select.options[i].setAttribute('selected', true);
				else
					select.options[i].selected = true;
		}
	}
	
	//	ensure checked checkbox from the group
	//	handling allways the checkbox onclick event
	CommonF.checkMutually = function(p_checkID){
		if (this.checked) return; 
	
		var anotherCheck = document.getElementById(p_checkID);
		if (!anotherCheck.checked) anotherCheck.checked = true;	
	}
	
	/********************************* form validation *************************************************/
	
	
	


	CommonF.formatCurrency = function(p_num, p_currencyChar){
	
		num = p_num.toString().replace(p_currencyChar,'');
		
		if(isNaN(num))
			num = "0";
			
		var sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		var cents = num%100;
		num = Math.floor(num/100).toString();
		
		if(cents<10)
			cents = "0" + cents;
			
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+','+
			
		num.substring(num.length-(4*i+3));
		
		return (((sign)?'':'-') + p_currencyChar + num + '.' + cents);
	}
	
	CommonF.currencyFormatToCents = function(p_num, p_currencyChar){
		if (isNaN(p_num))
			p_num = p_num.substring(1);
			 
		var float = parseFloat(p_num); 
		if (isNaN(float)) return 0;
		var cents = Math.round(float*100);
		
		return cents; 
	}

	/*
	<form name=currencyform>
	Enter a number then click the button: <input type=text name=input size=10 value="1000434.23">
	<input type=button value="Convert" onclick="this.form.input.value=formatCurrency(this.form.input.value);">
	<br><br>
	or enter a number and click another field: <input type=text name=input2 size=10 value="1000434.23" onBlur="this.value=formatCurrency(this.value);">
	</form>
	</center>
	*/
	
	/************************************ extjs hotfix *************************************************/
	
	CommonF.createExtCorrectedTabPanel = function(){
		Ext.CorrectedTabPanel = function(config) {
			Ext.CorrectedTabPanel.superclass.constructor.call(this, config);

			this.on('beforeremove', function(tabContainer, tabPanel) {
				return tabPanel.fireEvent('beforeclose', tabPanel);
			});
		};

		Ext.extend(Ext.CorrectedTabPanel, Ext.TabPanel);
	}	
	
	CommonF.getRealGridMouseOverCellIndex = function(p_grid, p_cellIndex){
	
		var colModel = p_grid.getColumnModel();
				
		if (Ext.isIE7 || Ext.isIE){
			var colCount = colModel.getColumnCount();
			
			var showedCount = 0;
			var hiddenCount = 0;
			for (var i=0;i<colCount;i++){
				if (showedCount === p_cellIndex) break;
				if (colModel.isHidden(i))
					hiddenCount++;
				else
					showedCount++;
			}
			
			var realCellIndex = hiddenCount + showedCount;
			
		}else{
			var realCellIndex = p_cellIndex;
		}
		
		return realCellIndex;
	}
	
	/************************************ & extjs hotfix *************************************************/
	
	//	TODO
	CommonF.setLanguage = function(){
		window.location.search = Ext.urlEncode({"lang":"sk","charset":"utf-8"}); 
	}
	
	CommonF.getMaxContentWidth = function(){
		return this.getContentWidth(false);
	}
	
	CommonF.getMaxContentHeight = function(){
		return this.getContentHeight(false);
	}
	
	CommonF.getContentHeight = function(p_isStatic){
		if (p_isStatic)
			return this.minContentHeight;
			
		var height = this.getRealContentHeight() - (CFTemplates.globalConfig.borderHeight*2 + 12);
		
		return Math.max(height, this.minContentHeight);
	}
	
	CommonF.getRealContentHeight = function(){
		var header = document.getElementById(CommonInterface.headerID);
		var height = document.body.clientHeight - header.offsetHeight;
		
		return height;
	}
	
	CommonF.getContentWidth = function(p_isStatic){
		if (p_isStatic)
			return this.minContentWidth;
			
		var sideBar = document.getElementById(CommonInterface.leftSidebarID);
		var width = document.body.clientWidth - sideBar.offsetWidth - 4;
		
		return Math.max(width, this.minContentWidth);
	}
	
	CommonF.loadXmlFile = function(p_fileName){
		try{ 	//	Firefox, Mozilla, Opera, etc.
			xmlDoc=document.implementation.createDocument("","",null);
			xmlDoc.async = false;
			xmlDoc.load(p_fileName);
		}
		catch(e){
			try{	//	IE
				xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
				xmlDoc.async = false;
				xmlDoc.load(p_fileName);
			}
			catch(e){
				try{	//Google Chrome
					var xmlhttp = new window.XMLHttpRequest();
					xmlhttp.open("GET", p_fileName, false);
					xmlhttp.send(null);
					xmlDoc = xmlhttp.responseXML.documentElement;
				}
				catch(e){
					CFlogging.writeError(e.message);
					return;
				}
			}
		}
		
		return xmlDoc;
	}

	CommonF.zerofill = function(p_string,p_length){
	
		var result = p_string+'';
		
		for(var i = result.length; i<p_length; i++){
			result='0'+result;		
		}
		return result;
	}
	
	CommonF.objectToArray = function(p_object){
		var array = [];
		for (var index in p_object){
			array.push(p_object[index]);
		}
		
		return array;
	}
	
	CommonF.arrayToObject = function(p_array){
		var object = {};
	
		for (var i=0;i<p_array.length;i++)
			object[i] = p_array[i];
	
		return object;
	}
	
	CommonF.refreshObject = function(p_oldObject, p_newObject){
		p_oldObject = p_oldObject || {};
	
		if (p_newObject) 
			for (var index in p_newObject)
				eval('p_oldObject.' + index + ' = ' + 'p_newObject.' + index);
			
		return p_oldObject;
	}
	
	CommonF.getFunctionName = function(p_function){
		var fn = p_function.toString();
		var fname = fn.substring(fn.indexOf("function") + 8, fn.indexOf("(")) || "anonymous"; 
		return fname;
	}
	
	CommonF.imgExt = function(){
		return (Ext.isIE6 ? 'gif' : 'png');
	}
	
	CommonF.imgTemplatePath = function(){
		return 'images/templates/' + window.templateName + '/';
	}
	
	CommonF.shortText = function(p_text, p_permittedCharCount){
		if ((p_permittedCharCount < 3) || (p_text.length <= p_permittedCharCount)) return p_text;
		else return (p_text.substr(0, p_permittedCharCount-2) + '...');
	}
	
	/****************************************************************************************************/
	
	CommonF.blinkBrowserTitle = function(p_title1, p_title2){
		if (window.hasFocus) return;
		this.stopBrowserTitleBlinking();
		this.title = document.title; 
		this.titleBlinkingTimerID = setInterval('CommonF.doBrowserTitleBlinking("'+p_title1+'", "'+p_title2+'")', 700);
	}
	
	CommonF.doBrowserTitleBlinking = function(p_title1, p_title2){
		if (document.title.indexOf(p_title1) < 0)
			document.title = p_title1;
		else
			document.title = '' || p_title2;
	}
	
	CommonF.stopBrowserTitleBlinking = function(){
		if (this.titleBlinkingTimerID){
			clearTimeout(this.titleBlinkingTimerID);
			this.titleBlinkingTimerID = null;
			document.title = this.title;
		}
	}
	
	/****************************************************************************************************/
	
	CommonF.formatMessage = function(p_message, p_args){
		
		if (!p_message) return '';
	
		var message = p_message.replace('{{', '{');
		message = message.replace('}}', '}');
		for (var i = 0; i < p_args.length; i++) {
			message = message.replace('{' + i + '}', p_args[i]);
		}
		
		return message;
	}
	
	
	CommonF.getLoginErrorString = function(p_errorMesageID){
		 switch (p_errorMesageID){
            case "0": return CFStr.LOGIN_STATUS_ALL_OK;  
            case "1": return CFStr.LOGIN_STATUS_LOGIN_OK;   
            case "2": return CFStr.LOGIN_STATUS_LOGIN_FAIL;  
            case "3": return CFStr.LOGIN_STATUS_ALREADY_LOGGED; 
            case "4": return CFStr.LOGIN_STATUS_AUTENTIFICATION_ERROR;  
            case "5": return CFStr.LOGIN_STATUS_SESSION_USED_ERROR; 
        }
	}
	