// *****************************************************************************
//
//   JavaScript within ContactForm page.  This version 25/03/09 - working!  See 'contact_test.js' for experimentation with email validation.
//
//
// *****************************************************************************
//
//
//
// Alex function to show today's date in Input box

function getTheDate() {

	var myDate = new Date();
	var thisDate;
	var myMonth;
	var monthName=new Array(12);

	monthName [0]="-Jan-";
	monthName [1]="-Feb-";
	monthName [2]="-Mar-";
	monthName [3]="-Apr-";
	monthName [4]="-May-";
	monthName [5]="-Jun-";
	monthName [6]="-Jul-";
	monthName [7]="-Aug-";
	monthName [8]="-Sep-";
	monthName [9]="-Oct-";
	monthName [10]="-Nov-";
	monthName [11]="-Dec-";

	myMonth=myDate.getMonth();

	thisDate=myDate.getDate() + monthName[myMonth] + myDate.getYear();
	document.getElementById("dateBox").value=thisDate;

}	
// Body onLoad function to point out that the form only works if you have an email client
//
function health_warning() {
	alert("You will require to have email set up on your computer to send this form.  Alternatively, send email to 'info@hallanshankers.co.uk'");
	document.getElementById("user_name").focus();
}
//
function email_warning() {
	alert("You will require to have email set up on your computer to use this link.  Alternatively, send email to 'info@hallanshankers.co.uk'");
	document.getElementById("user_name").focus();
}
//
//
// Alex function to check all inputs in 'ContactForm.html' upon Submit being clicked	(LINE 40!)
//
function chkForm (contactForm) {
	if (contactForm.user_name.value == '') {
	   alert("Please enter your name");
	   document.getElementById("user_name").focus();
	   return (false);
	}
//	
// Ensure 'email1' is like an email address
/*	with (contactForm)
	
	if (validate_email(email1,"This doesn't look like a valid e-mail address!")==false)
	  {email1.value="";email1.focus();return false;}
*/
// Alternative email1 validation
//	with (contactForm)

	var message;

	if ( stringEmpty( contactForm.email1.value )) {
             message = "Error! There is no email entry.";
             alert(message);
	     contactForm.email1.focus();
	     return (false);
	}
	else 
	if ( noAtSign( contactForm.email1.value )) {
             message = "Error! The address \"" + contactForm.email1.value + "\" does not contain an '@' character.";
             alert(message);
	     contactForm.email1.focus();
	     return (false);
	}
	else 
	if ( nothingBeforeAt( contactForm.email1.value )) {
             message = "Error! The address \"" + contactForm.email1.value;
             message += "\" must contain at least one character before the '@' character";
             alert(message);
	     contactForm.email1.focus();
	     return (false);
	}
	else 
	if ( noValidPeriod(contactForm.email1.value )) {
             message = "Error! The address \"" + contactForm.email1.value + "\" must contain a period ('.') character.";
             alert(message);
	     contactForm.email1.focus();
	     return (false);
	}
	else 
	if ( noValidSuffix( contactForm.email1.value )) {
             message = "Error! The address \"" + contactForm.email1.value;
             message += "\" must contain a two, three or four character suffix.";
             alert(message);
	     contactForm.email1.focus();
	     return (false);
	}
	else 
	if ( anySpaces( contactForm.email1.value )) {
             message = "Error! The address \"" + contactForm.email1.value;
             message += "\" contains at least one space.";
             alert(message);
	     contactForm.email1.focus();
	     return (false);
	}
// -----------------------------------------------------------------------------------------------------------------------
	if (contactForm.email2.value == '') { 
            alert("The 2nd email box is blank!"); 
            contactForm.email2.focus(); 
            return (false); 
        } 
        
	if  (contactForm.email1.value != contactForm.email2.value) { 
            alert("The 2 email addresses do not match!"); 
            contactForm.email1.focus(); 
            return (false); 
        } 

alert("Thank you for your message.  We will respond as soon as possible.")
return true;
}
//
//	
// There now follows a list of the sub-functions which are called by the main ones above
//
// Alex function to ensure 'email1' is like an email address - called by 'chkForm'
//
function validate_email(field,alerttxt)
{
with (field)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
if (apos<1||dotpos-apos<2) 
  {alert(alerttxt);return false;}
else {return true;}
}
}
//
function stringEmpty (formField) {
    // CHECK THAT 'EMAIL1' IS NOT EMPTY
    if ( formField.length < 1 ) {
        return ( true );
    }
    else 
    {
        return ( false );
    }
}
//
function noAtSign (formField) {
    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
    if (formField.indexOf ('@', 0) == -1) {
        return ( true )
    } else {
        return ( false );
    }
}
//
function nothingBeforeAt (formField) {
    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
    if ( formField.indexOf ( '@', 0 ) < 1 ) {
        return ( true )
    } else {
        return ( false );
    }
}
//
function noValidPeriod (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf ( '@', 0 ) > 1 && formField.charAt (formField.length - 1 ) == ']')
        return ( false );

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
    if (formField.indexOf ( '.', 0 ) == -1)
        return ( true );

    return ( false );
}
//
function noValidSuffix(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']') {
        return ( false );
    }

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
    var len = formField.length;
    var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;
    if ( ( len - pos ) < 2 || ( len - pos ) > 4 ) {
        return ( true );
    } else {
        return ( false );
    }
}
function anySpaces(formField) {
    // CHECK THAT THERE ARE NO SPACES IN THE STRING
    if (formField.indexOf ( ' ', 0 ) != -1)
        return ( true );

    return ( false );
}

//
//
// ************************************
//     End of Function Library
// ************************************
// ************************************

