function checkAccess()
{  
	var pin1 = document.getElementById('pin1').value;
	var pin2 = document.getElementById('pin2').value;
	var pin3 = document.getElementById('pin3').value;
	var pin4 = document.getElementById('pin4').value;
	var name = document.getElementById('fullname').value;
	var msg = '';
	
	if (pin1 == '' || pin2 == '' || pin3 == '' || pin4 == '')
	{
		msg = 'Please enter a value into each pin field';
		document.getElementById('error').innerHTML = msg;
		document.getElementById('error').style.display = 'block';
		return false;
	}
	else
	{
		var pin = pin1 + pin2 + pin3 + pin4;
	}

	if (name == '')
	{
		msg = 'Please enter a value into the name field';
		document.getElementById('error').innerHTML = msg;
		document.getElementById('error').style.display = 'block';
		return false;
	}

	var ajaxResult = 0;
	$.post("includes/ajax.php", {action : 1, pin : pin}, function(data){
	if (data.length > 0)
	{ 
		if (data == '0')
		{
			msg = 'The PIN you entered was incorrect';
			document.getElementById('error').innerHTML = msg;
			document.getElementById('error').style.display = 'block';
		}
		else
		{
			ajaxResult = 1;
		}
	}
	else
	{
		msg = 'There was an error checking your PIN, please try again';
		document.getElementById('error').value = msg;
		document.getElementById('error').style.display = 'block';
	}

	if (ajaxResult == 1)
	{
		return true;
	}
	else
	{
		return false;
	}

	});	
}

function cookieTime(tempId, tempPin, tempType)
{
	if (tempId != '' && tempPin != '')
	{
		$.post("includes/ajax.php", {action : 2, pin : tempPin, id : tempId}, function(data){
		if (data.length > 0)
		{ 
			var dataArray = data.split("|||");
			document.getElementById('name').value = dataArray[0];
			if (tempType == '1')
			{
				document.getElementById('telephone').value = dataArray[1];
			}
			else if (tempType == '2')
			{
				document.getElementById('email').value = dataArray[1];
			}
			
		}
		});
	}
	
}

function getCookie(cookieName)
{
	var i, x, y, ARRcookies = document.cookie.split(";");
	for (i = 0; i < ARRcookies.length; i++)
	{
		x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==cookieName)
		{
			return unescape(y);
		}
	}
}

function geolocate(timezone, cityPrecision, objectVar) 
{
	var api = (cityPrecision) ? "ip-city" : "ip-country";
	var domain = 'api.ipinfodb.com';
	var url = "http://" + domain + "/v3/" + api + "/?key=aa1d2709f3e9cce956e1798230407638ecd20766f19e289ca7e8224cbf05b2d2&format=json" + "&callback=" + objectVar + ".setGeoCookie";
	var geodata;
	var callbackFunc;
	var JSON = JSON || {};
 
	// implement JSON.stringify serialization
	JSON.stringify = JSON.stringify || function (obj) {
    var t = typeof (obj);
    if (t != "object" || obj === null) {
      // simple data type
      if (t == "string") obj = '"'+obj+'"';
        return String(obj);
    } else {
    // recurse array or object
      var n, v, json = [], arr = (obj && obj.constructor == Array);
      for (n in obj) {
        v = obj[n]; t = typeof(v);
        if (t == "string") v = '"'+v+'"';
        else if (t == "object" && v !== null) v = JSON.stringify(v);
        json.push((arr ? "" : '"' + n + '":') + String(v));
      }
      return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
    }
  };
 
  // implement JSON.parse de-serialization
  JSON.parse = JSON.parse || function (str) {
    if (str === "") str = '""';
      eval("var p=" + str + ";");
      return p;
  };
 
  //Check if cookie already exist. If not, query IPInfoDB
  this.checkcookie = function(callback) {
    geolocationCookie = getGeoCookie('geolocation');
    callbackFunc = callback;
    if (!geolocationCookie) {
      getGeolocation();
    } else {
      geodata = JSON.parse(geolocationCookie);
      callbackFunc();
    }
  }
 
  //API callback function that sets the cookie with the serialized JSON answer
  this.setGeoCookie = function(answer) {
    if (answer['statusCode'] == 'OK') {
      JSONString = JSON.stringify(answer);
      setCookie('geolocation', JSONString, 365);
      geodata = answer;
      callbackFunc();
    }
  }
 
  //Return a geolocation field
  this.getField = function(field) {
    try {
      return geodata[field];
    } catch(err) {}
  }
 
  //Request to IPInfoDB
  function getGeolocation() {
    try {
      script = document.createElement('script');
      script.src = url;
      document.body.appendChild(script);
    } catch(err) {}
  }
 
  //Set the cookie
  function setCookie(c_name, value, expire) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expire);
    document.cookie = c_name+ "=" +escape(value) + ((expire==null) ? "" : ";expires="+exdate.toGMTString());
  }
 
  //Get the cookie content
  function getGeoCookie(c_name) {
    if (document.cookie.length > 0 ) {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start != -1){
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end == -1) {
          c_end=document.cookie.length;
        }
        return unescape(document.cookie.substring(c_start,c_end));
      }
    }
    return '';
  }
}
