$(document).ready(function(){
	if($("#competition-pre").length>0){
		$("#link-pre").colorbox({width:"50%", innerHeight:"120px",inline:true, href:"#competition-pre"});
		$("#link-pre").click();
		$(".submit").css('display','none');
	}
	if($("#competition-thanks").length>0){
		$("#link-thanks").colorbox({width:"50%", innerHeight:"120px",inline:true, href:"#competition-thanks"});
		$("#link-thanks").click();
		$(".submit").css('display','none');
	}
	if($("#competition-post").length>0){
		$("#link-post").colorbox({width:"50%", innerHeight:"120px",inline:true, href:"#competition-post"});
		$("#link-post").click();
		$(".submit").css('display','none');
	}
	$('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
	if ($('#enter').length > 0) {
		$('#enter').submit(function(){
			
			var domain=$('#domain').val();
			
			if(domain=='greatukprizedraw.org.uk')
				$('#domain').val("");
			
			var name=$('#name').val();
			
			if(name=='John Smith')
				$('#name').val("");
			
			var email=$('#email').val();
			
			if(email=='johnsmith@email.com')
				$('#email').val("");
			
			var contact=$('#contact').val();
			
			if(contact=='01234567899')
				$('#contact').val("");
		});
		
		$.validator.addMethod("validDomain", function(value, element) {
			var enter_domain=$("#domain").val();
			suffix=enter_domain.substring(enter_domain.length-3,enter_domain.length);
			return suffix=='.uk';
		}, "Please enter your valid .uk domain");
		$.validator.addMethod("validPhone", function(value, element) {
			return  validatePhone($("#contact").val());
		}, "Please enter your contact number");
		
		var validator=$('#enter').validate({
			rules:{
				domain: {validDomain:true},
				contact: {
					number: true,
					minlength: 11,
					maxlength: 11
				}
			},
			messages:{
				domain: "Please enter your valid .uk domain",
				name: "Please enter your name and surname",
				email: "Please enter a valid email address",
				contact: "Please enter your contact number <br />(11 digits, no spaces)",
				age: "Please confirm you are over 18",
				terms: "Please confirm you have read the Terms and Conditions"
			},
			errorPlacement: function(error, element) {
				if (element.is(":checkbox")) {
					error.insertAfter(element);
				}
                else {
                    error.insertAfter(element.parent());
				}
            }
		});
	};
	$('#domain').focus(function() {
		var value=$(this).val();
		if(value=='greatukprizedraw.org.uk')
			$(this).val("");
	});
	$('#domain').blur(function() {
		if($(this).val()=="") {
			$(this).val('greatukprizedraw.org.uk');
		}
	});
	$('#name').focus(function() {
		var value=$(this).val();
		if(value=='John Smith')
			$(this).val("");
	});
	$('#name').blur(function() {
		if($(this).val()=="") {
			$(this).val('John Smith');
		}
	});
	$('#email').focus(function() {
		var value=$(this).val();
		if(value=='johnsmith@email.com')
			$(this).val("");
	});
	$('#email').blur(function() {
		if($(this).val()=="") {
			$(this).val('johnsmith@email.com');
		}
	});
	$('#contact').focus(function() {
		var value=$(this).val();
		if(value=='01234567899')
			$(this).val("");
	});
	$('#contact').blur(function() {
		if($(this).val()=="") {
			$(this).val('01234567899');
		}
	});
}); 
// Declaring required variables
var digits = "01234567899";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "+ ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters;
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 1;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) 
        	returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	strPhone=trim(strPhone)
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function checkInternationalPhone(strPhone){
	strPhone=trim(strPhone)
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


function validatePhone(number){
	
	if (number==""){
		return false
	}
	if (checkInternationalPhone(number)==false){
		return false
	}
	return true
}

function debug(){
	var head = document.getElementsByTagName("head")[0];         
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = 'https://getfirebug.com/firebug-lite.js';
	head.appendChild(script);
}

