function validateregionadminform()
{
    var error = '';
    
    if (document.saveregion.propcontinent.value != '' && document.saveregion.propcountry.value != '' && document.saveregion.propregion.value != '')
    {
        if ((document.saveregion.proploclocationlist.value != '' && document.saveregion.proploclocation.value != '') || (document.saveregion.proploclocationlist.value == '' && document.saveregion.proploclocation.value == ''))
        {
            error = 'Please select either a location from the list or enter your own location.';
        }
    }
    
    if (error == '')
    {
        document.saveregion.save.disabled = true;
        return true;
    }
    else
    {
        alert(error);
        return false;
    }
}

function validateavailabilityform()
{
	var error = '';
	var i = 0;
    var checkavaildate;
    
	for(i=3;i>0;i--)
	{
	    if (document.getElementById('fromdate' + i).value + document.getElementById('todate' + i).value + document.getElementById('description' + i).value + document.getElementById('bookingname' + i).value != '')
		{
			if (!checkdate(document.getElementById('fromdate' + i)))
			{
			    error = 'Please enter a valid date';
			}
			if (!checkdate(document.getElementById('todate' + i)))
			{
			    error = 'Please enter a valid date';
			}
		    if (document.getElementById('todate' + i).value == '')
			{
				error = 'Please enter a \'to date\' for the booking';
				document.getElementById('todate' + i).focus();
			}
		    if (document.getElementById('fromdate' + i).value == '')
			{
				error = 'Please enter a \'from date\' for the booking';
				document.getElementById('fromdate' + i).focus();
			}
		    if (document.getElementById('description' + i).value == '')
			{
				error = 'Please enter a description for the booking';
				document.getElementById('description' + i).focus();
			}
		    if (document.getElementById('bookingname' + i).value == '')
			{
				error = 'Please enter a name for the booking';
				document.getElementById('bookingname' + i).focus();
			}
			
			if (error == '')
			{
                error = doDateCheck(document.getElementById('fromdate' + i), document.getElementById('todate' + i))
            }
		}
	}
	
	//only validate the edit section if it is visible
	if (document.getElementById('availabilityadminedit').style.display == "block")
	{
	    if (!checkdate(document.saveavailibility.startdateedit))
	    {
	        error = 'Please enter a valid date';
	    }
	    if (!checkdate(document.saveavailibility.enddateedit))
	    {
	        error = 'Please enter a valid date';
	    }
	    if (document.saveavailibility.enddateedit.value == '')
	    {
		    error = 'Please enter a \'to date\' for the booking';
		    document.saveavailibility.enddateedit.focus();
	    }
	    if (document.saveavailibility.startdateedit.value == '')
	    {
		    error = 'Please enter a \'from date\' for the booking';
		    document.saveavailibility.startdateedit.focus();
	    }
	    if (document.saveavailibility.descriptionedit.value == '')
	    {
		    error = 'Please enter a description for the booking';
		    document.saveavailibility.descriptionedit.focus();
	    }
	    if (document.saveavailibility.bookingnameedit.value == '')
	    {
		    error = 'Please enter a name for the booking';
		    document.saveavailibility.bookingnameedit.focus();
	    }
    			
	    if (error == '')
	    {
            error = doDateCheck(document.getElementById('startdateedit'), document.getElementById('enddateedit'))
        }
	}
	    
	
    if (error == '')
    {
        return true;
    }
    else
    {
        alert(error);
        return false;
    }
}

