function rmbr(string){
    var regexp = /(<br>)|(<br\/>)/gi;
     // IE needs the newlines explicitly
    if (isIE == true) {
        string = string.replace(regexp, '\n');
    } else {
        string = string.replace(regexp, '');
    }

    return string;
}

function nl2br(string){
    var regexp = /\n/g;
    string = string.replace(regexp, '<br>\n');
    return string;
}

function ReassignTicketSuccess(t){
	$('assignedtoname').innerHTML = t.responseText;
	unsetStatus('', 1);	
}

function ReassignTicket(ticket,fuse,action){
	
	var userid = $('assigntoid').value;
	if(userid==0) return;
	
	setStatus('Assigning Ticket...');
	var opt = {
		method: 'post',
		postBody: 'ticket='+ticket+'&fuse='+fuse+'&action='+action+'&userid='+userid,			
		onSuccess: ReassignTicketSuccess,
		on404: on404,
		onFailure: onFailure
	}		
	new Ajax.Request('index.php', opt);
}

function ChangePrioritySuccess(t){
    $('tdPriority').innerHTML = "<a class='liveupdate' href=\"javascript:ShowEditBox('tdPriority','5','5013','priority');\">"+t.responseText+"</a>";
    $('originalPriority').value = t.responseText;
    if(t.responseText=='1') $('tdPriority').className = "innerPriorityOne";
    if(t.responseText=='2') $('tdPriority').className = "innerPriorityTwo";
    if(t.responseText=='3') $('tdPriority').className = "formField";
    unsetStatus('', 1);
}

function ChangePriority(ticket,fuse,action){
    var priority = $('chPriority').value;
	if(priority==-1){
	    $('tdPriority').innerHTML = "<a class='liveupdate' href=\"javascript:ShowEditBox('tdPriority','5','5013','priority');\">"+$('originalPriority').value+"</a>";
	    return;
	}
	
	setStatus('Changing Ticket Priority...');
	var opt = {
		method: 'post',
		postBody: 'ticket='+ticket+'&fuse='+fuse+'&action='+action+'&priority='+priority,			
		onSuccess: ChangePrioritySuccess,
		on404: on404,
		onFailure: onFailure
	}		
	new Ajax.Request('index.php', opt);
}

function ShowEditBox(id,fuse,action,field){
    if (field=='priority'){
        $(id).innerHTML = "<select onkeypress='return noenter();' id='chPriority' name='chPriority'>"
                         +"<option value=-1>--</option>"
                         +"<option value=3>"+lang('Low')+"</option>"
                         +"<option value=2>"+lang('Medium')+"</option>"
                         +"<option value=1>"+lang('High')+"</option></select><input type=button value='"+lang('go')+"' onClick=\"ChangePriority('"+$('ticketid').value+"','"+fuse+"','"+action+"');\" class='button_small' />";
	    $('chPriority').focus();
    }
    if (field=='subject'){
        $(id).innerHTML = "<input type='text' onkeypress='return noenter();' id='chSubject' name='chSubject' value='"+$('originalSubject').value+"' /><input type=button value='"+lang('go')+"' onClick=\"ChangeSubject('"+$('ticketid').value+"','"+fuse+"','"+action+"');\" class='button_small' />";
        $('chSubject').focus();
    }
    if (field=='log'){
        $(id).innerHTML = "<textarea class='textbox' WRAP='virtual' rows=10 cols=80 id='ch"+id+"'>"+rmbr($(id).innerHTML)+"</textarea>";
        $('editLinks_'+id).innerHTML = "<a class='liveupdate' href=\"javascript:SaveEditMessage('"+id+"','"+fuse+"','"+action+"');\">"+lang('Save')+"</a>&nbsp;&nbsp;&nbsp;<a class='liveupdate' href=\"javascript:CancelEditMessage('"+id+"');\">"+lang('Cancel')+"</a>";
        $('ch'+id).focus();
    }
    if (field=='type'){
        var select = "<select onkeypress='return noenter();' id='chType' name='chType'>"
                    +"<option value='-1' selected>--</option>";
        for (i=0;i<typeArray.length;i++){
            select = select+"<option value='"+typeArray[i]+"'>"+varLang(typeArray[i])+"</option>";
        }
        $(id).innerHTML = select+"</select><input type=button value='"+lang('go')+"' onClick=\"ChangeType('"+$('ticketid').value+"','"+fuse+"','"+action+"');\" class='button_small' />";
    }
}

