﻿// Form elements
var oDate = new Date();
var oDrpCountry;
var oDrpFrom;
var oDrpTo;
var oTxtDepDate;
var oTxtRetDate;
var oChkOneWay;
var oChkReturn;
var oDrpPaxAdt;
var oDrpPaxChd;
var oDrpPaxInf;
var oSelectedRoute;
var oDateIsEuro = false;

// Variables
var IsIframe = CheckIframe();
var oHightlightedAirports = new Array();
var oReturnedApts = new Array();
var IsFirstLoad = true;
var intMaxFromAirports = 0;
var oTerms;

// JScript File
function load()
{   
    // Collect needed form elements
    oDrpCountry = document.getElementById(ibeUniqueID + 'drpCountry');
    oDrpFrom = document.getElementById(ibeUniqueID + 'drpFrom');
    oDrpTo = document.getElementById(ibeUniqueID + 'drpTo');    
    oTxtDepDate = document.getElementById(ibeUniqueID + 'txtDepDate');
    oTxtRetDate = document.getElementById(ibeUniqueID + 'txtRetDate');
    oChkOneWay = document.getElementById(ibeUniqueID + 'rdoOneWay');
    oChkReturn = document.getElementById(ibeUniqueID + 'rdoReturn');
    oDrpPaxAdt = document.getElementById(ibeUniqueID + 'drpAdults'); 
    oDrpPaxChd = document.getElementById(ibeUniqueID + 'drpChildren');
    oDrpPaxInf = document.getElementById(ibeUniqueID + 'drpInfants');
    oTerms = Resource.split(',');
     
    Wait(true);
    
    // Set up dropdowns
    try
    {
        oHightlightedAirports = eval('hightlightedAirports_' + oDrpCountry.options[oDrpCountry.selectedIndex].value);
    }
    catch(err)
    {
        oHightlightedAirports = new Array();           
    }
    
    // Fetch airports in this country 
    ibe.FilterFrom(oDrpCountry.options[oDrpCountry.selectedIndex].value,Build);
    IsFirstLoad = false;
}



