var formValid=true;

function validateShareForm() {
	formValid=true;

	// Clear label values
	clearLabelValue("GlobalShare_ShareErrorLabel");

	// Textbox values
	validateFormValue(document.forms[0].GlobalShare_ShareYourName.value,"Your Name");

	// Dropdowns
	//var countryIndex=document.forms[0].country.selectedIndex;
	//validateFormValue(document.forms[0].country.options[countryIndex].value,"RequiredCountryLabel");
	
	// Email validation
	validateEmail(document.forms[0].GlobalShare_ShareYourEmail.value);
	validateEmail(document.forms[0].GlobalShare_ShareFriendEmail.value);
	
	// Date validation
	//validateBirthdate(document.forms[0].month.options[birthMonthIndex].value,document.forms[0].day.options[birthDayIndex].value,document.forms[0].year.options[birthYearIndex].value,"RequiredDOBLabel");
	
	// Lastly, display error message if we have a form error
	if (!formValid)
	{
		document.getElementById("GlobalShare_ShareErrorLabel").innerHTML="Please supply valid information for all fields.";
		//window.scrollTo(0,0);
	}
	else 
	{
		// Submit!
		js_isshare_on=false;
        window.frames['SessionManager'].location = "session_manager.aspx?context=Share&value=false";
	    $('#ShareContentContainer').hide();
        $('#ShareContainer').slideUp();
        image_swap("nav_share","off");
        image_swap("nav_signup","off");
        myTimeout=setTimeout("submitFormInContext('Share')",250);
	}
}

function validateSignupForm() {
	formValid=true;

	// Clear label values
	clearLabelValue("GlobalSignup_SignupErrorLabel");

	// Textbox values
	validateFormValue(document.forms[0].GlobalSignup_SignupFName.value,"First Name");
	validateFormValue(document.forms[0].GlobalSignup_SignupLName.value,"Last Name");
	validateFormValue(document.forms[0].GlobalSignup_SignupZip.value,"Zip");
	validateFormValue(document.forms[0].GlobalSignup_SignupMobile.value,"Mobile Number");

	// Dropdowns
	var countryIndex=document.forms[0].GlobalSignup_SignupCountry.selectedIndex;
	validateFormValue(document.forms[0].GlobalSignup_SignupCountry.options[countryIndex].value,"");

	var genderIndex=document.forms[0].GlobalSignup_SignupGender.selectedIndex;
	validateFormValue(document.forms[0].GlobalSignup_SignupGender.options[genderIndex].value,"");
	
	// Email validation
	validateEmail(document.forms[0].GlobalSignup_SignupEmail.value);
	
	// Date validation
	var birthMonthIndex=document.forms[0].GlobalSignup_SignupBirthMonth.selectedIndex;
	var birthDayIndex=document.forms[0].GlobalSignup_SignupBirthDay.selectedIndex;
	var birthYearIndex=document.forms[0].GlobalSignup_SignupBirthYear.selectedIndex;
	validateBirthdate(document.forms[0].GlobalSignup_SignupBirthMonth.options[birthMonthIndex].value,document.forms[0].GlobalSignup_SignupBirthDay.options[birthDayIndex].value,document.forms[0].GlobalSignup_SignupBirthYear.options[birthYearIndex].value);
	
	// Lastly, display error message if we have a form error
	if (!formValid)
	{
		document.getElementById("GlobalSignup_SignupErrorLabel").innerHTML="Please supply valid information for all fields.";
		//window.scrollTo(0,0);
	}
	else 
	{
		// Submit!
		js_isSignup_on=false;
        window.frames['SessionManager'].location = "session_manager.aspx?context=Signup&value=false";
	    $('#SignupContentContainer').hide();
        $('#SignupContainer').slideUp();
        image_swap("nav_signup","off");
        image_swap("nav_share","off");
        image_swap("nav_right","off");
        myTimeout=setTimeout("submitFormInContext('Signup')",250);
	}
}