function ChangeType(ticket, fuse, action)
{
    var type = $('chType').value;
	if(type==-1){
	    $('tdType').innerHTML = "<a class='liveupdate' href=\"javascript:ShowEditBox('tdType','5','5017','type');\">"+varLang($('originalType').value)+"</a>";
	    return;
	}
	
	setStatus('Changing Ticket Type...');
	var opt = {
		method: 'post',
		postBody: 'ticket='+ticket+'&fuse='+fuse+'&action='+action+'&type='+type,			
		onSuccess: ChangeTypeSuccess,
		on404: on404,
		onFailure: onFailure
	}		
	new Ajax.Request('index.php', opt);
}

function ChangeTypeSuccess(t)
{
    $('tdType').innerHTML = "<a class='liveupdate' href=\"javascript:ShowEditBox('tdType','5','5017','type');\">"+varLang(t.responseText)+"</a>";
    $('originalType').value = t.responseText;
    unsetStatus('', 1);
}

function DeleteLog(log,fuse,action)
{
    var logid = log.substring(4,log.length);
    strMessage = lang('Click OK if you are sure you want to delete the selected entry.');
    var answer = confirm(strMessage);
    if (!answer)return;
    setStatus('Deleting Log Message...');
    var opt = {
		method: 'post',
		postBody: 'fuse='+fuse+'&action='+action+'&log='+logid,			
		onSuccess: DeleteLogSuccess,
		on404: on404,
		onFailure: onFailure
	}
	new Ajax.Request('index.php', opt);
}

function DeleteLogSuccess(t)
{
    $('div_log_'+t.responseText).innerHTML = '';
    unsetStatus('', 1);
}

