<?xml version="1.0" encoding="UTF-8"?>
<dynaForm type="xmlform" name="cases/cases_Scheduler_New" width="70%" enabletemplate="1" mode="edit">
  <menuUser type="xmlmenu" xmlfile="cases/cases_Scheduler_NewOptions">
    <en/>
  </menuUser>
  <GENERAL_INF type="title" enablehtml="1">
    <en><![CDATA[General Information]]></en>
  </GENERAL_INF>
  <PROPERTIES_INF type="subtitle" enablehtml="1">
    <en><![CDATA[Enter the username and password of the user who will be assigned to the initial task in the case.]]></en>
  </PROPERTIES_INF>
  <SCH_UID type="hidden" />
  <SCH_LIST type="hidden" />
  <PHP_CURRENT_DATE type="hidden" />
  <SCH_USER_NAME type="text" maxlength="100" validate="Any" required="1" readonly="0" size="40" mode="edit">
    <en><![CDATA[User Name]]></en>
  </SCH_USER_NAME>
  <SCH_USER_PASSWORD type="password" maxlength="100" validate="Any" required="1" readonly="0" size="40" mode="edit">
    <en><![CDATA[Password]]></en>
  </SCH_USER_PASSWORD>
  <SCH_USER_UID type="hidden" dependentfields="PRO_UID"/>
  <TEST_CONNECTION type="button" onclick="case_userSchedulerValidate(getField('SCH_USER_NAME').value,getField('SCH_USER_PASSWORD').value);">
    <en><![CDATA[Test User]]></en>
  </TEST_CONNECTION>
  <EDIT_USER type="button" onclick="resetForm();">
    <en><![CDATA[Edit User]]></en>
  </EDIT_USER>
  <PERFORM_TASK type="subtitle" enablehtml="1">
    <en><![CDATA[Properties]]></en>
  </PERFORM_TASK>
  <PRO_UID type="hidden" required="1" style="width:300px" dependentfields="TAS_UID" mode="edit">
</PRO_UID>
  <TAS_UID type="dropdown" required="1" style="width:300px" options="Array">
    <en><![CDATA[Task]]></en>
  </TAS_UID>
  <SCH_NAME type="text" maxlength="100" validate="Any" required="1" readonly="0" size="40" mode="edit" mask="" strto="" dependentfields="" defaultvalue="" hint="" formula="" function="" sqlconnection="" savelabel="0">
    <en><![CDATA[Name]]></en>
  </SCH_NAME>
  <SCH_OPTION type="dropdown" required="1" readonly="0" savelabel="0" mode="edit" options="Array">
    <en><![CDATA[Perform this task]]><option name="0"><![CDATA[-Select-]]></option><option name="1"><![CDATA[Daily]]></option><option name="2"><![CDATA[Weekly]]></option><option name="3"><![CDATA[Monthly]]></option><option name="4"><![CDATA[One time only]]></option><option name="5"><![CDATA[Every]]></option></en>
  </SCH_OPTION>
  <SELECT_TIME_DAY type="subtitle" enablehtml="1">
    <en><![CDATA[Select the date and time for case(s) to be initiated.]]></en>
  </SELECT_TIME_DAY>
  <SCH_START_TIME type="text" maxlength="5" validate="Any" mask="##:##" required="0" readonly="0" size="5" mode="edit" strto="" dependentfields="" defaultvalue="" hint="" formula="" function="" sqlconnection="" savelabel="0">
    <en><![CDATA[Execution time]]></en>
  </SCH_START_TIME>
  <SELECT_DATE type="subtitle" enablehtml="1">
    <en><![CDATA[Select date]]></en>
  </SELECT_DATE>
  <SCH_START_DATE type="date" defaultValue="@#PHP_CURRENT_DATE" relativedate="1" beforedate="0m" afterdate="5y" mask="Y-m-d " editable="0" required="0" readonly="0" showtime="0" defaultvalue_sel="empty" size="15" mode="edit">
    <en><![CDATA[Start date]]></en>
  </SCH_START_DATE>
  <SELECT_1 type="subtitle" enablehtml="1">
    <en><![CDATA[Daily]]></en>
  </SELECT_1>
  <!--
<SCH_DAYS_PERFORM_TASK type="radiogroup" defaultValue="1" required="0" mode="edit" options="Array">
<en>Perform this task
<option name="1">Every Day</option>

<option name="2">Weekdays</option>
<option name="3">Every</option>