function Populate(dropDownList)
{   ////////////debugger
    // Save the selected option reading to be re added
    var oDefaultOption = dropDownList.options[0];
    var oSelectedOption = dropDownList.options[dropDownList.selectedIndex];
    var filterSet = false;
    
    // Clear current list
    dropDownList.options.length = 0;
    
    // Re add the select
    dropDownList.options[0] = new Option(oDefaultOption.text,oDefaultOption.value);
    
    // if needed add the show all option
    if((oReturnedApts.length < intMaxFromAirports) && (dropDownList.id.indexOf('To') == -1 ))
    {
        dropDownList.options[dropDownList.options.length] = new Option(oTerms[0].toString().replace('#',oDrpCountry.options[oDrpCountry.selectedIndex].text) ,"999");
        filterSet = true;
    }
    
     // Add hightlighted airports if there are any and they appear in the list
    if(oHightlightedAirports.length > 0)
    {
      
        for(var intHightlighted = 0; intHightlighted < oHightlightedAirports.length; intHightlighted++)
        {
            //////////debugger
            // Check if the highlighted airports are in the array for this ropdown build, if so add them
            if(oReturnedApts.contains(oHightlightedAirports[intHightlighted].substring(0,3),'Code',false) == true)
            {
                // check if dropDownList.options.length == 1, if so then add first dashed line
                if((dropDownList.options.length == 1) || (filterSet == true && dropDownList.options.length == 2)) {dropDownList.options[dropDownList.options.length] = new Option("-------------------------------------------","0");}
                dropDownList.options[dropDownList.options.length] = new Option(oHightlightedAirports[intHightlighted].split('|')[1],oHightlightedAirports[intHightlighted].split('|')[0]);
                
                // check for default value if this is the first load
                if(IsFirstLoad == true)
                {
                    if(dropDownList.options[dropDownList.options.length -1].value == defaultDepart)
                    {
                        dropDownList.options[dropDownList.options.length -1].selected = 'true';      
                        IsFirstLoad = false;
                    }
                    else if(dropDownList.options[dropDownList.options.length -1].value == oSelectedOption.value)
                    {
                        dropDownList.options[dropDownList.options.length -1].selected = 'true';      
                    } 
                }
                else
                {
                    if(dropDownList.options[dropDownList.options.length -1].value == oSelectedOption.value)
                    {
                        dropDownList.options[dropDownList.options.length -1].selected = 'true';      
                    }                
                }
            }
        }
        
        // Add separater if dropDownList.options.length > 1
        if(dropDownList.options.length> 1) {dropDownList.options[dropDownList.options.length] = new Option("-------------------------------------------","0");}
    }
    
    
    // Add items to dropdown
    if(oReturnedApts != null)
    {
        for(var i = 0; i < oReturnedApts.length; i++)
        {
            dropDownList.options[dropDownList.options.length] = new Option(oReturnedApts[i].Name,oReturnedApts[i].Code);
            
            
            // check for default value if this is the first load
            if(IsFirstLoad == true)
            {
                if(dropDownList.options[dropDownList.options.length -1].value == defaultDepart)
                {
                    dropDownList.options[dropDownList.options.length -1].selected = 'true';      
                    IsFirstLoad = false;
                }
                else if(dropDownList.options[dropDownList.options.length -1].value == oSelectedOption.value)
                {
                    dropDownList.options[dropDownList.options.length -1].selected = 'true';      
                }                
            }           
            else
            {
                if(dropDownList.options[dropDownList.options.length -1].value == oSelectedOption.value)
                {
                    dropDownList.options[dropDownList.options.length -1].selected = 'true';      
                }                
            }
        }
    }
    else
    {
        // As no airports have been added, if not then add please select departure option and select it
        dropDownList.options[dropDownList.options.length] = new Option('- select departure airport -','0');
    }
    
    // if there is only one airport in the array then select it
    if(oReturnedApts != null && oReturnedApts.length == 1)
    {
        dropDownList.options[dropDownList.options.length -1].selected = 'true';        
    }
}