function validatetariffform()
{
	var error = '';
	var i = 0;
    var checkavaildate;
    var typechecked;
    
	for(i=3;i>0;i--)
	{
        if (document.getElementById('startdate' + i).value + document.getElementById('enddate' + i).value + document.getElementById('tardescription' + i).value + document.getElementById('cost' + i).value != '')
		{
			if (isNaN(document.getElementById('cost' + i).value))
			{
				error = 'The cost value that you enter must be a number. Please enter a numeric cost for the booking \'' + document.getElementById('tardescription' + i).value + '\'';
				document.getElementById('cost' + i).focus();
			}

			if (document.getElementById('cost' + i).value == '')
			{
				error = 'Please enter a tariff cost for the booking \'' + document.getElementById('tardescription' + i).value + '\'';
				document.getElementById('cost' + i).focus();
			}
			
		    var bolIsChecked
		    bolIsChecked = false;
    		
		    for (k=0;k<document.getElementById('tartype' + i).value.length-1;k++)
		    {  
		        typechecked = eval('document.savetariff.tartype' + i + '[' + k + '].checked');
		        
		        if (typechecked)
			        {bolIsChecked = true;}
			}
		    
		    if (!bolIsChecked)
		    {
			    error = 'Please select a valid tariff type for the booking \'' + document.getElementById('tardescription' + i).value + '\'';
			    eval('document.savetariff.tartype' + i + '[1].focus()');
		    }
			
			if (!checkdate(document.getElementById('enddate' + i)))
			{
			    error = 'Please enter a valid date for the booking \'' + document.getElementById('tardescription' + i).value + '\'';
			}
			
			if (document.getElementById('enddate' + i).value == '')
			{
				error = 'Please enter an \'end date\' for the booking \'' + document.getElementById('tardescription' + i).value + '\'';
				document.getElementById('enddate' + i).focus();
			}
			
			if (!checkdate(document.getElementById('startdate' + i)))
			{
			    error = 'Please enter a valid date';
			}
			
			if (document.getElementById('startdate' + i).value == '')
			{
				error = 'Please enter a \'start date\' for the booking \'' + document.getElementById('tardescription' + i).value + '\'';
				document.getElementById('startdate' + i).focus();
			}
			
			if (document.getElementById('tardescription' + i).value == '')
			{
				error = 'Please enter a tariff description';
				document.getElementById('tardescription' + i).focus();
			}
			
			if (error == '')
			{
                error = doDateCheck(document.getElementById('startdate' + i), document.getElementById('enddate' + i))
            }
		}
	}
	
	//only validate the edit section if it is visible
	if (document.getElementById('tariffadminedit').style.display == "block")
	{
		if (isNaN(document.savetariff.tarcostedit.value))
		{
			error = 'Please enter a number for this cost';
			document.savetariff.tarcostedit.focus();
		}
			
		if (document.savetariff.tarcostedit.value == '')
		{
			error = 'Please enter a tariff cost';
			document.savetariff.tarcostedit.focus();
		}
		
		var bolIsChecked
		bolIsChecked = false;
		
		for (i=0;i<document.savetariff.tartypeedit.length;i++)
		{
			if (document.savetariff.tartypeedit[i].checked)
			{
				bolIsChecked = true;
			}
		}
		
		if (!bolIsChecked)
		{
			error = 'Please select a valid tariff type';
		}
			
		if (!checkdate(document.savetariff.tarenddateedit))
		{
		    error = 'Please enter a valid date';
		}
			
		if (document.savetariff.tarenddateedit.value == '')
		{
			error = 'Please enter a \'end date\' for the booking';
			document.savetariff.tarenddateedit.focus();
		}
			
		if (!checkdate(document.savetariff.tarstartdateedit))
		{
		    error = 'Please enter a valid date';
		}
			
		if (document.savetariff.tarstartdateedit.value == '')
		{
			error = 'Please enter a \'start date\' for the booking';
			document.savetariff.tarstartdateedit.focus();
		}
			
		if (document.savetariff.tardescriptionedit.value == '')
		{
			error = 'Please enter a tariff description';
			document.savetariff.tardescriptionedit.focus();
		}
			
		if (error == '')
		{
            error = doDateCheck(document.savetariff.tarstartdateedit, document.savetariff.tarenddateedit)
        }
	}
	
    if (error == '')
    {
        return true;
    }
    else
    {
        alert(error);
        return false;
    }
}

function validateaccomadminform(){
    var error = '';
    
    if (document.saveaccommodation.numshowerrooms.value == ''){
        error = 'Please enter the amount of shower rooms in the property';
        document.saveaccommodation.numshowerrooms.focus();
    }
    
    if (document.saveaccommodation.numbathrooms.value == ''){
        error = 'Please enter the amount of bathrooms in the property';
        document.saveaccommodation.numbathrooms.focus();
    }
    
    if (document.saveaccommodation.numbunkbeds.value == ''){
        error = 'Please enter the amount of bunk beds in the property';
        document.saveaccommodation.numbunkbeds.focus();
    }
    
    if (document.saveaccommodation.numsinglebeds.value == ''){
        error = 'Please enter the amount of single beds in the property';
        document.saveaccommodation.numsinglebeds.focus();
    }
    
    if (document.saveaccommodation.numdoublebeds.value == ''){
        error = 'Please enter the amount of double beds in the property';
        document.saveaccommodation.numdoublebeds.focus();
    }
    
    if (document.saveaccommodation.numbedrooms.value == ''){
        error = 'Please enter the amount of bedrooms in the property';
        document.saveaccommodation.numbedrooms.focus();
    }
    
    if (document.saveaccommodation.numguests.value == ''){
        error = 'Please enter the amount of guests that can stay in the property';
        document.saveaccommodation.numguests.focus();
    }
    
    if (document.saveaccommodation.propinfo.value == ''){
        error = 'Please enter a description about this property';
        document.saveaccommodation.propinfo.focus();
    }
    
//     if (!document.saveaccommodation.propcatering[0].checked  && !document.saveaccommodation.propcatering[1].checked && !document.saveaccommodation.propcatering[2].checked && !document.saveaccommodation.propcatering[3].checked){
//        error = 'Please enter a catering option for this property.';
//        document.saveaccommodation.propcatering[0].focus();
//    }
//    
//    if (!document.saveaccommodation.proplocation.checked){
//        error = 'Please enter a location option for this property.';
//    }

    if (error == '')
    {
        return true;
    }
    else
    {
        alert(error);
        return false;
    }
}


