
function checkBoxCreateHiddenField(chbox, layer, fieldName, fieldValue)
{
	var layerIns = document.getElementById(layer);
	
	if(!chbox.checked)
	{
		// Create hidden field
		btn = document.createElement("input");
		btn.setAttribute("type", "hidden");
		btn.setAttribute("name", fieldName);
		btn.setAttribute("id", fieldName);
		btn.setAttribute("value", fieldValue);

		// Add element inside layer
		layerIns.appendChild(btn);
	
	} else if (chbox.checked && document.getElementById(fieldName)) {
		
		var fieldID = document.getElementById(fieldName);
		layerIns.removeChild(fieldID);
	}
		
}

/* Function for a layers (layer showing and hiding, eg. in control panel dynamic menu) */
function combo(r_id, refer) 
{
	if (refer) 
		document.getElementById(r_id).style.display="";
	else
		document.getElementById(r_id).style.display="none";
}

// Form onsubmit confirm
function confirmSubmitForm(confimMsg) {
	
	// Prepare variables
	var answer = confirm(confimMsg)
	
	// Run procedure
	if (answer)
		return true; 
	else
		return false;
}

// Functions for simple text editor
function textSimpleEditorFormat(action) { 
	Editor.execCommand(action, false, null); 
} 

function textSimpleEditorColour(colour) { 
	Editor.execCommand("forecolor", false, colour); 
} 

/* Tooltip for a 'title' tag in weblinks */
function tooltip(d,E,b,i,a)
{
 	d=document;
	E=d.documentElement;
	b=d.body;
	if(!E)return;
	
  	for(i=0;a=b.getElementsByTagName("a")[i];i++)
	{
		if(a.title)
		{
      		with(a.t=d.createElement("div"))
			{
        		id="tooltip"
        		innerHTML=a.title.replace(/\|/g,"<br />")
			}
			
      		a.onmouseover=function(e)
			{
        		with(this)
				{
					title="";onmousemove(e)
				}
        
				b.appendChild(this.t)
			}
			
      		a.onmouseout=function(x)
			{
        		with(this)
				{
					title=t.innerHTML.replace(/<br \/>/g,"\|")
				}
        	
				if(x=d.getElementById("tooltip"))b.removeChild(x)
      		}
			
      		a.onmousemove=function(e)
			{
        		e=e||event;with(this.t.style)
				{
         			left=e.clientX+(E.scrollLeft||b.scrollLeft)+"px"
         			top=e.clientY+(E.scrollTop||b.scrollTop)+"px"
        		}
      		}
    	}
  	}
}

function addEvent(O,E,F,x)
{
	return(x=O.addEventListener)?x(E,F,1):(x=O.attachEvent)?x('on'+E,F):!1
}
addEvent(window,'load',tooltip);

/* Function unhide sections when checbox field is checked  */
function unhideLayerWitchCheckbox(form_name, checkbox_name, layer_name)
{
	if (document.forms[form_name].elements[checkbox_name].checked) 
	{
		document.getElementById(layer_name).style.display = '';
		
	} else {
		
		document.getElementById(layer_name).style.display = 'none';
	}
}

/* Function hide sections when checbox field is checked  */
function hideLayerWitchCheckbox(form_name, checkbox_name, layer_name)
{
	if (document.forms[form_name].elements[checkbox_name].checked) 
	{
		document.getElementById(layer_name).style.display = 'none';
		
	} else {
		
		document.getElementById(layer_name).style.display = '';
	}
}

/* Function change element class if checbox field is checked  */
// changeType - define class change direction. If you set classChange on 'reverse' when layer changing class from  defClass to changClass, if you set classChange on '' - changClass to defClass
function changeLayerClassWitchCheckbox(form_name, checkbox_id, layer_name, changClass, defClass, changeType)
{
	if (changeType == 'reverse')
	{
		var changClass = defClass;
		var defClass = changClass;
	}
	
	if (document.getElementById(checkbox_id).checked) 
	{
		document.getElementById(layer_name).className = changClass;
		
	} else {
		
		document.getElementById(layer_name).className = defClass;
	}
}

/* Function disabled fields when checbox field is checked  */
function disableFieldWitchCheckbox(form_name, checkbox_name, field_id)
{
	if (document.forms[form_name].elements[checkbox_name].checked) 
	{
		document.getElementById(field_id).disabled=true;
		
	} else {
		
		document.getElementById(field_id).disabled=false;
	}
}

