var calendarYUI;
function displayYUICalendar(inId, inContainer)
{
    if (!calendarYUI)
    {
	var options = {mindate: "1/1/1920",
		       navigator: true,
		       draggable: true,
		       close:	  true
	};
		       

	calendarYUI = new YAHOO.widget.Calendar(inId, inContainer, options);
	calendarYUI.selectEvent.subscribe(selectEvent, calendarYUI, true);
	YAHOO.util.Event.on(document, "click", clickCallback);
    }
    calendarYUI.show();
    calendarYUI.render();
}

function clickCallback(e)
{
    var el = YAHOO.util.Event.getTarget(e);
    var dialogE1 = document.getElementById("cal1Container");
    var dateButton = document.getElementById("date_input"); 
    
    if (el != dialogE1 && !YAHOO.util.Dom.isAncestor(dialogE1, el) && 
	el != dateButton && !YAHOO.util.Dom.isAncestor(dateButton, el))
    {
	calendarYUI.hide();	
    }
}

function selectYUIdate()
{
    var date = document.subscriptionForm.date_birth.value;
    if (!isValidDate(date))
	return;
    calendarYUI.cfg.setProperty("selected", date, false);
    date = date.split("/");
    calendarYUI.cfg.setProperty("pagedate", date[1] + "/" + date[2], false);
    calendarYUI.show();
    calendarYUI.render();
}

function selectEvent(type, inDate, obj)
{
    var dates = inDate[0];
    var date  = dates[0];
    var year = date[0], month = date[1], day=date[2];
    document.subscriptionForm.date_birth.value = day + "/" + month + "/" + year;
    calendarYUI.hide();	
}

function verify(){
	lclMsg = "";
	if (document.subscriptionForm.title.value == ""){
		lclMsg += "- Included title\n"; 
	}
	if (document.subscriptionForm.first_name.value == ""){
		lclMsg += "- Included your first name \n"; 
	}
	if (document.subscriptionForm.family_name.value == ""){
		lclMsg += "- Included your family name \n"; 
	}
	if (document.subscriptionForm.email.value == ""){
		lclMsg += "- Included a valid email address \n"; 
	} else {
		if (isValidEmail(document.subscriptionForm.email.value) != true) {
			lclMsg += "- Please check that your email address is valid \n"; 
		}
	}
	function isValidEmail(str) {
   		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	}

	var date = document.subscriptionForm.date_birth.value;
	if (!isValidDate(date))
	    lclMsg += "- Please enter valid date of birth \n"; 
	
	if(lclMsg!="")
	{
	    lclMsg = "Please ensure you have...\n" + lclMsg;
	    alert(lclMsg);
	    return false;
	}
	
	return true;
}
function isValidDate(inDate)
{
    var	date = inDate.split("/");
    var day = date[0], month = date[1], year = date[2];

    if (date.length != 3			  	||
	isNaN(day) || isNaN(month) || isNaN(year)	||
	(day.length < 1 || day.lenth > 2)		||
	(month.length < 1 || month.length > 2)	||
	year.length != 4				||
	(month < 0 || month > 12) 			||
	(day < 0 || day > 31))
    {
	return(0);
    }
    else
    {
	if ((month == 2 || month == 4 || month == 6 || month == 9 ||
	     month == 11) && day == 31)
		return(0);
	if (month == 2)
	{
	    var isLeapYear = (year % 4 == 0 && (year % 100 != 0 || 
						year % 400 == 0));
	    if (day > 29)
		return(0);
	    else if (!isLeapYear && day > 28)
		return(0);
	}

    }
    return(1);
}

