var reportMode = "f"; //can be "r" for registration or "f" for feedback
var pollDivId = "post-event-poll";
var thankYouDivId = "thank-you2";
var fields = ["comment1", "comment2"]; //other fileds like comments, name etc
var validations = new Array();

function poll_onload()
{
    var o = $('feedback-btn');
    if(o != undefined)
        Event.observe(o, 'click', submitBtn_click, false);
    
    var postPollSubmitted = (Cookies.getCookie("postPollSubmitted") == "true") ? true : false;
    showPoll(!postPollSubmitted);
    
    //validations
    //validations.push(new ValidationObject("comments", "NotEmpty", "Comment"));
}

function submitBtn_click(e)
{	
    var v;
	var arr = $H([]);
	var f = $H(fields);
	var id = 1;
	  
	//find all rb's with name q1 - qN
	for(var i=1; i<100; i++)
	{
		o = $('q'+i+'1');
		if(o != null)
		{
			id = i;
			for(var j=1; j<100; j++)
			{
				o = $('q'+i+''+j);
				if(o != null && o.type == "radio" && o.checked)
				{
					arr['q'+i] = o.value.strip();
				}
				else if(o == null)
				{
					break;
				}
			}
		}
	}
	
	//other fields
	for(var i=0; i<fields.length; i++)
	{
	    v = null;
	    o = $(fields[i]);
	    
	    if(o != null && o.value != null && o.value != "")
        {
            v = o.value.strip();
            arr[fields[i]] = v;
        }
	}
	
	//validate
	if(validations != null)
    {
        var errorMessage = "Please enter the following information:";
        var invalidCount = 0;
        for(var i=0; i<validations.length; i++)
        {
            if(!validations[i].isValid())
            {
                invalidCount++;
                errorMessage += "\n"+invalidCount+") "+validations[i].invalidMessage;
            }
        }
        if(invalidCount < 1)
        {
            submitPoll(arr);
        }
        else
        {
            alert(errorMessage);
            return;
        }
    }
    else
    {
        submitPoll(arr);
    }
}

function submitPoll(arr)
{
    if(arr.toQueryString().length > 0)
    {
        //alert(arr.toQueryString());
        if(typeof(sendFeedback) != "undefined")
        {
            sendFeedback(1, arr.toQueryString());
        }
        else
        {
            var userData = getUserData();
            if(userData != null)
            {
                var img = new Image();
                var url = reportUrl + "t=" + reportMode + "&" + "cid=" + userData.id + "&" + arr.toQueryString();
                alert(url);
                //img.src = url + "&i="+new Date().getTime()+Math.random()*1000;
            }
        }

        Cookies.setCookie("postPollSubmitted",true, 365);
        showPoll(false);
    }
}

function showPoll(show)
{
    var thankYouDiv = $(thankYouDivId);
    var pollDiv = $(pollDivId);
    
    if(thankYouDiv == undefined || pollDiv == undefined)
        return;
    
    if(show)
    {
        $(thankYouDivId).style.display = "none";
        $(pollDivId).style.display = "";
    }
    else
    {
        $(pollDivId).style.display = "none";
        $(thankYouDivId).style.display = "";  
    }
}

function getUserData()
{
	var o = new Object();
	if(Cookies.accept)
	{
		o = new Object();
		o.id = Cookies.getCookie("cid");
		if(o.id == null)
		{
			o.id = new Date().getTime()+Math.random(10000);
			Cookies.setCookie("cid", o.id, 365);
		}
		o.isRegistered = (Cookies.getCookie("isRegistered") == "true")?true:false;
		o.sentFeedback = Cookies.getCookie("sentFeedback");
		o.targetUrl = Cookies.getCookie("targetUrl");
	}
	return o;
}

//VALIDATION
function ValidationObject(obj, validationType, invalidMessage)
{
    if(obj != null && typeof(obj)=="string")
    {
        this.obj = document.getElementById(obj);
    }
    this.validationType = validationType;
    this.invalidMessage = invalidMessage;
}
ValidationObject.prototype.getValue = function()
{
    var value = null;
    if(this.obj != null)
    {
        if(this.obj.nodeName != null)
        {
            switch(this.obj.nodeName)
            {
                case "INPUT":
                {
                    switch(this.obj.type)
                    {
                        case "text":
                        case "textbox":
                        {
                            value = this.obj.value.strip();
                            break;
                        }
                        case "check":
                        case "checkbox":
                        {
                            value = this.obj.checked;
                            break;
                        }
                    }
                    break;
                }
                case "SELECT":
                {
                    if(this.obj.options != null && this.obj.selectedIndex != null)
                    {
                        value = this.obj.options[this.obj.selectedIndex].innerHTML;
                    }
                }
            }
        }
    }
    
    return value;
}
ValidationObject.prototype.isValid = function()
{
    var value = this.getValue();
    switch(this.validationType)
    {
        case "NotEmpty":
        {
            return ValidateNotEmpty(value);
            break;
        }
        case "Email":
        {
            return ValidateEmail(value);
            break;
        }
        case "Checked":
        {
            return ValidateChecked(value);
            break;
        }
        default:
        {
            return false;
        }
    }
}

function ValidateNotEmpty(value)
{
    if(value != null && value.length > 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

function ValidateEmail(value)
{
    if(ValidateNotEmpty(value))
    {
        try
        {
            var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	        if (filter.test(value))
	        {
	             return true;
	        }
	        else
	        {
	            return false;
	        }
        }
        catch(ex)
        {
            return true;
        }
    }
    else
    {
        return false;
    }
}
function ValidateChecked(value)
{
    if(value != null && (value == true || value == "true"))
    {
        return true;
    }
    else
    {
        return false;
    }
}

Event.observe(window, 'load', poll_onload, false);
