function VoteList() {}

VoteList.vl = null;
VoteList.vt = null;
VoteList.vc = null;
VoteList.vi = null;
VoteList.vd = null;
VoteList.vid = null;
// Name-value separator
VoteList.nvs = "$$:$$";
// Attribute separator
VoteList.as = "$$/$$";
VoteList.re = new RegExp("^T[0-9]{2}$");

VoteList.CONFIRM_VOTE = "Confirm your votes &#187;";
VoteList.NO_IDEAS = "No ideas added.";
VoteList.IDEA_SING = "idea";
VoteList.IDEA_PLUR = "ideas";

VoteList.init = function() {
	VoteList.vl = document.getElementById("vbox_selected_submit");
	VoteList.vt = document.getElementById("vote-title");
	VoteList.vc = document.getElementById("vote-cat");
	VoteList.vi = document.getElementById("vote-image");
	VoteList.vd = document.getElementById("vote-desc");
	VoteList.vid = document.getElementById("Vote");
	VoteList.buildList();
}

VoteList.isIdea = function(id) {
	return VoteList.re.test(id);
}

VoteList.addListEntry = function(id, lang, ideaName, ideaDesc, cat, imageLink) {
	if (VoteList.getCookie(id)) {
		return;
	}
	VoteList.emptyCart();
	VoteList.addIdea(id, {name:ideaName, desc:ideaDesc, lang:lang, cat:cat, imageLink:imageLink});
}

VoteList.buildListHTML = function(listNum, id, ideaLang, ideaName, ideaDesc, cat, imageLink) {
	VoteList.vt.innerHTML = ideaName;
	VoteList.vc.innerHTML = cat;
	VoteList.vc.className = "categories " + cat;
	VoteList.vd.innerHTML = ideaDesc;
	VoteList.vi.setAttribute("src", imageLink);
	VoteList.vid.setAttribute("value", id);
}

VoteList.checkCookie = function() {
	var loc = window.location.toString();
	if (loc.indexOf("submit") == -1) {
		return;
	}
	
	var ca = document.cookie.split("; ");
	
	var count = 0;
	for (c in ca) {
		var vals = ca[c].split("=");
		id = vals[0];
		//alert(id);
		if (VoteList.isIdea(id)) {
			if (VoteList.getIdea(id, "lang").toLowerCase() != VoteList.getCookie("django_language")) {
				VoteList.removeIdea(id);
				continue;
			}
			count++;
		}
	}
	if (count == 0) {
		window.location = "vote.html";
	}
}

VoteList.buildList = function() {
	var ca = document.cookie.split("; ");
	
	var count = 0;
	for (c in ca) {
		var vals = ca[c].split("=");
		id = vals[0];
		//alert(id);
		if (VoteList.isIdea(id)) {
			VoteList.buildListHTML((count + 1), id, VoteList.getIdea(id, "lang"), VoteList.getIdea(id, "name"), VoteList.getIdea(id, "desc"), VoteList.getIdea(id, "cat"), VoteList.getIdea(id, "imageLink"));
			count++;
		}
	}
	if (count == 0) {
	}
}

VoteList.emptyCart = function() {
	var ca = document.cookie.split("; ");
	for (c in ca) {
		var vals = ca[c].split("=");
		id = vals[0];
		if (VoteList.isIdea(id)) {
			VoteList.removeIdea(id);
		}
	}
}

VoteList.buildCheckoutHTML = function() {
}

// Returns null if no valid param is available from the URL
VoteList.getParamFromUrl = function(param) {
	var query = null;
	if (window && window.location) {
		query = window.location.search;
	}
	else {
		return null;
	}

	var paramEQ = param + "=";
	if (query.indexOf(paramEQ) != -1) {

		var args = query.split("&");
		for (arg in args) {
			if (args[arg].indexOf(paramEQ) != -1) {
				return args[arg].substring(args[arg].indexOf("=") + 1, args[arg].length);
				//alert(id);
			}
		}
	}
	else {
		return null;
	}
}

VoteList.setCookie = function(id, val, days, path) {
	//alert("Setting cookie " + id + " to: " + val);
	try {
		var expires='';
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}
		var thePath = '; path=/';
		if (path) {
			thePath = '; path=' + path;
		}
		document.cookie = id+'='+escape(val)+expires+thePath;
	}
	catch (e) {
		alert(e);
	}
}

VoteList.getCookie = function(cookieName) {
	var nameEQ = cookieName + '=';
	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 unescape(c.substring(nameEQ.length, c.length));
		}
	}
	return null;
}

VoteList.clearCookie = function(id) {
	VoteList.setCookie(id, "", -1);
}

