
var valid = 365;
var day = 86400;
var cn = "___srv";


		function sendVote(formId, surveyDivId, surveyId, templateName) {
			var theForm = document.getElementById(formId);
			jQuery(theForm).submit(function() { 
			  return false;
			});
			jQuery(theForm).ajaxSubmit();
			var e = document.getElementById(surveyDivId);
			if (e) {
				data = {}
				data.id = surveyId;
				showResults(data, e.parentNode.id, surveyId, templateName);
			}
		}
	

function errorHandler(type, error) {
}

function showResults(data, surveyDivId, surveyId, templateName) {
    if (readCookie(cn)) {
        cleanUpCookie(readCookie(cn));
    }
    var temp;
    var now = new Date();
    var date = Math.round(now.getTime() / 1000);
    var str = data.id + "-" + date;
    if (readCookie(cn)) {
        temp = readCookie(cn);
        temp += "," + str;
    } else {
        temp = str;
    }
    createCookie(cn, temp, valid);
	loadSurveyWithTemplate(surveyDivId, surveyId, templateName)
}




function loadSurvey(surveyDivId, surveyId) {
	loadSurveyWithTemplate(surveyDivId, surveyId, 'surveys/survey');
}

function loadSurveyWithTemplate(surveyDivId, surveyId, templateName, forwardDestination) {
  var cookie = checkCookie(surveyId);
  var url = decodeURIComponent(displayURL) + "surveyId=" + surveyId + "&template=" + templateName;
  if (cookie) {
    url += "&cookie=true";
  }
  if (forwardDestination) {
    url += "&forwardDestination=" + encodeURIComponent(forwardDestination);
  }
  jQuery.ajax({
    url: url,
	cache: false,
    success: function(data) {
      showSurvey(data, surveyDivId);
    }
  });
}

function showSurvey(data, surveyDivId) {
	$("#"+surveyDivId).html(data);
}

function cleanUpCookie(val) {
    var lines = val.split(",");
    var temp;
    var resval;
    var now = new Date();
    var date = Math.round(now.getTime() / 1000);
    var result = new Array();
    for (var i = 0; i < lines.length; i++) {
        temp = lines[i].split("-");
        if (temp[1] > date - (valid * day)) {
            result.push(lines[i]);
        }
    }
    if (lines.length != result.length) {
        if (result[0]) {
            resval = result[0];
        }
        for (var i = 1; i < result.length; i++) {
            resval += "," + result[i];
        }
        createCookie(cn, resval, valid);
    }
}

function checkCookie(id) {
    if (readCookie(cn)) {
        var lines = readCookie(cn).split(",");
        for (i = 0; i < lines.length; i++) {
            temp = lines[i].split("-");
            if (temp[0] == id) {
                return true;
            }
        }
    }
    return false;
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else {
        var expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1,c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}

var displayURL = encodeURIComponent("/surveys/survey?");




