survey = {
    // base title is used to form the title string and is either the service name or capitalized phrase like the 
    baseTitle : 'Copac',
    serviceName : 'Copac',
	currentPage : 1,
	finalPage: 11,
    loadPopup:function(){  
    	//loads popup only if it is disabled
    	if(popupStatus==0){  
    		$("#backgroundPopup").css({  
    			"opacity": "0.7"  
    		});
    		$("#backgroundPopup").fadeIn("slow");  
    		$("#surveyPopup").fadeIn("slow");  
    		popupStatus = 1;  
    	}  
    },
    disablePopup:function(){
    	//disables popup only if it is enabled  
    	if(popupStatus==1){  
    		$("#backgroundPopup").fadeOut("slow");
    		$("#surveyPopup").fadeOut("slow");  
    		popupStatus = 0;
    	}  
    },
    centerPopup:function(){  
		//request data for centering
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		var popupHeight = $("#surveyPopup").height();  
		var popupWidth = $("#surveyPopup").width();
		//centering  
		$("#surveyPopup").css({
		    "position": "absolute",
		    "top": windowHeight/2-popupHeight/2,
		    "left": windowWidth/2-popupWidth/2
		});
		//only need force for IE6
		$("#backgroundPopup").css({  
		    "height": windowHeight,
		    "width": windowWidth
		});  
    },
    proceed:function() {
    	var srchStr="survey=1";
    	return document.cookie.indexOf(srchStr) == -1;
    },
    changePage:function(fromPage, toPage) {
		hidePage = "#surveyPage-" + fromPage.toString();
		showPage = "#surveyPage-" + toPage.toString();
		$(hidePage).hide();
		$(showPage).show();
    },
	checkPage:function(page) {
		var group = new Array();
		var filled = false;
		group[1] = "locationGroup";
		group[2] = "sectorGroup";
		group[3] = "roleGroup";
		group[5] = "frequencyGroup";
		group[6] = "easeGroup";
		group[7] = "usefulGroup";
		group[8] = "recommendGroup";
		if (typeof(group[page]) == "undefined") {
			filled = true
		} else {
			var value = $("input[name=" + group[page] + "]:checked").val();
			filled = (typeof(value) != "undefined");
		}
		if (!filled) {
    		alert("Please select one of the options.");
		}
		return filled;
	},
    setCookies:function(tryLater) {
		var expiryDate = new Date();
		if (tryLater) {
			expiryDate.setHours(expiryDate.getHours() + 24)
				} else {
			expiryDate.setFullYear(2012,3,1);
		}
		document.cookie = 'survey=1; expires=' + expiryDate.toGMTString() + '; path=/'
    },
    submitResults:function() {
		answers = {
			// Page One
			'locSelect' : $("input[name=locationGroup]:checked").val(),
			'locOther' : $.trim($("#otherLocation").val()).replace(/\n/g,"<br/>"),

			// Page Two
			'sectSelect' : $("input[name=sectorGroup]:checked").val(),
			'sectOther' : $.trim($("#otherSector").val()).replace(/\n/g,"<br/>"),

			// Page Three
			'roleSelect' : $("input[name=roleGroup]:checked").val(),
			'roleOther' : $.trim($("#otherRole").val()).replace(/\n/g,"<br/>"),

			// Page Four
			'subj-1' : $("input[name=subjArea1]").attr('checked') == true ? 'Y' : 'N',
			'subj-2' : $("input[name=subjArea2]").attr('checked') == true ? 'Y' : 'N',
			'subj-3' : $("input[name=subjArea3]").attr('checked') == true ? 'Y' : 'N',
			'subj-4' : $("input[name=subjArea4]").attr('checked') == true ? 'Y' : 'N',
			'subj-5' : $("input[name=subjArea5]").attr('checked') == true ? 'Y' : 'N',
			'subj-6' : $("input[name=subjArea6]").attr('checked') == true ? 'Y' : 'N',
			'subj-7' : $("input[name=subjArea7]").attr('checked') == true ? 'Y' : 'N',
			'subj-8' : $("input[name=subjArea8]").attr('checked') == true ? 'Y' : 'N',
			'subj-9' : $("input[name=subjArea9]").attr('checked') == true ? 'Y' : 'N',
			'subj-10' : $("input[name=subjArea10]").attr('checked') == true ? 'Y' : 'N',
			'subj-11' : $("input[name=subjArea11]").attr('checked') == true ? 'Y' : 'N',    		
			'subj-12' : $("input[name=subjArea12]").attr('checked') == true ? 'Y' : 'N',
			'subj-13' : $("input[name=subjArea13]").attr('checked') == true ? 'Y' : 'N',
			'subj-14' : $("input[name=subjArea14]").attr('checked') == true ? 'Y' : 'N',
			'subj-15' : $("input[name=subjArea15]").attr('checked') == true ? 'Y' : 'N',
			'subj-16' : $("input[name=subjArea16]").attr('checked') == true ? 'Y' : 'N',
			'subj-17' : $("input[name=subjArea17]").attr('checked') == true ? 'Y' : 'N',
			'subj-18' : $("input[name=subjArea18]").attr('checked') == true ? 'Y' : 'N',
			'subj-19' : $("input[name=subjArea19]").attr('checked') == true ? 'Y' : 'N',
			'subj-20' : $("input[name=subjArea20]").attr('checked') == true ? 'Y' : 'N',
			'subj-21' : $("input[name=subjArea21]").attr('checked') == true ? 'Y' : 'N',
			'subj-22' : $("input[name=subjArea22]").attr('checked') == true ? 'Y' : 'N',
			'subj-23' : $("input[name=subjArea23]").attr('checked') == true ? 'Y' : 'N',
			'subj-24' : $("input[name=subjArea24]").attr('checked') == true ? 'Y' : 'N',
			'subj-25' : $("input[name=subjArea25]").attr('checked') == true ? 'Y' : 'N',
			'subj-26' : $("input[name=subjArea26]").attr('checked') == true ? 'Y' : 'N',
			'subjOther' : $.trim($("#otherSubjectArea").val()).replace(/\n/g,"<br/>"),

			// Page Five
			'freq' : $("input[name=frequencyGroup]:checked").val(),

			// Page Six
			'ease' : $("input[name=easeGroup]:checked").val(),

			// Page Seven
			'useful' : $("input[name=usefulGroup]:checked").val(),

			// Page Eight
			'recom' : $("input[name=recommendGroup]:checked").val(),

			// Page Nine
			'valueAns' : $.trim($("#valueText").val()).replace(/\n/g,"<br/>"),

			// Page Ten
			'changeAns' : $.trim($("#changeText").val()).replace(/\n/g,"<br/>"),

			// Page Eleven
			'email' : $.trim($("#email").val()).replace(/\n/g,"<br/>")
		};

		$.post("bin/survey-2012.py", answers, function(data){alert(data);}, "json");
    },
	displaySurvey:function(invokedAfterLoad) {
        $('span.service-name').text(survey.serviceName);
	    $("#surveyTitle").text(survey.baseTitle + ' User Satisfaction Survey 2012');
	    $('span.zetoc').show();
	    if (survey.proceed() || !invokedAfterLoad) {
			if (!invokedAfterLoad) {
				// Reinitialise
				$("#surveyIntro").show();
				$("#navControl").hide();
				for (page = 1; page <= survey.finalPage; page++) {
					pageID = "#surveyPage-" + page.toString();
					$(pageID).hide();
				}
			}
			survey.centerPopup();
			survey.loadPopup();
		}
	},
	initialise:function() {
        // Navigation.
        $("#acceptButton").click(function() {
                survey.currentPage = 1;
		$("#surveyTitle").text(survey.baseTitle + ' Survey - About You');
        	$("#surveyIntro").hide();
			$("#surveyPage-1").show();
			$("#navControlPrev").hide();
			$("#navControlNext").show();
	    	$("#navControl").show();
			$("#submitButton").hide();
        });
        $("#navControlPrev").click(function() {
	        if (survey.currentPage > 1) {
			    survey.changePage(survey.currentPage, --survey.currentPage);
	        }
	        if (survey.currentPage == 1) {
			    $("#navControlPrev").hide();
	        }
	        if (survey.currentPage == 4) {
                    $("#surveyTitle").text(survey.baseTitle + ' Survey - About You');
                }
	        if (survey.currentPage == survey.finalPage-1) {
    	                    $("#surveyTitle").text(survey.baseTitle + ' Survey - Using the service');
			    $("#navControlNext").show();
			    $("#submitButton").hide();		
	        }
        });
        $("#navControlNext").click(function() {
		    if(survey.checkPage(survey.currentPage)) {
			    if (survey.currentPage < survey.finalPage) {
				    survey.changePage(survey.currentPage, ++survey.currentPage);
			    }
			    if (survey.currentPage == 2) {
				    $("#navControlPrev").show();
   			    }
                            if (survey.currentPage == 5) {
    	                        $("#surveyTitle").text(survey.baseTitle + ' Survey - Using the service');
                            }

			    if (survey.currentPage == survey.finalPage) {
                                    $("#surveyTitle").text(survey.baseTitle + ' User Satisfaction Survey 2012');
				    $("#navControlNext").hide();
	   			    $("#submitButton").show();
   			    }
		    }
	    });

		// Close the popup.
		$("#declineButton").click(function() {
		    survey.setCookies(true);
			survey.disablePopup();
		});
        $("#laterButton").click(function() {
		    survey.setCookies(true);
	        survey.disablePopup();
        });
        $("#cancelButton").click(function() {
	        survey.setCookies(false);
	        survey.disablePopup();
        });
        $("#submitButton").click(function() {
	        survey.submitResults();
	        survey.setCookies(false);
	        survey.disablePopup();
		$("input:radio").attr("checked", false);
		$("input:checkbox").attr("checked", false);
		$("input:text").val("");
		$("textarea").val("");

        });
        $("#surveyPopupClose").click(function(){  
	        survey.disablePopup();
        });  
        $("#backgroundPopup").click(function(){  
	        survey.disablePopup();
        });
	}
}
popupStatus=0;

//Press Escape event!  
$(document).keypress(function(e){  
    if(e.keyCode==27 && popupStatus==1){  
		survey.disablePopup();
    }  
});

$(document).ready(function() {
	survey.initialise();
	survey.displaySurvey(true);
});

