/* 
event_management.js: library of javascript for event management
    david.kelly@fanore.com (http://www.fanore.com/website_design.htm)
    
    
    
*/
// checks that an event editing form has been completed correctly
function check_event(){
    var progress_bar_url=$F('progress_bar_url');
    if ($F('private_name').blank()){
        show_error_message('Please enter the private name of this event!','error_message_panel', false);
        $('private_name').focus();
        return false;
    
    }
    if ($F('event_group').blank()){
        show_error_message('Please enter the type event (e.g. Course, One Day Workshop, etc.)!','error_message_panel', false);
        $('event_group').focus();
        return false;
    }
    $('event_group_other').value=$F('event_group');
   // alert($F('event_group_other'));
    if ($F('event_title').blank()){
        show_error_message('Please enter the name of this event!','error_message_panel', false);
        $('event_title').focus();
        return false;
    }
    
    if ($F("venue").blank()){
        show_error_message('Please select the venue!!','error_message_panel', false);
        $('venue').focus();
        return false;
    
    }





    var start_date=0;
    var end_date=0;
    var cut_off_date=0;
    var cut_off_time=0;
    var publish_end_date=0;
    var publish_end_time=0;
    var rule_day=0;
    var rule_month=0;
    var rule_year=0;
    var rule_hour=0;
    var rule_minute=0;
// event start date validation    
    var el_day=get_el("start_day")
    if (el_day.selectedIndex < 1){
        show_error_message("Please select the day of the month that this event starts!",'error_message_panel', false);
        el_day.focus();
        return false
    }
    else{
        rule_day = el_day[el_day.selectedIndex].value;
    }
    var el_month=get_el("start_month")
    if (el_month.selectedIndex < 1){
        show_error_message("Please select the month that this event starts!",'error_message_panel', false);
        el_month.focus();
        return false;                }
    else{
        rule_month = el_month[el_month.selectedIndex].value;
    }
   // alert(rule_month);
   // alert(el_month[el_month.selectedIndex].text);
    var el_year=get_el("start_year")
    if (el_year.selectedIndex < 1){
        show_error_message("Please select the year that this event starts!",'error_message_panel', false);
        el_year.focus();
        return false;                }
    else{
        rule_year = el_year[el_year.selectedIndex].value;
    }
    var el_hour=get_el("start_hour")
    if (el_hour.selectedIndex < 1){
        show_error_message("Please select the start hour value to use for this event!",'error_message_panel', false);
        el_hour.focus();
        return false;                }
    else{
        rule_hour = el_hour[el_hour.selectedIndex].value;
    }
    
    var el_minute=get_el("start_minute")
    if (el_minute.selectedIndex < 1){
        show_error_message("Please select the start minute value to use for this event!",'error_message_panel', false);
        el_minute.focus();
        return false;                }
    else{
        rule_minute = el_minute[el_minute.selectedIndex].value;
    }
    
    var s_date=rule_day + " " + rule_month +  " " + rule_year  + " " + rule_hour + ":" + rule_minute 
    var d=new Date(s_date);
    //alert(d.getDate());
    //alert(parseInt(el_day.selectedIndex));
    //we test the date is valid by checking the date value against the selectedIndex value (should always be 1 less than selectedIndex)
    if (d.getDate()!=(parseInt(el_day.selectedIndex))){
        show_error_message("You have selected an invalid day of the month, please check your entry!",'error_message_panel', false);
        el_day.focus();
        return false;
    }
    start_date=s_date;
// event end date validation
    var el_day=get_el("end_day")
    if (el_day.selectedIndex < 1){
        show_error_message("Please select the day of the month that this event finishes!",'error_message_panel', false);
        el_day.focus();
        return false
    }
    else{
        rule_day = el_day[el_day.selectedIndex].value;
    }
    var el_month=get_el("end_month")
    if (el_month.selectedIndex < 1){
        show_error_message("Please select the month that this event finishes!",'error_message_panel', false);
        el_month.focus();
        return false;                }
    else{
        rule_month = el_month[el_month.selectedIndex].value;
    }
    var el_year=get_el("end_year")
    if (el_year.selectedIndex < 1){
        show_error_message("Please select the year that this event finishes!",'error_message_panel', false);
        el_year.focus();
        return false;                }
    else{
        rule_year = el_year[el_year.selectedIndex].value;
    }
    var el_hour=get_el("end_hour")
    if (el_hour.selectedIndex < 1){
        show_error_message("Please select the end hour value to use for this event!",'error_message_panel', false);
        el_hour.focus();
        return false;                }
    else{
        rule_hour = el_hour[el_hour.selectedIndex].value;
    }
    
    var el_minute=get_el("end_minute")
    if (el_minute.selectedIndex < 1){
        show_error_message("Please select the end minute value to use for this event!",'error_message_panel', false);
        el_minute.focus();
        return false;                }
    else{
        rule_minute = el_minute[el_minute.selectedIndex].value;
    }
    
    var s_date=rule_day + " " + rule_month +  " " + rule_year  + " " + rule_hour + ":" + rule_minute 
    var d=new Date(s_date);
    //alert(d.getDate());
    //alert(parseInt(el_day.selectedIndex));
    //we test the date is valid by checking the date value against the selectedIndex value (should always be 1 less than selectedIndex)
    if (d.getDate()!=parseInt(el_day.selectedIndex)){
        show_error_message("You have selected an invalid day of the month, please check your entry!",'error_message_panel', false);
        el_day.focus();
        return false;
    }
    end_date=s_date;
    
    $('start_date').value=start_date;
    $('end_date').value=end_date;
// event cut_off date validation    
    var el_day=get_el("cut_day")
    if (el_day.selectedIndex < 1){
        show_error_message("Please select the day of the month on which bookings are cut off!",'error_message_panel', false);
        el_day.focus();
        return false
    }
    else{
        rule_day = el_day[el_day.selectedIndex].value;
    }
    var el_month=get_el("cut_month")
    if (el_month.selectedIndex < 1){
        show_error_message("Please select the month  on which bookings are cut off!",'error_message_panel', false);
        el_month.focus();
        return false;                }
    else{
        rule_month = el_month[el_month.selectedIndex].value;
    }

    var el_year=get_el("cut_year")
    if (el_year.selectedIndex < 1){
        show_error_message("Please select the year on which bookings are cut off!",'error_message_panel', false);
        el_year.focus();
        return false;                }
    else{
        rule_year = el_year[el_year.selectedIndex].value;
    }
    
    var el_hour=get_el("cut_hour")
    if (el_hour.selectedIndex < 1){
        show_error_message("Please select the hour on which bookings are cut off!",'error_message_panel', false);
        el_hour.focus();
        return false;                }
    else{
        rule_hour = el_hour[el_hour.selectedIndex].value;
    }
    
    var el_minute=get_el("cut_minute")
    if (el_minute.selectedIndex < 1){
        show_error_message("Please select the minute on which bookings are cut off!",'error_message_panel', false);
        el_minute.focus();
        return false;                }
    else{
        rule_minute = el_minute[el_minute.selectedIndex].value;
    }
    
    var s_date=rule_day + " " + rule_month +  " " + rule_year + " " + rule_hour + ":" + rule_minute 
    var d=new Date(s_date);
    
    //alert(d);
    //return false;
    //alert(parseInt(el_day.selectedIndex));
    //we test the date is valid by checking the date value against the selectedIndex value (should always be 1 less than selectedIndex)
    if (d.getDate()!=(parseInt(el_day.selectedIndex))){
        show_error_message("You have selected an invalid day of the month, please check your entry!",'error_message_panel', false);
        el_day.focus();
        return false;
    }
//    alert(s_date);
    $('cut_off_date').value=s_date;

    
// event cut_off date validation    
    var el_day=get_el("publish_end_day")
    if (el_day.selectedIndex < 1){
        show_error_message("Please select the day of the month that this event will cease to be published on the web site!",'error_message_panel', false);
        el_day.focus();
        return false
    }
    else{
        rule_day = el_day[el_day.selectedIndex].value;
    }
    var el_month=get_el("publish_end_month")
    if (el_month.selectedIndex < 1){
        show_error_message("Please select the month that this event will cease to be published on the web site!",'error_message_panel', false);
        el_month.focus();
        return false;                }
    else{
        rule_month = el_month[el_month.selectedIndex].value;
    }

    var el_year=get_el("publish_end_year")
    if (el_year.selectedIndex < 1){
        show_error_message("Please select the year that this event will cease to be published on the web site!",'error_message_panel', false);
        el_year.focus();
        return false;                }
    else{
        rule_year = el_year[el_year.selectedIndex].value;
    }
    
    var el_hour=get_el("publish_end_hour")
    if (el_hour.selectedIndex < 1){
        show_error_message("Please select the hour that this event will cease to be published on the web site!",'error_message_panel', false);
        el_hour.focus();
        return false;                }
    else{
        rule_hour = el_hour[el_hour.selectedIndex].value;
    }
    
    var el_minute=get_el("publish_end_minute")
    if (el_minute.selectedIndex < 1){
        show_error_message("Please select the minute that this event will cease to be published on the web site!",'error_message_panel', false);
        el_minute.focus();
        return false;                }
    else{
        rule_minute = el_minute[el_minute.selectedIndex].value;
    }
    
    var s_date=rule_day + " " + rule_month +  " " + rule_year + " " + rule_hour + ":" + rule_minute 
    var d=new Date(s_date);
    
    //alert(d);
    //return false;
    //alert(parseInt(el_day.selectedIndex));
    //we test the date is valid by checking the date value against the selectedIndex value (should always be 1 less than selectedIndex)
    if (d.getDate()!=(parseInt(el_day.selectedIndex))){
        show_error_message("You have selected an invalid day of the month, please check your entry!",'error_message_panel', false);
        el_day.focus();
        return false;
    }
    $('publish_end_date').value=s_date;

    
    
//    if ($F("accredita").blank()){
//        show_error_message('Please select the venue!!','error_message_panel');
//        $('venue').focus();
//        return false;
//    
//    }
    
    var el=get_el('FCKDATASOURCE');
    if (el){    
        var oEditor = FCKeditorAPI.GetInstance('txtHTML') ;
        var datasource_name = 'fd_' + el.value;
       $(datasource_name).value= oEditor.GetXHTML(true);  
    }       
        

    //return false;
    $('save').target='keyhole'; 
// display a window indicating save progress 
            var w=380;
            var h=50;
            var winLeft=Math.round((screen.width - w)/2);
            var winTop=Math.round((screen.height - h)/2);
            var sVersion = navigator.appVersion;
             
           if (sVersion.indexOf('MSIE') != -1 && sVersion.substr(sVersion.indexOf('MSIE')+5,1) > 4)
          {
        		var settings="dialogWidth:380px; dialogHeight:50px; center:yes";
        		var x=window.showModelessDialog(progress_bar_url+ '&b=IE',null,settings);
                x.opener=window
            }
            else
            {
        		//var x=window.open(progress_bar_url+ '&b=NN','','width=370,height=115', true);
                var x=window.open(progress_bar_url+ '&b=NN','','width=380px,height=50', true);
        
           }
           
            x.focus()
   
   

            

    $('save').submit();
    
    
}

