var hide_timer=null;
var hide_obj=null;
var cur_menu=null;

function cleantime() {
	clearTimeout(hide_timer);
	hide_timer=null;
}


function showPull(id,target)
{
	cleantime();
	if(cur_menu!=null && cur_menu!=id) hide_now();

	cur_menu=id;
	locobj=document.getElementById(id);
	x=locobj.offsetLeft;
	y=locobj.offsetTop;
	obj=document.getElementById(target);
	obj.style.top=y+18+'px';
	obj.style.left=x-15+'px';
	obj.style.visibility='visible';
}

function hide_now() {
	cleantime();
	if(hide_obj!=null) {
		hide_obj.style.visibility='hidden';
		hide_obj=null;
	}
}

function hidePull(id)
{
	hide_obj=document.getElementById(id);
	hide_timer=setTimeout('hide_now()',140);
}

function changeClass(target,cssClass)
{
	obj = document.getElementById(target);
	obj.className=cssClass;
}


function isEnter(event)
{
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) return true;
}

function limitText(limitField, limitCount) {
	var limitNum = document.getElementById(limitField).getAttribute('maxLength');
	if (document.getElementById(limitField).value.length > limitNum) {
		document.getElementById(limitField).value = document.getElementById(limitField).value.substring(0, limitNum);
	} else {
		var o=document.getElementById(limitCount);
		if(o!=null) {
			setTimeout(function() {
				o.innerHTML= limitNum - document.getElementById(limitField).value.length;
			}, 50);
		}
	}
}

function URLDecode(encoded)
{
	// Replace + with ' '
	// Replace %xx with equivalent character
	// Put [ERROR] in output if %xx is invalid.
	var HEXCHARS = "0123456789ABCDEFabcdef";
	var plaintext = "";
	var i = 0;
	while (i < encoded.length) {
	var ch = encoded.charAt(i);
	if (ch == "+") {
		plaintext += " ";
		i++;
	} else if (ch == "%") {
		if (i < (encoded.length-2)
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
			plaintext += ch;
			i++;
		}
	} // while
	return plaintext;
};

// AJAX CALLS
/**
 * Concatenates the values of a variable into an easily readable string
 * by Matt Hackett [scriptnode.com]
 * @param {Object} x The variable to debug
 * @param {Number} max The maximum number of recursions allowed (keep low, around 5 for HTML elements to prevent errors) [default: 10]
 * @param {String} sep The separator to use between [default: a single space ' ']
 * @param {Number} l The current level deep (amount of recursion). Do not use this parameter: it's for the function's own use
 */
function print_r(x, max, sep, l) {
	l = l || 0;
	max = max || 10;
	sep = sep || ' ';
	if (l > max) {
		return "[WARNING: Too much recursion]\n";
	}
	var
		i,
		r = '',
		t = typeof x,
		tab = '';
	if (x === null) {
		r += "(null)\n";
	} else if (t == 'object') {
		l++;
		for (i = 0; i < l; i++) {
			tab += sep;
		}
		if (x && x.length) {
			t = 'array';
		}
		r += '(' + t + ") :\n";
		for (i in x) {
			try {
				r += tab + '[' + i + '] : ' + print_r(x[i], max, sep, (l + 1));
			} catch(e) {
				return "[ERROR: " + e + "]\n";
			}
		}
	} else {
		if (t == 'string') {
			if (x == '') {
				x = '(empty)';
			}
		}
		r += '(' + t + ') ' + x + "\n";
	}
	return r;
};


function mislukt(a,b){
	alert('Error');
}


function gelukt(request,opt){
	var formObj= document.getElementById(opt['form']);
	var targetObj= document.getElementById(formObj.target.value);
	//alert(print_r(opt));
	if (request.responseText)
	{
		targetObj.innerHTML = request.responseText;
	}
}

function do_ajax_post()
{
	doLoading();
	Ext.Ajax.request({
	//	url: '/ajax/do_ajaxcall/ajax_doajaxcall.html',
		form: 'test_from',
		params: 'return_html',
		success: gelukt,
		failure: mislukt
	});
}



function doLoading()
{
	document.getElementById('finalCSS').innerHTML = '<img src="/img/loading.gif" width="24" height="24" border="0" alt="" align="left"> <span style="margin-left: 5px;font-size:18px;"><b>Loading...</b></span>';
}

function clearFields()
{
	document.getElementById('inputCSStext').value = '';
	document.getElementById('inputCSStext').className = 'activeInput';
	document.getElementById('inputCSSfile').value = '';
	document.getElementById('inputCSSfile').className = 'activeInput';
}

function checkVars()
{
	return('action=submitCSS&cssText='+
		escape(document.getElementById('inputCSStext').value)+
		'&cssFile='+document.getElementById('inputCSSfile').value+
		'&optionRemovelastsemicolon='+document.getElementById('optionRemovelastsemicolon').checked+
		'&optionCompressfont='+document.getElementById('optionCompressfont').checked+
		'&compression1='+document.getElementById('compression1').checked+
		'&compression2='+document.getElementById('compression2').checked+
		'&compression4='+document.getElementById('compression4').checked
	);
}

function copyToClipboard(s)
{
	var div = document.getElementById(s);
	div.contentEditable = 'true';
	var controlRange;
	if (document.body.createControlRange) {
		controlRange = document.body.createControlRange();
		controlRange.addElement(div);
		controlRange.execCommand('Copy');
	}
	div.contentEditable = 'false';
}

function contactUs(pre,post,domain,text)
{
	document.write('<a href="mailto:'+pre+'@'+post+domain+'">'+text+'</a>')
}

function fnSelect(objId) {
	fnDeSelect();
	if (document.selection) {
		var range = document.body.createTextRange();
		range.moveToElementText(document.getElementById(objId));
		range.select();
	}
	else if (window.getSelection) {
		var range = document.createRange();
		range.selectNode(document.getElementById(objId));
		window.getSelection().addRange(range);
	}
}

function fnDeSelect() {
	if (document.selection) document.selection.empty();
	else if (window.getSelection)
	window.getSelection().removeAllRanges();
}

function setChecked()
{
	//document.getElementById('optionOptimizeshorthands').checked = false;
	document.getElementById('optionStripremarks').checked = false;
	document.getElementById('optionRemovelastsemicolon').checked = false;
	document.getElementById('optionCompressfont').checked = false;
	document.getElementById('optionCompresscolor').checked = false;
	//document.getElementById('optionSortselectors').checked = false;
	//document.getElementById('optionSortproperties').checked = false;
	document.getElementById('optionSavefile').checked = false;
	document.getElementById('optionShowlog').checked = false;

	for(var i=0; i<arguments.length; i++)
		document.getElementById(arguments[i]).checked = true;
}


