/**
 * all function 20MAR2002 Version 6.0
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : all function for all pages
 //Copyright (c) 2007 osTicket.com
 */
 
/**
 * all function getCannedResponse
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to getCannedResponse
 */
function getCannedResponse(idx,fObj)
{
    if(idx==0) { return false; }
    Http.get({
        url: "ajax.php?api=kbase&f=cannedResp&id="+idx,
        callback: setCannedResponse
    },[fObj]);
}
/**
 * all function setCannedResponse
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to setCannedResponse
 */
function setCannedResponse(xmlreply,fObj)
{
    if (xmlreply.status == Http.Status.OK)
    {
        var resp=xmlreply.responseText;
        if(fObj.response && resp){
            fObj.response.value=(fObj.append && fObj.append.checked)?trim(fObj.response.value+"\n\n"+resp):trim(resp)+"\n\n";
        }else {
            alert("Invalid form or tag");
        }
    }
    else{
        alert("Cannot handle the AJAX call. Error#"+ xmlreply.status);
    }
}
/**
 * all function getSelectedCheckbox
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to getSelectedCheckbox
 */
function getSelectedCheckbox(formObj) {
   var retArr = new Array();
   var x=0;
	for (var i= 0; i< formObj.length; i++)
    {
        fldObj = formObj.elements[i];
        if ((fldObj.type == 'checkbox') && fldObj.checked)
			retArr[x++]=fldObj.value;
   	}
   return retArr;
} 

/**
 * all function selectAll
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to selectAll
 */
function selectAll(formObj,task,highlight)
{
   var highlight = highlight || false;

   for (var i=0;i < formObj.length;i++)
   {
      var e = formObj.elements[i];
      if (e.type == 'checkbox')
      {
         if(task==0) {
            e.checked =false;
         }else if(task==1) {
            e.checked = true;
         }else{
            e.checked = (e.checked) ? false : true;
         }
         
	     if(highlight) {
			highLight(e.value,e.checked);
		 }
       }
   }
   //Return false..to mute submits or href.
   return false;
}
/**
 * all function reset_all
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to reset_all
 */
function reset_all(formObj){
    return selectAll(formObj,0,true);
}
/**
 * all function select_all
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to select_all
 */
function select_all(formObj,highlight){
    return selectAll(formObj,1,highlight);
}
/**
 * all function toogle_all
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to toogle_all
 */
function toogle_all(formObj,highlight){

	var highlight = highlight || false;
    
	return selectAll(formObj,2,highlight);
}
/**
 * all function set_data
 * Created By: Tanvi.M.Patel
 * Created On: 17 March 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to set the data in hidden field
 */
function set_data(data){
		document.getElementById('setdata').value=data;
}
/**
 * all function checkbox_checker
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to checkbox_checker
 */
function checkbox_checker(formObj, min,max,sure,action)
{
	var checked=getSelectedCheckbox(formObj); 
	var total=checked.length;
    var action= action?action:"process";
	var data = document.getElementById('setdata').value;
 	if(formObj.chkParent.checked)
	{
		total=total-1;
	}
	if (max>0 && total > max )
 	{
 		msg="You're limited to only " + max + " selections.\n"
 		msg=msg + "You have made " + total + " selections.\n"
 		msg=msg + "Please remove " + (total-max) + " selection(s)."
 		alert(msg)
 		return (false);
 	}
 
 	if (total< min )
 	{
 		//alert("Please select at least " + min + " record  to " + data + ". " + total + " entered so far.")
		alert("Please select at least " + min + " record  to " + data)
 		return (false);
 	}
   
  if(sure){
  	if(confirm("PLEASE CONFIRM\n About to "+ action +" "+ total + " record(s).")){
 		return (true);
  	}else{
        reset_all(formObj);
	 	return (false);
  	}
  }
 
  return (true);
}
/**
 * all function toggleLayer
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to toggleLayer
 */
