/* 
iitd_bookings.js: library of javascript for booking events
    david.kelly@fanore.com (http://www.fanore.com/website_design.htm)
    
    
    
*/
function checkout(){
    $('do_checkout').value='1';
    check_basket();
}
function delete_basket(param_id){
    if (confirm('are you sure you want to delete your basket?')){
        window.location.href="../basket-action-deletebasket.htm"
    }
    return void[0];
}
//check_basket: ensure that user has entered only a valid numeric for any input elements
function check_basket(){
    //alert('check_basket');
    var f=$('basket');
    var buttons=f.getInputs('text');
    for (var i=0;i<buttons.length;i++){
        if(buttons[i].id.toString().include('quantity')){
            var el_id=buttons[i].id.toString().replace('quantity_','');
            //alert(el_id);
            if ($F(buttons[i].id).blank()){
                alert("Please only enter numeric values for the number of places ");
                $(buttons[i].id).value="1"
                $(buttons[i].id).focus();
                return false;
            }
            //check is numeric
            if (isNaN($F(buttons[i].id))){
                alert("Please only enter numeric values for the number of places ");
                $(buttons[i].id).value="1"
                $(buttons[i].id).focus();
                return false;
            }
            if (parseInt($F(buttons[i].id))<= 0){
                alert("Please only enter positive values for the number of places ");
                $(buttons[i].id).value="1"
                $(buttons[i].id).focus();
                return false;
            
            }
            // work out how many places have been booked, and how many provisional bookings exist and check amount requested against these figures
            var p = parseInt($F('places_' + el_id))
            var b = $F('bookings_' + el_id)
            var provisional = parseInt($F('provisional_' + el_id))
            var requested =  parseInt($F('quantity_' + el_id))
            if (p !=0){ //there IS a cap on number of places
                var remaining = p - provisional
                if (remaining < requested){
                    alert("We're sorry but there are only " + remaining + " places remaining for the selected course");
                    $(buttons[i].id).value=remaining.toString();
                    $(buttons[i].id).focus();
                    return false;
                }
               
            }
        };
    }
    $('your_details').value='';
    $('basket').submit();
}


//check_course_registration: validate the course registration dialogue
function check_course_registration(){
    if ($F('txt_firstname').blank()){
        alert("Please enter your firstname !");
        $('txt_firstname').focus();
        return void[0];
    }
    if ($F('txt_surname').blank()){
        alert("Please enter your surname !");
        $('txt_surname').focus();
        return void[0];
    }
    if ($F('txt_user').blank()){
        alert("Please enter your email address !");
        $('txt_user').focus();
        return void[0];
    }
    var s=$F('txt_user');
    if (!fixstring(s,true)){
        alert("The format of the email address you have entered is not supported on this system, please check your entry!");
        $('txt_user').focus();
        return void[0];
    
    }
    if ($F('address1').blank()){
        alert("Please enter your full address !");
        $('address1').focus();
        return void[0];
    }
    if ($F('address2').blank()){
        alert("Please enter your full address !");
        $('address2').focus();
        return void[0];
    }
    if ($F('address4').blank()){
        alert("Please select the county !");
        $('address4').focus();
        return void[0];
    }
    var telephone=fix_telephone($F('telephone'));
    $('telephone').value=telephone;
    
    var mobile=fix_telephone($F('mobile'));
    $('mobile').value=mobile;
    
    if (($F('telephone').blank())&&($F('mobile').blank())){
        alert("Please enter either a land line or mobile telephone number !");
        $('telephone').focus();
        return void[0];
    }
    if ($F('pps').blank()){
        alert("Please enter your PPS number !");
        $('pps').focus();
        return void[0];
    }
    
    //date_of_birth
            var rule_day='01';
            var rule_month='';
            var rule_year='';
            
            var el_day=get_el("dob_day")
            if (el_day.selectedIndex < 1){
                alert("Please select the Day!");
                el_day.focus();
                return void[0];          }
            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 month!");
                el_month.focus();
                return void[0];      }
            else{
                rule_month = el_month[el_month.selectedIndex].value;
            }
            
            var el_year=get_el("dob_year")
            if (el_year.selectedIndex < 1){
                alert("Please select the year!");        
                el_year.focus();
                return void[0]; }
            else{
                rule_year = el_year[el_year.selectedIndex].value;
            }
            var s_date=rule_day + " " + rule_month +  " " + rule_year 
            if (parseInt(rule_year) > 1992){
                alert("Please check your date of birth as our courses are for persons aged 18 and over!");
                el_year.focus();
                return void[0];
            
            }
            $('dob').value=s_date;
            
    
     //return void[0];
    
    $('your_details').value='';
    $('course_registration').submit();
}
// populate_billing: auto-populate billing form using hidden employer detail field values
function populate_billing(){
    $('company').value=$F('e_company')
    $('address1').value=$F('e_address1');
    $('address2').value=$F('e_address2');
    $('address3').value=$F('e_address3');
    $('address4').value=$F('e_address4');
    $('telephone').value=$F('e_telephone');
    $('list_locations').value=$F('e_address4');
}