/* Function disabled fields when checbox | radio field is checked (function checks fields in loop)  */
function disableFieldWitchCheckboxWitchInterval(form_name, checkbox_name, field_id)
{
	var timer = setInterval(
		function xssa() 
		{
			if (document.getElementById(checkbox_name).checked == true) 
			{
				document.getElementById(field_id).disabled=true;
				
			} else if (document.getElementById(checkbox_name).checked == false) {
				
				document.getElementById(field_id).disabled=false;
			}
		}
	,100);
}

/* Function enable fields when checbox is checked  */
function enableFieldWitchCheckboxByFieldIds(form_name, checkbox_id, field_id)
{
	if (document.getElementById(checkbox_id).checked) 
	{
		document.getElementById(field_id).disabled=false;
		
	} else {
		
		document.getElementById(field_id).disabled=true;
	}
}

// Function protect the emails inserted on this website
// # - domain - this is the email domain (eg.: when You email address is john.smith@yourmail.com - then mail domain is it: 'yourmail.com')
// # - user - this is the email user (eg.: when You email address is john.smith@yourmail.com - then mail user is it: 'john.smith')
function mailProtect(domain, user)
{
    pr_1 = "mai";
	pr_2 = "lto:";
	at = "@";   
	locationstring = pr_1+pr_2+user+at+domain;
	window.location = locationstring;
}

// Function permits on only one selection checbox
function oneCheckBoxSelect(chBox_no, formName, chBoxName, howChBoxs)
{
	var gn=document.forms[formName].elements[chBoxName]
	var len=howChBoxs
		
	for (j=0; j < len ; j++ )
	{
		if(gn[j].checked==true)
		{
			gn[j].checked = false
		}				
			
		if(j == chBox_no)
		{
			gn[j].checked=true
		}		
	}		
}	

// Function add limit on selected checkboxes inside some layer
// # - chBoxLimit - define limit for selected checkboxes
// # - lrId - define layer name (in this layer You must create checkboxes)
// # - msg - when user exceet limit show him the message
function selectedChboxesLimiter(chBoxLimit, lrId, msg)
{
	// Prepare variables
	var chboxes = document.getElementById(lrId).getElementsByTagName('input');
	var cnt = 0;
	
	// Check selected checkboxes in area
	for (var i=0; i<chboxes.length; i++) 
	{	
		if (chboxes[i].getAttribute("type") == "checkbox" && chboxes[i].checked)
		{
			if(cnt<chBoxLimit)
			{
				chboxes[i].checked = true;
				
			} else {
				
				chboxes[i].checked = false;
				alert(msg);
			}
			
			cnt++;
		}
	}
}

// Check shop item availability
function shopItem_checkAvailability()
{
	// Prepare variables
	var attribsSplit = document.getElementById(lr_sItemFieldSplitAttribs).value;
	var attribOne = document.getElementById(lr_sItemFieldOptionFirst).value;
	var attribTwo = document.getElementById(lr_sItemFieldOptionSecond).value;
	var checkHowAttrs = document.getElementById(lr_sItemFieldSplitAttribs).value;
	
	// Hide layer with 'item available' message
	document.getElementById(lr_sItemMsgAvailable).style.display='none';
	document.getElementById(lr_sItemFieldAddToCart).style.display='';
	
	// Check attribute amount
	if(checkHowAttrs.replace(/\|\|([0-9]+)\|\|/g, "") == '')
	{
		var checkHowAttrs = 1; 
		var optionIDs = '||'+attribOne+'||'+attribOne+'||';
		
	} else {
		
		var checkHowAttrs = 2;
		var optionIDs = '||'+attribOne+'||'+attribTwo+'||';
	}
	
	if(typeof(prodAvailArr) != 'undefined') 
	{
		if((checkHowAttrs == 2 && attribOne != '' && attribTwo != '') || (checkHowAttrs == 1 && attribOne != ''))
		{
			// Show 'This item is not available now' message when item quantity is less than 1
			if(typeof(prodAvailArr[attribsSplit][optionIDs]['qty']) != 'undefined' && prodAvailArr[attribsSplit][optionIDs]['qty'] < 1)
			{
				document.getElementById(lr_sItemMsgAvailable).style.display='';
				document.getElementById(lr_sItemFieldAddToCart).style.display='none';
			}
			
			// Show attribute price
			if(typeof(prodAvailArr[attribsSplit][optionIDs]['price']) != 'undefined' && prodAvailArr[attribsSplit][optionIDs]['price'] > 0)
			{
				document.getElementById(lr_sItemMainPrice).style.display='none';
				document.getElementById(lr_sItemMainPriceMsg).style.display='none';
				document.getElementById(lr_sItemAttrPrice).firstChild.data=prodAvailArr[attribsSplit][optionIDs]['price']+' '+document.getElementById('currencyCodeActualHid').value;
				document.getElementById('productAvailable').value = prodAvailArr[attribsSplit][optionIDs]['qty'];
			}	
		}
	}
}

