function slideInOut(obj)
{	
	if ($(obj+":first").is(":hidden")) {
		$(obj).slideDown("normal");
	} else {
		$(obj).slideUp("normal");
	}

}

function slideIn(obj){ $(obj).slideUp("normal"); }
function slideOut(obj){ $(obj).slideDown("normal"); }

function slideInOutPanel(divID,btnID)
{
    if ($(divID+":first").is(":hidden")) {
        $(btnID).removeClass('x-tool-collapse-south');
        $(btnID).addClass('x-tool-expand-south');
    } else {
        $(btnID).removeClass('x-tool-expand-south');
        $(btnID).addClass('x-tool-collapse-south');
    }
    slideInOut(divID);
}

function slideInPanel(divID,btnID)
{
    $('#'+btnID).removeClass('x-tool-expand-south');
    $('#'+btnID).addClass('x-tool-collapse-south');
    $(divID).slideUp("normal");
}

function slideOutPanel(divID,btnID)
{
    $('#'+btnID).removeClass('x-tool-collapse-south');
    $('#'+btnID).addClass('x-tool-expand-south');
    $(divID).slideDown("normal");
}

//functions for the panel class
var addClassBtn = function() {
    $('#'+this.id).addClass('x-btn-over');
};
var removeClassBtn = function() {
    $('#'+this.id).removeClass('x-btn-over');
};
var addClassFieldFocus = function() {
    $('#'+this.id).addClass('x-form-focus');
};
var removeClassFieldFocus = function() {
    $('#'+this.id).removeClass('x-form-focus');
};

$("select").bind("focus",addClassFieldFocus);
$("select").bind("blur",removeClassFieldFocus);
$("input").bind("focus",addClassFieldFocus);
$("input").bind("blur",removeClassFieldFocus);
$("textarea").bind("focus",addClassFieldFocus);
$("textarea").bind("blur",removeClassFieldFocus);
$(".x-btn-wrap").bind("mouseover",addClassBtn);
$(".x-btn-wrap").bind("mouseout",removeClassBtn);

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    } else {
        var expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}

function countCharLeft(txtarea,counterarea,text,numcharslimit) {
  var txtareaObj=getObject(txtarea);
  var counterareaObj=getObject(counterarea);
  var longitud=numcharslimit - txtareaObj.value.length;
  if(longitud <= 0) {
    longitud=0;
    text='<span class="disable"> '+text+' </span>';
    txtareaObj.value=txtareaObj.value.substr(0,numcharslimit);
  }
  counterareaObj.innerHTML = text.replace("{CHAR}",longitud);
}

function textCounter(field, cntfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	} else {
		cntfield.value = maxlimit - field.value.length;
	}
}

function toggleVisibility(elId) {
    var elem = document.getElementById(elId);
    if (elem.style.display == "none") {
        elem.style.display = "";
    } else {
        elem.style.display = "none";
    }
    document.body.focus();
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

//create cookies
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function setFocus(id){
	document.getElementById(id).focus();
}

function clearText(thefield){
	if (thefield.defaultValue==thefield.value) {
		thefield.value = "";
	}
} 

/*
* This function will control the checkbox with the "All" id. Whenever you click on the check all checkbox, it will check or uncheck all
* of the checkboxes on the form, but if you click in one by one checkbox, it will not enable the check all checkbox unless you use this function.
* This will help the user control be more friendly and error free
* Parameters:
* chk_All_ID = id of the checkbox that represents the "check all"
* formID	 = id of the form that wraps the checkboxes with the files 
*/
function uncheck_chkall(chk_All_ID,formID)
{
	var chk_to_uncheck = document.getElementById(chk_All_ID);
	var fmobj = document.forms[formID];
	var chkCounter = 0;
	var chkCheckedCounter = 0;
	
	for (var i=0;i<fmobj.elements.length;i++)
	{
		var e = fmobj.elements[i];
		if ( (e.id != chk_All_ID ) && (e.type=='checkbox') && (!e.disabled)) 
		{
			chkCounter = chkCounter + 1;
			if(e.checked)
			{
				chkCheckedCounter = chkCheckedCounter + 1;
			}
		}
	}
	
	el = document.getElementById(chk_All_ID);

	if(chk_to_uncheck.checked == true || chk_to_uncheck.checked == 'on' )
	{
		if(chkCheckedCounter == chkCounter)
		{
			chk_to_uncheck.checked = true;
			el.onclick = function onclick(event) { check(formID,'uncheck',chk_All_ID); };
		}
		else
		{
			chk_to_uncheck.checked = false;
			el.onclick = function onclick(event) { check(formID,'check',chk_All_ID); };
		}
	}
	else
	{
		if(chkCheckedCounter == chkCounter)
		{
			chk_to_uncheck.checked = true;
			el.onclick = function onclick(event) { check(formID,'uncheck',chk_All_ID); };
		}
		else
		{
			chk_to_uncheck.checked = false;
			el.onclick = function onclick(event) { check(formID,'check',chk_All_ID); };
		}
	}
}

/*
* This function will control the checkboxes on the form. It will check all checkboxes on the form or uncheck based on the onClick event
* from the checkbox with the "All" Id.
* This function will dinamicly change the onClick event of the checkbox All changing the type of the action that will be taken.
* Parameters:
* formID 	= id of the form that wraps the checkboxes with the files
* type 		= type of action (check or uncheck)
* chkAllID	= id of the checkbox that represents the "check all"
*/
function check(formID,type,chkAllID)
{
	var fmobj = document.forms[formID];
	
	for (var i=0;i<fmobj.elements.length;i++)
	{
		var e = fmobj.elements[i];
		
		if ( (e.id != chkAllID ) && (e.type=='checkbox') && (!e.disabled)) 
		{
			if(type == 'check')
			{
				e.checked = true;
				el = document.getElementById(chkAllID);
				el.onclick = function onclick(event) { check(formID,'uncheck',this.id); };
			}
			else
			{
				e.checked = false;
				el = document.getElementById(chkAllID);
				el.onclick = function onclick(event) { check(formID,'check',this.id); };
			}
		}
	}
	return true;
}