// display the trainers addressbook
function trainers(){

         var l=current_element.cumulativeOffset().left-100;
         var t=current_element.cumulativeOffset().top-50;
         var w=document.viewport.getWidth()-420;
         var d_top=document.viewport.getHeight-400;
         if (parseInt(l) > parseInt(w)){
         //   l=w;
         }
         if (parseInt(t)> parseInt(d_top)){
          //  t=d_top;
         } 
        $('trainers_addressbook').setStyle({top: t + 'px', left: l + 'px'}); 
        new Effect.Appear('trainers_addressbook');
}
//list_trainers: used to update addressbook 
function list_trainers(param_record){
    var el=get_el("organisations");
    if (el){
        //if (el[el.selectectedIndex].value)
        var url="../asp/ajax.asp"
        var p="objectid=1152&mode=100&recordid=" + param_record + "&tag=trainers_addressbook&o=" + $F('organisations')
        
        ajax_get_url(url,p, 'list_contacts', '')
    }
}

function update_trainers(){
    // checks what records have been selected and adds them to the list (if necessary)
    var el=get_el("current"); // comma delimited list of 'address book' trainers, used to loop to see if any are selected
    var buffer=el.value;
    var el=get_el("trainers"); // contains comma delimited list of 'currently assigned' trainer id's
    var svalue=el.value;
    //alert(svalue);
    if (svalue.charAt(svalue.length - 1) == ",") svalue = svalue.substr(0,svalue.length - 1);
	var s_col=svalue.split(",");
    
    if (buffer.charAt(buffer.length - 1) == ",") buffer = buffer.substr(0,buffer.length - 1);
	var sdata=buffer.split(",");
    
    for (var i=0;i<sdata.length;i++){ // loop through the possiblities
        if($F('o_' + sdata[i])!=null){ // check if the 'candidate trainer' is selected for addition
            if (check_trainer(s_col,sdata[i])){ // check that 'candidate trainer' is not already assigned
                //get to here then ok to add this record
                //alert($('u_' + sdata[i]).innerText);
                //alert($F('organisations'));
                var org=get_el('organisations');
                var s_name=$('u_' + sdata[i]).innerText + ' (' + org[org.selectedIndex].text + ')';
                var el=get_el('list_trainers');
                //alert($('trainers').value);
                $('trainers').value+=',' + sdata[i];
                //alert(el.options);
                //alert(el.length);
                //el.length++;
                //var n_opt=new Option(s_name, sdata[i].toString());
                //el.options[el.length-1]=n_opt;
                el.innerHTML+='<a href="javascript: remove_trainer(' + sdata[i] + ');" id="traininglink' + sdata[i] + '">' + s_name + '</a><br/>';
                //alert(s_name);
                //alert($('trainers').value);
                
                var uid=sdata[i];
                
            }
        };
    }
}

