/*
 * Using Prototype Updater object to load the content of
 * toAppear php page into <div id="content">.
 *
 * @param 	toAppear	The name of the .php page to load.
 *
 */
function inout(toAppear) {
	new Ajax.Updater('content', toAppear);
}


/*
 * Using Prototype Updater object to load the content of
 * toAppear php page into <div id="divId">.
 *
 * @author: Hoby RAVELOMANANTSOA h.ravelomanantsoa@devcommunication.com
 * @param: divId : id of the div where the content will be put
 * @param: toAppear	The name of the .php page to load.
 */
function inout2(divId, toAppear) {
	new Ajax.Updater(divId, toAppear, {evalScripts: true});
	$(document.body).setStyle('cursor: default;');
}

/*
 * inoutForm submit a form identified by its id formId to the url,
 * and put the response content into a div divDestinationId
 *
 * @author: hoby RAVELOMANANTSOA h.ravelomanantsoa@devcommunication.com
 * @param: formId: the id of the form to submit
 * @param: url: the url of server-side handler
 * @param: divDestinationId: the id of a div where to put the response.
 */
function inoutForm (formId, url, divDestinationId) {
	var form_content = $(formId).serialize(true);
	/*$(divDestinationId).innerHTML =  "<img src=\"img/spinner.gif\"/>";*/
	new Ajax.Updater(divDestinationId, url, {parameters: form_content, evalScripts: true});	
}

/*
* display the url into the divDestinnation and send complementary parameters,
 *
 * @author: Modeste TATA m.tata@devcommunication.com
 * @param: params: the parameters to send
 * @param: url: the url of server-side handler
 * @param: divDestinationId: the id of a div where to put the response.
*/
function inout_using_post(divDestinationId,url,parms) {
	/*$(divDestinationId).innerHTML =  "<img src=\"img/spinner.gif\"/>";*/
	new Ajax.Updater(divDestinationId, url, {method: 'post', parameters: parms, evalScripts: true});
}

/*
 * setNameAsId set the attribute name of an element as it's id value;
 *
 * @author: Hoby RAVELOMANANTSOA h.ravelomanantsoa@devcommunication.com
 * @param: id the id of the element;
 */
function setNameAsId (id) {
	$(id).writeAttribute('name', id);
}

/*
 * append a textbox at the end of div 'divId'
 *
 * @param: divId is the div id
 * @param: value is the value of the textbox
 */
function appendTextbox(divId, value) {
    var id = new Date();
    id = id.getTime();
    var text = "<span id='"+id+"'><br/><input type='textbox' name='"+ divId +"[]' value='"+ value +"'/><img ";
    text += "src='img/silk_delete.png' alt='-' title='Delete' onclick=\"removeBox('"+id+"')\" /></span>";
    new Insertion.Bottom(divId, text);
}

/*
 * append a listbox at the end of div with id 'divId'
 *
 * @param: divId is the div id
 * @param: optionArray contains the options
 */
function appendListbox(divId, optionArray) {
    var id = new Date();
    id = id.getTime();
    var text = "<span id='"+id+"'><select name='" + divId + "[]' onchange=\"checkIfAll(this,'"+divId+"_appendListbox');\">";
    text += "<option value=''></option>";

    for (val=0; val < optionArray.length ; val=val+1 ) {
	text += "<option value='" + optionArray[val] + "'>" + optionArray[val] + "</option>";
    }
    text += "</select>&nbsp;<a href='#' onclick=\"removeBox('"+id+"');return false;\"><img src='img/silk_delete.png' alt='-' title='Delete' /></a><br/></span>";

    new Insertion.Bottom(divId, text);
}

/*
 * ask with a popup if the user accept or not the question in 'txt'
 * @param: txt is the text to show in the popup
 */
function ask_confirmation(txt) {
    var resultat = confirm(txt);
    if (resultat=="1") {
	return true;
    } else {
     	return false;
    }
}

/*
 * this function is used to show in settings the output from
 * 'console.inc'
 *
 * @param: protocol (sip|iax|dnsmgr)
 * @param: Cmd is the command to have the settings
 *
 */
function showSettings(protocol,Cmd) {
    $('hide_'+protocol+'_settings').show();
    $('show_'+protocol+'_settings').hide();

    consoleResponse(protocol+'_show_settings',Cmd,0);

    $(protocol+'_show_settings').show();
  }

/*
 * this function is used to hide the showed settings
 * @param: protocol (sip|iax|dnsmgr)
 */
function hideSettings(protocol) {
    $(protocol+'_show_settings').hide();

    $('show_'+protocol+'_settings').show();
    $('hide_'+protocol+'_settings').hide();
}

/*
 * this function updates 'divId' with the output from the command
 *
 * @param: divId is the target div id
 * @param: Cmd is the command to execute
 * @param: Vb is the verbosity
 */
function consoleResponse(divId,Cmd, Vb) {
    new Ajax.Updater(divId, 'include/console.inc',
		     {
			 method: 'get',
			 parameters: { command: Cmd, verbose: Vb }
		     }
		     );
}

/*
 * this function check if the selected option is 'all'.
 * If it's the case, it hides 'divId'
 *
 * @param: selSelectObject is the select object
 * @param: divId is the target id
 *
 */
function checkIfAll(selSelectObject, divId) {
  if (selSelectObject.options[selSelectObject.selectedIndex].value == "all") {
      $(divId).hide();
  } else {
      $(divId).show();
  }
}
/*
 * this function unchecks the 'id' element
 *
 * @param: id is the target id
 */
function uncheck(id) {
    if (document.getElementById(id).checked == true ) {
	document.getElementById(id).checked=false;
    }
}

/*
 * this function removes the 'id' element
 *
 * @param: id is the target id
 */
function removeBox(id) {
    $(id).remove();
}

/*
 * this function hides the 'id' element
 *
 * @param: id is the target id
 */
function hideBox(id) {
    $(id).hide();
}

/*
 * this function shows the 'id' element
 *
 * @param: id is the target id
 */
function showBox(id) {
    $(id).show();
}

/*
 * this function hides the 'id' element after 15 seconds
 *
 * @param id is the target id
 */
function autoHideMsgBox(id) {
  	setTimeout("Effect.Fade('"+id+"')",15000);
}

/*
 * this function hides all sections in res_ldap
 */
function hideLdapSectionAll() {
    $('res_ldap_general','res_ldap_config','res_ldap_extensions','res_ldap_sip','res_ldap_iax').invoke('hide');
}

/*
 * this function paints the actions buttons on form change
 */
function formChanged() {
 /*   try {
	$('submit_button').setStyle({
	      backgroundColor: '#009f00',
	      fontWeight: 'bold',
	      color: 'white'
      });

	$('reset_button').setStyle({
	      backgroundColor: '#ffa500',
	      fontWeight: 'bold',
	      color: 'white'
      });
    } catch(e) {
	alert(e.message);
    }
	*/
}

/*
 * this function unchecks the checkboxes in cdr advanced search
 */
function uncheckAdvancedSearch(name,selected) {
    var types =new Array("exact","begin","contains","end");
    for (val=0; val < types.length ; val=val+1 ) {
	if (types[val] != selected ) {
	    uncheck(name +"_"+types[val]);
	}
    }
}