function CancelEditMessage(id)
{
    $(id).innerHTML = nl2br($('ch'+id).value);
    $('editLinks_'+id).innerHTML = "<a class='liveupdate' href=\"javascript:ShowEditBox('"+id+"','5','5015','log');\">"+lang('Edit')+"</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='liveupdate' href=\"javascript:DeleteLog('"+id+"','5','5016');\">"+lang('Delete')+"</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}

function SaveEditMessage(log,fuse,action)
{
    var message = $('ch'+log).value;
    var ticket = $('ticketid').value;
    var logid = log.substring(4,log.length);
    setStatus('Editing Log Message...');
    var opt = {
		method: 'post',
		postBody: 'fuse='+fuse+'&action='+action+'&log='+logid+'&message='+_escapeString(message),
		onSuccess: SaveEditMessageSuccess,
		on404: on404,
		onFailure: onFailure
	}
	new Ajax.Request('index.php', opt);
}

function SaveEditMessageSuccess(t)
{
    var ret = t.responseText.split("|");
    var x;
    var message = ret[1];
    for (x in ret) {
      if (x > 1) {
        message = message + "|" + ret[x];
      }
    }
    $('log_'+ret[0]).innerHTML = nl2br(message);
    $('editLinks_log_'+ret[0]).innerHTML = "<a class='liveupdate' href=\"javascript:ShowEditBox('log_"+ret[0]+"','5','5015','log');\">"+lang('Edit')+"</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='liveupdate' href=\"javascript:DeleteLog('log_"+ret[0]+"','5','5016');\">"+lang('Delete')+"</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
    unsetStatus('', 1);
}

function ChangeSubject(ticket,fuse,action)
{
    var subject = $('chSubject').value;
	if(subject==$('originalSubject').value || subject == ''){
	    $('tdSubject').innerHTML = "<a class='liveupdate' href=\"javascript:ShowEditBox('tdSubject','5','5014','subject');\">"+$('originalSubject').value+"</a>";
	    return;
	}
	
	setStatus('Changing Ticket Subject...');
	var opt = {
		method: 'post',
		postBody: 'ticket='+ticket+'&fuse='+fuse+'&action='+action+'&subject='+_escapeString(subject),			
		onSuccess: ChangeSubjectSuccess,
		on404: on404,
		onFailure: onFailure
	}		
	new Ajax.Request('index.php', opt);
}

function ChangeSubjectSuccess(t)
{
    $('tdSubject').innerHTML = "<a class='liveupdate' href=\"javascript:ShowEditBox('tdSubject','5','5014','subject');\">"+t.responseText+"</a>";
    $('originalSubject').value = t.responseText;
    unsetStatus('', 1);
}

function itemsFilledOut(form){
    strAlertMessage = '\n' + lang("You must complete all the fields before proceeding");
    bolShowMessage = false;
    
 	if (typeof document.forms['SupportForm'].messagetype != 'undefined' &&  document.forms['SupportForm'].messagetype.value == 0){
         strAlertMessage += '\n' + lang("Select a valid ticket type");
         bolShowMessage = true;
    }

 	if (typeof document.forms['SupportForm'].subject  != 'undefined' && document.forms['SupportForm'].subject.value == ""){
         strAlertMessage += '\n' + lang("You can not leave the subject blank");
         bolShowMessage = true;
    }
     
 	if (typeof document.forms['SupportForm'].message != 'undefined' && document.forms['SupportForm'].message.value == ""){
 		 strAlertMessage += '\n' + lang("You can not leave the message blank");
         bolShowMessage = true;
    }

    if (bolShowMessage){
        alert(strAlertMessage);
        return false;
    }else{
        submitonce(document.forms['SupportForm']);
        document.forms['SupportForm'].submit();
    }
}

function itemSelected(form){
    strAlertMessage = '\n' + lang("You need to select entry(s) to perform this action on");
    intCount = document.forms['troubletickets'].elements.length;
    bolShowMessage = true;
    for (x=1;x<=intCount;x++)
    {
        if (document.forms['troubletickets'].elements[x-1].checked == true){
             bolShowMessage=false;
        }
    }

    if (bolShowMessage){    
        alert(strAlertMessage);
        document.forms['assignadmin'].elements['adminid'].selectedIndex=0;
        return false;
    }else{
        return true;
    }
}

function submitDelete(myform){
    strMessage = '\n' + lang('Click OK if you are sure you want to delete the selected entries');
    strWorkItems = "";
    intItemChecked=0;

    intCount = document.forms['troubletickets'].elements.length;    
    
    for (x=1;x<=intCount;x++)
    {
        if (document.forms['troubletickets'].elements[x-1].checked == true){
             strWorkItems += document.forms['troubletickets'].elements[x-1].value;
             strWorkItems += " ";
             intItemChecked++;
        }
    }

    var tString;
    String(tString);
    tString = (strWorkItems.substring(0,strWorkItems.length-1));    
    myform.selectedworks.value=tString; 

    var answer=confirm(strMessage);
    if (answer){
        submitonce(myform);
        myform.submit();
    }
}

function submitAdminEntries(myform){
    strWorkItems = "";
    intItemChecked=0;

    for (x=1;x<=intCount;x++)
    {
        if (document.forms['troubletickets'].elements[x-1].checked == true){
             strWorkItems += document.forms['troubletickets'].elements[x-1].value;
             strWorkItems += " ";
             intItemChecked++;
        }
    }

    var tString;
    String(tString);
    tString = (strWorkItems.substring(0,strWorkItems.length-1));
    myform.selectedworks.value=tString;
    submitonce(myform);
    myform.submit();
}

function highPriorityWarning(myform){
    if (myform.options[myform.selectedIndex].value=="1"){
        strMessage = '\n' + lang('Click OK to confirm this is a high priority ticket');
        var answer=confirm(strMessage);
        if (!answer){
            myform.selectedIndex=2;
        }
    }
}