var emailAddress;

function postSignUp(myForm) {
	//disable submit button
	$("#submit").attr("disabled", "true"); 
	
	var errorMsg = "";
	var error = 0;
	$("#val-msg").html(errorMsg);
	var email = decodeURI($("#updateForm").find("#emailAddress").val());
	var doc = document.updateForm;
	
	if (email == "") {
		errorMsg = "Please enter your email address";
		error = 1;
	} else {
		var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
		
		for (var i=0; i < email.length; i++) {
			var letter = email.charAt(i).toLowerCase();
			if (validchars.indexOf(letter) == -1) {
				errorMsg = "Please enter a valid email address";
				error = 1;
			}					
		}
	    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
			errorMsg = "Your email is invalid, It must contain an @ sign";
			error = 1;
		} else if (email.indexOf("@") != email.lastIndexOf("@")) { // can only have one @ sign
			errorMsg = "Your email is invalid, It must only contain one @ sign";
			error = 1;
	    } else if (email.lastIndexOf(".") < (email.indexOf("@")+3)) {  // last dot must be atleast 2 chars after the @
			errorMsg = "Please enter a valid email address";
			error = 1;
	    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
			errorMsg = "Please enter a valid email address";
			error = 1;
	    } else if (email.length <= email.lastIndexOf(".")+2) {
			errorMsg = "Please enter a valid email address";
			error = 1;
		} else if (doc.contact1195.checked == false) {		
			errorMsg = "Please confirm you're happy to receive updates from Team Sky";
			error = 1;
			//naughty naughty doc.contact1195.checked = true;
		}
	}
	
	if (error===1) {
		$("#submit").removeAttr("disabled"); 
		$('#val-msg').html('<div class="val-fail">'+errorMsg+'</div>')
					.hide()
					.fadeIn(500, function() {}); 
		return false;
	} else {
		var postURL = "/updates_capture_process/1,27227,,00.xml";
					
		$.post(postURL, $("#updateForm").serialize(), function(responseData){
		  	  			  
			if ( $('outcome',responseData).attr("type") == "error" ) {
				
				$("#val-msg").html("<div class='val-fail'><p><b>" + $('outcome',responseData).find("response").text() + "</b><br/>" + $('outcome',responseData).find("details").text() + "</p></div>");
				// turn the submit button on for now.
				$("#submit").removeAttr("disabled"); 
			
			} else if ( $('outcome',responseData).attr("type") == "success" ) {
			
				$('#updateForm').html("<div id='message'></div>");  
				$('#message').html("<p><b>" + $('outcome',responseData).find("response").text() + "</b><br/>" + $('outcome',responseData).find("details").text() + "</p>")
				.hide()
				.fadeIn(500, function() {});  						
			}
			  
	  	}, "xml"); 
		return false;
	}
}

function openVideo(bcID) {
	
	if (typeof bcID == "undefined") {
		var xtraVars = "?width=630&height=570";
	} else {
		var xtraVars = "?bctid="+bcID+"&width=630&height=570";
	}
	tb_show("", "/video_overlay/0,27395,,00.html"+ xtraVars +"" ,"");
}

function openSignUp() {
	//alert("open video");
	tb_show("", "/updates_form/0,27219,,00.html?height=280&width=420&modal=true" ,"");
}
