/** DEFAULT HELPERS FOR ALL FILES**/
/** DEFAULT HELPERS FOR ALL FILES**/
/** Define here current file version for Java-script files  **/
var jsFileVersion = '05';
/** OK document on-line let's fill up some stuff in it! **/
window.onload = loaderInc;
function loaderInc(){
	/** Basic functions calls **/
	setGetUrl();
	/** Check document language and set file with language dictionary files **/
	var docLang = (getUrl['lang']) ? getUrl['lang'] : 'en';//document.documentElement.lang;
	var callLang = getScript(getUrl['origin']+'/jsexec/lang.'+docLang+'.js');
	/** Loads all sites list and map  according to language **/
	var callCity = getScript(getUrl['origin']+'/jsexec/def.city.'+docLang+'.js');
	
	var secondCall = function(){
		customInc();
	};
	getScript(getUrl['origin']+'/jsexec/def.dif.'+jsFileVersion+'.js',secondCall);
};

/** Loading scripts files asynchronously with callback **/
function getScript(url,callback){
	var script = document.createElement('script');
	script.src = url;
	script.async=true;
	var head = document.getElementsByTagName('head')[0], done=false;
	script.onload = script.onreadystatechange = function(scope){
		if(!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')){
			done=true;
			if(callback)
				callback();
			return true;
		}
	};
	head.insertBefore(script, head.firstChild);
	// head.appendChild(script);
};

/** Holding URL request parameters and HTML attributes **/
var getUrl={};
/** Getting and filling URL parameters and request if such exists ; bounded to vars **/
function setGetUrl(){
	/** Current full url domain **/
	getUrl['full'] = window.location.href;
	/** Current full domain without ending slash **/
	getUrl['origin'] = window.location.protocol + '//' + window.location.host;
	/** Current path without domain **/
	getUrl['path'] = window.location.pathname;
	getUrl['lang'] = (document.documentElement.lang) ? document.documentElement.lang : "";
	/** assign any search if such exists **/
	getUrl['search'] = window.location.search;
	var url_obj = window.location.href.split('?');
	if(url_obj.length > 1){
		var get_pairs = url_obj[1].split('&');
		getUrl['vars'] = {};
		for (var i in get_pairs){
			var temp = get_pairs[i].split('=');
			getUrl['vars'][temp[0]] =temp[1];
		}
	}
};

/**
* Filling (building) for select element numeric options elements and "value" attribute
* @select_obj : gets the object itself, @option_from/to : is automatically will increase or decrease by need
*/
 function fillNumericOption(select_obj,option_from,option_to){
	//for(;option_from!=(option_to +1);){
	(option_from > option_to) ? option_to=(option_to - 1) : option_to = (option_to + 1);
	for(;option_from!=option_to;){
		var select_option = document.createElement("option");
		select_option.innerHTML = option_from;
		select_option.setAttribute("value",option_from);
		select_obj.appendChild(select_option);
		(option_from > option_to) ? option_from-- : option_from++ ;
	}
 };

/**
* Filling (building) for select element numeric value options with compatible values from castData
* @select_obj : gets the object itself, @cust_data : value that should be filled, @cust_gender : gender for language man or women
*/
 function fillCustDataOption(select_obj,cust_data,cust_gender){
	/** Check if gender for language is specified **/
	(!cust_gender) ? cust_gender=1 : cust_gender ;
	/** check if such object is exists in array ! **/
	if(custData[cust_data][cust_gender]){
		var cust_data_run = custData[cust_data][cust_gender].length;
		for(run=0;run<cust_data_run;run++){
			var select_option = document.createElement("option");
			select_option.innerHTML = custData[cust_data][cust_gender][run];
			select_option.setAttribute("value",run+1);
			select_obj.appendChild(select_option);
		}
	/** Try to run default language selector in case this translate doesn't exists **/
	}else if(custData[cust_data][1]){
		var cust_data_run = custData[cust_data][1].length;
		for(run=0;run<cust_data_run;run++){
			var select_option = document.createElement("option");
			select_option.innerHTML = custData[cust_data][1][run];
			select_option.setAttribute("value",run+1);
			select_obj.appendChild(select_option);
		}
	}
 };
/**
* Simple AJAX get to relative URL with optional callback
**/
 function getData(url,values){
	document.body.style.cursor = "wait";
	var post_values = '?'; 
	for(var key in values){
		if(values.hasOwnProperty(key)){
			post_values += '&' + key + '=' + values[key];
		 }
	}
	var http = new XMLHttpRequest();
	http.open("GET", getUrl.origin+"?"+values, true);
	http.onreadystatechange = function() {//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200) {
			//alert(http.responseText);
		}
		document.body.style.cursor = "";
	}
	http.send(null);
};
/**
* Posting values to relative URL with optional callback
**/
function postData(url,values,callback){
	document.body.style.cursor = "wait";
	var post_values = '?'; 
	for(var key in values){
		if(values.hasOwnProperty(key)){
			post_values += '&' + key + '=' + values[key];
		 }
	}
	var http = new XMLHttpRequest();
	http.open("POST", getUrl.origin + url , true);
	http.onreadystatechange = function(){
		// function call when the state changes.
		if(http.readyState == 4 && http.status == 200){
			document.body.style.cursor = "";
			if(callback)
				callback(http.responseText);
		}
	}
	// Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
	// http.setRequestHeader("Content-length", values.length);
	// http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	// http.setRequestHeader("Content-length", params.length);
	// http.setRequestHeader("Connection", "close");
	http.send(post_values);
};