// Change styles for selected option
function shopItem_selectedAtributeOption(option, attribLrl)
{
	// Prepare variables
	var optionBlock = document.getElementById(attribLrl).getElementsByTagName('div');
	
	// Check selected options in area
	for (var i=0; i<optionBlock.length; i++) 
	{
		// Change option class name
		optionBlock[i].className = lr_sItemClassNameNotSelectedOption;
	}
	
	// Change option class name
	option.className = lr_sItemClassNameSelectedOption;
}

/* Function show section when we select something in selected field  */
function showLayerWitchSelect(form_name, select_name, layer_name, value, operator)
{
	if (operator=='==')
	{
		if (document.getElementById(select_name).value==value) 
		{
			document.getElementById(layer_name).style.display = '';
		
		} else {
	
			document.getElementById(layer_name).style.display = 'none';
		}
		
	} else if (operator=='!=') { 
	
		if (document.getElementById(select_name).value!=value) 
		{
			document.getElementById(layer_name).style.display = '';
		
		} else {
	
			document.getElementById(layer_name).style.display = 'none';
		}
	}
}

// Function rewrite actual fileld value to other field (works only on FF)
function rewiteFieldValue(field_id, that_value)
{
	document.getElementById(field_id).value=that_value;
}

// Function generate zebra effect in tables
// tableID - define table id from 'id' attribute
// bgColorFirst - define first color eg. #FFFFFF
// bgColorSecond - define second color eg. #000000
function tableZebraEffect(tableID, bgColorFirst, bgColorSecond) 
{
	// Prepare variables
	var even = false;
	var table = document.getElementById(tableID);
	
	// Prepare variables
	var tbodies = table.getElementsByTagName("tbody");

	for(var h = 0; h < tbodies.length; h++)
	{ 
		var trs = tbodies[h].getElementsByTagName("tr");
		
		for(var i = 0; i < trs.length; i++) 
		{	
			tds = trs[i].getElementsByTagName("td");
			
			for(var j = 0; j < tds.length; j++)
				tds[j].style.backgroundColor = (even) ? bgColorFirst : bgColorSecond;
			
			even = !even;
		}
	}
}

/* Function add BB tags*/
function BBtags(form_name, element_name, function_type, tag_type)
{
	p="document.forms[form_name].elements[element_name]";
	
	var stack=[], tags=[];

	function add_to_form(what)
	{
		c=eval(p);
	
		if(document.selection)
		{
			c.focus();document.selection.createRange().text=what;
	
		} else if (c.selectionStart || c.selectionStart=="0") {
	
			c.value=c.value.substring(0,c.selectionStart)+what+c.value.substring(c.selectionEnd,c.value.length);
	
		} else { 
	
			c.value+=what; 
		}
	}
	
	if (function_type=='emoticon') { add_to_form(tag_type); }
}

// Function opening new popup window with image inside (eg.: see in article inside image)
function popupWitchImage(width, height, image_src, title)
{
	var new_win=window.open('',"displayWindow","height="+height+" ,width="+width+" ,toolbar=no,menubar=no,location=no,status=no,resizable=yes,left=0,top=0,scrollbars=no", '', title); 
	new_win.document.open(); 
	new_win.document.write ('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>'+title+'</TITLE></HEAD><BODY><STYLE>body {padding: 0px 0px 0px 0px; margin: 0px 0px 0px 0px;}</STYLE><div align="center"><a href="#" onClick="parent.close();"><img src="'+image_src+'" border="0" alt=""></a></div></BODY></HTML>'); 
	new_win.document.close(); 
	new_win.focus(); 
}

// Function opening popup window with indicated url
// url - url open in popup window eg.: popup_calendar.php
// width - width of open window
// height - height of open window
// scrollbars - if want see new window with scrollbars set 'yes', if you don't - set 'no'
function popupSite(url, width, height, scrollbars)
{
	new_win = window.open(url, "displayWindow", "height="+height+", width="+width+", scrollbars="+scrollbars+", left=500, top=350");
}

// Function correctly show flash elements in all browsers (IE bugfix)
/*disable object embed activation click in ie by rl
use:
	flash.show(path,width,height,id,title,alter,params);
params ex: 
	wmode="transparent", quality="high", bgcolor="#ffcc33", allowScriptAccess="sameDomain", style="" */