function validateRSVPForm(browserContext) {
	formValid=true;

    if (browserContext=="NonIE") {
	    // Clear label values
	    clearLabelValue("NonIERSVPPopup_RSVPFormErrorLabel");

	    // Textbox values
	    validateFormValue(document.forms[0].NonIERSVPPopup_RSVPFName.value,"");
	    validateFormValue(document.forms[0].NonIERSVPPopup_RSVPLName.value,"");
	    validateFormValue(document.forms[0].NonIERSVPPopup_RSVPZip.value,"");

	    // Dropdowns
	    var countryIndex=document.forms[0].NonIERSVPPopup_RSVPCountry.selectedIndex;
	    validateFormValue(document.forms[0].NonIERSVPPopup_RSVPCountry.options[countryIndex].value,"");

	    // Email validation
	    validateEmail(document.forms[0].NonIERSVPPopup_RSVPEmail.value);
    	
	    // Date validation
	    var birthMonthIndex=document.forms[0].NonIERSVPPopup_RSVPBirthMonth.selectedIndex;
	    var birthDayIndex=document.forms[0].NonIERSVPPopup_RSVPBirthDay.selectedIndex;
	    var birthYearIndex=document.forms[0].NonIERSVPPopup_RSVPBirthYear.selectedIndex;
	    validateBirthdate(document.forms[0].NonIERSVPPopup_RSVPBirthMonth.options[birthMonthIndex].value,document.forms[0].NonIERSVPPopup_RSVPBirthDay.options[birthDayIndex].value,document.forms[0].NonIERSVPPopup_RSVPBirthYear.options[birthYearIndex].value);
    	
	    // Lastly, display error message if we have a form error
	    if (!formValid)
	    {
		    document.getElementById("NonIERSVPPopup_RSVPFormErrorLabel").innerHTML="Please supply valid information for all required fields.";
		    //window.scrollTo(0,0);
	    }
	    else 
	    {
		    // Submit!
            submitFormInContext('RSVP');
	    }
    }
    else
    {
	    // Clear label values
	    clearLabelValue("IERSVPPopup_RSVPFormErrorLabel");

	    // Textbox values
	    validateFormValue(document.forms[0].IERSVPPopup_RSVPFName.value,"");
	    validateFormValue(document.forms[0].IERSVPPopup_RSVPLName.value,"");
	    validateFormValue(document.forms[0].IERSVPPopup_RSVPZip.value,"");

	    // Dropdowns
	    var countryIndex=document.forms[0].IERSVPPopup_RSVPCountry.selectedIndex;
	    validateFormValue(document.forms[0].IERSVPPopup_RSVPCountry.options[countryIndex].value,"");

	    // Email validation
	    validateEmail(document.forms[0].IERSVPPopup_RSVPEmail.value);
    	
	    // Date validation
	    var birthMonthIndex=document.forms[0].IERSVPPopup_RSVPBirthMonth.selectedIndex;
	    var birthDayIndex=document.forms[0].IERSVPPopup_RSVPBirthDay.selectedIndex;
	    var birthYearIndex=document.forms[0].IERSVPPopup_RSVPBirthYear.selectedIndex;
	    validateBirthdate(document.forms[0].IERSVPPopup_RSVPBirthMonth.options[birthMonthIndex].value,document.forms[0].IERSVPPopup_RSVPBirthDay.options[birthDayIndex].value,document.forms[0].IERSVPPopup_RSVPBirthYear.options[birthYearIndex].value);
    	
	    // Lastly, display error message if we have a form error
	    if (!formValid)
	    {
		    document.getElementById("IERSVPPopup_RSVPFormErrorLabel").innerHTML="Please supply valid information for all required fields.";
		    //window.scrollTo(0,0);
	    }
	    else 
	    {
		    // Submit!
            submitFormInContext('RSVP');
	    }
    }	    
}

