////////////////////////////////////////////////////////////GLOBALS
var g_ajax_timeout = 50000;
var g_topLoginUserText = "User name";
var g_topLoginPassText = "Password";
var g_topSearchUpdated = false;
var g_topLoginUserUpdated = false;
var g_topLoginPassUpdated = false;
var g_listFlag = '';
var request = null;
var g_errorCode = '';

//FIX Css Bug in specify navigator - this is master - should be copied to the specific location
function fixCSS() {
	jQuery(document).ready(function() {
		
		//MOSILLA
		if(jQuery.browser.mozilla){
			
		}
		else
		{
			//IE
			if (navigator.appVersion.indexOf('MSIE 6.0')>-1)
			{
				//IE 6
				
			}
			else
			{
				//IE 7
				
			}
		}
	});
}

//=========================================== TOP SEARCH ===============================================
function topSeachFocus(txtTopSearch){
	if(g_topSearchUpdated == false) {
		document.getElementById("txtTopSeach").value = "";
		document.getElementById("txtTopSeach").focus();
	}
}
//------------------------------------------------------------------------------------------------------
function topInpSeachClick(txtTopSearch){
	if(document.getElementById("txtTopSeach").value == g_topSearchInitText) {
		document.getElementById("txtTopSeach").value = "";
		document.getElementById("txtTopSeach").focus();
	}
}
//------------------------------------------------------------------------------------------------------
function topSearchBlur(txtTopSearch){
	if(txtTopSearch.value == "") txtTopSearch.value = g_topSearchInitText;
}
//------------------------------------------------------------------------------------------------------
function topSearchKeyUp(txtTopSearch,evt){
	g_topSearchUpdated = true;
	if(evt.keyCode == 13) topSearchClick();
}
//------------------------------------------------------------------------------------------------------
function topSearchClick(){
	var txtTopSeach = getElement("txtTopSeach");
	var searchQuery = trim(txtTopSeach.value);
	if(g_topSearchUpdated == false) searchQuery = "";
	var frmTopSearch = getElement("frmTopSearch");		
	
	frmTopSearch.searchquery.value = searchQuery;
	frmTopSearch.submit();
}
//=========================================== TOP SEARCH END===============================================

//=========================================== TOP LOGIN ================================================

function topLoginKeyUp(obj,evt) {
	doOnEnter(evt,'topLoginClick');
}
function topPasswordFocus(){
	jQuery('.passwordField').toggle(); 
	setTimeout(function() { 
		jQuery('.passwordField:visible').get(0).focus();
	}, 50);
}
function topUsernameFocus(){
	var obj = jQuery('#topUsername');
	if(obj.val() == 'Email') {
		obj.val('');
	}
}
function topPasswordBlur(){
	if (jQuery('#topPassword').val() == '') {
		jQuery('.passwordField').toggle();
	}
}
function topUsernameBlur(){
	var obj = jQuery('#topUsername');
	if(obj.val() == '') {
		obj.val('Email');
	}
}

function topLoginClick() {
	jQuery('#frmTopLogin').submit();
}
//=========================================== TOP LOGIN ================================================

/////////////////////////////////////// LOGIN FUNCTION ///////////////////////////////
//---------------------------------------------------------- Globals Login
var g_func = "<?php echo $this->layout()->incorrect?>";

function loginClick(){
	var username = jQuery('#username').val();
	var password = jQuery('#password').val();
			
    jQuery('#topUsername').val(username);
	jQuery('#topPassword').val(password);
	jQuery('#frmTopLogin').submit();
	
//	if(username == 'Email'){
//		location.href=g_loginPath;
//	}else{
//		
//	}
}
function vipClick(){
	jQuery('#frmVIP').submit();
}
//------------------------------------------------------------------------------------			
function initLogin(){    
    jQuery.noConflict(); 
    jQuery(document).ready(function (){       
       if(g_func == "1") jQuery('topPassword').focus();
	   else jQuery('topUsername').focus();
	   //pageAutosuggest();	  	
    });      
}	
//-------------------------------------------------------------------------------------
function rememberClick() {
	jQuery('#frmForgot').submit();
}
/////////////////////////////////////// END LOGIN /////////////////////////////////////
function closeMessage(msg)
{
	jQuery('div#'+msg).css('display','none');
}

