var submitcount = 0;

function isEmpty(val) {
	return (val.search(/[a-zA-Z0-9]/gi) == -1)
}

function isValidDate(val) {
	return (val.search(/^(((0[1-9]|[12]\d|3[01])\/(0[13578]|1[02])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)\/(0[13456789]|1[012])\/((1[6-9]|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])\/02\/((1[6-9]|[2-9]\d)\d{2}))|(29\/02\/((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/gi) != -1)
}

function isValidEmail(val) {
	return (val.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/gi) != -1)
}

function isValidInteger(val) {
	return (val.search(/[^0-9]/gi) == -1)
}

function isValidMemberNumber(val) {
	if (val.length!=8)
		return false;
	else
		return (val.search(/\d/gi) != -1);
}

function DateDiff( start, end, interval, rounding ) {

    var iOut = 0;
    
    // Create 2 error messages, 1 for each argument. 
    var startMsg = "Check the Start Date and End Date\n"
        startMsg += "must be a valid date format.\n\n"
        startMsg += "Please try again." ;
		
    var intervalMsg = "Sorry the dateAdd function only accepts\n"
        intervalMsg += "d, h, m OR s intervals.\n\n"
        intervalMsg += "Please try again." ;

    var bufferA = Date.parse( start ) ;
    var bufferB = Date.parse( end ) ;
    	
    // check that the start parameter is a valid Date. 
    if ( isNaN (bufferA) || isNaN (bufferB) ) {
        alert( startMsg ) ;
        return null ;
    }
	
    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error. 
        alert( intervalMsg ) ;
        return null ;
    }
    
    var number = bufferB-bufferA ;
    
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            iOut = parseInt(number / 86400000) ;
            if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
            break ;
        case 'h': case 'H':
            iOut = parseInt(number / 3600000 ) ;
            if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
            break ;
        case 'm': case 'M':
            iOut = parseInt(number / 60000 ) ;
            if(rounding) iOut += parseInt((number % 60000)/30001) ;
            break ;
        case 's': case 'S':
            iOut = parseInt(number / 1000 ) ;
            if(rounding) iOut += parseInt((number % 1000)/501) ;
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        alert(intervalMsg) ;
        return null ;
    }
    
    return iOut ;
}

function txtFocus(txt,val) {
	if (txt.value=='' || txt.value==val)
		txt.value='';
}

function txtBlur(txt,val) {
	if (txt.value=='')
		txt.value=val;
}

function validateEnquiry(frm) {
	var intError = 0;
	var strDescription = 'Your information was not submitted because:\n';
	var focus_field = null;

	if (isEmpty(frm.txtRank.value)) {
	     strDescription = strDescription + ' -You did not enter your rank\n';
	     if (focus_field == null) focus_field = frm.txtRank;
	     intError = 1; 
	}

	if (!frm.cmbTitle.selectedIndex) {
	     strDescription = strDescription + ' -You did not select your title\n';
	     if (focus_field == null) focus_field = frm.cmbTitle;
	     intError = 1; 
	}

	if (isEmpty(frm.txtLastName.value)) {
	     strDescription = strDescription + ' -You did not enter your surname\n';
	     if (focus_field == null) focus_field = frm.txtLastName;
	     intError = 1; 
	}
	
	if (isEmpty(frm.txtFirstName.value)) {
	     strDescription = strDescription + ' -You did not enter your first name\n';
	     if (focus_field == null) focus_field = frm.txtFirstName;
	     intError = 1; 
	}

	if (isEmpty(frm.txtLocation.value)) {
	     strDescription = strDescription + ' -You did not enter your location name\n';
	     if (focus_field == null) focus_field = frm.txtLocation;
	     intError = 1; 
	}

	if (isEmpty(frm.txtPhone.value)) {
	     strDescription = strDescription + ' -You did not enter your contact phone number\n';
	     if (focus_field == null) focus_field = frm.txtPhone;
	     intError = 1; 
	}

	if (isEmpty(frm.txtEmail.value)) {
	     strDescription = strDescription + ' -You did not enter your email\n';
	     if (focus_field == null) focus_field = frm.txtEmail;
	     intError = 1; 
	}else if (!isValidEmail(frm.txtEmail.value)) {
	     strDescription = strDescription + ' -You did not enter valid email\n';
	     if (focus_field == null) focus_field = frm.txtEmail;
	     intError = 1; 
	}

	if (isEmpty(frm.txtComment.value)) {
	     strDescription = strDescription + ' -You did not enter your comment\n';
	     if (focus_field == null) focus_field = frm.txtComment;
	     intError = 1; 
	}

	if (intError == 1) {
		alert(strDescription);

		if (focus_field != null)
			focus_field.focus();

		return false;
	}
	
	return true;
}