//check_invoice(): validate the invoice details form
function check_invoice(){
 //company
    if ($F('company').blank()){
        alert("Please enter the company name ");
        $('company').focus();
        return void[0];
    }
    //address 1 to 4
    if ($F('address1').blank()){
        alert("Please enter at least a two line address ");
        $('address1').focus();
        return void[0];
    }
    if ($F('address2').blank()){
        alert("Please enter at least a two line address ");
        $('address2').focus();
        return void[0];
    }
    if ($F('list_locations').blank()){
        alert("Please select the county from the list provided ");
        $('list_locations').focus();
        return void[0];
    
    }
    $('address4').value=$F('list_locations')

    if (($F('contact_firstname').blank()||$F('contact_surname').blank()) && ($F('po').blank())){
        alert("Please enter either the name of the person to whom the invoice should be addressed or a purchase order number ");
        $('contact_firstname').focus();
        return void[0];
    }
    if ($F('telephone').blank()){
        alert("Please enter the telephone number of the person to whom the invoice should be addressed ");
        $('telephone').focus();
        return void[0];
    }
    var telephone=fix_telephone($F('telephone'));
    $('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 ");
        $('telephone').value=telephone;
        $('telephone').focus();
        return void[0];
    }
    //alert($F('your_details'));
    $('invoice_details').value='';
    $('invoice').submit();

}


//populate_attendee: auto-populate the 'attendee detail' form using user specific information
function populate_attendee(){
    // only accessible if user's account details are not already in use
    $('firstname').value=$F('h_firstname');
    $('surname').value=$F('h_surname');
    $('email').value=$F('h_email');
    $('address1').value=$F('h_address1');
    $('address2').value=$F('h_address2');
    $('address3').value=$F('h_address3');
    $('address4').value=$F('h_address4');
    $('telephone').value=$F('h_telephone');
    $('mobile').value=$F('h_mobile');
    $('pps').value=$F('o_pps');

    //list_locations
    if (!$F('h_address4').blank()){
        var el=get_el('list_locations');
        for (var i=0;i<el.options.length;i++){
            if ($F('h_address4')==el.options[i].value){
                el.selectedIndex=i;
            }
        }
    }
    
    //populate the date of birth field
    
    var d=new Date($F('o_year'), $F('o_month'), $F('o_day'));
        var el=get_el('dob_day');
        for (var i=0;i<el.options.length;i++){
            if ($F('o_day')==el.options[i].value){
                el.selectedIndex=i;
            }
        }
        //alert($F('o_month'))
        var el=get_el('dob_month');
        for (var i=0;i<el.options.length;i++){
            if ($F('o_month')==el.options[i].value){
                el.selectedIndex=i;
            }
        }
        
        var el=get_el('dob_year');
        for (var i=0;i<el.options.length;i++){
            if ($F('o_year')==el.options[i].value){
                el.selectedIndex=i;
            }
        }
        
    
    $('account_id').value=$F('h_owner');
    $('h_detail').value='1';
    $('row_detail_level').hide();
    $('u_detail').innerHTML='Reset form'
    $('u_link').href='javascript: unpopulate_attendee();'
    
}
//unpopulate_attendee: undo the 'populate attendee' option
function unpopulate_attendee(){
    $('firstname').value='';
    $('surname').value='';
    $('email').value='';
    $('address1').value='';
    $('address2').value='';
    $('address3').value='';
    $('address4').value='';
    $('telephone').value='';
    $('mobile').value='';
    $('pps').value='';
    var el=get_el('list_locations');
    el.selectedIndex=-1
    var el=get_el('dob_day');
    el.selectedIndex=-1
    var el=get_el('dob_month');
    el.selectedIndex=-1
    var el=get_el('dob_year');
    el.selectedIndex=-1
    
    
    $('account_id').value=$F('h_original');
    $('h_detail').value='0';
    $('row_detail_level').show();
    $('u_detail').innerHTML=$F('h_caption');
    $('u_link').href='javascript: populate_attendee();'
}