function check_trainer(param_sel,param_new){
             for (var x=0;x<param_sel.length;x++){
                    if (param_sel[x]==param_new){
                        return false;
                    }
              }
            
return true;
}

//close_addressbook
function close_addressbook(){
    $('trainers_addressbook').hide();
}


//setup_personal_form: make sure the gender drop down reflects what was entered previously
function setup_personal_ac_form(){
    //gender
    if (!$F('h_gender').blank()){
        var el=get_el('gender');
        for (var i=0;i<el.options.length;i++){
            if ($F('h_gender')==el.options[i].value){
                el.selectedIndex=i;
            }
        }
    }
    //year started
    if (!$F('h_year_started').blank()){
        var el=get_el('year_started');
        for (var i=0;i<el.options.length;i++){
            if ($F('h_year_started')==el.options[i].value){
                el.selectedIndex=i;
            }
        }
    }
    //age
    if (!$F('h_age').blank()){
        var el=get_el('age');
        for (var i=0;i<el.options.length;i++){
            if ($F('h_age')==el.options[i].value){
                el.selectedIndex=i;
            }
        }
    }
    //occupation
    if (!$F('occupation_other').blank()){
        var el=get_el('occupation');
        for (var i=0;i<el.options.length;i++){
            if ($F('occupation_other')==el.options[i].value){
                el.selectedIndex=i;
            }
        }
    }
    //qualification
    if (!$F('h_qualification').blank()){
        var el=get_el('opt_qualification');
        //alert(el.length);
        for (var i=0;i<el.length;i++){
            if ($F('h_qualification')==el[i].value){
                el[i].checked=true;
            }
        }
    }

    //year_qualified
    if (!$F('h_year_qualified').blank()){
        var el=get_el('year_qualified');
        for (var i=0;i<el.options.length;i++){
            if ($F('h_year_qualified')==el.options[i].value){
                el.selectedIndex=i;
            }
        }
    }

    
    //courses
    if (!$F('h_courses').blank()){
        var el=get_el('courses');
        for (var i=0;i<el.options.length;i++){
            if ($F('h_courses')==el.options[i].value){
                el.selectedIndex=i;
            }
        }
    }
    enable_tab_tracking('personalac');

}


