﻿//regular expressions

reg01=/^[A-Z]{1,2}[0-9][0-9a-z]?$/i;/*Post Code out*/

reg02=/^[0-9]{1}[A-Z]{2}$/i;/*Post Code In*/

reg04=/^[a-z-'\s]+$/i;/*alpha, apostrophe, dash, with spaces (Mandatory)*/

reg06=/^[-'\w\s]+$/i;/*alphanumeric, apostrophe, dash, with spaces (Mandatory)*/

reg07=/^[\w]+$/i;/*alphanumeric, no spaces(Mandatory)*/

reg08=/^[-'\w\s]*$/i;/*alphanumeric, apostrophe, dash, with spaces (Optional)*/

reg09=/^0*[1-9]+(\s?[0-9]+)*$/;/*for validating phone numbers. Allows a single space between numbers*/

reg11=/^(0*[1-9][0-9]*)+$/;/*mandatory numeric fields(doesn't allow all zeros)*/

reg12=/^[a-z-'\s]*$/i;/*alpha, apostrophe, dash, with spaces (Optional)*/

reg14=/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/;/*Email*/


function validateDetails(bUpdate)
{
	var msgText = '';

	with(theForm)
	{
	    msgText += flagField(fName,"text",reg04);
	    msgText += flagField(sName,"text",reg04);
	    msgText += flagField(addr1,"text",reg06);
	    msgText += flagField(addr2,"text",reg08);
	    msgText += flagField(city,"text",reg04);
	    msgText += flagField(county,"text",reg12);
	    msgText += flagField(postCodeOut,"pc",reg01);
	    msgText += flagField(postCodeIn,"pc",reg02);
	    msgText += flagField(secA,"text",reg06);
	    msgText += flagField(hPhone,"phone",reg09,true);
	    msgText += flagField(mPhone,"phone",reg09,false);
	    msgText += flagField(email,"email",reg14);
	    msgText += flagField(dobYr,"dob");
	    msgText += flagField(emergName,"text",reg04);
	    msgText += flagField(emergPhone,"phone",reg09,true);

        if(!bUpdate)
        {
            //ensure tc boxes have been checked
           if(!chkTC.checked)
           {
                msgText += "Please confirm you have read and agree to the Terms and Conditions.\n";
           }
        } 
   
	    if (msgText.length > 0 )
		    {
		    alert("There is a problem with the following fields:\n" + msgText);
		    return(false);
		    }
	    else
		    {
		    paddate(dobDay);
		    paddate(dobMth);
		    return(true);
		    }
	    }
    }
    
function flagField(field, typ, reg, bReq)
{
    var retText = "";
    
    switch(typ)
   {
	    case 'text':
	    { 
            if(!regCheck(reg,field))
            {
                 retText = eval("lab" + field.name).innerText + "\n";
	             highlightField(field);
            }
	        break;
	    } 
	    case 'phone':
	    { 
	        if(field.value=='')
	        {
	             if(bReq)
	             {  
	                 retText = eval("lab" + field.name).innerText + "\n";
		             highlightField(field);
		         }
	        }
	        else
	        {
	            if(!regCheck(reg,field))
	            {
	                 retText = eval("lab" + field.name).innerText + "\n";
		             highlightField(field);
	            }
	        }
	        break;
	    } 
	    case 'pc':
	    { 
	        if(!regCheck(reg,field))
	        {
	             retText = eval("lab" + field.name).innerText + "\n";
		         highlightField(field); 
			}

			break;

		}

	    case 'dob':
	    {
	        with(theForm){
			    //validate the date

			    if(!checkDate(dobDay.value,dobDay,dobMth.value,dobMth,field.value,field)){

				    retText = eval("lab" + field.name).innerText  + " - Invalid date\n";

		            highlightField(dobDay);
		            highlightField(dobMth);
		            highlightField(field);
			    }else{

				    //date is valid but check that year is 4 digits

				    if(dobYr.value.length!=4){

				        retText = eval("lab" + field.name).innerText  + " - Year not 4 digits\n";

		                highlightField(field); 
				    }

				    else

				    {

				        if(!CheckPastDate(Number(dobDay.value),Number(dobMth.value),Number(field.value),216))

				        {

				            retText = eval("lab" + field.name).innerText  + " - You must be at least 18 years old\n";

		                    highlightField(dobDay);
		                    highlightField(dobMth);
		                    highlightField(field);
				        }

				    }

			    }

			} 

			break;		

	    } 
	    case 'email':
	    { 
	        if(field.value=='')
	        {
	             retText = eval("lab" + field.name).innerText + "\n";
		         highlightField(field); 
	        }
	        else
	        {
	            var field2 = eval("theForm.re" + field.name);
	            if(field.value == field2.value)
	            {
	                //validate email address
				    if(!regCheck(reg,field)){

					    retText = "Email - Invalid format\n";

		                highlightField(field);
		                highlightField(field2); 
				    }

	            }   
	            else 
		        {
		           retText = 'Email - Fields do not match\n';
		           highlightField(field); 
		           highlightField(field2); 
		        }
	        }
	        break;
	    } 
   }
   return retText;
}

function highlightField(field)
{
	field.innerText.color = "red";
}



//strips off leading and trailing spaces

function Trim(strText){

	var trimReg=/^\s+|\s+$/g;

    return strText.replace(trimReg,'');

}



//regular expression function used to validate against most regular expressions

function regCheck(exp,objElem){

	var regObj=new RegExp(exp);

	if(!regObj.test(Trim(objElem.value))){

		objElem.focus();

		objElem.select();

		return false;

	}

	return true;

}



//Regular expression to check for a number. Can supply an optional

//length and mandatory argument

function isNum(objElem,len,bOpt){

	bOpt=(bOpt)?"*":"+";//default is mandatory

	var reg, undefined;

	

	if((len=="")||(len==" ")||(len==undefined)){

		//no length specified. Just add on the optional indicator

		reg="^[0-9]" + bOpt+"$";

	}else{

		//length specified. Add on the length and the optional indicator

		reg="^([0-9]{"+len+"})"+bOpt+"$";

	}

	var regObj=new RegExp(reg);

	if(!regObj.test(Trim(objElem.value))){

		objElem.focus();	

		objElem.select();

		return false;

	}

	return true;

}



/*****************************************************************************/

/* Date validation routines													 */

/*****************************************************************************/



//THIS CHECKS IF THE DATES INPUTTED ARE VALID DATES

//Pass in the value and the name of the object in pairs. If a field

//is invalid it will be highlighted

function checkDate(d,dname,m,mname,y,yname){



    var d_ok=false;



     // Date Checks

	switch(m+""){

        case "2":{}

        case "02":{if(y%4==0&&(y%400==0||y%100!=0)){

               		d_ok=d>0&&d<=29;

				}else{

					d_ok=d>0&&d<=28;

				}

					    

				if(!d_ok){

					highlightField(dname);

					return false;

				}

				break;

			}

		case "4":{}  // April 

	    case "04":{} // April 

        case "6":{}  // June 

        case "06":{}// June 

        case "9":{}  // Sep 

        case "09":{} // Sep 

        case "11":{							

				if (d>= 1&&d<=30){

					d_ok=true;

				}else{			

					highlightField(dname);

					return false;

				}

				break;

			}

	    default:{

				if (d>=1&&d<=31){

					d_ok=true;

				}else{

					highlightField(dname);

					return false;

				}		        

		        

				if(m>=1&&m<=12){

					d_ok=true;

				}else{				

					highlightField(mname);

					return false;

				}

			}

	  }



	 //Check to see if a valid number was entered in year field

	if(!isNum(yname)){d_ok=false;}

	if(y==""){d_ok=false;}

	if(!d_ok){	

		highlightField(yname);

		return false;

	}

	return true;

}



function CheckPastDate(day,month,year,iOffset){

    //debugger;

	if(isNaN(iOffset))iOffset=0;

	tDate=new Date();

	mEnt=(year*12)+month;

	mCurr=(tDate.getFullYear()*12)+(tDate.getMonth()+1);

	

	if((mEnt+iOffset)<mCurr){return true;}

	if((mEnt+iOffset)==mCurr){

		if(day<tDate.getDate()+1){return true;}

	}

	return false;

}





function paddate(datefield){

	var val=Trim(datefield.value);

	if(val.length==1){

		if(!isNaN(val)){

			datefield.value = "0" + val;

		}

	}

}



/*****************************************************************************/

/* Non-validation functions													 */

/*****************************************************************************/

function updateOptions(varSecQ, varGender, varTransPref, varDob)
{
    if(typeof(varSecQ) == "number")
   { 
        varSecQ = varSecQ - 1;
        document.all('secQ').selectedIndex=varSecQ;
    }
    
    if(typeof(varTransPref) == "number")
   { 
        varTransPref = varTransPref - 1;
        document.all('transPref').selectedIndex=varTransPref;
   }
   
   with(theForm)
   {
        if(varGender=='F')
        { 
            genderF.checked = true;
        }
        else
        {
            genderM.checked = true;
        } 
   
        dobDay.value = varDob.substr(0,2);
        dobMth.value = varDob.substr(3,2);
        dobYr.value = varDob.substr(6,4);
   }
}

function gotoSecurity()
{
    alert ("membership.js - line 506 - continue fixing it...");
    
    var unameField  = $('login_uname');
    var uname       = Trim(unameField.val ());
   
    if(uname=="")
    {
        alert("Please enter your Email Address.");
        unameField.select();
    }
    else
    {  
        document.location.href = "member-security.asp?uname=" + uname;
    } 
}

function checkForm()
{
    with(theForm)
    { 
        if((Trim(fName.value)=="") || (Trim(sName.value)=="") || (Trim(postCodeOut.value)=="") || (Trim(postCodeIn.value)=="") || (Trim(secA.value)==""))
        {
            alert("Please enter details in all fields.");
            fName.select(); 
            return false; 
        }
        else
        {
            return true;
        }    
    }
}

function validatePassword()
{
    var msgText = "";
   
    with(theForm)
    {  
        if(Trim(new_pwd.value) == Trim(old_pwd.value))
        {
            msgText = "Current and New Passwords cannot be identical.";       
        }
        else
        {  
            if(Trim(new_pwd.value).length < 6 || Trim(new_pwd.value).length > 12)
            {
                msgText = "New Password must be between 6 and 12 characters in length.";       
            }
            else
            { 
                msgText = flagField(new_pwd,"text",reg07);
                if (msgText.length > 0)
                {
	                msgText = "The New Password entered is invalid.\nOnly alphanumeric characters (no spaces) are permitted.";
	            }
	        }
	    }
	      
        if (msgText.length > 0 )
	    {
	        alert(msgText);
	        old_pwd.select(); 
	        return(false);
	    }
        else
	    {
	        return(true);
	    } 
	} 
}

function changePassword()
{
    var email = theForm.hidEmail.value;
    document.location.href = "member-change-pwd.asp?email=" + email;
}

function focusOnField(field)
{
    if (field) {
        field.focus();
    }
}

function chgFormAction(val,bEventFree,bEventFreeAll)
{
    if((bEventFree) && (!bEventFreeAll))
   { 
        if(val == 0)
       {
            bookingForm.action = "booking-processfree.asp";
       }
       else 
       {
            bookingForm.action = "booking-process.asp";
       }
    }
}