function showpic(img,xp,yp,lk){
	document.getElementById("thumb").style.left = xp +'px';
	document.getElementById('thumb').style.top = yp + 'px';
	document.getElementById('thumbimg').src=  'thumbs/' + img;
	document.getElementById('goto').href = lk;
	if (lk != '') {
		document.getElementById('goto').target = '_blank';
	} else {
		document.getElementById('goto').target = '';
	}
	document.getElementById('thumb').style.display = 'block';
}

function hideMe() {
	document.getElementById('thumb').style.display = 'none';
	document.getElementById('thumbimg').src=  'thumbs/blank.gif';
}

function checkit() {
//check for valid link
	x=document.getElementById("link").value;
	if (document.getElementById("amount").value == '50')  {
		if (x.length == 0) {
			alert("Link is blank : you can specify it later");
		} else {
			if (!checkurl(x)) {
				alert("Your link doesn't appear to be a valid URL");
				return false;
			}
		}
	} else {
		x.value = '';
	}
	
//check for name
	var x=document.getElementById("fullname").value;
	if (x.length<1) {
		document.getElementById("fullname").focus();
		alert("Please enter your name as on your payment card");
		return false;
	}

//check for postcode
	e=document.getElementById("postcode");
	if (!checkPC()) {
	    e.focus();
//		alert("Not a valid UK Postcode");
		return false;
	}

//check for phone number
	x=document.getElementById("phone").value;
	if (x.length<1) {
		document.getElementById("phone").focus();
		alert("Our card processor requires you enter your phone number.\r\nWe are not going to use it unless there is a problem.");
		return false;
	}
	
//check email
	var e=document.getElementById("email");
	if (!checkEmail(e)) {
		e.focus();
		alert("Sorry, that doesn't look like a proper email address.\r\nOur card processor needs to send you a confirmation email, and we will send you a token to change your message and picture.");
		return false;
	}
	
//change value of description
	var x=document.getElementById("amount");
    desstr = x.options[x.selectedIndex].text + " (row:" + document.getElementById("row").value + " col:" + document.getElementById("col").value + ")";
	document.getElementById("description").value = desstr;
	
//build billing address
	x=document.getElementById("addr1").value;
	if (x.length<1) {
		document.getElementById("addr1").focus();
		alert("Sorry, first line of address missing");
		return false;
	}
	var y = x;
	x = document.getElementById("addr2").value; 
	if (x.length>0){
		y = y + ", " + x;
	}
	x = document.getElementById("town").value;
	if (x.length<1) {
		document.getElementById("town").focus();
		alert("Sorry, post town/city is missing");
		return false;
	}
	y = y + ", " + x;
	x = document.getElementById("county").value;		
	if (x.length>0) {
		y = y + ", " + x;
	}
	document.getElementById("billing_address").value = y;

//if no tagtext default to name
	e=document.getElementById("tagText");
	if (e.value.length == 0) {
		e.value = document.getElementById("fullname").value;
		alert("Message text will default to your name, you can change it later");
	}
	
	//everything seems to be ok so set the cookies and return
	cookieForm();
	return true;
}

function checkEmail(formInput) {

    if (typeof(formInput) != "object") {
        alert("Validation not supported on this browser.");
        return(false);
    }

    var message;

    if (stringEmpty(formInput.value)) {
    //    message = "Error! There is no input value entered.";
    //    alert(message);
	    return (false);
    } else if (noAtSign( formInput.value )) {
    //    message = "Error! The address \"" + formInput.value + "\" does not contain an '@' character.";
    //    alert(message);
	    return (false);
    } else if (nothingBeforeAt(formInput.value)) {
    //    message = "Error! The address \"" + formInput.value;
    //    message += "\" must contain at least one character before the '@' character";
    //    alert(message);
	    return (false);
    } else if (noLeftBracket(formInput.value)) {
    //    message = "Error! The address \"" + formInput.value;
    //    message += "\" contains a right square bracket ']',\nbut no corresponding left square bracket '['.";
    //    alert(message);
	    return (false);
    } else if (noRightBracket(formInput.value)) {
    //    message = "Error! The address \"" + formInput.value;
    //    message += "\" contains a left square bracket '[',\nbut no corresponding right square bracket ']'.";
    //    alert( message);
	    return (false);
    } else if (noValidPeriod(formInput.value)) {
    //    message = "Error! The address \"" + formInput.value + "\" must contain a period ('.') character.";
    //    alert(message);
	    return (false);
    } else if (noValidSuffix(formInput.value)) {
    //    message = "Error! The address \"" + formInput.value;
    //    message += "\" must contain a two, three or four character suffix.";
    //    alert(message);
	    return (false);
    }
    
    return (true);
}

function checkValid (formField) {
    if ( checkValidation ( formField ) == true ) {
        //alert ( 'E-Mail Address Validates OK' );
        return true;
    }

    return ( false );
}

function stringEmpty (formField) {
    // CHECK THAT THE STRING 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 noLeftBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
    if ( formField.indexOf ( '[', 0 ) == -1 && formField.charAt (formField.length - 1) == ']') {
        return ( true )
    } else {
        return ( false );
    }
}

function noRightBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
    if (formField.indexOf ( '[', 0 ) > -1 && formField.charAt (formField.length - 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 checkPC(){ //check postcode format is valid
	e=document.getElementById("postcode");
	test = e.value;
 	size = test.length;
 	test = test.toUpperCase(); //Change to uppercase
 	while (test.slice(0,1) == " ") { //Strip leading spaces
		test = test.substr(1,size-1);
		size = test.length;
  	}
 	while(test.slice(size-1,size)== " ") { //Strip trailing spaces
  		test = test.substr(0,size-1);size = test.length
  	}
 	e.value = test; //write back to form field
//	return false;
 	if ((size < 6) || (size > 8)){ //Code length rule
     alert(test + " is not a valid postcode - wrong length");
    //  e.focus();
  		return false;
  	}
 	if (!(isNaN(test.charAt(0)))) { //leftmost character must be alpha character rule
   alert(test + " is not a valid postcode - cannot start with a number");
//   e.focus();
   		return false;
  	}
 	if (isNaN(test.charAt(size-3))) { //first character of inward code must be numeric rule
   alert(test + " is not a valid postcode - alpha character in wrong position");
//   e.focus();
   		return false;
  	}
 	if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
//   e.focus();
   		return false;
  	}
 	if (!(isNaN(test.charAt(size-1)))) { //third character of inward code must be alpha rule
   alert(test + " is not a valid postcode - number in wrong position");
//   e.focus();
   		return false;
  	}
 	if (!(test.charAt(size-4) == " ")) {//space in position length-3 rule
   alert(test + " is not a valid postcode - no space or space in wrong position");
//   e.focus();
   		return false;
   	}
 	count1 = test.indexOf(" ");
	count2 = test.lastIndexOf(" ");
 	if (count1 != count2) {//only one space rule
   alert(test + " is not a valid postcode - only one space allowed");
//   e.focus();
   		return false;
  	}
//alert("Postcode Format OK");
	return true;
}

function checkurl(url)
{
     return url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
} 