function fix_telephone(param_value){
    var ret_value='';
    var wip_value=param_value;
    var a_values='1234567890'
    //alert(wip_value.length);
    for (var i=0;i<=wip_value.length;i++){
    //alert(wip_value.charAt(i))
        for (var x=0;x<=a_values.length;x++){
            
            if (wip_value.charAt(i)==a_values.charAt(x)){
                ret_value+=wip_value.charAt(i)
            }
        }
    } 
    //alert(ret_value);
    return ret_value;   
}



//check_personal_ac; check that personal information has been entered 
function check_personal_ac(){
    var fetac=parseInt($F('flag_fetac'))
    $('your_details').value="";
    //firstname
    if ($F('firstname').blank()){
        alert("Please enter the attendee\'s first name");
        $('firstname').focus();
        return false;
    }
    
    //surname
    if ($F('surname').blank()){
        alert("Please enter  the attendee\'s surname");
        $('surname').focus();
        return false;
    }
    //email
    if ($F('email').blank()){
        alert("Please enter   the attendee\'s  email address");
        $('email').focus();
        return false;
    }
    var email=$F('email')
    if (!fixstring(email,true)){
        alert("Sorry, but  the attendee\'s  email address does not seem to be valid or is in a format that is not supported on our system");
        $('email').focus();
        return false;
    }
    //address 1 to 4
    if ($F('address1').blank()){
        alert("Please enter at least a two line address");
        $('address1').focus();
        return false;
    }
    if ($F('address2').blank()){
        alert("Please enter at least a two line address");
        $('address2').focus();
        return false;
    }
    if ($F('list_locations').blank()){
        alert("Please select the county from the list provided");
        $('list_locations').focus();
        return false;
    }
    $('address4').value=$F('list_locations');
    //telephone or mobile
    if (($F('telephone').blank())&&($F('mobile').blank())){
        alert("Please enter either telephone or mobile number");
        $('telephone').focus();
        return false;
    }
    
    var telephone=fix_telephone($F('telephone'));
    var mobile=fix_telephone($F('mobile'));
    $('telephone').value=telephone;
    $('mobile').value=mobile;
    if (((telephone.blank())||(telephone.length<5))&&((mobile.blank())||(mobile.length<5))){
        alert("Please check the telephone number that you have entered as it does not seem to be a valid number");
        $('telephone').value=telephone;
        $('mobile').value=mobile;
        $('telephone').focus();
        return false;
    }
    //not fetac ==> we need age
    if ($F('age').blank()){
        alert("Please select  the attendee\'s age range from the list provided");
        $('age').focus();
        return false;
    }
    if (fetac==1){
    
            //pps
            if ($F('pps').blank()){
                alert("Please enter  the attendee\'s  PPS number");
                $('pps').focus();
                return false;
            }
            //dob
            var rule_day='';
            var rule_month='';
            var rule_year='';
            
            var el_day=get_el("dob_day")
            if (el_day.selectedIndex < 1){
                alert("Please select the attendee\'s  date of birth");
                el_day.focus();
                return false
            }
            else{
                rule_day = el_day[el_day.selectedIndex].value;
            }
            var el_month=get_el("dob_month")
            if (el_month.selectedIndex < 1){
                alert("Please select  the attendee\'s date of birth");
                el_month.focus();
                return false;                }
            else{
                rule_month = el_month[el_month.selectedIndex].value;
            }
           // alert(rule_month);
           // alert(el_month[el_month.selectedIndex].text);
            var el_year=get_el("dob_year")
            if (el_year.selectedIndex < 1){
                alert("Please select the attendee\'s date of birth");        
                el_year.focus();
                return false;                }
            else{
                rule_year = el_year[el_year.selectedIndex].value;
            }
            
            var s_date=rule_day + " " + rule_month +  " " + rule_year 
            var d=new Date(s_date);
            //alert(s_date);
            //alert(d);
            //alert(d.getDate())
            //alert((parseInt(el_day.selectedIndex)));
            //we test the date is valid by checking the date value against the selectedIndex value (should always be 1 less than selectedIndex)
            if (d.getDate()!=(parseInt(el_day.selectedIndex))){
                alert("You have selected an invalid day of the month, Please check your entry");
                el_day.focus();
                return false;
            }
            $('dob').value=s_date;
     }
        
    //gender             
    if ($F('gender').blank()){
        alert("Please select  the attendee\'s gender");
        $('gender').focus();
        return false;
    }
    
    // now check the employment details section
     //company
    if ($F('emp_company').blank()){
        alert("Please enter the  the attendee\'s company name ");
        $('emp_company').focus();
        return false;
    }
    //address 1 to 4
    if ($F('emp_address1').blank()){
        alert("Please enter at least a two line address ");
        $('emp_address1').focus();
        return false;
    }
    if ($F('emp_address2').blank()){
        alert("Please enter at least a two line address ");
        $('emp_address2').focus();
        return false;
    }
    if ($F('emp_list_locations').blank()){
        alert("Please select the county from the list provided ");
        $('emp_list_locations').focus();
        return false;
    
    }
    $('emp_address4').value=$F('emp_list_locations')
    //telephone
    if ($F('emp_telephone').blank()){
        alert("Please enter the company telephone number ");
        $('emp_telephone').focus();
        return false;
    }
    var telephone=fix_telephone($F('emp_telephone'));
    $('emp_telephone').value=telephone;
    if ((telephone.blank())||(telephone.length<5)){
        alert("Please check the telephone number that you have entered as it does not seem to be a valid number ");
        $('emp_telephone').value=telephone;
        $('emp_telephone').focus();
        return false;
    }
    
    
    //job title
    if ($F('emp_jobtitle').blank()){
        alert("Please enter  the attendee\'s job title");
        $('emp_jobtitle').focus();
        return false;
    }
    
    
    
    if ($F('year_started').blank()){
        alert("Please select the year  the attendee started with current employer");
        $('year_started').focus();
        return false;
    }
    
    
    //check the educational section of the form
    var el=document.forms['personalac'].elements['opt_qualification']
    //alert(el.length);
   // alert($F('opt_qualification'));
    //var el=get_el("opt_qualification");
    var s_value='';
    //alert(Object.inspect(el));
    for (var i=0; i<el.length;i++){
        //alert(el[i].checked);
        if (el[i].checked==true){
            s_value=el[i].value;
            
        }
    }
    if (s_value.blank()){
        alert("Please select  the attendee\'s highest educational qualification to date");
        el[0].focus();
        return false;
    }
    $('h_qualification').value=s_value;
    //year_qualified
    if ($F('year_qualified').blank()){
        alert("Please select the year that  the attendee achieved the level of education specified");
        $('year_qualified').focus();
        return false;
    }

    
    
    $('personalac').submit();
}