/////////////////////////////////////// BEGIN RESET FORM //////////////////////////////
function clear_form_elements(ele) {	
	jQuery(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
            	jQuery(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
}


//--------------------------------------------------------------------------
/**
 * Function load scripts on demand
 * 
 * @return
 */
jQuery.getScripts = function(scripts, onCompleteFunction, iscache) {
	if(iscache == undefined) var iscache = true;				
	if (g_combineJS) {		
		var groups = new Array();
		var single = new Array();
		for(s in scripts) {
			if (scripts[s].indexOf('.js') == -1) {
				groups.push(scripts[s]);
			} else {
				single.push(scripts[s]);
			}
		}
		if (groups.length > 0) {
			jQuery.getScriptsCombined(groups, 'group', onCompleteFunction, iscache);
		}
		if (single.length > 0) {
			jQuery.getScriptsCombined(single, 'single', onCompleteFunction, iscache);
		}
		
	} else {
		var arrScriptNames = getLoadedScripts();
		var arrNotExistingScripts = Array();
		
		//isScriptExists(arrScripts);
		for(var i=0;i<scripts.length;i++){
			if(!in_array(scripts[i], arrScriptNames)) {
				arrNotExistingScripts.push(scripts[i]);
			}
		}	
				
		//if there is no scripts, run the con complete function
		if(arrNotExistingScripts.length == 0){
			runFunctionByName(onCompleteFunction);
			return(false);
		}		
		i = 1;
		var len = arrNotExistingScripts.length;	
		if(iscache == true) jQuery.ajaxSetup({cache:true});
		var onScriptLoaded = function(data, response) { 
			if (i++ == len) { //run the onComplete function on load all scripts
				runFunctionByName(onCompleteFunction);
			}
		} ;	
		for(var s in arrNotExistingScripts){ 
			jQuery.getScript(arrNotExistingScripts[s], onScriptLoaded); 		
		};
	}
};

/**
 * Function load combined scripts on demand
 * 
 * @return
 */
jQuery.getScriptsCombined = function(scripts, type, onCompleteFunction, isCache) {
	if (isCache == undefined || isCache) {
		jQuery.ajaxSetup({
					cache: true
				});
	}
		
	var debug = 0;
	var loadedScripts = getLoadedScripts();
	var scriptsToLoad = new Array();
	for(var i = 0; i < scripts.length; i++) {
		switch(type) {
			case 'group':
				var script = g_minifyGroup + scripts[i] + '.js&debug=' + debug;
				break;
			case 'single':
			default:
				var script = g_minifySingle + scripts[i] + '&debug=' + debug;
				break;
		}
		if (!in_array(script, scripts)) {
			scriptsToLoad.push(script);
		}
	}
	
	if (scriptsToLoad.length == 0) {
		runFunctionByName(onCompleteFunction);
		return false;
	}		
	
	var i = 1;
	var onScriptLoaded = function(data, response) {
		if (i++ == scriptsToLoad.length) { //run the onComplete function on load all scripts
			runFunctionByName(onCompleteFunction);
		}
	};	
	
	for(var s in scriptsToLoad){ 
		jQuery.getScript(scriptsToLoad[s], onScriptLoaded); 		
	};		
}

jQuery.getStyle = function(style) {		
	if(typeof document.createStyleSheet == 'function') {
		document.createStyleSheet(style);
	} else {
		var newCSS	 = document.createElement('link');
		newCSS.rel	 = 'stylesheet';
		newCSS.type	 = 'text/css';
		newCSS.media = 'all';
		newCSS.href	 = style;
		// var styles	= "@import url(' " + url + " ');";
		// newSS.href	='data:text/css,'+escape(styles);
		document.getElementsByTagName('head')[0].appendChild(newCSS);
	}
}

/**
 * Function load stylesheets on demand
 * 
 * @return
 */
jQuery.getStyles = function(styles, onCompleteFunction, iscache) {
	/*if (isCache == undefined || isCache) {
		jQuery.ajaxSetup({
					cache: true
				});
	}*/
	if (g_combineCSS) {		
		var groups = new Array();
		var single = new Array();
		for(var s in styles) {
			if (styles[s].indexOf('.css') == -1) {
				groups.push(styles[s]);
			} else {
				single.push(styles[s]);
			}
		}
		if (groups.length > 0) {
			jQuery.getStylesCombined(groups, 'group', onCompleteFunction, iscache);
		}
		if (single.length > 0) {
			jQuery.getStylesCombined(single, 'single', onCompleteFunction, iscache);
		}
	} else {
		var loadedStyles = getLoadedStyles();
		var stylesToLoad = Array();
		for(var s in styles) {
			if (!in_array(styles[s], loadedStyles)) {
				stylesToLoad.push(styles[s]);
			}
		}
		//if there is no styles, run the complete function
		if(stylesToLoad.length == 0){
			//runFunctionByName(onCompleteFunction);
			return false;
		}		
		var i = 1;
		/*var onScriptLoaded = function(data, response) { 
			if (i++ == stylesToLoad.length) { //run the onComplete function on load all scripts
				runFunctionByName(onCompleteFunction);
			}
		} ;	*/
		for(var s in stylesToLoad) { 
			//jQuery.getScript(stylesToLoad[s], onScriptLoaded);			
			jQuery.getStyle(stylesToLoad[s]);
		};
	}
};

jQuery.getStylesCombined = function(styles, type, onCompleteFunction, isCache) {
	/*if (isCache == undefined || isCache) {
		jQuery.ajaxSetup({
					cache: true
				});
	}*/		
	var debug = 0;
	var loadedStyles = getLoadedStyles();
	var stylesToLoad = new Array();
	for(var s in styles) {
		switch(type) {
			case 'group':
				var style = g_minifyGroup + styles[s] + '.css&debug=' + debug;
				break;
			case 'single':
			default:
				var style = g_minifySingle + styles[s] + '&debug=' + debug;
				break;
		}
		if (!in_array(style, loadedStyles)) {
			stylesToLoad.push(style);
		}
	}
	if (stylesToLoad.length == 0) {
		//runFunctionByName(onCompleteFunction);
		return false;
	}			
	var i = 1;
	/*var onScriptLoaded = function(data, response) {
		if (i++ == scriptsToLoad.length) { //run the onComplete function on load all scripts
			runFunctionByName(onCompleteFunction);
		}
	};*/		
	for(var s in stylesToLoad) { 
		//jQuery.getScript(stylesToLoad[s], onScriptLoaded);
		jQuery.getStyle(stylesToLoad[s]);
	};
}

//---------------------------------------------------------------------------
// get radio buttons value
function getRadioValue(radioName){
	var value = jQuery("input[name='"+radioName+"']:radio:checked").val();
	return(value);
}

//---------------------------------------------------------------------------
// run function by function name
function runFunctionByName(functionName){	
	//trace(typeof eval(functionName));
	if((eval("typeof "+functionName+" == 'function'"))){
		eval(functionName+"()");
	}
	
}

//---------------------------------------------------------------------------
/**
 * Function return a list of loaded scripts
 * 
 * @return array
 */
function getLoadedScripts() {
	var scripts = new Array();
	jQuery('head > script').each(function(index) {
		var src = jQuery(this).attr('src');
		if (src != null) {
			src = src.substring(src.indexOf(window.location.hostname));// + window.location.hostname.length
			scripts.push(src);
		}
	});
	return scripts;
}

/**
 * Function return a list of loaded stylesheets
 * 
 * @return array
 */
function getLoadedStyles() {
	var styles = new Array();
	jQuery("head > link[rel='stylesheet']").each(function(index) {
		var src = jQuery(this).attr('href');
		if (src != null) {
			src = src.substring(src.indexOf(window.location.hostname));// + window.location.hostname.length
			styles.push(src);
		}
	});
	return styles;
}

//---------------------------------------------------------------------------
// get if the script is in the scripts list or not.
function in_array(needle, haystack) {
	for(var i=0; i < haystack.length; i++) {
		if(haystack[i] && haystack[i].indexOf(needle) != -1) {
			return true;			
		}
	}	
	return false;
}

/////////////////////////////////////// BEGIN DISPLAY ERROR LOADER //////////////////////
function displayError(response, errorType, errorThrown) {
//	if(response.status != 0) {
		if (typeof hideLoader == 'function') {
			hideLoader();
		}		
		try {
			if(errorType == 'timeout') {
				displayLoaderErrorMsg('timeout');
		    } else {
		    	var htmlResponse = response.responseText;
				//g_errorCode = jQuery(htmlResponse).contents().find('#errorCode').html();
				if (htmlResponse.indexOf(';Code:') !== -1) {
					g_errorCode = parseInt(htmlResponse.split(';')[1].substr(6,4));	
				}
		    	displayLoaderErrorMsg();	    
		    }		
		}catch (e) {			
		}
//	}
}

//--------------------------------------------------------------------------
//get array with all form elements
function getObjFormElements(formID){
	var obj = new Object();
	var form = getElement(formID);
	var name,value,type,flagUpdate;
		
	//enabling all form items connected to mx
	for(var i=0; i < form.elements.length; i++){
		name = form.elements[i].name;
		value = form.elements[i].value;
		type = form.elements[i].type;
		
		flagUpdate = true;
		switch(type){
			case "checkbox":
				value = form.elements[i].checked;
			break;
			case "radio":
				if(form.elements[i].checked == false) flagUpdate = false;				
			break;
		}
		if(flagUpdate == true && name != undefined) obj[name] = value;
	}		
	return(obj);
}

//--------------------------------------------------------------------------
// function telling if some key is number
function isNumberKey(evt){	
   var charCode = (evt.which) ? evt.which : evt.keyCode
   if (charCode > 39 && charCode != 46 && (charCode < 48 || charCode > 57)) return false; 
   return true;
}
/////////////////////////////////////// END DISPLAY ERROR LOADER /////////////////////////

