var oNode = oNodeParent = null;

/*@cc_on
(function(f){
window.setTimeout = f(window.setTimeout);
window.setInterval = f(window.setInterval);
})(function(f){
return function(c,t){
var a = Array.prototype.slice.call(arguments,2);
if(typeof c != "function")
c = new Function(c);
return f(function(){
c.apply(this, a)
}, t)
}
});
@*/


function evalScripts(__t){
	if((/\S/).test(__t) && (/<script[^>]*>/mi).test(__t)){
		for(var _i = 1,_ss = __t.split(/<script[^>]*>/mi),_s = _ss[_i], _cnt = _ss.length ;_i<_cnt;_i++,_s = _ss[_i]){
			try{
				eval(_s.substring(0,_s.indexOf('</'+'script>')));
			}catch(e){
				alert(e);
			}
		}
	}
}

function editContent(id,a, grpId){

	if(typeof a!= 'undefined'){
		a.style.display = 'none';
	}
	var f = document.createElement('FORM'),
		i_id = document.createElement('INPUT'),
		i_action = document.createElement('INPUT'),
		i_submit = document.createElement('INPUT'),
		i_cancel = document.createElement('INPUT'),
		t = document.createElement('textarea'),
		oNodeParent = document.getElementById(id);
	t.rows = 8; t.cols = 30;
	t.name = t.id = 'content';
	i_cancel.type='reset', i_cancel.value="cancel";
	i_cancel.onclick = function(){ restore(id,a); };
	i_submit.type = "submit";
	i_submit.value="save changes";

	i_action.type = i_id.type='hidden';
	i_id.name = i_id.id="grpId";
	i_id.value = grpId;


	i_action.value = 'update_homepage';
	i_action.name='no_cmd';


	f.id = "form_for_"+id;
	f.action = window.location;
	f.method = 'post';

	f.style.display = 'block';
	f.style.width="100%"
	f.appendChild(i_action);
	f.appendChild(i_id);
	f.appendChild(t);
	f.appendChild(i_submit);
	f.appendChild(i_cancel);

	oNode = oNodeParent.firstChild;
		t.value = oNode.innerHTML;
	oNodeParent.replaceChild(f,oNode);
	make_iactEd('content',grpId);
}

function showPreview(path,img,div){
	$(div).empty();
	if(img && img!=""){
		$(div).innerHTML = 'loading...';
		var i = new Image(100,100);
		i = $(i);
		i.src = path+img;
		i.addEvent('load', function () {
				this.style.margin = '4px 4px 4px 0';
				this.style.border = '1px solid #EA0478';
				$(div).empty()
				$(div).appendChild(this);
			}
		)
	}
}

function restore(id,a){
	if(!confirm("Are you sure you wish to cancel?\n All changes will be lost")) return;

	a.style.display = 'block';

	var oNodeParent = document.getElementById(id),
		f = oNodeParent.firstChild;
	oNodeParent.replaceChild(oNode, f);

}

function getParentForm(e){
	do{
		e = e.parentNode;
	}
	while(e && e.nodeName.toUpperCase() != 'FORM');
	return e;
}

function addFile(name,div){
	var f = document.createElement('input');
	var d = document.createElement('input');
	f.type = 'file';
	d.type = 'text';
	f.name = name;
	d.name = 'no_descr[]';
	f.className = 'inputtype';
	d.className = 'inputtype';
	$(div).appendChild(document.createElement('BR'));
	$(div).appendChild(f);
	$(div).appendChild(d);
}