function invoice_editor(param_element, param_direction){
    if (param_direction==0){
        $('utility').innerHTML=$(param_element).innerHTML;
        $(param_element).innerHTML=$(param_element).innerHTML.toString().replace("form","_form");
        //alert($(param_element).innerHTML);
        position_utility();        
    }
    else{
        //alert($(param_element).innerHTML);
        $(param_element).innerHTML=$(param_element).innerHTML.toString().replace("_form","form");
        new Effect.DropOut('utility');
        }
}

function validate_invoice_modification(param_form_id){
    //alert(param_form_id);
    switch(param_form_id){
        case 'line_item_editor_form':
            // ensure that a numeric value has been entered for the price and that a valid event has been selected
            var f=$(param_form_id);
            var el=f['price'];
            if (isNaN(el.value)){
                alert("please enter only a numeric value for the price!");
                return false;
            };
            break;
        case 'invoice_date_form':
            //validate the invoice date
            //alert("here");
                var f=$(param_form_id);
                    var rule_day=0;
                    var rule_month=0;
                    var rule_year=0;
                // event start date validation    
                    var el_day=f["invoice_day"]
                    //alert(el_day.selectedIndex);
                    if (el_day.selectedIndex < 1){
                        show_error_message("Please select the day of the month!",'error_message_panel', false);
                        el_day.focus();
                        return false
                    }
                    else{
                        rule_day = el_day[el_day.selectedIndex].value;
                    }
                    var el_month=f["invoice_month"]
                    if (el_month.selectedIndex < 1){
                        show_error_message("Please select the month!",'error_message_panel', false);
                        el_month.focus();
                        return false;                }
                    else{
                        rule_month = el_month[el_month.selectedIndex].value;
                    }
                   // alert(rule_month);
                   // alert(el_month[el_month.selectedIndex].text);
                    var el_year=f["invoice_year"]
                    if (el_year.selectedIndex < 1){
                        show_error_message("Please select the year!",'error_message_panel', false);
                        el_year.focus();
                        return false;                }
                    else{
                        rule_year = el_year[el_year.selectedIndex].value;
                    }
                    var s_date=rule_day + " " + rule_month +  " " + rule_year  
                    var d=new Date(s_date);
                    //alert(d.getDate());
                    //alert(parseInt(el_day.selectedIndex));
                    //we test the date is valid by checking the date value against the selectedIndex value (should always be 1 less than selectedIndex)
                    if (d.getDate()!=(parseInt(el_day.selectedIndex))){
                        show_error_message("You have selected an invalid day of the month, please check your entry!",'error_message_panel', false);
                        el_day.focus();
                        return false;
                    }
                    var el=f['date_invoiced']
                    el.value=s_date;
            break;
        default:
    }
    //$('utility').hide();
    $(param_form_id).request({onComplete: function(){ window.location.reload()} }) 
    //alert($(param_form_id).serialize());  
   //$(param_form_id).submit();
}