</en>
</SCH_DAYS_PERFORM_TASK>
-->
  <SCH_DAYS_PERFORM_TASK type="hidden" defaultValue="1" value="1" required="0" mode="edit" options="Array">
</SCH_DAYS_PERFORM_TASK>
  <!--
<SCH_DAYS_PERFORM_TASK_OPT_3 type="text" maxlength="3" validate="Int" required="0" readonly="0" defaultvalue="1" size="3" mode="edit">
<en>Days</en>
</SCH_DAYS_PERFORM_TASK_OPT_3>
-->
  <SELECT_2 type="subtitle" enablehtml="1">
    <en><![CDATA[Weekly]]></en>
  </SELECT_2>
  <SCH_EVERY_DAYS type="text" maxlength="2" defaultValue="1" validate="Int" required="0" readonly="0" size="2" mode="edit">
    <en><![CDATA[Every]]></en>
  </SCH_EVERY_DAYS>
  <SCH_WEEK_DAYS type="checkgroup" required="0" mode="edit" options="Array" COLS="2" defaultvalue="" hint="" sqlconnection="" readonly="0" savelabel="0">
    <en><![CDATA[Select the day(s) of the week below]]><option name="1"><![CDATA[Monday]]></option><option name="2"><![CDATA[Tuesday]]></option><option name="3"><![CDATA[Wednesday]]></option><option name="4"><![CDATA[Thursday]]></option></en>
  </SCH_WEEK_DAYS>
  <SCH_WEEK_DAYS_2 type="checkgroup" required="0" mode="edit" options="Array" defaultvalue="" hint="" sqlconnection="" readonly="0" savelabel="0">
    <en><![CDATA[]]><option name="5"><![CDATA[Friday]]></option><option name="6"><![CDATA[Saturday]]></option><option name="7"><![CDATA[Sunday]]></option></en>
  </SCH_WEEK_DAYS_2>
  <SELECT_3 type="subtitle" enablehtml="1">
    <en><![CDATA[Monthly]]></en>
  </SELECT_3>
  <SCH_START_DAY type="radiogroup" required="0" mode="edit" options="Array">
    <en><![CDATA[]]><option name="1"><![CDATA[Day of month]]></option><option name="2"><![CDATA[The day]]></option></en>
  </SCH_START_DAY>
  <SCH_START_DAY_OPT_1 type="text" maxlength="2" validate="Int" mask="##" required="0" readonly="0" defaultvalue="1" size="2" mode="edit">
    <en><![CDATA[]]></en>
  </SCH_START_DAY_OPT_1>
  <SCH_START_DAY_OPT_2_WEEKS type="dropdown" required="0" readonly="0" savelabel="0" mode="edit" options="Array" dependentfields="" defaultvalue="" hint="" sqlconnection="">
    <en><![CDATA[]]><option name="1"><![CDATA[First]]></option><option name="2"><![CDATA[Second]]></option><option name="3"><![CDATA[Third]]></option><option name="4"><![CDATA[Fourth]]></option><option name="5"><![CDATA[Last]]></option></en>
  </SCH_START_DAY_OPT_2_WEEKS>
  <SCH_START_DAY_OPT_2_DAYS_WEEK type="dropdown" required="0" readonly="0" savelabel="0" mode="edit" options="Array">
    <en><![CDATA[]]><option name="1"><![CDATA[Monday]]></option><option name="2"><![CDATA[Tuesday]]></option><option name="3"><![CDATA[Wednesday]]></option><option name="4"><![CDATA[Thursday]]></option><option name="5"><![CDATA[Friday]]></option><option name="6"><![CDATA[Saturday]]></option><option name="7"><![CDATA[Sunday]]></option></en>
  </SCH_START_DAY_OPT_2_DAYS_WEEK>
  <SCH_MONTHS type="checkgroup" mode="edit" options="Array">
    <en><![CDATA[Of the month(s)]]><option name="1"><![CDATA[Jan]]></option><option name="2"><![CDATA[Feb]]></option><option name="3"><![CDATA[Mar]]></option><option name="4"><![CDATA[Apr]]></option></en>
  </SCH_MONTHS>
  <SCH_MONTHS_2 type="checkgroup" mode="edit" options="Array">
    <en><![CDATA[]]><option name="5"><![CDATA[May]]></option><option name="6"><![CDATA[Jun]]></option><option name="7"><![CDATA[Jul]]></option><option name="8"><![CDATA[Aug]]></option></en>
  </SCH_MONTHS_2>
  <SCH_MONTHS_3 type="checkgroup" mode="edit" options="Array">
    <en><![CDATA[]]><option name="9"><![CDATA[Sep]]></option><option name="10"><![CDATA[Oct]]></option><option name="11"><![CDATA[Nov]]></option><option name="12"><![CDATA[Dec]]></option></en>
  </SCH_MONTHS_3>
  <!-- Advanced Options  //-->
  <!--<ADVANCED_4 type="subtitle" enablehtml="1">