function generateSeo(fromwhere, prefix, towhere, andwhere){
	try{
		var v = document.getElementById(fromwhere).value;
		if((/\S/).test(v)) {
			//v = v.replace(/%20|_|\s|\&|\.|\:|;/g,'-').replace(/,|"|\?|%|@|#|\$|\!|\^|\*|\(|\)|'/g,'').replace(/-+/g,'-').replace(/-$/,'').toLowerCase();
			v = v.replace(/%20|_|\s|\&|\.|\:|;/g,'').replace(/,|"|\?|%|@|#|\$|\!|\^|\*|\(|\)|\/|\\|'/g,'').replace(/-+/g,'').replace(/-$/,'').toLowerCase();
			$(towhere).innerHTML = (prefix||'')+v;
			$(andwhere).value = v;
		} else { throw new Exception('Something went horribly wrong') };
	} catch (e){
		document.getElementById(towhere).innerHTML = '';
	}
}

window.CurField = "";
window.CurFieldInitValue = "";
window.Content = Array();
window.CurWhere = "";
window.CurResponse = "";
window.CurAction = "";
window.sub = "";
window.active = "";
window.err = "";

function createRequestObject(){
	// create the XML object
	var request_o;
	var browser = navigator.appName;
	if( browser == "Microsoft Internet Explorer"){
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		request_o = new XMLHttpRequest();
	}
	return request_o;
}

var http = createRequestObject();


function handleResponse(action,err){

	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;

		if(CurAction=="verifyField"){
			CurAction="";
			if(response==""){
				if(CurField != 'val_undefined'){
					$(CurField).innerHTML = '<img src="'+SITE_LINK+'img/ico_status_OK.gif" width="16" height="16" style="margin:-4px 0px;"/>';
				}
				$('no_cmd').value = action;
				if(typeof sub != "undefined") $(sub).disabled = false;
				if(err != 'err_undefined') $(err).innerHTML = '';
			} else {
				if(CurField != 'val_undefined'){
					$(CurField).innerHTML = '<img src="'+SITE_LINK+'img/ico_status_NOK.gif" width="16" height="16" style="margin:-4px 0px;"/>';
				}
				$('no_cmd').value = 'false';
				if(typeof sub != "undefined") $(sub).disabled = true;
				if(response!="empty" && response!="invalid") $(err).innerHTML = response;
				else if(err != 'err_undefined') $(err).innerHTML = '';
			}
		}
	}
}

function handleTemplatePreview(div){
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		$(div).innerHTML = response;
		evalScripts(response);
	}
}

function handleCart(){
	if(http.readyState == 4){ //Finished loading the response
		var response = http.responseText;
		eval(response);
	}
}