function select_attendees(param_form){
    var el=get_el('opt_select');
    //alert(el.length);
    for (var i=0;i<el.length;i++){
        if (el[i].checked==true){
            el[i].checked=false;
        }
        else{
            el[i].checked=true;
        }
        
    }
}

function message_attendees(param_form){
    var buffer='0'
    var el=get_el('opt_select');
    //alert(el.length);
    for (var i=0;i<el.length;i++){
        if (el[i].checked==true){
            buffer+=',' + el[i].value;
        }
    }
    if (buffer.length==1){
        alert("please select one or more attendees before proceeding");
        return void[0];
    }
    $('attendees').value=buffer;
//    $('form_select').action='../asp/object_administration.asp?objectid=1152&mode=6&recordid='
    $('form_select').submit();
}

function message_trainers(param_form){
    var buffer='0'
    var el=get_el('opt_select');
    //alert(el.length);
    for (var i=0;i<el.length;i++){
        if (el[i].checked==true){
            buffer+=',' + el[i].value;
        }
    }
    if (buffer.length==1){
        alert("please select one or more attendees before proceeding");
        return void[0];
    }
    $('accounts').value=buffer;
//    $('form_select').action='../asp/object_administration.asp?objectid=1152&mode=6&recordid='
    $('form_trainers').submit();
}