//populate_detail: triggered by change to 'level of detail available' selection list
function populate_detail(){
    $('h_detail').value=$F('detail_level');
    if (parseInt($F('detail_level'))==0){
        $('u_option').hide();
        $('row_address1').hide();
        $('row_address2').hide();
        $('row_address3').hide();
        $('row_list_locations').hide();
        $('row_telephone').hide();
        $('row_mobile').hide();
        $('row_pps').hide();
        $('row_dob').hide();
            
    }
    else{
        $('u_option').show();
        $('row_address1').show();
        $('row_address2').show();
        $('row_address3').show();
        $('row_list_locations').show();
        $('row_telephone').show();
        $('row_mobile').show();
        $('row_pps').show();
        $('row_dob').show();
    }

}
//check_personal; check that personal information has been entered 
function check_personal(){
    $('personal_details').value="";
    var is_personal=false;
    if ($('account_id').value==$F('h_owner')){
        is_personal=true;
    }
    
    
    var l_detail=parseInt($F('h_detail'))
   // alert(l_detail);
    //firstname
    if ($F('firstname').blank()){
        if (is_personal){
            alert("Please enter your first name");
        }
        else{
            alert("Please enter the attendee\'s first name");
        }
        $('firstname').focus();
        return void[0];
    }
    
    //surname
    if ($F('surname').blank()){
        if (is_personal){
            alert("Please enter your surname");
        }
        else{
            alert("Please enter  the attendee\'s surname");
        }
        $('surname').focus();
        return void[0];
    }
    if (l_detail==0){
        //email
        if ($F('email').blank()){
            alert("Please enter  the attendee\'s  email address");
            $('email').focus();
            return void[0];
        }
        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 void[0];
        }
        $('personal').submit();
        return void[0];
    }    
    //address 1 to 4
    if ($F('address1').blank()){
        if (is_personal){
            alert("Please enter at least a two line address");
        }
        else{
            alert("Please enter at least a two line address for the attendee");
        }
        $('address1').focus();
        return void[0];
    }
    if ($F('address2').blank()){
        if (is_personal){
            alert("Please enter at least a two line address");
        }
        else{
            alert("Please enter at least a two line address  for the attendee");
        }
        $('address2').focus();
        return void[0];
    }
    if ($F('list_locations').blank()){
        alert("Please select the county from the list provided");
        $('list_locations').focus();
        return void[0];
    }
    $('address4').value=$F('list_locations');
    //telephone or mobile
    if (($F('telephone').blank())&&($F('mobile').blank())){
        if (is_personal){
            alert("Please enter either telephone or mobile number");
        }
        else{
            alert("Please enter either telephone or mobile number  for the attendee");
        }
        $('telephone').focus();
        return void[0];
    }
    
    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 void[0];
    }
    
    //email
    if ($F('email').blank()){
        if (is_personal){
            alert("Please enter your email address");
        }
        else{
            alert("Please enter  the attendee\'s  email address");
        }
        $('email').focus();
        return void[0];
    }
    var email=$F('email')
    if (!fixstring(email,true)){
        if (is_personal){
            alert("The email address entered does not seem to be valid or is in a format that is not supported on our system");
        }
        else{
            alert("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 void[0];
    }
    
    //pps
    if ($F('pps').blank()){
        if (is_personal){
            alert("Please enter your PPS number");
        }
        else{
            alert("Please enter the attendee\'s  PPS number");
        }
        $('pps').focus();
        return void[0];
    }
        //dob
        var rule_day='';
        var rule_month='';
        var rule_year='';
        
        var el_day=get_el("dob_day")
        if (el_day.selectedIndex < 1){
            if (is_personal){
                alert("Please select your date of birth");
            }
            else{
                alert("Please select the attendee\'s  date of birth");
            }
            el_day.focus();
            return void[0];
        }
        else{
            rule_day = el_day[el_day.selectedIndex].value;
        }
        var el_month=get_el("dob_month")
        if (el_month.selectedIndex < 1){
            if (is_personal){
                alert("Please select your date of birth");
            }
            else{
                alert("Please select  the attendee\'s date of birth");
            }
            el_month.focus();
            return void[0];     }
        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){
            if (is_personal){
                alert("Please select your date of birth");
            }
            else{
                alert("Please select the attendee\'s date of birth");        
            }
            el_year.focus();
            return void[0];               }
        else{
            rule_year = el_year[el_year.selectedIndex].value;
        }
        var d=new Date();
        if ((parseInt(d.getFullYear)-parseInt(rule_year))<18){
            alert("Please check the date of birth as participants must be at least 18 years of age!");
            el_year.focus();
            return void[0];
        
        }
        var s_date=rule_day + " " + rule_month +  " " + rule_year 
        var d=new Date(s_date);
        //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 void[0];
        }
        
        $('dob').value=s_date;
    
    $('personal').submit();
}


//check_attendees: check that an attendee record has been selected for each of the places being booked
function check_attendees(){
    var f=$('attendees');
    var children=f.getInputs();
    //alert(children.length);
    for (var i=0;i<children.length;i++){
        if (children[i].id.toString().include('ac')){
            if (parseInt($F(children[i].id))==0){
                alert("Please make sure that you have entered at least the name and email address for each attendee");
                return void[0];
            }
        }
    }
    $('participant_details').value='';
    $('attendees').submit();
}








