function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();
var nocache = 0;
var pcFocused = false;
var pcSubmitted = false;

function checkcode() {
	pcSubmitted = true;
	pcFocused = false;
	var postcard = encodeURI(document.getElementById('postcode').value);
	nocache = Math.random();
	// Pass the login variables like URL variable
	http.open('get', 'checkPostcard.php?postcard='+postcard+'&nocache = '+nocache);
	http.onreadystatechange = loginReply;
	http.send(null);
}
function loginReply() {	
	if(http.readyState == 4){
		var response = http.responseText;
		if(response == 0){
			resp = "Please enter a Postcode";
			showError(resp);
		} else if(response == 1){
			resp = "The Postcode You entered is not available";
			showError(resp);
		} else if(response == 2){
			resp = "The Postcode You entered is already used";
			showError(resp);
		} else if(response == 3){
			resp = "The Postcode You entered is expired";
			showError(resp);
		} else if(response == 5){
			window.location.href='freeOffer.php';
		}
	}
}

function showError(resp) {
	pcSubmitted = false;
	document.getElementById('tool_tip').style.display='block';
	document.getElementById('presponse').innerHTML = resp;
	alertTime = setTimeout("hideError()",7000);
}

function hideError() {	
	clearTimeout(alertTime);
	document.getElementById('tool_tip').style.display='none';
	if (!pcFocused) {
		document.getElementById('postcode').value = "";
	}	
}

function checkField() {
	pcFocused = true;
	if (pcSubmitted && document.getElementById('postcode').value.length) {
		hideError();
	}
}

function killFocus() {
	pcFocused = false;
}