function PopulateFromRoute(dropDownList,endToUse)
{   
    //////debugger

    // Save the selected option reading to be re added
    var oDefaultOption = dropDownList.options[0];
    var oSelectedOption = dropDownList.options[dropDownList.selectedIndex];
    var filterSet = false;
    
    // Clear current list
    dropDownList.options.length = 0;
    
    // Re add the select
    dropDownList.options[0] = new Option(oDefaultOption.text,oDefaultOption.value);
    
    // if needed add the show all option
    if((oReturnedApts.length < intMaxFromAirports) && (dropDownList.id.indexOf('To') == -1 ))
    {
        dropDownList.options[dropDownList.options.length] = new Option(oTerms[0].toString().replace('#',oDrpCountry.options[oDrpCountry.selectedIndex].text) ,"999");
        filterSet = true;
    }
    
     // Add hightlighted airports if there are any and they appear in the list
    if(oHightlightedAirports.length > 0)
    {
        var direction = dropDownList.id.indexOf('To') == -1 ? 'From' : 'To';
      
        for(var intHightlighted = 0; intHightlighted < oHightlightedAirports.length; intHightlighted++)
        {
            ////////debugger
            // Check if the highlighted airports are in the array for this ropdown build, if so add them
            if(oReturnedApts.hasWithin(oHightlightedAirports[intHightlighted].substring(0,3),'Code',direction) == true)
            {
                // check if dropDownList.options.length == 1, if so then add first dashed line
                if((dropDownList.options.length == 1) || (filterSet == true && dropDownList.options.length == 2)) {dropDownList.options[dropDownList.options.length] = new Option("-------------------------------------------","0");}
                dropDownList.options[dropDownList.options.length] = new Option(oHightlightedAirports[intHightlighted].split('|')[1],oHightlightedAirports[intHightlighted].split('|')[0]);
                
                // check for default value if this is the first load
                if(IsFirstLoad == true)
                {
                    if(dropDownList.options[dropDownList.options.length -1].value == defaultDepart)
                    {
                        dropDownList.options[dropDownList.options.length -1].selected = 'true';      
                        IsFirstLoad = false;
                    }
                    else if(dropDownList.options[dropDownList.options.length -1].value == oSelectedOption.value)
                    {
                        dropDownList.options[dropDownList.options.length -1].selected = 'true';      
                    } 
                }
                else
                {
                    if(dropDownList.options[dropDownList.options.length -1].value == oSelectedOption.value)
                    {
                        dropDownList.options[dropDownList.options.length -1].selected = 'true';      
                    }                
                }
            }
        }
        
        // Add separater if dropDownList.options.length > 1
        if(dropDownList.options.length> 1) {dropDownList.options[dropDownList.options.length] = new Option("-------------------------------------------","0");}
    }
    
    
    // Add items to dropdown
    if(oReturnedApts != null)
    {
        for(var i = 0; i < oReturnedApts.length; i++)
        {
            dropDownList.options[dropDownList.options.length] = new Option(eval('oReturnedApts[i].' + endToUse + ['.Name']),eval('oReturnedApts[i].' + endToUse + ['.Code']));
            
            
            // check for default value if this is the first load
            if(IsFirstLoad == true)
            {
                if(dropDownList.options[dropDownList.options.length -1].value == defaultDepart)
                {
                    dropDownList.options[dropDownList.options.length -1].selected = 'true';      
                    IsFirstLoad = false;
                }
                else if(dropDownList.options[dropDownList.options.length -1].value == oSelectedOption.value)
                {
                    dropDownList.options[dropDownList.options.length -1].selected = 'true';      
                }                
            }           
            else
            {
                if(dropDownList.options[dropDownList.options.length -1].value == oSelectedOption.value)
                {
                    dropDownList.options[dropDownList.options.length -1].selected = 'true';      
                }                
            }
        }
    }
    else
    {
        // As no airports have been added, if not then add please select departure option and select it
        dropDownList.options[dropDownList.options.length] = new Option('- select departure airport -','0');
    }
    
    // if there is only one airport in the array then select it
    if(oReturnedApts != null && oReturnedApts.length == 1)
    {
        dropDownList.options[dropDownList.options.length -1].selected = 'true';        
    }
}


function CheckIframe()
{
    
    if(window.location.href.indexOf('iframe') > -1)
    {
        return true;
    }
    return false;    
}

/*######################################################*/
//                                                      //
//                Route logic methods                   //
//                                                      //
/*######################################################*/
function LoadDepartures(dropDownList)
{   ////////////debugger
    Wait(true);
    
    // Work out what called this method and tailor ajax call ie country or to change
    if(dropDownList.id.indexOf("drpCountry") > -1)
    {
        IsFirstLoad = true;
        try
        {
            oHightlightedAirports = eval('hightlightedAirports_' + oDrpCountry.options[oDrpCountry.selectedIndex].value);
        }
        catch(err)
        {
            oHightlightedAirports = new Array();           
        }
        
        // Fetch airports in this country                
        ibe.FilterFrom(oDrpCountry.options[oDrpCountry.selectedIndex].value,Build);
        
    }
    else
    {   
        // If nothing is selected in the to box there is nothign to do
        if(oDrpTo.value != '0')
        {
            ////////////////////debugger
            // Fetch airports in this country that fly to the selected to point
            ibe.FilterFromByToAirport(oDrpCountry.options[oDrpCountry.selectedIndex].value,oDrpTo.value,BuildRoute);
        }
        else
        {
            
            // Fetch airports in this country  
            ibe.FilterFrom(oDrpCountry.options[oDrpCountry.selectedIndex].value,Build);
        }
    }
    

}

function BuildRoute(res)
{   
    oReturnedApts = res.value;
    PopulateFromRoute(oDrpFrom,'From');
    if(oDrpTo.value != '0' && oDrpFrom.value != '0')
    {
        UpdatePaxNumbers();
    }
    Wait(false);
}