function validateMemberForm() {
	formValid=true;

    // Clear label values
    clearLabelValue("FormErrorLabel");

    // Textbox values
    validateFormValue(document.forms[0].FName.value,"");
    validateFormValue(document.forms[0].LName.value,"");
    validateFormValue(document.forms[0].Zip.value,"");

    // Dropdowns
    var countryIndex=document.forms[0].Country.selectedIndex;
    validateFormValue(document.forms[0].Country.options[countryIndex].value,"");

    // Email validation
    validateEmail(document.forms[0].Email.value);
	
    // Date validation
    var birthMonthIndex=document.forms[0].BirthMonth.selectedIndex;
    var birthDayIndex=document.forms[0].BirthDay.selectedIndex;
    var birthYearIndex=document.forms[0].BirthYear.selectedIndex;
    validateBirthdate(document.forms[0].BirthMonth.options[birthMonthIndex].value,document.forms[0].BirthDay.options[birthDayIndex].value,document.forms[0].BirthYear.options[birthYearIndex].value);
	
    // Lastly, display error message if we have a form error
    if (!formValid)
    {
	    document.getElementById("FormErrorLabel").innerHTML="Please supply valid information for all required fields.";
	    //window.scrollTo(0,0);
    }
    else 
    {
	    // Submit!
        submitFormInContext('Member');
    }
}

function validateFormValue (field, default_value) {
	if (field!="" && field!=default_value) {
		return true;
	}
	else {
		formValid=false;
		return false;
	}
}

function clearLabelValue(elementname) {
	document.getElementById(elementname).innerHTML="&nbsp;";
}

function validateEmail(emailaddress) {
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(emailaddress)) 
	{
		return true;
	}
	else 
	{
		formValid=false;
		return false;
	}
}

function validateBirthdate(month,day,year) {
	var monthInt=parseInt(month, 10);
	var dayInt=parseInt(day, 10);
	var yearInt=parseInt(year, 10);
	
	// Month Check
    if (isNaN(monthInt)) 
    {
		formValid=false;
        return false;
    } 
    else 
    {
        if (monthInt<1 || monthInt>12) {
			formValid=false;
            return false;
        }
    }
    
    // Day Check
    if (isNaN(dayInt)) 
    {
		formValid=false;
        return false;
    } 
    else 
    {
        var monthDays = new Array(31,31,29,31,30,31,30,31,31,30,31,30,31);
        var maxMonth = monthDays[monthInt];
        if (dayInt<1 || dayInt>maxMonth) {
			formValid=false;
            return false;
        }
	}
	
	// Year Check
	if (isNaN(yearInt)) 
    {
		formValid=false;
        return false;
    } 
    else 
    {
        if (yearInt<1900 || yearInt>2007) {
			formValid=false;
            return false;
        }
    }
    
    return true;    
}

function validateMobile(mobile) {
	var parsedMobile=mobile;
	var returnValue=true;
	var MobileInt=0;
	
	if (mobile=="") {
		// null value
		returnValue=false;
	}
	else
	{
		// Parse mobile
		parsedMobile=parsedMobile.replace(/\./g,"");
		parsedMobile=parsedMobile.replace(/-/g,"");
		parsedMobile=parsedMobile.replace(/\)/g,"");
		parsedMobile=parsedMobile.replace(/\(/g,"");
		parsedMobile=parsedMobile.replace(/ /g,"");

		if (parsedMobile=="") 
		{
			// null value
			returnValue=false;
		}
		else 
		{
			// Check length
			if (parsedMobile.length!=10)
			{
				returnValue=false;
			}
			else 
			{
				// Check numbers
				var i=0;
				while (i<parsedMobile.length)
				{
					mobileInt=parseInt(parsedMobile.substring(i,i+1), 10);
					if (isNaN(mobileInt)) 
					{
						// Invalid character
						returnValue=false;
					}
					i++;
				}
			}
		}
	}

	if (!returnValue) 
	{
	}	
	
	formValid=returnValue;
	return returnValue;
}