function verifyNickname(thisLabel, thisValue, action, submit_button){
	var fieldValue 		= thisValue;
	CurAction			= "verifyField";
	CurField			= "val_"+thisLabel;
	sub					= submit_button;
	err					= "err_"+thisLabel;

	var str = "value="+fieldValue+"&type="+action;

	http.open('post',  (SITE_LINK || '')+'modules/checkNick.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = function() { handleResponse(action, err); }
	http.send(str);
}

function verifyGroupName(thisLabel, thisValue, action, submit_button){
	var fieldValue 		= thisValue;
	CurAction			= "verifyField";
	CurField			= "val_"+thisLabel;
	sub					= submit_button;
	err					= "err_"+thisLabel;

	var str = "value="+fieldValue;

	http.open('post',  (SITE_LINK || '')+'modules/checkGroup.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = function() { handleResponse(action,err); }
	http.send(str);
}

function verifyEmail(thisLabel, thisValue, action, submit_button){
	var fieldValue 		= thisValue;
	CurAction			= "verifyField";
	CurField			= "val_"+thisLabel;
	sub					= submit_button;
	err					= "err_"+thisLabel;

	var str = "value="+fieldValue;

	http.open('post',  (SITE_LINK || '')+'modules/checkEmail.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = function() { handleResponse(action,err); }
	http.send(str);
}

function verifyDelay(thisLabel, thisValue, action, submit_button,func){
	window.clearTimeout(active);
	active = window.setTimeout(func,750,thisLabel,thisValue,action,submit_button);
}

function expandDiv(what) {
	if (document.getElementById(what).style.display == "none") {
		document.getElementById(what).style.display = "";
		//lastOpenDiv = what;
	} else {
		document.getElementById(what).style.display = "none";
		//lastOpenDiv = "";
	}
}

function showTemplatePreview(id,div){
	http.open('post',  SITE_LINK+'modules/showTemplatePreview.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = function() { handleTemplatePreview(div); }
	http.send("id="+id);
}

/* -------------- CART FUNCTIONS ----------------------- */

function sendToCart(str){
	http.open('post',SITE_LINK+'modules/cart.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = function() { handleCart(); }
	http.send(str);
}

/* --------------- ACTIVATE/DISABLE GROUP -------------- */

function setGrpState(grp,seo,state){
	http.open('post',SITE_LINK+'_config.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = function() {
		if(http.readyState != 4) return;
		else document.location = SITE_LINK+seo+'/';
	}
	http.send('no_cmd=set_group_state&grp='+grp+'&state='+state);
}

/* ------------- GENERATE PAYPAL CODE ----------------- */

function generatePayPal(arr,div){
	var ppcode = '[PAYPAL:';
	var tmp = [];
	for(var i=0;i<arr.length; i++){
		if(arr[i].value!=''){
			tmp.push(arr[i].value);
		}else{
			tmp.push('');
		}
	}
	ppcode += tmp.join('+++')+']';
	$(div).value = ppcode;
}

/* ########################################################################################################################################################################################################################################################################*/
	// -------------------------------------------------------------------------------------------------------------
	// Fade effect taken from:
	// http://clagnut.com/sandbox/imagefades/
	// All credits for the fade effect goes to Richard Rutter
	// Modified/Improved by Interact 2006-2008
	// -------------------------------------------------------------------------------------------------------------
	function fadeDiv(what, action, howmuch) {
	  image = document.getElementById(what);
	  //image.style.style = 'inline';
	  if(action=="out"){
		setOpacity(image, 100);
		fadeOut(what,100);
	  } else if(action=="in") {
		image.style.display = "";
		setOpacity(image, 0);
		fadeIn(what,0);
	  } else if(howmuch && action==""){
		setOpacity(image, howmuch);
	  }
	}
// ------------------------------------------------------------------------------------------------------------------------------------------------------------ //
	function setOpacity(obj, opacity) {
	  opacity 					= (opacity == 100)?99.999:opacity;
		if(obj.style.filter){
	 		obj.style.filter 			= "alpha(opacity:"+opacity+")";		// IE/Win
		} else if(obj.style.filter){
			obj.style.filter 			= "alpha(opacity="+opacity+")";		// Safari<1.2, Konqueror
		} else if(obj.style.KHTMLOpacity){
			obj.style.KHTMLOpacity 		= opacity/100; 						// ie
	  	} else if(obj.style.MozOpacity){
			obj.style.MozOpacity 		= opacity/100;						// Older Mozilla and Firefox
	 	 } else if(obj.style.opacity){
			obj.style.opacity 			= opacity/100;						// Safari 1.2, newer Firefox and Mozilla, CSS3
	 	 } else {
			 obj.style.opacity 			= opacity/100;
		}
	}

// ------------------------------------------------------------------------------------------------------------------------------------------------------------ //
	function fadeIn(objId,opacity) {
	  if (document.getElementById) {
		obj = document.getElementById(objId);
		obj.style.display = "";
		if (opacity <= 100) {
		  setOpacity(obj, opacity);
		  opacity += 50;
		  window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
		}
	  }
	}
// ------------------------------------------------------------------------------------------------------------------------------------------------------------ //
	function fadeOut(objId,opacity) {
	  if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity >= 0) {
		  setOpacity(obj, opacity);
		  opacity -= 20*1.4;
		  window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 100);
		} else {
			obj.style.display = "none";
			setOpacity(obj, '100');
		}
	  }
	}
// ------------------------------------------------------------------------------------------------------------------------------------------------------------ //
	function makeWizzy(){
		var wizElements = document.getElementsByTagName('textarea');
		for (var i=0;i<wizElements.length;i++){
			var relAttribute = String(wizElements[i].getAttribute('rel'));
			if (relAttribute.toLowerCase().match('wizzy')){
				make_iactEd(wizElements[i].id,'full');
			}
		}
	}

	function updateWizzyFileds(){
		var wizElements = document.getElementsByTagName('textarea');
		for (var i=0;i<wizElements.length;i++){
			var relAttribute = String(wizElements[i].getAttribute('rel'));
			if (relAttribute.toLowerCase().match('wizzy')){
				//window[wizElements[i].id+"_ED"].instanceById(wizElements[i].id).saveContent();
				updateTextArea(wizElements[i].id);
			}
		}
	}