function Build(res)
{   
        oReturnedApts = res.value;
        intMaxFromAirports = oReturnedApts.length;
        Populate(oDrpFrom);
        LoadArrivals(oDrpTo);
        
        if(oDrpTo.value != '0' && oDrpFrom.value != '0')
        {
            UpdatePaxNumbers();
        }
        Wait(false);
}

function LoadArrivals(dropdownlist)
{   
    //////////debugger
    Wait(true);
    // Check to see if the remove filter has been selected
    if(oDrpFrom.value == "999")
    {
        ////////////////////debugger
        oReturnedApts = ibe.FilterFrom(oDrpCountry.options[oDrpCountry.selectedIndex].value).value;
        firstLoad = true;
        Populate(oDrpFrom);

    }
    else
    {
        ////////////////////debugger
        oReturnedApts = ibe.GetArrivals(oDrpFrom.value).value;
        PopulateFromRoute(oDrpTo,'To');
        if(oDrpTo.value != '0' && oDrpFrom.value != '0')
        {
            UpdatePaxNumbers();
        }
    }
    
    Wait(false);
}


// Disables controls on the form
// whilst waiting for AJAX to return
function Wait(disable)
{   
    
    if(disable == true)
    {   
        document.getElementById('wait').style.display = '';
    }
    else
    {
        document.getElementById('wait').style.display = 'none';
    }
    oDrpCountry.disabled = disable;
    oDrpFrom.disabled = disable;
    oDrpTo.disabled = disable;
}