function validateDate(day,month,year) 
{
	if (day < 29) {
		return true;
	}
	else {
		if (month == "Jan" | month == "Mar" | month == "May" | month == "Jul" | month == "Aug" | month == "Oct" | month == "Dec" ) {
			return true;
		}
		else {
			if (month != "Feb") {
				if (day <= 30) {
					return true;
				}
				else {
					return false;
				}
			}
			else {
				//check for leap year because it is Feruary
				if ( (1996-year)%4 == 0) {
					if (day <= 29) {
						return true;
					}
					else {
						return false;
					}
				}
				else {
					if (day <=28) {
						return true;		
					}
					else
					{
						return false;
					}
				}
			}
		}
	}
}


function checkdate(objName)
{
    var datefield = objName;
    if (chkdate(objName) == false)
    {
        datefield.select();
        alert("That date is invalid.  Please try again.");
        datefield.focus();
        return false;
    }
    else
    {
        return true;
    }
}

function chkdate(objName)
{
    //var strDatestyle = "US"; //United States date style
    var strDatestyle = "EU";  //European date style
    var strDate;
    var strDateArray;
    var strDay;
    var strMonth;
    var strYear;
    var intday;
    var intMonth;
    var intYear;
    var booFound = false;
    var datefield = objName;
    var strSeparatorArray = new Array("-"," ","/",".");
    var intElementNr;
    var err = 0;
    var strMonthArray = new Array(12);
    strMonthArray[0] = "Jan";
    strMonthArray[1] = "Feb";
    strMonthArray[2] = "Mar";
    strMonthArray[3] = "Apr";
    strMonthArray[4] = "May";
    strMonthArray[5] = "Jun";
    strMonthArray[6] = "Jul";
    strMonthArray[7] = "Aug";
    strMonthArray[8] = "Sep";
    strMonthArray[9] = "Oct";
    strMonthArray[10] = "Nov";
    strMonthArray[11] = "Dec";
    strDate = datefield.value;
    if (strDate.length < 1)
    {
        return true;
    }
    
    for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++)
    {
        if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1)
        {
            strDateArray = strDate.split(strSeparatorArray[intElementNr]);
            if (strDateArray.length != 3)
            {
                err = 1;
                return false;
            }
            else
            {
                strDay = strDateArray[0];
                strMonth = strDateArray[1];
                strYear = strDateArray[2];
            }
            booFound = true;
        }
    }
    
    if (booFound == false)
    {
        if (strDate.length>5)
        {
            strDay = strDate.substr(0, 2);
            strMonth = strDate.substr(2, 2);
            strYear = strDate.substr(4);
        }
    }
    
    if (strYear)
    {    
        if (strYear.length == 2)
        {
            strYear = '20' + strYear;
        }
        
        if (strYear.length > 4)
        {
            alert("That date is invalid.  Please try again.");
        }
    }
    else
    
    {
        return false;
    }
    
    
    // US style
    if (strDatestyle == "US")
    {
        strTemp = strDay;
        strDay = strMonth;
        strMonth = strTemp;
    }
    
    intday = parseInt(strDay, 10);
    if (isNaN(intday))
    {
        err = 2;
        return false;
    }
    
    intMonth = parseInt(strMonth, 10);
    if (isNaN(intMonth))
    {
        for (i = 0;i<12;i++)
        {
            if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase())
            {
                intMonth = i+1;
                strMonth = strMonthArray[i];
                i = 12;
            }
        }
        if (isNaN(intMonth))
        {
            err = 3;
            return false;
        }
    }
    intYear = parseInt(strYear, 10);
    if (isNaN(intYear))
    {
        err = 4;
        return false;
    }

    if (intMonth>12 || intMonth<1)
    {
        err = 5;
        return false;
    }
    
    if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1))
    {
        err = 6;
        return false;
    }
    if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1))
    {
        err = 7;
        return false;
    }
    if (intMonth == 2)
    {
        if (intday < 1)
        {
            err = 8;
            return false;
        }
        if (LeapYear(intYear) == true)
        {
            if (intday > 29)
            {
                err = 9;
                return false;
            }
        }
        else
        {
            if (intday > 28)
            {
                err = 10;
                return false;
            }
        }
    }
    if (strDatestyle == "US")
    {
        datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
    }
    else
    {
        datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
    }
    return true;
}

function LeapYear(intYear)
{
    if (intYear % 100 == 0)
    {
        if (intYear % 400 == 0)
        {
            return true;
        }
    }
    else
    {
        if ((intYear % 4) == 0)
        {
            return true;
        }
    }
    return false;
}

function doDateCheck(from, to)
{
    if (Date.parse(from.value) <= Date.parse(to.value))
    {
        return '';
    }
    else
    {
        if (from.value == "" || to.value == "")
        {
            return 'Both dates must be entered.';
        }
        else 
        {
            return 'The \'To Date\' must occur after the \'From Date\'.';
        }   
   }
}