var flash = {   
	show: function(path,width,height,id,title,alter,params){
	
		document.write(flash.build(path,width,height,id,title,alter,params));
	},
	param: function(params) {
		var tab = new Array();
		var obj = new Object();
		obj.list = new Array();
		if(params !='') {
			params = params.split(' ').join('');
			tab = params.split(',');
			for(var i=0; i<tab.length;i++) {
				obj[tab[i].split('="')[0]] = tab[i].split('="')[1].split('"')[0];
				obj.list.push(tab[i].split('=')[0]);
			}
		} else obj = '';
		return obj;
	},
	build: function(path,width,height,id,title,alter,params) {
		var txt;
		txt = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"';
		txt += ' title="'+title+'"';
		txt += ' alt="'+alter+'"';
		txt += ' width="'+width+'" height="'+height+'"';
		txt += ' id="'+id+'"';
		txt += ' style="'+flash.param(params)['style']+'">';
		txt +='<param name="movie" value="'+path+'" />';
		for(var i=0; i<flash.param(params)['list'].length;i++) {
			if(flash.param(params)['list'][i] != 'style') {
				txt +='<param name="'+flash.param(params)['list'][i]+'" value="'+flash.param(params)[flash.param(params)['list'][i]]+'"/>';
			}
		}
		
		txt += '<embed src="'+path+'"';
		txt += ' width="'+width+'" height="'+height+'"';
		txt += ' title="'+title+'"';
		txt += ' alt="'+alter+'"';
		for(var i=0; i<flash.param(params)['list'].length;i++) {
			if(flash.param(params)['list'][i] != 'style') {
				txt +=' '+flash.param(params)['list'][i]+'="'+flash.param(params)[flash.param(params)['list'][i]]+'"';
			}
		}
		
		txt +=' type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
		txt +='</object>';
		return txt;
	}
}


// Function counts string signs in input or textarea and block field when user insert too many signs
// cResultLayer - html layer name where counter insert result
// cFieldLayer - html layer name where counted field
// DmaxLenght - define field maximum lenght
function stringSignsCounterNblock(cFieldLayer, cResultLayer, DmaxLenght)
{
	var d=document.getElementById(cFieldLayer);
	var maxlength=DmaxLenght;
	
	function blockRow()
	{
		if (d.getAttribute && d.value.length > maxlength)
		{
			d.value=d.value.substring(0, maxlength);
			d.scrollTop=1000000; // Bugfix for FF, field scroolbar it will not go to top when function cut string
		}
	}
	
	document.getElementById(cResultLayer).innerHTML=d.value.length;
	blockRow();
	
	d.onkeydown=d.onkeyup=d.onclick=d.onmouseout=function(okd)
	{	
		with(this)
		{
			if(d.value.length > maxlength)
			{
				document.getElementById(cResultLayer).innerHTML=maxlength;
			
			} else {
				
				document.getElementById(cResultLayer).innerHTML=d.value.length;
			}
		}
		
		blockRow();
	}
}

/*###############################################################################################*/
/*# SIMPLE HTML EDITOR ################################################################################*/
/*###############################################################################################*/

function sHTMLEd_showHideSource(editorID, editorDivID, sourceFieldID, themeHeadSourceID)
{
	if(document.getElementById(sourceFieldID).style.display == 'none')
	{
		document.getElementById(sourceFieldID).style.display = '';
		document.getElementById(editorDivID).style.display = 'none';
		
		// Get source from HTML editor field
		var insertSource = document.getElementById(sourceFieldID);
		var headSectionSource = document.getElementById(themeHeadSourceID);
		//var themeBodySource = document.getElementById(editorID).contentWindow.document.body.innerHTML;
		var themeBodySource = document.getElementById(editorID).contentWindow.document.body.innerHTML;
		
		//insertSource.value = document.getElementById(editorID).contentWindow.document.documentElement.innerHTML;
		var overallHtml = headSectionSource.value+themeBodySource+"\r\n</body>\r\n</html>";
		//overallHtml = overallHtml.replace(/{%7B}/gi, "{");
		//overallHtml = overallHtml.replace(/{%7D}/gi, "}");
		insertSource.value = overallHtml;

	
	} else {
	
		document.getElementById(sourceFieldID).style.display = 'none';
		document.getElementById(editorDivID).style.display = '';
		
		// Get source from source field
		var insertSource = document.getElementById(editorID);
		var overallHtml = document.getElementById(sourceFieldID).value;
		//overallHtml = overallHtml.replace(/{%7B}/gi, "{");
		//overallHtml = overallHtml.replace(/{%7D}/gi, "}");
		//insertSource.contentWindow.innerHTML = document.getElementById(sourceFieldID).value;
		insertSource.contentWindow.document.body.innerHTML = overallHtml;
	}
}