function UpdateDate(IsFrom,txtDate)
{
    
    // Parse the text value if it is a valid date
    var oPattern = new RegExp(/^\d{1,2}([\/-])\d{1,2}\1\d{4}$/);
    var oFromDate = new Date(new Date().getTime() + 86400000);
    var oToDate = new Date(new Date().getTime() + (86400000 * 2));
    var oSeparator = '/';
    
    if(oTxtDepDate.value.indexOf('-') > -1){
        oSeparator = '-';
        }
    
    // Check for correct format xx/xx/xxxx
    if(oPattern.test(txtDate.value) == true)
    {
        oFromDate = new Date(oTxtDepDate.value);
        oToDate = new Date(oTxtRetDate.value);
        
        // Check for US culture and change the dates round
        if (country != "us")
        {
            oFromDate = new Date(oTxtDepDate.value.split(oSeparator)[2],oTxtDepDate.value.split(oSeparator)[1] -1 ,oTxtDepDate.value.split(oSeparator)[0])
            oToDate = new Date(oTxtRetDate.value.split(oSeparator)[2],oTxtRetDate.value.split(oSeparator)[1] -1 ,oTxtRetDate.value.split(oSeparator)[0])
        }

        if(IsFrom)
        {
            if((oFromDate < new Date()) || (oFromDate > new Date(new Date().getTime() + (86400000 * advanceDays))))
            {
                oFromDate = new Date(new Date().getTime() + 86400000);
            }
        }
        else
        {
            if((oToDate < new Date()) || (oToDate > new Date(new Date().getTime() + (86400000 * advanceDays))))
            {
                oToDate = new Date(oFromDate.getTime() + 86400000);
            }            
        }
        
        
        // Check which date we are working with
        if(IsFrom)
        {
            // we are working with the from date so if the date now surpasses return alter the return
            if(oFromDate > oToDate)
            {
                // Set to date to be dat after from date
                oToDate = new Date(oFromDate.getTime() + 86400000)
            }
        }
        else
        {
            // Working with the to date
            if(oFromDate > oToDate)
            {
                // Set to date to be dat after from date
                oFromDate = new Date(oToDate.getTime() - 86400000)
            }
        }
        
        if (country != "us")
        {
            oTxtDepDate.value = (oFromDate.getDate().toString().length == 1 ? '0' + oFromDate.getDate().toString() : oFromDate.getDate().toString())  + oSeparator + 
            ((oFromDate.getMonth() + 1).toString().length == 1 ? '0' + (oFromDate.getMonth() + 1).toString() : (oFromDate.getMonth() + 1).toString()) + oSeparator +
            oFromDate.getFullYear();
            
            oTxtRetDate.value = (oToDate.getDate().toString().length == 1 ? '0' + oToDate.getDate().toString() : oToDate.getDate().toString())  + oSeparator + 
            ((oToDate.getMonth() + 1).toString().length == 1 ? '0' + (oToDate.getMonth() + 1).toString() : (oToDate.getMonth() + 1).toString()) + oSeparator +
            oToDate.getFullYear();
        }
        else
        {
            oTxtDepDate.value = ((oFromDate.getMonth() + 1).toString().length == 1 ? (oFromDate.getMonth() + 1).toString() : (oFromDate.getMonth() + 1).toString()) + oSeparator +
            (oFromDate.getDate().toString().length == 1 ? oFromDate.getDate().toString() : oFromDate.getDate().toString())  + oSeparator + 
            oFromDate.getFullYear();
            
            oTxtRetDate.value = ((oToDate.getMonth() + 1).toString().length == 1 ? (oToDate.getMonth() + 1).toString() : (oToDate.getMonth() + 1).toString()) + oSeparator +
            (oToDate.getDate().toString().length == 1 ? oToDate.getDate().toString() : oToDate.getDate().toString())  + oSeparator + 
            oToDate.getFullYear();        
        }
    }
    else
    {
        if (country != "us")
        {
            oTxtDepDate.value = (oFromDate.getDate().toString().length == 1 ? '0' + oFromDate.getDate().toString() : oFromDate.getDate().toString())  + oSeparator + 
            ((oFromDate.getMonth() + 1).toString().length == 1 ? '0' + (oFromDate.getMonth() + 1).toString() : (oFromDate.getMonth() + 1).toString()) + oSeparator +
            oFromDate.getFullYear();
            
            oTxtRetDate.value = (oToDate.getDate().toString().length == 1 ? '0' + oToDate.getDate().toString() : oToDate.getDate().toString())  + oSeparator + 
            ((oToDate.getMonth() + 1).toString().length == 1 ? '0' + (oToDate.getMonth() + 1).toString() : (oToDate.getMonth() + 1).toString()) + oSeparator +
            oToDate.getFullYear();
        }
        else
        {
            oTxtDepDate.value = ((oFromDate.getMonth() + 1).toString().length == 1 ? (oFromDate.getMonth() + 1).toString() : (oFromDate.getMonth() + 1).toString()) + oSeparator +
            (oFromDate.getDate().toString().length == 1 ? oFromDate.getDate().toString() : oFromDate.getDate().toString())  + oSeparator + 
            oFromDate.getFullYear();
            
            oTxtRetDate.value = ((oToDate.getMonth() + 1).toString().length == 1 ? (oToDate.getMonth() + 1).toString() : (oToDate.getMonth() + 1).toString()) + oSeparator +
            (oToDate.getDate().toString().length == 1 ? oToDate.getDate().toString() : oToDate.getDate().toString())  + oSeparator + 
            oToDate.getFullYear();        
        }
    }
}

function OneWayToggle(OneWay)
{
    if (OneWay == false)
    {
        oTxtRetDate.disabled = false;
        UpdateDate(false,oTxtRetDate);
    }
    else
    {
        oTxtRetDate.disabled = true;
    }
}


/*######################################################*/
//                                                      //
//              Pax dropdown logic                      //
//                                                      //
/*######################################################*/

function UpdateInf(AdultDropdown,InfantDropDown)
{   //////debugger
    var SelectedInfants =  oDrpPaxInf.value;
    // Clear the infant drop down
    oDrpPaxInf.length = 0;
    var Item;
    
    // Rebuild with the amount selected in the adult dropdown
    for(var i = 0; i <= AdultDropdown.value ; i++)
    {
        if(i < 10)
        {
            Item = document.createElement("option");
            Item.value = i;
            Item.text = i;
            oDrpPaxInf.options.add(Item);
        }
    }
    
    oDrpPaxInf.value = SelectedInfants;
    
}