VoteList.addIdea = function(id, ideaObj, days, path) {
	var val = "";
	for (idea in ideaObj) {
		val += idea + VoteList.nvs + ideaObj[idea] + VoteList.as;
	}
	val = val.substring(0, val.length-VoteList.as.length);
	//alert("Setting idea " + id + " to " + val);
	//document.cookie = id + "=" + escape(val);
	try {
		VoteList.setCookie(id, val);
	}
	catch (e) {
		alert(e);
	}
}

// Returns attribute referenced by attrName or null if it does not exist
// Returns ideaObject if no attrName specified
VoteList.getIdea = function(id, attrName) {
	var val = VoteList.getCookie(id);
	if (!val) {
		return null;
	}
	var attrs = val.split(VoteList.as);
	var ideaObj = {};
	for (var i=0; i < attrs.length; i++) {
		var attr = attrs[i].split(VoteList.nvs);
		ideaObj[attr[0]] = attr[1];
	}
	if (attrName != undefined) {
		if (attrName in ideaObj) {
		  return ideaObj[attrName];
		}
		return null
	}
	return ideaObj;
}

VoteList.removeIdea = function(id) {
	VoteList.clearCookie(id);
	//VoteList.countDown();
}

VoteList.validateVoteForm = function(f) {
	var valid = true;
	//var alertString = "Please complete the following fields:\n";
	//if (f.EmailAddress.value == "" || f.EmailAddress.value == null) {
	if(helps_utils.isFieldEmpty(f.EmailAddress)) {

		//alertString += "Email address";
		valid = false;
	}
	if (Recaptcha.get_response() == "") {
		valid = false;
		document.getElementById("feedback_captcha").style.color = "#F00";
	} else {
		document.getElementById("feedback_captcha").style.color = "#000";
	}
	
	if(!VoteList.validateEmail(f.EmailAddress.value)) {
    document.getElementById("EmailAddressLabel").style.color = "#F00";
		return false;
  } else {
    document.getElementById("EmailAddressLabel").style.color = "#000";
  }

	
	if (!valid) {
		//alert(helps_utils.EN_FIELD_EMPTY_MSG);
		return false;
	}
	return true;
}

VoteList.validateOrgForm = function(f) {
	var valid = true;
	if(helps_utils.isFieldEmpty(f.OrgName)) {
		valid = false;
	}
	if(helps_utils.isFieldEmpty(f.OrgMail)) {
		valid = false;
	}
	if(helps_utils.isFieldEmpty(f.OrgPhone)) {
		valid = false;
	}
	if(helps_utils.isFieldEmpty(f.OrgWebsite)) {
		valid = false;
	}
	if(helps_utils.isFieldEmpty(f.OrgEmail)) {
		valid = false;
	}
	var count = 0;
	if (document.getElementById("OrgWorkYes").checked == true) {
    count++;
  }
	if (document.getElementById("OrgWorkNo").checked == true) {
    count++;
  }
  if (count == 0) {
		valid = false;
    document.getElementById("OrgWorkLabel").style.color = "#F00";
  } else {
    document.getElementById("OrgWorkLabel").style.color = "#000";
  }
	if(helps_utils.isFieldEmpty(f.OrgYourName)) {
		valid = false;
	}
		
	if (!valid) {
		//alert(helps_utils.EN_FIELD_EMPTY_MSG);
		return false;
	}
	
	if(!VoteList.validateEmail(f.OrgEmail.value)) {
    document.getElementById("OrgEmailLabel").style.color = "#F00";
		//alert(helps_utils.EN_VALID_EMAIL_MSG)
		return false;
  } else {
    document.getElementById("OrgEmailLabel").style.color = "#000";
  }
	return true;
}

VoteList.checkEmail = function(email) {
	if (!Vote.validateEmail(email)) {
		Vote.validEmail = false;
		return;
	}
	else {
		//alert("Valid email!");
		Vote.validEmail = true;

		if (document.getElementById("recaptchaContainer").style.display == "none") {
			Recaptcha.create(Vote.recaptchaKey, "recaptchaDiv", Vote.recaptchaOptions);
			document.getElementById("recaptchaContainer").style.display = "block"
			return;
		}
	}
}

VoteList.validateEmail = function(email) {
  
  if (email.indexOf("@") > 0) {
    var ss = email.substring(email.indexOf("@")+1, email.length);

    if (ss.indexOf(".") > 0) {
      var ss2 = ss.substring(ss.indexOf(".")+1, ss.length)
      if (ss2.length > 1) {
        //helps_utils.setLabelOk(field);
        //helps_utils.clearFieldFeedback("feedback_" + field.name);
        return true;
      }
    }
  }
  //helps_utils.setLabelError(field);
  //helps_utils.setFieldFeedback("feedback_" + field.name, helps_utils.EN_VALID_EMAIL_MSG);
  return false;
}

//VoteList.checkCookie();
