var oNode = oNodeParent= null;


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 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 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 = '';
		//alert('Nothing to generate from.\nPlease try again.')
	}
}

var CurField = "";
var CurFieldInitValue = "";
var Content = Array();
var CurWhere = "";
var CurResponse = "";
var CurAction = "";
var sub = "";
var active = "";
var 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 = strip_spaces(http.responseText);
		var response = http.responseText;
		//hideSave();

		if(CurAction=="verifyField"){
			//alert(response);
			CurAction="";
			if(response==""){
				$(CurField).innerHTML = '<img src="img/ico_status_OK.gif" width="16" height="16" style="margin:-4px 0px;"/>';
				$('no_cmd').value = action;
				if(sub!="") $(sub).disabled = false;
				$(err).innerHTML = '';
			} else {
				$(CurField).innerHTML = '<img src="img/ico_status_NOK.gif" width="16" height="16" style="margin:-4px 0px;"/>';
				$('no_cmd').value = 'false';
				if(sub!="") $(sub).disabled = true;
				if(response!="empty" && response!="invalid") $(err).innerHTML = response;
				else $(err).innerHTML = '';
			}

		}
	}
}

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;

	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 verifyEmail(thisLabel, thisValue, action, submit_button){
	var fieldValue 		= thisValue;
	CurAction			= "verifyField";
	CurField			= "val_"+thisLabel;
	sub					= submit_button;
	err					= "err_"+thisLabel;

	var str = "value="+fieldValue;
	//alert(str);

	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 = "";
		}
	} 
	
/* ########################################################################################################################################################################################################################################################################*/
	// -------------------------------------------------------------------------------------------------------------
	// 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.visibility = 'visible';
	  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');
		}
	  }
	}
// ------------------------------------------------------------------------------------------------------------------------------------------------------------ //