function export_attendees(param_form){
    var buffer='0'
    var el=get_el('opt_select');
    //alert(el.length);
    for (var i=0;i<el.length;i++){
        if (el[i].checked==true){
            buffer+=',' + el[i].value;
        }
    }
    if (buffer.length==1){
        alert("please select one or more attendees before proceeding");
        return void[0];
    }
    $('export_accounts').value=buffer;
//    $('form_select').action='../asp/object_administration.asp?objectid=1152&mode=6&recordid='
//    $('form_export').target=""
    $('form_export').submit();
}



function show_recipients(){
    
    var el=get_el("list_types");
    if (el.selectedIndex==0){
        $('list_attendees').hide();
        $('list_bookers').hide();
        $('list_trainers').hide();
        $('show_list').innerText="show"
        return void[0];
    }
    if ($('show_list').innerText=="show"){
        switch(parseInt($F('list_types'))){
            case 0://attendees
                $('list_bookers').hide();
                $('list_trainers').hide();
                $('list_attendees').show();
                $('show_list').innerText="hide"
                 //alert("here");
                 break;
            case 1: //bookers
                $('list_attendees').hide();
                $('list_trainers').hide();
                $('list_bookers').show();
                $('show_list').innerText="hide"
                 //alert("here");
                 break;
            case 2://trainers
                $('list_attendees').hide();
                $('list_bookers').hide();
                $('list_trainers').show();
                $('show_list').innerText="hide"
                 //alert("here");
                 break;
            
            default:   
        }
    }
    else{
        $('list_bookers').hide();
        $('list_attendees').hide();
        $('list_trainers').hide();
        $('show_list').innerText="show"
    }    
}