function UpdatePaxNumbers()
{   ////debugger
    // Check if a complete route is selected
    if((oDrpFrom.value != "0") && (oDrpTo.value != "0"))
    {
        // Save the current numbers
        var intAdt = oDrpPaxAdt.value;
        var intChd = oDrpPaxChd.value;
        var intInf = oDrpPaxInf.value;    
        intMax = MaxPax;
        
        oSelectedRoute = ibe.GetRoute(oDrpFrom.value,oDrpTo.value).value;
        var oCodeshare = oSelectedRoute.IsCodeshare.toString();

        // Check if it is a code share
        if(oCodeshare == 'true')
        {
            intMax = 4;
        }
        else
        {
            intMax = MaxPax
        }
            
            
            
        // ReDraw with the correct amount of available pax
        oDrpPaxAdt.length = 0;
        oDrpPaxChd.length = 0;
        oDrpPaxInf.length = 1;
        
        //Check that there are only a combination of 4 adults and children
        if(oCodeshare == 'true')
        {
            if (intAdt > intMax) intAdt = 1;
	        if (intChd > (intMax - intAdt)) intChd = intMax - intAdt;
	        
	        for (var i = 1; i <= intMax - intChd; i++)
	        {
	            oDrpPaxAdt.options[i - 1] = new Option(i, i);
	        }
	    }
	    else
	    {
	        for (var i = 1; i <= intMax; i++)
	        {
	            oDrpPaxAdt.options[i - 1] = new Option(i, i);
	        }
	    }
		
		//added to ensure that you can't book more infants then adults
	    for (var i = 0; i <= intAdt; i++)
	    {
		    oDrpPaxInf.options[i] = new Option(i, i);
	    }
		
		// If codeshare then strict to 4 pax with seats
		if(oCodeshare == 'true')
		{
	        for (var i = 0; i <= intMax - intAdt; i++)
	        {
		        oDrpPaxChd.options[i] = new Option(i, i);
	        }
	    }
	    else
	    {
	        for (var i = 0; i <= intMax; i++)
	        {
		        oDrpPaxChd.options[i] = new Option(i, i);
	        }	    
	    }

        
        // If possible re-select the values    	    
	    oDrpPaxAdt.value = intAdt;
	    oDrpPaxChd.value = intChd;
	    oDrpPaxInf.value = intInf;
    }
}