<en>Advanced Options</en>
</ADVANCED_4>-->
  <!--
<SCH_START_DATE type="date" relativedate="1" beforedate="0m" afterdate="1y" mask="Y-m-d " editable="0" required="0" readonly="0" showtime="0" defaultvalue_sel="empty" size="15" mode="edit">
<en>Start Date</en>
</SCH_START_DATE>
//-->
  <!--
<SCH_END_DATE_CHK type="checkbox" value="On" falsevalue="Off" defaultvalue="Off" labelonright="1" enablehtml="1">
<en>End Date</en>
</SCH_END_DATE_CHK>
-->
  <SCH_END_DATE type="date" relativedate="1" beforedate="0m" afterdate="5y" mask="Y-m-d " editable="0" required="0" readonly="0" showtime="0" defaultvalue_sel="empty" size="15" mode="edit">
    <en><![CDATA[End Date]]></en>
  </SCH_END_DATE>
  <SCH_REPEAT_TASK_CHK type="checkbox" value="On" falsevalue="Off" defaultvalue="Off" labelonright="1" enablehtml="1">
    <en><![CDATA[Repeat Task]]></en>
  </SCH_REPEAT_TASK_CHK>
  <!--<SCH_REPEAT_EVERY type="text" maxlength="4" validate="Int" required="0" readonly="0" size="4" mode="edit">-->
  <!--<en>Every</en>-->
  <!--</SCH_REPEAT_EVERY>-->
  <SCH_REPEAT_EVERY type="text" maxlength="5" validate="Real" mask="##.##" required="0" readonly="0" size="5" mode="edit">
    <en><![CDATA[Execute every]]></en>
  </SCH_REPEAT_EVERY>
  <!--<SCH_REPEAT_EVERY type="text" maxlength="5" validate="Any" mask="##:##" required="0" readonly="0" size="5" mode="edit" strto="" dependentfields=""  defaultvalue_sel="empty" hint="" formula="" function=""  sqlconnection="" savelabel="0">-->
  <!--<en>Every</en>-->
  <!--</SCH_REPEAT_EVERY>-->
  <!--SCH_START_TIME-->
  <SCH_ADVANCED type="hidden">
    <en/>
  </SCH_ADVANCED>
  <SCH_REPEAT_EVERY_OPT type="dropdown" required="0" readonly="0" savelabel="0" mode="edit" options="Array">
    <en><![CDATA[]]><option name="1"><![CDATA[Minutes]]></option><option name="2"><![CDATA[Hours]]></option></en>
  </SCH_REPEAT_EVERY_OPT>
  <!--