function check_message(){
    var progress_bar_url=$F('progress_bar_url');
    if ($F('list_types').blank()){
        show_error_message('Please select the recipients!','error_message_panel');
        $('list_types').focus();
        return false;
    
    }
    if ($F('s_subject').blank()){
        show_error_message('Please enter the message subject!','error_message_panel');
        $('s_subject').focus();
        return false;
    
    }
    
    
    //sceduled send date
    if ($F('start_day').blank()){
        show_error_message('Please select the day that this message is to be scheduled for!','error_message_panel');
        $('start_day').focus();
        return false;
    }
    if ($F('start_month').blank()){
        show_error_message('Please select the month that this message is to be scheduled for!','error_message_panel');
        $('start_month').focus();
        return false;
    }
    if ($F('start_year').blank()){
        show_error_message('Please select the year that this message is to be scheduled for!','error_message_panel');
        $('start_year').focus();
        return false;
    }
    if ($F('start_hour').blank()){
        show_error_message('Please select the hour that this message is to be scheduled for!','error_message_panel');
        $('start_hour').focus();
        return false;
    }
    if ($F('start_minute').blank()){
        show_error_message('Please select the minute that this message is to be scheduled for!','error_message_panel');
        $('start_minute').focus();
        return false;
    }
    var scheduled_date=$F('start_day') + " " + $F('start_month') +  " " + $F('start_year')  + " " + $F('start_hour') + ":" + $F('start_minute');
    var js_date=new Date(scheduled_date);
    var test_date=new Date();
    if (js_date < test_date){
        show_error_message('Please set the schedule at least twenty minutes ahead of now to ensure that you have time to review the result!','error_message_panel');
        $('start_minute').focus();
        return false;
    
    }
    if (js_date.getDate()==test_date.getDate()){//need to check the hours
        if ((js_date.getHours() < test_date.getHours())){
            show_error_message('Please set the schedule at least twenty minutes ahead of now to ensure that you have time to review the result!','error_message_panel');
            $('start_minute').focus();
            return false;
        }
    }
    //alert("here")
    var el=get_el('FCKDATASOURCE');
    if (el){    
        var oEditor = FCKeditorAPI.GetInstance('txtHTML') ;
        var datasource_name = 'fd_' + el.value;
       $(datasource_name).value= oEditor.GetXHTML(true);  
    }       

    
    if ($F('fd_description').blank()){
        show_error_message('Please enter a message to be sent!','error_message_panel');
        //$('s_message').focus();
        return false;
    }
    
    if (!$F('s_file1').blank()){
        $('a1').value='1';
    }
    if (!$F('s_file2').blank()){
        $('a2').value='1';
    }
    
   //alert("here")
    $('message').target='keyhole'; 
// display a window indicating save progress 
            var w=380;
            var h=50;
            var winLeft=Math.round((screen.width - w)/2);
            var winTop=Math.round((screen.height - h)/2);
            var sVersion = navigator.appVersion;
             
           if (sVersion.indexOf('MSIE') != -1 && sVersion.substr(sVersion.indexOf('MSIE')+5,1) > 4)
          {
        		var settings="dialogWidth:380px; dialogHeight:50px; center:yes";
        		var x=window.showModelessDialog(progress_bar_url+ '&b=IE',null,settings);
                x.opener=window
            }
            else
            {
        		//var x=window.open(progress_bar_url+ '&b=NN','','width=370,height=115', true);
                var x=window.open(progress_bar_url+ '&b=NN','','width=380px,height=50', true);
        
           }
           
            x.focus()
   
   

            

    $('message').submit();
}

function check_trainers_message(){
    var progress_bar_url=$F('progress_bar_url');
    if ($F('s_subject').blank()){
        show_error_message('Please enter the message subject!','error_message_panel');
        $('s_subject').focus();
        return false;
    
    }
    
    
    var el=get_el('FCKDATASOURCE');
    if (el){    
        var oEditor = FCKeditorAPI.GetInstance('txtHTML') ;
        var datasource_name = 'fd_' + el.value;
       $(datasource_name).value= oEditor.GetXHTML(true);  
    }       

    
    if ($F('fd_description').blank()){
        show_error_message('Please enter a message to be sent!','error_message_panel');
        //$('s_message').focus();
        return false;
    }
    
    if (!$F('s_file1').blank()){
        $('a1').value='1';
    }
    
    $('message').target='keyhole'; 
// display a window indicating save progress 
            var w=380;
            var h=50;
            var winLeft=Math.round((screen.width - w)/2);
            var winTop=Math.round((screen.height - h)/2);
            var sVersion = navigator.appVersion;
             
           if (sVersion.indexOf('MSIE') != -1 && sVersion.substr(sVersion.indexOf('MSIE')+5,1) > 4)
          {
        		var settings="dialogWidth:380px; dialogHeight:50px; center:yes";
        		var x=window.showModelessDialog(progress_bar_url+ '&b=IE',null,settings);
                x.opener=window
            }
            else
            {
        		//var x=window.open(progress_bar_url+ '&b=NN','','width=370,height=115', true);
                var x=window.open(progress_bar_url+ '&b=NN','','width=380px,height=50', true);
        
           }
           
            x.focus()
   
   

            

    $('message').submit();


}
//preview_message(): display a preview of the message to the user
function preview_message(){
       var el=get_el('FCKDATASOURCE');
    if (el){    
        var oEditor = FCKeditorAPI.GetInstance('txtHTML') ;
       $('fd_preview').value= oEditor.GetXHTML(true);  
    }  
    $('fd_subject').value=$('s_subject').value;
    $('message_preview').target='_blank';   
    $('message_preview').submit();   
//    return false;
}

function remove_trainer(param_id){
   //b4 removal
   var buf=$('trainers').value;
   var obuffer='0'
   var adata=buf.split(",");
   for (var i=0;i<adata.length;i++){
        if (parseInt(param_id)!=parseInt(adata[i])){
            obuffer+=',' + adata[i];
        }
   }
   $('trainers').value=obuffer;
   Element.remove('traininglink' + param_id);
   return void[0];
   
}