/** 
* Simulating (HIDDEN) form post action with session to URL
* pass actual URL and Object Literal values
**/
function hiddenGetUrl(url, values){
	document.body.style.cursor = "wait";
	values = values || {};
	// var server_loc = window.location.protocol + "//" + window.location.host;
	// var form = document.createElement("form"), {action: server_loc + url,
									// method: "POST",
									// style: "display: none"});
	// The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", "GET");
	// form.setAttribute("method", "POST");
    // form.setAttribute("action", server_loc + url);
    form.setAttribute("action", getUrl.origin + url);
	// Move the submit function to another variable so that it doesn't get overwritten.
	form._submit_function_ = form.submit;
	for(var key in values) {
		if(values.hasOwnProperty(key)){
			var hiddenField = document.createElement("input");
			hiddenField.setAttribute("type", "hidden");
			hiddenField.setAttribute("name", key);
			hiddenField.setAttribute("value", values[key]);
			form.appendChild(hiddenField);
		 }
	}
	document.body.appendChild(form);
	// Call the renamed function
	form._submit_function_(); 
	// remove the child after submission, except if a session is used and those data are saved
	// document.body.removeChild(form);
};


/** This to load language script or fall back to English **/
function langObj(translate){
	if(langDic[translate])
		return langDic[translate];
	else
		return translate;
};

/** Canceling object's event bubbling, for browsers compatibility **/
function getEventValue(scope){
	return scope.srcElement ? scope.srcElement.value : scope.target.value;
};

/** get events target object compatibility **/
function getEventTarget(scope){
	var evnt = scope || window.event;
	return evnt.target || evnt.srcElement;
};

/** Canceling all event default methods and bubbling **/
function cancelElementEvent(scope){
	var elem ,evn = scope ? scope : scope.event;
	elem = evn.srcElement ? evn.srcElement : evn.target;
	evn.stopPropagation ? evn.stopPropagation() : evn.returnValue = false;
	evn.preventDefault ? evn.preventDefault() : evn.returnValue = false;
	return elem;
}

/** canceling object's event bubbling, for browsers compatibility **/
function cancelEventBubble(scope){
	var evnt = scope ? scope : window.event;
	if (evnt.stopPropagation) evnt.stopPropagation();
	if (evnt.cancelBubble!=null) evnt.cancelBubble = true;
};

/** canceling object's event default execution, for browsers compatibility **/
function cancelEventDefault(scope){
	var evnt = scope ? scope:window.event;
	evnt.preventDefault ? evnt.preventDefault() : evnt.returnValue = false;
};

/** remove event listener full browser support **/
function removeEvent(elem,evnt,handler){
	if (elem.removeEventListener)elem.removeEventListener(evnt,handler,false);
	if (elem.detachEvent)elem.detachEvent('on'+evnt,handler); 
};

/** add event listener full browser support **/
function addEvent(elem,evnt,handler){
	if(elem.attachEvent){return elem.attachEvent('on'+evnt,handler);
	}else{return elem.addEventListener(evnt,handler,false);}
};

/** Popup promotion window (disallowing more than one window) **/
/** Gets passed object the the URL that should be opened, this is not for anchors. Fuck IE because i don't want to use call nor bind for this event **/
var popupWinObj = function(elem_Obj,targ_Url,win_height,win_width){
	elem_Obj.setAttribute('data-hid-url',targ_Url);
	elem_Obj.setAttribute('data-hid-h',win_height ? ",height=" + win_height : "");
	elem_Obj.setAttribute('data-hid-w',win_width ? ",width=" + win_width : "");
	elem_Obj.setAttribute('data-hid-id',Math.random().toString(16).slice(2));
	this.popupWin;
	popupOpen = function(evnt){
		var  winBuild = getEventTarget(evnt);
		/** check if pointer to window exists || closed **/
		if(this.popupWin == null || this.popupWin.closed)
			this.popupWin = window.open(winBuild.getAttribute('data-hid-url'),winBuild.getAttribute('data-hid-id'),"toolbar=0,scrollbars=1,resizable=1,status=0,location=0"+winBuild.getAttribute('data-hid-h')+winBuild.getAttribute('data-hid-w'));
		else
			/** bring to front because such window is already opened **/
			this.popupWin.focus()
	};
	addEvent(elem_Obj,"click",this.popupOpen);
};