function FindFlights()
{
    ValidIBE = true;
    // finds the maximum number of pax
    var oNumberOfPax = MaxPax;
    // Check route is bookable online
    if(true == false)
    {
        ValidIBE = false;
    }

    // If all ok then submit
    if(ValidIBE == false)
    {
        return false;
    }
    else
    {   
        // Submit
        //oViewState = document.aspnetForm.__VIEWSTATE.value;
        //document.aspnetForm.__VIEWSTATE.name = 'NOVIEWSTATE';
        //document.aspnetForm.action = FlightsTab.GetPostAddress(oFrom.value,oTo.value).value;
        
        oForm = document.forms['aspnetForm']
		if (!oForm) {
			oForm = document.aspnetForm;
		}
	
		var address = ibe.GetAddress(oDrpFrom.value,oDrpTo.value,(oChkOneWay.checked == false ? '0' : '1'),oTxtDepDate.value,oTxtRetDate.value,oDrpPaxAdt.value,oDrpPaxChd.value,oDrpPaxInf.value).value;
		if(address.length > 0)
		{
		    oForm.action = address;
		}
		else
		{
    		oForm.action = '/book/book.aspx';
        }
    }
    
    // Examine if this will need to go to the group form or not if so redirect to group form
    // Groups rules: if 10 + for adults or adt + chd > 9 go to groups form
    // Checks if number of passengers is greater than intMax which is set when the user selects a destination
    if((oDrpPaxAdt.value == oNumberOfPax) || ((parseInt(oDrpPaxAdt.value) + parseInt(oDrpPaxChd.value)) > oNumberOfPax))
    {        
        oForm.target = '_blank';
        window.open(FlightsTab.GetGroupsAddress().value + '?from=' + oDrpFrom.value + '&to=' +  oDrpTo.value + '&depDate=' + oTxtDepDate.value + '&retDate=' + oTxtRetDate.value + '&oneWay=' + oOneWay,"");  
        return false;
    }
        
    oForm.target = '_blank';
    
    // Prepare fly request
    document.getElementById('cityfrominput0').value = oDrpFrom.value;
    document.getElementById('citytoinput0').value = oDrpTo.value;
    document.getElementById('cityfrominput1').value = oDrpTo.value;
    document.getElementById('citytoinput1').value = oDrpFrom.value;        
    document.getElementById('day0').value = GetDatePart("Date",oTxtDepDate);
    document.getElementById('month0').value = GetDatePart("Month",oTxtDepDate);
    
    if(oChkOneWay.checked == true)
    {
        document.getElementById('oneway').value = '1';
    }        
    else
    {
        document.getElementById('day1').value = GetDatePart("Date",oTxtRetDate);
        document.getElementById('month1').value = GetDatePart("Month",oTxtRetDate);        
    }
    document.getElementById('fb_numpaxadt').value = oDrpPaxAdt.value;        
    document.getElementById('fb_numpaxchd').value = oDrpPaxChd.value;        
    document.getElementById('fb_numpaxinf').value = oDrpPaxInf.value;

    if (oForm.action.indexOf('/click-') > -1 || oForm.action.indexOf('/interactive') > -1) {
        window.open(oForm.action);
        oForm.target = "_self";
        oForm.action = document.URL;
        return false;
    }
    else {
        oForm.__VIEWSTATE.name = 'NOVIEWSTATE';
        return;
    }
}



function GetDatePart(requiredPart,containingElement)
{
    if (country != "us")
    {
        switch(requiredPart)
        {
            case 'Date':
                if(containingElement.value.split('/')[0].length > 2){
                    oDateIsEuro = true;
                    return containingElement.value.split('-')[0]
                }
                return containingElement.value.split('/')[0];
                break;            
            case 'Month':
                if(oDateIsEuro){
                    return containingElement.value.split('-')[1]
                }
                return containingElement.value.split('/')[1];
                break;
            case 'Year':
                if(oDateIsEuro){
                    return containingElement.value.split('-')[2]
                }
                return containingElement.value.split('/')[2];
                break;
            default:
                return '0';
                break;                        
        }
    }
    else
    {
        switch(requiredPart)
        {
            case 'Date':
                return containingElement.value.split('/')[1];
                break;            
            case 'Month':
                return containingElement.value.split('/')[0];
                break;
            case 'Year':
                return containingElement.value.split('/')[2];
                break;
            default:
                return '0';
                break;                        
        }    
    }
}


// Array .hasWithin
Array.prototype.hasWithin = function (element,property,genre)
{   
    for(var i = 0; i < this.length; i++)
    {
        try
        {        
            if(eval('this[' + i + '].' + genre + '.' + property) == element)
            return true;
        }
        catch (exception)
        {
            alert(element + property + genre);
        }
    }
    
    return false;
}

// Array .contains
Array.prototype.contains = function (element,property,likeComparison) 
{
    if(property == '')
    {
        for (var i = 0; i < this.length; i++) 
        {
            if (this[i] == element) 
            {
                return true;
            }
        }
    }
    else if(property != '' && likeComparison)
    {
        
        for (var i = 0; i < this.length; i++) 
        {
            if (eval('this[' + i + '].' + property) == eval('element.' + property)) 
            {
                return true;
            }
        }        
    }
    else 
    {
        for (var i = 0; i < this.length; i++) 
        {
            if (eval('this[' + i + '].' + property) == element) 
            {
                return true;
            }
        }                
    }
    return false;
};


Array.prototype.clear=function()
  {
      this.length = 0;
  };