﻿$(document).ready
(
    function() 
    {
        //debugger;
        $(".datepicker").datepicker(
        {
            dateFormat: siteJqueryDateickerFormat,           //"d M yy" formats to Wednesday 17 September 2008
            showOn: "both", 
            buttonImage: imagesiteroot + "/_images/date.png", 
            buttonImageOnly: true, 
            yearRange:'+0:+4',
            defaultDate:'0',
            minDate:0
        });
        //setTimeout('btnEditHide()',100);
    }
);

function btnEditHide()
{
        $(".btnEdit").attr("style","float:left;display:none");
}


//Calculate and pt onto screen the departure date
function calculateDepartureDate() {

    //debugger;

    //Need to use the global system format here!!!!!!!!!!!!!!!!!!!!!!!!!!! !
    var dstart = new Date(getDateFromFormat($('.txtDateStart').attr("value"), siteShortDateFormat));
    var nights = $('.ddlNights').attr("options")[$('.ddlNights').attr("options").selectedIndex].value;
    var dend = addDays(dstart, nights);
    $('.txtDateEnd').attr("value", dend.formatDate(siteShortDateFormat));
}



function ValidateForm()
{
    var blnError = false;
    //debugger;
    try
    {

            
        //2nd: If user has blanked the owned by then blank the iD also
        $('.rdoSex').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($('.rdoMale')[0].firstChild.checked==false) 
            {
                if ($('.rdoFemale')[0].firstChild.checked==false) 
                {
                    //debugger;
                    blnError = true;
                } 
            } 
            if (blnError==true)
            {
                $('.lblSex').text("Required");
            }
            else
            {
                $('.lblSex').text("");
            }
        })
        
            

                
                    
        //2nd: If user has blanked the owned by then blank the iD also
        $('.txtClub').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') 
            {
                //debugger;
                $('.hidClubID').attr("value","");
            } 
        })

        
        $('.mandatory').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') {
                //debugger;
                $(this).next().text("Required");
                if (!blnError) {
                    //$(this).focus();
                }
                blnError = true;
            } else {
                //debugger;
                $(this).next().text("");    
            }
        })


        $('.mandatorydropdown').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value") == '' || $(this).attr("value") == '00000000-0000-0000-0000-000000000000') {
                //debugger;
                $(this).next().text("Required");
                if (!blnError) {
                    //$(this).focus();
                }
                blnError = true;
            } else {
                //debugger;
                $(this).next().text("");
            }
        })
        
        
        $('.ddlAdType').each(function() {
            //debugger;
            if ($(this).attr("options")[$(this).attr("options").selectedIndex].innerHTML == 'Text') {
                $('.textadvert').each(function() {
                    //if its blank set warning in the warning span immediately after the textbox
                    //debugger;
                    if ($(this).attr("value") == '') {
                        //debugger;
                        $(this).next().text("Required");
                        if (!blnError) {
                            //$(this).focus();
                        }
                        blnError = true;
                    } else {
                        //debugger;
                        $(this).next().text("");
                    }
                })

            }
            else {
                // this is an image ad so validate ad input requirement
                $('.imageadvert').each(function() {
                    //if its blank set warning in the warning span immediately after the textbox
                    //debugger;
                    if ($(this).attr("value") == '') {
                        //debugger;
                        $(this).next().text("Required");
                        if (!blnError) {
                            //$(this).focus();
                        }
                        blnError = true;
                    } else {
                        //debugger;
                        $(this).next().text("");
                    }
                })             
            }
        })
                
        
        //Check decimal values
        $('.isdecimal').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') 
            {
                //debugger;
                $(this).attr("value","0");
            } 
            if ( IsNumeric($(this).attr("value"))==false)
            {
                blnError = true;
                $(this).next().text("Numeric only please");
            }
            else
            {
                //$(this).next().text("");
                var val=$(this).attr("value");
                if(val <0 || val >9999)
                {
                    blnError = true;
                    $(this).next().text("valid range 0 to 9999");                    
                }
                else
                {

                    $(this).next().text("");
                }
            }
            
            
            
        })

        //Check decimal values
        $('.txtFreePeriod').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') 
            {
                //debugger;
                $(this).attr("value","0");
            } 
            if ( IsInteger($(this).attr("value"))==false)
            {
                blnError = true;
                $(this).next().text("Integer only please");
            }
            else
            {
                $(this).next().text("");
                var val=$(this).attr("value");
                if(val <0 || val >9999)
                {
                    blnError = true;
                    $(this).next().text("valid range 0 to 9999");                    
                }
                else
                {
                    $(this).next().text("");
                        
                }
            }
            
            
            
        })
                
        $('.mandatorydate').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') {
                //debugger;
                $(this).next().next().text("Required");
                if (!blnError) {
                    //$(this).focus();
                }
                blnError = true;
            } 
            else 
            {   
                $(this).next().next().text("");    
            }
        })        
        
        $('.isdate').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') 
            {
                //Do nothing
            } 
            else 
            {   // Check if the date is a valid date
                var res = isDate($(this).attr("value"),siteShortDateFormat);
                //debugger;
                if(res==0)
                {
                    $(this).next().next().text("Invalid date");
                    blnError = true;
                    
                }
                else
                {
                    $(this).next().next().text("");  
                }
            }
        })
        
        //Difference between this and the madatory check above is the status field is further away
        $('.mandatorylookup').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($(this).attr("value")=='') {
                //debugger;
                $(this).next().next().next().text("Required");
                if (!blnError) {
                    $(this).focus();
                }
                blnError = true;
            } else {
                //debugger;
                $(this).next().next().next().text("");    
            }
        })
                
        var txtEmail;
        var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

        $('.email').each(function() {
            txtEmail = $(this).attr("value");
            if  (!filter.test(txtEmail)) {
                $(this).next().text("Invalid Email address");
                if (!blnError) {
                    $(this).focus();
                }
                blnError = true;
            } else {
               $(this).next().text("");
            }
       })
       
       
       //DOB format validator
        var txtDOB;
        var filter = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
        var blnDateError = false;
        
        $('.date').each(function() {
            blnDateError = false;
            txtEmail = $(this).attr("value");
            if ((txtEmail!='') && (!filter.test(txtEmail))) {
            
                if (isNaN(Date.parse(txtEmail))) {
                    $(this).next().next().text("Invalid Date");
                    if (!blnError) {
                        $(this).focus();
                    }
                    blnDateError = true
                    blnError = true;
                } 
            }
            if (!blnDateError) {
                $(this).next().next().text("");                    
            }
       })
       
        //If we get here the date is present and is a valid date
        //now verify that the end date is >= to startdate
        $('.txtDateStart').each(function() {
            //debugger;
            if ($(this).attr("value")=='') 
            {
                //debugger;
                //Do nothing
            } 
            else
            {
                //Ensure end date is after the start date
                var dstart =$(this).attr("value");
                var dend =$('.txtDateEnd').attr("value");
                
                if ( dend =='')
                {
                    //Do Nothing
                }
                else
                {
                    //debugger;
                    var res = compareDates(dstart,siteShortDateFormat,dend,siteShortDateFormat);
                    //debugger;
                    if (res==1 )
                    {
                      // date 2 is earlier than date 1
                      $('.txtDateEnd').next().next().text("This date is before the Start");
                      blnError = true;
                    }
                
                }
            }
        }) 
        
        
        
        
              
    }
    catch (e) {
    }
    
    if (blnError) 
    {
        //debugger;
        $('.divMessages').attr("innerText","Please correct the entries in red below.");
        return false;
    } else {
        $('.status').text("");
        return true;
    }
}





