function clearCookie() {
	$.cookie("medsuppghi", null);
}

function existCookie() {
	if ($.cookie("medsuppghi") != null)
		return true;
	return false;
}

function createCookie(value) {
	var date = new Date();
	date.setTime(date.getTime() + (240 * 60 * 1000)); // 4 hours
	$.cookie("medsuppghi", value, {path: '/', expires: date});
}

function getCookie() {
	var cookie = $.cookie("medsuppghi");
	var obj = jQuery.parseJSON(cookie);
	return obj;
}

function setValuesToForm(obj) {
	if (obj){
		$('input[name="affID"]').val(obj.AffId);
		$('input[name="affName"]').val(obj.AffName);
		$('input[name="affAdSource"]').val(obj.AffAdSource);
		$('input[name="ROIID"]').val(obj.ROIID);
		$('input[name="campId"]').val(obj.CampId);
		$('input[name="utm_source"]').val(obj.utmSource);
	}
}

function getParameterByName(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

function clearCookieAndSetGHIFormFromAjax(data) {
	clearCookie();
	var utmSource = getParameterByName('utm_source');
	data["utmSource"] = utmSource;
	createCookie(JSON.stringify(data));
	setValuesToForm(getCookie());
}

function setGHIFromCookie() {
	setValuesToForm(getCookie());
}

function setGHIFromAjax() {
	ajax = new AJAX('http://www.healthinsurancefinders.com/GHISessionData.aspx', 'GetAPSSessionData', 'clearCookieAndSetGHIFormFromAjax');
	ajax.doCall('', []);
}