function validateLogin(frm) {
	var intError = 0;
	var strDescription = 'Your information was not submitted because:\n';
	var focus_field = null;

	if (isEmpty(frm.txtSurname.value) || (frm.txtSurname.value == 'Surname')) {
	     strDescription = strDescription + ' -You did not enter your surname\n';
	     if (focus_field == null) focus_field = frm.txtSurname;
	     intError = 1; 
	}

	if (isEmpty(frm.txtMemberShipNo.value) || (frm.txtMemberShipNo.value == 'Membership No.')) {
	     strDescription = strDescription + ' -You did not enter your membership no.\n';
	     if (focus_field == null) focus_field = frm.txtMemberShipNo;
	     intError = 1; 
	}

	if (intError == 1) {
		alert(strDescription);

		if (focus_field != null)
			focus_field.focus();

		return false;
	}
	
	return true;
}

function validateMemberApplicationRequiredField(frm) {
	var errMsg = '';
	
	/***** Member detail *****/
	if (!frm.cmbService.selectedIndex) {
		errMsg += ' -You did not select your service\n';
	}

	if (isEmpty(frm.txtRank.value)) {
		errMsg += ' -You did not enter your rank\n';
	}
	
	if (!frm.cmbTitle.selectedIndex) {
		errMsg += ' -You did not select your title\n';
	}
	
	if (isEmpty(frm.txtLastName.value)) {
		errMsg += ' -You did not enter your surname\n';
	}
	
	if (isEmpty(frm.txtFirstName.value)) {
		errMsg += ' -You did not enter your first name\n';
	}
	
	if (isEmpty(frm.txtAddress1.value)) {
		errMsg += ' -You did not enter the first line of your address\n';
	}
	
	if (isEmpty(frm.txtSuburb.value)) {
		errMsg += ' -You did not enter your suburb\n';
	}
	
	if (isEmpty(frm.txtState.value)) {
		errMsg += ' -You did not enter your state\n';
	}
	
	
	if (!frm.cmbCountry.selectedIndex) {
		errMsg += ' -You did not select your country\n';
	}

	if (isEmpty(frm.txtPhone.value)) {
		errMsg += ' -You did not enter your daytime contact number\n';
	}
	
	if (isEmpty(frm.txtEmail.value)) {
		errMsg += ' -You did not enter your email address\n';
	}
	
	if (isEmpty(frm.txtServiceNo.value)) {
		errMsg += ' -You did not enter your service/AGS/PMKeys No.\n';
	}
	
	if (!frm.cmbHearAboutADLP.selectedIndex) {
		errMsg += ' -You did not select "How did you hear about ADLP?"\n';
	}
	
	/***** Partner details *****/
	if (frm.chkPartner.checked) {
		if (isEmpty(frm.txtPartnerLastName.value)) {
			errMsg += ' -You did not enter partner\'s surname\n';
		}
		if (isEmpty(frm.txtPartnerFirstName.value)) {
			errMsg += ' -You did not enter partner\'s first name\n';
		}
		if (isEmpty(frm.txtPartnerEmail.value)) {
			errMsg += ' -You did not enter partner\'s email\n';
		}
	}
	
	/***** Payment detail *****/
	if (!frm.rdoMembership[0].checked && !frm.rdoMembership[1].checked) {
		errMsg += ' -You did not select type of membership\n';
	}
	
	if (!frm.cmbCard.selectedIndex) {
		errMsg += ' -You did not select your credit card type\n';
	}

	if (isEmpty(frm.txtCardNumber.value)) {
		errMsg += ' -You did not enter your card number\n';
	}
	

	if (isEmpty(frm.txtCardName.value)) {
		errMsg += ' -You did not enter your cardholders name\n';
	}

	if ((!frm.c_cardexpmon.selectedIndex) || (!frm.c_cardexpyear.selectedIndex)) {
		errMsg += ' -You did not enter your credit card expiry date\n';
	}
	
	return errMsg;
}