<SUB_SAVE type="subtitle" enablehtml="1">
<en> </en>
</SUB_SAVE>
//-->
  <SELECT_PLUGIN type="subtitle" enablehtml="1">
    <en><![CDATA[Start a case based on following registered Plugin.]]></en>
  </SELECT_PLUGIN>
  <SAVE type="button" onClick="validateSchedulerFields(this.form)">
    <en><![CDATA[Save]]></en>
  </SAVE>
  <BTNCANCEL type="button" onClick="javascript:backToList();">
    <en><![CDATA[Cancel]]></en>
  </BTNCANCEL>
  <SHOW_HIDE_JS type="javascript"><![CDATA[
hideAll();
hideProperties();


function $_GET(q,s) {
    s = (s) ? s : self.location.search;
    var re = new RegExp('&'+q+'=([^&]*)','i');
    return (s=s.replace(/^\?/,'&').match(re)) ? s=s[1] : s='';
}

function backToList(){
  //alert($_GET('PRO_UID'));
  self.location = 'cases_Scheduler_List?PRO_UID='+$_GET('PRO_UID');
}
function hideProperties(){
        // hiddeRow('EDIT_USER');
        // whenever a user is logged then the following section must be enabled
        contractSubtitle('PERFORM_TASK');
        // end of enabling

        // whenever a user is logged then the following 3 subtitles must be enabled
        document.getElementById('form[EDIT_USER]').style.display='none';
        document.getElementById('form[PERFORM_TASK]').style.display='none';
        document.getElementById('form[SELECT_TIME_DAY]').style.display='none';
        document.getElementById('form[SELECT_PLUGIN]').style.display='none';
        // end of enabling
}

function showProperties(){
    expandSubtitle('PERFORM_TASK');
    document.getElementById('form[TEST_CONNECTION]').style.display='none';
    document.getElementById('form[SCH_USER_NAME]').readOnly=true;
    document.getElementById('form[SCH_USER_PASSWORD]').readOnly=true;
    document.getElementById('form[EDIT_USER]').style.display='';
    document.getElementById('form[PERFORM_TASK]').style.display='';
    document.getElementById('form[SELECT_TIME_DAY]').style.display='';



}

function hideAll() {
contractSubtitle('SELECT_PLUGIN');
  contractSubtitle('SELECT_TIME_DAY');
	contractSubtitle('SELECT_DATE');
	contractSubtitle('SELECT_1');
	contractSubtitle('SELECT_2');
	contractSubtitle('SELECT_3');

	getField('SAVE').disabled=true;
	// document.getElementById('form[SELECT_TIME_DAY]').style.display='none';
	document.getElementById('form[SELECT_DATE]').style.display='none';
	document.getElementById('form[SELECT_1]').style.display='none';
	document.getElementById('form[SELECT_2]').style.display='none';
	document.getElementById('form[SELECT_3]').style.display='none';
    document.getElementById('startTime').style.display='none';
    document.getElementById('everyTime').style.display='none';

}

function showPluginSelection(opt,pro_uid) {
document.getElementById("pluginsListContent").innerHTML ="";
if(opt!=""){
var oRPCProcess = new leimnud.module.rpc.xmlhttp({
            url   : '../cases/cases_SchedulerGetPlugins',
            async : true,
            method: 'POST',
            args  : "action=pluginCaseSchedulerForm&selectedOption="+opt+"&pro_uid="+pro_uid
       });
       oRPCProcess.callback = function(rpc){
       //alert(rpc.xmlhttp.responseText);
       if(rpc.xmlhttp.responseText!=""){

        document.getElementById("pluginsListContent").innerHTML = rpc.xmlhttp.responseText;
       }

       }.extend(this);

       oRPCProcess.make();
}
}
function showSelection(opt) {

	if (opt!=0) {
    expandSubtitle('SELECT_TIME_DAY');
    getField('SAVE').disabled=false;

    var oRPCProcess = new leimnud.module.rpc.xmlhttp({
            url   : '../cases/cases_SchedulerGetPlugins',
            async : true,
            method: 'POST',
            args  : "action=pluginsList"
       });

       oRPCProcess.callback = function(rpc){
       //alert(rpc.xmlhttp.responseText);
       if(rpc.xmlhttp.responseText!=""){
        document.getElementById('form[SELECT_PLUGIN]').style.display='';
        expandSubtitle('SELECT_PLUGIN');
        document.getElementById("pluginsListDropdown").innerHTML = rpc.xmlhttp.responseText;
       }

       }.extend(this);

       oRPCProcess.make();

	} else {
    contractSubtitle('SELECT_TIME_DAY');
    getField('SAVE').disabled=true;
	}

	switch(opt){
		case '0' :
      hideAll();
    break;
		case '1' : // Daily
      expandSubtitle('SELECT_DATE');
      expandSubtitle('SELECT_1');
      contractSubtitle('SELECT_2');
      contractSubtitle('SELECT_3');
      //document.getElementById('form[ADVANCED_4]').style.display='';
      //expandSubtitle('ADVANCED_4');

      document.getElementById('endDateTable').style.display='';
      document.getElementById('startTime').style.display='';
      document.getElementById('everyTime').style.display='none';
    break;
		case '2' : // Weekly
      contractSubtitle('SELECT_DATE');
      contractSubtitle('SELECT_1');
      expandSubtitle('SELECT_2');
      contractSubtitle('SELECT_3');
      //document.getElementById('form[ADVANCED_4]').style.display='';
      hideRow('SCH_EVERY_DAYS');
      //expandSubtitle('ADVANCED_4');

      document.getElementById('endDateTable').style.display='';
      document.getElementById('startTime').style.display='';
      document.getElementById('everyTime').style.display='none';
    break;
		case '3' : // Monthly
      contractSubtitle('SELECT_DATE');
      contractSubtitle('SELECT_1');
      contractSubtitle('SELECT_2');
      expandSubtitle('SELECT_3');
      //document.getElementById('form[ADVANCED_4]').style.display='';
      getField('SCH_START_DAY_OPT_1').disabled=true;
      getField('SCH_START_DAY_OPT_1').style.display="none";
      getField('SCH_START_DAY_OPT_2_WEEKS').disabled=true;
      getField('SCH_START_DAY_OPT_2_WEEKS').style.display="none";
      getField('SCH_START_DAY_OPT_2_DAYS_WEEK').disabled=true;
      getField('SCH_START_DAY_OPT_2_DAYS_WEEK').style.display="none";

      document.getElementById('endDateTable').style.display='';
      document.getElementById('startTime').style.display='';
      document.getElementById('everyTime').style.display='none';
      //expandSubtitle('ADVANCED_4');
    break;
    case '4' : // One time only
      expandSubtitle('SELECT_DATE');
      contractSubtitle('SELECT_1');
      contractSubtitle('SELECT_2');
      contractSubtitle('SELECT_3');

      document.getElementById('endDateTable').style.display='none';
      document.getElementById('startTime').style.display='';
      document.getElementById('everyTime').style.display='none';
      //document.getElementById('form[ADVANCED_4]').style.display='';

      //expandSubtitle('ADVANCED_4');
    break;
    case '5' : // Every
      contractSubtitle('SELECT_DATE');
      contractSubtitle('SELECT_1');
      contractSubtitle('SELECT_2');
      contractSubtitle('SELECT_3');

      document.getElementById('endDateTable').style.display='none';
      document.getElementById('startTime').style.display='none';
      document.getElementById('everyTime').style.display='';
    break;
  }
}


leimnud.event.add(getField('SCH_OPTION'), 'change', function() {
		var opt = getField('SCH_OPTION').value;
		showSelection(opt);

});

leimnud.event.add(getField('SCH_START_DAY][1'), 'click', function() {
		getField('SCH_START_DAY_OPT_1').disabled=false;
		getField('SCH_START_DAY_OPT_1').style.display="";
		getField('SCH_START_DAY_OPT_2_WEEKS').disabled=true;
                getField('SCH_START_DAY_OPT_2_WEEKS').style.display="none";
		getField('SCH_START_DAY_OPT_2_DAYS_WEEK').disabled=true;
		getField('SCH_START_DAY_OPT_2_DAYS_WEEK').style.display="none";
});

leimnud.event.add(getField('SCH_START_DAY][2'), 'click', function() {
		getField('SCH_START_DAY_OPT_1').disabled=true;
		getField('SCH_START_DAY_OPT_1').style.display="none";
		getField('SCH_START_DAY_OPT_2_WEEKS').disabled=false;
		getField('SCH_START_DAY_OPT_2_WEEKS').style.display="";
		getField('SCH_START_DAY_OPT_2_DAYS_WEEK').disabled=false;
		getField('SCH_START_DAY_OPT_2_DAYS_WEEK').style.display="";
});

leimnud.event.add(getField('SCH_DAYS_PERFORM_TASK][1'), 'change', function() {
		getField('SCH_DAYS_PERFORM_TASK_OPT_3').disabled=true;
});
leimnud.event.add(getField('SCH_DAYS_PERFORM_TASK][2'), 'change', function() {
		getField('SCH_DAYS_PERFORM_TASK_OPT_3').disabled=true;
});
leimnud.event.add(getField('SCH_DAYS_PERFORM_TASK][3'), 'change', function() {
		getField('SCH_DAYS_PERFORM_TASK_OPT_3').disabled=false;
});

leimnud.event.add(getField('SCH_DAYS_PERFORM_TASK_OPT_3'), 'change', function() {
		days = getField('SCH_DAYS_PERFORM_TASK_OPT_3').value;
		if(!(days>=1 && days<=365)){
			msgBox('Days must be between 1 and 365', 'alert'); // confirm, info
			getField('SCH_DAYS_PERFORM_TASK_OPT_3').value = '1';
		}
});

leimnud.event.add(getField('SCH_EVERY_DAYS'), 'change', function() {
		days = getField('SCH_EVERY_DAYS').value;
		if(!(days>=1 && days<=52) ) {
			msgBox('Weeks must be between 1 and 52', 'alert');
			getField('SCH_EVERY_DAYS').value = '';
		}
});

leimnud.event.add(getField('SCH_START_DAY_OPT_1'), 'change', function() {
		days = getField('SCH_START_DAY_OPT_1').value;
		if(!(days>=1 && days<=31) ) {
			msgBox('The days must be between 1 and 31', 'alert');
			getField('SCH_START_DAY_OPT_1').value = '1';
		}
});

leimnud.event.add(getField('SCH_START_TIME'), 'keyup', function() {
  if (getField('SCH_START_TIME').value.charAt(0)) {
    if (isNaN(getField('SCH_START_TIME').value.charAt(0)) == true) {
      getField('SCH_START_TIME').value = getField('SCH_START_TIME').value.slice(0, -1);
    }
  }
  if (getField('SCH_START_TIME').value.charAt(1)) {
    if (isNaN(getField('SCH_START_TIME').value.charAt(1)) == true) {
      getField('SCH_START_TIME').value = getField('SCH_START_TIME').value.slice(0, -1);
    }
  }
  var oVar = getField('SCH_START_TIME').value.charAt(2);
  if (isNaN(oVar) == true) {
    if (! getField('SCH_START_TIME').value.charAt(3)) {
      getField('SCH_START_TIME').value = getField('SCH_START_TIME').value.slice(0, -1);
    }
    if (getField('SCH_START_TIME').value.charAt(4)) {
      if (isNaN(getField('SCH_START_TIME').value.charAt(4)) == true) {
        getField('SCH_START_TIME').value = getField('SCH_START_TIME').value.slice(0, -1);
      }
    }
  }
});

leimnud.event.add(getField('SCH_START_TIME'), 'change', function() {
    hours = getField('SCH_START_TIME').value;
    answer = hours;
    var parties = answer.split(':');

    if(parties[0]>23){
        msgBox(G_STRINGS.ID_SCHEDULER_CANT_PUT_TIME, 'alert');
        getField('SCH_START_TIME').value='';
        //getField('SCH_REPEAT_UNTIL').value='';
        //getField('SCH_REPEAT_UNTIL').focus();
        getField('SAVE').focus();

    }
    if(parties[1]>59){
        msgBox('The minutes can not be greater than 59', 'alert');
        getField('SCH_START_TIME').value='';
        //getField('SCH_REPEAT_UNTIL').value='';
        //getField('SCH_REPEAT_UNTIL').focus();
        getField('SAVE').focus();
    }
});

leimnud.event.add(getField('SCH_REPEAT_EVERY'), 'change', function() {
    hours = getField('SCH_REPEAT_EVERY').value;
    answer = hours;
    var parties = answer.split('.');

    if(parties[0]>23){
        msgBox(G_STRINGS.ID_SCHEDULER_CANT_PUT_TIME, 'alert');
        getField('SCH_REPEAT_EVERY').value='';
        getField('SAVE').focus();

    }
    if(parties[1]>99){
        msgBox('The minutes can not be greater than 99', 'alert');
        getField('SCH_REPEAT_EVERY').value='';
        getField('SAVE').focus();
    }
});


/*
leimnud.event.add(getField('SCH_OPTION][1'), 'change', function() {
		showSelection('1');
});
leimnud.event.add(getField('SCH_OPTION][2'), 'change', function() {
		showSelection('2');
});
leimnud.event.add(getField('SCH_OPTION][3'), 'change', function() {
		showSelection('3');
});
leimnud.event.add(getField('SCH_OPTION][4'), 'change', function() {
		showSelection('4');
});
*/

function case_userSchedulerValidate(username, password) {
    var user_uid;

    if(username=='' || password==''){
     msgBox(G_STRINGS.ID_CASE_SCHEDULER_VALIDATE_ALERT, "alert");
     return false;
     }

    var oRPC = new leimnud.module.rpc.xmlhttp({
       	    url   : '../cases/cases_SchedulerValidateUser',
             async : true,
             method: 'POST',
             args  : "USERNAME="+username+"&PASSWORD="+password+"&PRO_UID="+$_GET('PRO_UID')
    });

    oRPC.callback = function(rpc){
       sresponse = rpc.xmlhttp.responseText.parseJSON();

       if (sresponse.status_code != 0) {
         msgBox(sresponse.message, "alert");
         getField('BTNCANCEL').focus();
         return false;
       }

       getField('SCH_USER_UID').value = sresponse.message;
       user_uid = sresponse.message;

       var oRPCProcess = new leimnud.module.rpc.xmlhttp({
       	    url   : '../cases/cases_SchedulerGetProcesses',
            async : true,
            method: 'POST',
            args  : "USR_UID="+user_uid+"&PRO_UID="+$_GET('PRO_UID')
       });

       oRPCProcess.callback = function(rpc){
            if (getField('PRO_UID')!=null){
                var process_uid = getField('PRO_UID').value;
            } else {
                var process_uid = gup('PRO_UID');
            }
            loadTasksDropdown(process_uid,user_uid);

       }.extend(this);

       oRPCProcess.make();


      }.extend(this);
    oRPC.make();


}
/*
 * @function validateSchedulerFields
 * @author gustavo cruz gustavo[at]colsoa[dot]com
 * @desc This function validates the special cases for the scheduler data fields.
 * @return true/false boolean
 */

function validateSchedulerFields(oForm) {

    var schedulerTitle = document.getElementById('form[SCH_NAME]').value;
    schedulerTitle = schedulerTitle.replace(/^\s*|\s*$/g,"");
    if (schedulerTitle == '') {
        document.getElementById('form[SCH_NAME]').focus();
        msgBox("@G::LoadTranslation(ID_REQUIRED_NAME_CASE_SCHEDULER)", 'alert');
        return false;
    }

    var listNames = document.getElementById('form[SCH_LIST]').value;
    listNames = listNames.split("^");
    for (var i= 1; i<listNames.length; i++) {
        if (schedulerTitle.toLowerCase() == (listNames[i]).toLowerCase()) {
            if (schedulerTitle == listNames[i]) {
                document.getElementById('form[SCH_NAME]').focus();
                msgBox("@G::LoadTranslation(ID_DUPLICATE_CASE_SCHEDULER_NAME)", 'alert');
                return false;
            }
        }
    }
    getField("SCH_NAME").value = schedulerTitle;

    var validFields = true;
    var requiredFields = '';
    var badFormatFields = '';
    var startDate = document.getElementById('form[SCH_START_DATE]').value;
    var endDate = document.getElementById('form[SCH_END_DATE]').value;

    startDate = startDate.split("-");
    endDate = endDate.split("-");
    startDate = new Date(startDate[0],startDate[1],startDate[2]);
    endDate   = new Date(endDate[0],endDate[1],endDate[2]);

        if (document.getElementById('form[SCH_START_DATE]').value=='') {
            requiredFields = requiredFields + 'Start Date,';
            validFields = false;
        } else {
            if (document.getElementById('form[SCH_END_DATE]').value!='') {
                if (startDate>endDate) {
                    badFormatFields = badFormatFields + 'Start Date can\'t be greater than End Date';
                    validFields = false;
                }
            }
        }
        // check if the field time is compliant with the regular expression
        // for time format hh:mm
        var regExpString = "([0-1]\\d|2[0-3]):([0-5]\\d)";
        var timeRegexp = new RegExp(regExpString);
        if (document.getElementById('form[SCH_OPTION]').value != 5) {
            if (!timeRegexp.test(document.getElementById('form[SCH_START_TIME]').value)) {
                msgBox('The time format is invalid','alert');
                return false;
            }
            if (document.getElementById('form[SCH_START_TIME]').value=='') {
                requiredFields = requiredFields + 'Execution Time,';
                validFields = false;
            }
        } else {
            if (document.getElementById('form[SCH_REPEAT_EVERY]').value=='') {
                requiredFields = requiredFields + 'Every,';
                validFields = false;
            }
        }
        switch (document.getElementById('form[SCH_OPTION]').value) {
            // daily
            case '1':
                //if(document.getElementById('form[SCH_DAYS_PERFORM_TASK][1]').checked==true||document.getElementById('form[SCH_DAYS_PERFORM_TASK][2]').checked==true||document.getElementById('form[SCH_DAYS_PERFORM_TASK][3]').checked==true){
                if(document.getElementById('form[SCH_DAYS_PERFORM_TASK]').value!=''){
                    validFields = true;
                } else {
                    requiredFields = requiredFields + 'Weekdays,';
                    validFields = false;
                }
                break;
                // weekly
            case '2':
                if (document.getElementById('form[SCH_EVERY_DAYS]').value=='') {
                    requiredFields = requiredFields + 'Number of Weeks,';
                    validFields = false;
                } else {
                    for (week=1;week<=4;week++) {
                        weekIdOne = 'form[SCH_WEEK_DAYS]['+week+']';
                        if (document.getElementById(weekIdOne).checked==true) {
                            validFields = true;
                            break;
                        } else {
                            validFields = false;
                        }
                    }
                    if (!validFields){
                        for (week=1;week<=3;week++){
                            weekIdTwo = 'form[SCH_WEEK_DAYS_2]['+(week+4)+']';
                            if (document.getElementById(weekIdTwo).checked==true) {
                                validFields = true;
                                break;
                            } else {
                                validFields = false;
                            }
                        }
                    }
                    if (!validFields){
                        requiredFields = requiredFields + 'Weeks,';
                        validFields = false;
                    }
                }
                break;
                //monthly
            case '3':
                if (document.getElementById('form[SCH_START_DAY][1]').checked==true||document.getElementById('form[SCH_START_DAY][2]').checked==true){
                    for (month=1;month<=4;month++){
                        monthIdOne = 'form[SCH_MONTHS]['+month+']';
                        monthIdTwo = 'form[SCH_MONTHS_2]['+(month+4)+']';
                        monthIdThree = 'form[SCH_MONTHS_3]['+(month+8)+']';

                        if (document.getElementById(monthIdOne).checked==true||document.getElementById(monthIdTwo).checked==true||document.getElementById(monthIdThree).checked==true){
                            validFields = true;
                            break;
                        } else {
                            validFields = false;
                        }
                    }
                    if (!validFields){
                        requiredFields = requiredFields + 'Months,';
                        validFields = false;
                    }
                } else {
                    requiredFields = requiredFields + 'Start Day,';
                    validFields = false;
                }
                break;
            //run once
            case '4':
                validFields = true;
                break;
            //run every
            case '5':
                validFields = true;
                break;
            default:
                validFields = false;
            break;
        }
        if (requiredFields!='') {
            var message = "The fields "+requiredFields+" can\'t be empty";
            msgBox(message, 'alert');
            return false;
        } else {
            if (badFormatFields!='') {
                var message =  badFormatFields;
                msgBox(message, 'alert');
                return false;
            } else {
                oForm.submit();
            }
        }
}

function resetForm(){
document.getElementById('form[SCH_USER_NAME]').readOnly=false;
document.getElementById('form[SCH_USER_PASSWORD]').readOnly=false;
document.getElementById('form[TEST_CONNECTION]').style.display='';
hideAll();
hideProperties();
}

function loadTasksDropdown(process,user_uid){
    //alert(process);
    var oRPCTasks = new leimnud.module.rpc.xmlhttp({
       	    url   : '../cases/cases_SchedulerGetTasks',
             async : true,
             method: 'POST',
             args  : "USR_UID="+user_uid+"&PRO_UID="+process
    });

    oRPCTasks.callback = function(rpc){
       document.getElementById("taskListDropdown").innerHTML = rpc.xmlhttp.responseText;
       // if the user is assigned to the task the default task value will be diferent than char

       if (getField('TAS_UID').value!='char'){
           showProperties();
       } else {
           msgBox("The User login data seems to be invalid.","alert");
       }
    }.extend(this);
    oRPCTasks.make();
}
leimnud.event.add(getField('SCH_REPEAT_TASK_CHK'), 'click', function() {
	if(getField('SCH_REPEAT_TASK_CHK').checked){
		enable(getField('SCH_REPEAT_EVERY'));
		enable(getField('SCH_REPEAT_EVERY_OPT'));
		// enable(getField('SCH_REPEAT_UNTIL'));
		// enable(getField('SCH_REPEAT_STOP_IF_RUNNING'));
	}	else {
		<!--disable(getField('SCH_REPEAT_EVERY'));-->
		<!--disable(getField('SCH_REPEAT_EVERY_OPT'));-->
		// disable(getField('SCH_REPEAT_UNTIL'));
		// disable(getField('SCH_REPEAT_STOP_IF_RUNNING'));
	}

});


function collapseAdvancedOptions(){
   var flag = document.getElementById('EditLogAdvancedOptions').innerHTML;
   if (flag=='visible'){
        //contractSubtitle('ADVANCED_4');
        document.getElementById('EditLogAdvancedOptions').innerHTML = 'hidden';
        document.getElementById('checkAdvanced').checked = false;

        //document.getElementById('form[SCH_END_DATE]').value = '';
        document.getElementById('form[SCH_END_DATE][div]').innerHTML = '';
        document.getElementById('form[SCH_REPEAT_TASK_CHK]').checked = false;


   } else {
        //expandSubtitle('ADVANCED_4');
        document.getElementById('checkAdvanced').checked = true;
        document.getElementById('EditLogAdvancedOptions').innerHTML = 'visible';
   }
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

]]></SHOW_HIDE_JS>
</dynaForm>