function toggleLayer(whichLayer) {
    var elem, vis;

    if( document.getElementById ) // this is the way the standards work
        elem = document.getElementById( whichLayer );
    else if( document.all ) // this is the way old msie versions work
        elem = document.all[whichLayer];
    else if( document.layers ) // this is the way nn4 works
        elem = document.layers[whichLayer];
  
    vis = elem.style;
    // if the style.display value is blank we try to figure it out here
    if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
        vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
    vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

/**
 * all function showHide
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to showHide
 */
function showHide(){

	for (var i=0; i<showHide.arguments.length; i++){
        toggleLayer(showHide.arguments[i]);
	}
    return false;
}
/**
 * all function visi
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to visi
 */
function visi(){	 
	for (var i=0; i<visi.arguments.length; i++){
        var element = document.getElementById(visi.arguments[i]);
        element.style.visibility=(element.style.visibility == "hidden")?"visible" : "hidden";
    }
}
/**
 * all function visible
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to visible
 */
function visible(id){
	var element = document.getElementById(id).style.visibility="visible";
}

/**
 * all function highLight
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to highLight
 */
function highLight(trid,checked) {

    var class_name='highlight';
    var elem;

    if( document.getElementById )
        elem = document.getElementById( trid );
    else if( document.all )
        elem = document.all[trid];
    else if( document.layers )
        elem = document.layers[trid];
    if(elem){
        var found=false;
        var temparray=elem.className.split(' ');
        for(var i=0;i<temparray.length;i++){
            if(temparray[i]==class_name){found=true;}
        }
        if(found && checked) { return; }

        if(found && checked==false){ //remove
            var rep=elem.className.match(' '+class_name)?' '+class_name:class_name;
            elem.className=elem.className.replace(rep,'');
        }
        if(checked && found==false) { //add
            elem.className+=elem.className?' '+class_name:class_name;
        }
    }
}
/**
 * all function highLightToggle
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to highLightToggle
 */
function highLightToggle(trid) {

    var class_name='highlight';
    var e;
    if( document.getElementById )
        e = document.getElementById(trid);
    else if( document.all )
        e = document.all[trid];
    else if( document.layers )
        e = document.layers[trid];

    if(e){
        var found=false;
        var temparray=e.className.split(' ');
        for(var i=0;i<temparray.length;i++){
            if(temparray[i]==class_name){found=true;}
        }
        if(found){ //remove
            var rep=e.className.match(' '+class_name)?' '+class_name:class_name;
            e.className=e.className.replace(rep,'');
        }else { //add
            e.className+=e.className?' '+class_name:class_name;
        }
    }
}
/**
 * all function trim
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to trim
 */
function trim (str) {
    str = this != window? this : str;
    return str.replace(/^\s+/,'').replace(/\s+$/,'');
}
/**
 * all function strcmp
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to strcmp
 */
function strcmp(){
    var arg1=arguments[0];
    if(arg1) {
        for (var i=1; i<arguments.length; i++){
            if(arg1==arguments[i])
                return true;
        }
    }
    return false;
}

/**
 * all function defCombo
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to defCombo
 */
function defCombo(combo, val) {
for(var i=0; i<combo.length; i++)
if(combo.options[i].value == val) combo.options[i].selected = true;
}

/**
 * all function setCheckedValue
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : 
 // 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;
		}
	}
}

/**
 * all function change
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to change
 */
function change(sid, targetid){

var dropdownIndex = document.getElementById(sid).selectedIndex;
var dropdownValue = document.getElementById(sid)[dropdownIndex].value;

if(dropdownValue != ""){
ajax_loadContent(targetid,'companynet.sorder.php?id='+dropdownValue);
}

}
/**
 * all function setchange
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to setchange
 */
function setchange(sid, targetid, val){
	
ajax_loadContent(targetid,'companynet.sorder.php?id='+sid+'&val='+val);

}
/**
 * all function showimage
 * Created By: Krunal
 * Created On: 06 January 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to showimage
 */
function showimage(sid, targetid){

var dropdownIndex = document.getElementById(sid).selectedIndex;
var dropdownValue = document.getElementById(sid)[dropdownIndex].value;

if(dropdownValue != ""){
ajax_loadContent(targetid,'image.php?id='+dropdownValue);
}
	
}
/**
 * all function fnChangeLanguage
 * Created By: Tanvi.M.Patel
 * Created On: 17 March 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to change language
 */
function fnChangeLanguage(langId)
{
		window.location = document.getElementById('incomingstring').value+"langid="+langId;
}
function fnChangeDateFormat(url, format)
{
	url = url+"&format="+format;
	window.location = url;
}
/**
 * all function fnBlankUsername
 * Created By: Krunal
 * Created On: 18 March 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to change language
 */
function fnBlankUsername()
{
	if(document.form1.username.value=="Username")
	{
		document.form1.username.value="";
		//document.form1.password.value="";
	}
}
/**
 * all function fnFillUsername
 * Created By: Krunal
 * Created On: 18 March 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : to change language
 */
function fnFillUsername()
{
	if(document.form1.username.value=="")
	{
		document.form1.username.value="Username";
		//document.form1.password.value="Password";
	}
		
}
/**
 * all function fnCheckUserAvailability
 * Created By: Nirav Patel
 * Created On: 2 April 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : To Check is User is available or not
 */
function fnCheckUserAvailability()
{
		strAjaxURL = jQuery("#hdnBaseURL").val();
		strURL = strAjaxURL + "ajax/checkuser.php";
		//alert(strURL);
		if(jQuery("#txtUsername").val() == "")
		{
			alert("Please Enter Username");
			jQuery("#txtUsername").focus();
			return false;
		}
		jQuery("#ajax_loader").show();
		intUserId = "";
		
		if(jQuery("#hdnAction").val() == "editUser" || jQuery("#hdnAction").val() == "changeConfiguration")
		{
			intUserId = jQuery("#intId").val();	
		}
		
		strPostData = "username="+jQuery("#txtUsername").val()+"&strPage="+jQuery("#hdnAction").val()+"&intUserId="+intUserId;
		
		jQuery.ajax({
		  type: "POST",				
		  data: strPostData,
		  async:   true,
		  url: strURL,
		  cache: false,
		  success: function(response){			  			  
			  jQuery("#ajax_loader").hide();
			  jQuery("#divUserAvailable").html(response);
		  }
		});
}
/**
 * all function showSmtpDiv
 * Created By: Nirav Patel
 * Created On: 2 April 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : To Change display style to "Block" of  SMTP div
 */

function showAuthSmtpDiv()
{
	document.getElementById("divSmtp").style.display = "block";	 
	document.getElementById("divHostName").style.display = "block";	 
	document.getElementById("divUserPassword").style.display = "block";	 	
}
function showNoAuthSmtpDiv()
{
	document.getElementById("divSmtp").style.display = "block";	 
	document.getElementById("divHostName").style.display = "block";	 
	document.getElementById("divUserPassword").style.display = "none";	
}
/**
 * all function hideSmtpDiv
 * Created By: Nirav Patel
 * Created On: 2 April 2010
 * Last Modified By:
 * Last modified On:
 * Purpose : To Change display style to "none" of  SMTP div
 */
function hideSmtpDiv()
{
	document.getElementById("divSmtp").style.display = "none";	 
}
function fnChangeDefaultLanguage(langCode)
{
	site_url = document.getElementById("siteurl").value;
	redirectUrl = site_url+"settings.php?f=def&langcode="+langCode;
	window.location = redirectUrl;
}