function validateMemberApplication(frm) {
	var errMsg = validateMemberApplicationRequiredField(frm);
	if (errMsg=="") {
		if (!isValidEmail(frm.txtEmail.value)) {
			errMsg += ' -You did not enter valid email address\n';
		} 
	}//errMsg==""?
	
	if (errMsg != '') {
			alert('Your information was not submitted because:\n' + errMsg);
			return false;
	} else { //errMsg==''
		if (checkCard(frm.cmbCard.options[frm.cmbCard.selectedIndex].value,frm.txtCardNumber.value) == false) {
			return false;
		} else {//valid credit card
			var now = new Date();
			if ((now.getFullYear() == frm.c_cardexpyear.options[frm.c_cardexpyear.selectedIndex].value)
				&& (now.getMonth() > (frm.c_cardexpmon.options[frm.c_cardexpmon.selectedIndex].value - 1)))
			{
				alert("Your credit card has expired");
				return false;
			} else {//credit card doesn't expire
				return true;
			}//credit card doesn't expire
		}//valid credit card
	}// errMsg==''
}

function validatePartnerApplicationRequiredField(frm) {
	var errMsg = '';
	
	/***** Member detail *****/
	if (isEmpty(frm.txtPartnerLastName.value)) {
		errMsg += ' -You did not enter your surname\n';
	}
	
	if (isEmpty(frm.txtPartnerFirstName.value)) {
		errMsg += ' -You did not enter your first name\n';
	}
	
	if (isEmpty(frm.txtEmail.value)) {
		errMsg += ' -You did not enter your email address\n';
	}
	
	if (isEmpty(frm.txtMemberLastName.value)) {
		errMsg += ' -You did not enter member\'s surname\n';
	}
	
	if (isEmpty(frm.txtMembershipNumber.value)) {
		errMsg += ' -You did not enter membership number\n';
	}
	
	/***** Payment detail *****/
	/*if (!frm.chkPartner.checked) {
		errMsg += ' -You did not select type of membership\n';
	}*/
	
	if (!frm.cmbCard.selectedIndex) {
		errMsg += ' -You did not select your credit card type\n';
	}

	if (isEmpty(frm.txtCardNumber.value)) {
		errMsg += ' -You did not enter your card number\n';
	}
	

	if (isEmpty(frm.txtCardName.value)) {
		errMsg += ' -You did not enter your cardholders name\n';
	}

	if ((!frm.c_cardexpmon.selectedIndex) || (!frm.c_cardexpyear.selectedIndex)) {
		errMsg += ' -You did not enter your credit card expiry date\n';
	}
	
	return errMsg;
}

function validatePartnerApplication(frm) {
	var errMsg = validatePartnerApplicationRequiredField(frm);
	
	if (errMsg=="") {
		if (!isValidEmail(frm.txtEmail.value)) {
			errMsg += ' -You did not enter valid email address\n';
		} 
	}//errMsg==""?
	
	if (errMsg=="") {
		if (!isValidMemberNumber(frm.txtMembershipNumber.value)) {
			errMsg += ' -You did not enter valid membership number\n';
		} 
	}//errMsg==""?
	
	if (errMsg != '') {
			alert('Your information was not submitted because:\n' + errMsg);
			return false;
	} else { //errMsg==''
		if (checkCard(frm.cmbCard.options[frm.cmbCard.selectedIndex].value,frm.txtCardNumber.value) == false) {
			return false;
		} else {//valid credit card
			var now = new Date();
			if ((now.getFullYear() == frm.c_cardexpyear.options[frm.c_cardexpyear.selectedIndex].value)
				&& (now.getMonth() > (frm.c_cardexpmon.options[frm.c_cardexpmon.selectedIndex].value - 1)))
			{
				alert("Your credit card has expired");
				return false;
			} else {//credit card doesn't expire
				return true;
			}//credit card doesn't expire
		}//valid credit card
	}// errMsg==''
}

function confirmOrder(frm) {
	if (!frm.chkTerm.checked)
	{
		alert("To proceed with this order, please check the box to indicate that you have read and accept the Terms and Conditions for Accor Defence Leisure Program.");
		return false;
	}
	else
	{
		if (submitcount == 0)
		{
			//clickAction = confirm("Thank you - your booking will take a minute to process.\nPlease do not click the YES:SUBMIT BOOKING, Back, Refresh or Stop buttons until the next page is displayed.\nClick OK to continue.");
			//if (clickAction == true)
			//{
				submitcount++;
				return true;
			//}
			//else {return false;}
		}
		else
		{
			alert("Thank you, your booking is being processed.\nPlease be patient.");
			return false;
		}
	}
}

function doBooking(frm) {
	if (submitcount == 0)
	{
		submitcount++;
		frm.submit();
	}
}
