this.validations = new Array();

function register_onload()
{
    EventManager.Add('register-btn', 'click', register_click);
    EventManager.Add('smsalert', 'click', smsalert_click);


    //validations.push(new ValidationObject("name", "NotEmpty", "Your name"));    
    validations.push(new ValidationObject("firstname", "NotEmpty", "Your first name"));
    validations.push(new ValidationObject("lastname", "NotEmpty", "Your last name"));
    validations.push(new ValidationObject("position", "NotEmpty", "Your job title"));
    validations.push(new ValidationObject("company", "NotEmpty", "Your company name"));
    //validations.push(new ValidationObject("country", "NotEmpty", "Your country"));
    validations.push(new ValidationObject("email", "Email", "Your email address"));
    //validations.push(new ValidationObject("accept", "Checked", "Agree to terms and conditions"));
    //validations.push(new ValidationObject("location", "NotEmpty", "Your location"));

}

function sendRegistration()
{
    if(!userData.isRegistered)
	{
	    var o;
	    var v;
	    var arr = new Array();
	    var fields = ["name", "firstname", "lastname", "email", "position", "company", "location", "country", "email-summary", "sms-alert", "mobile-area", "mobile"];
    	
    	for(var i in fields)
    	{
    	    v = getValue(fields[i]);
            if(v != null)
            {
               arr[fields[i]] = v;
            }
    	}
	    
	    //custom1-customN
	    var custom = 1;
        while(custom != null)
        {  
            v = getValue("custom"+custom.toString());
            if(v != null)
            {
               arr["custom"+custom] = v;
               custom++;
            } 
            else
            {
                custom = null;
            }
        }
    	
	    //send data
	    var img = new Image();
        var url = reportUrl + "t=r&" + "cid=" + userData.id;
        url += "&" + getQueryString(arr);
        img.src = url + "&i="+new Date().getTime()+Math.random()*1000;
        //alert("Log registration: "+url);
	    
        setCookie('isRegistered', true, 365);
        userData.isRegistered = true;
	    
        initPlayer();
	}
}

function getValue(id)
{
    var value = null;
    o = document.getElementById(id);
    if(o != null)
    {
        try
        {
            if(o.value != null && o.value != "" && (o.type == "text" || o.type == "textbox" || o.type == "textarea"))
            {
                value = escape(o.value);
            }
            else if(o.type == "check" || o.type == "checkbox")
            {
                value = escape(o.checked);
            }
            else if(o.options != null)
            {
                value = escape(o.options[o.selectedIndex].innerHTML);
            } 
        }
        catch(e)
        {          
        }
    }
    return value; 
}

function register_click(e)
{
    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)
        {
            sendRegistration();
        }
        else
        {
            alert(errorMessage);
            return;
        }
    }
    else
    {
        sendRegistration();
    }
}

function register_keypress(e)
{
    if(e.keyCode == 13)
	{
		sendRegistration();
	}
}

function smsalert_click()
{
    var o = document.getElementById("smsalert");
    var smsCats = document.getElementById("smscategories");
    if(o!= null && smsCats != null)
    {       
        if(o.checked)
        {
            smsCats.style.display = "block";
        }
        else
        {
            smsCats.style.display = "none";
        }
    }
}

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 = escape(this.obj.value);
                            break;
                        }
                        case "check":
                        case "checkbox":
                        {
                            value = escape(this.obj.checked);
                            break;
                        }
                    }
                    break;
                }
                case "SELECT":
                {
                    if(this.obj.options != null && this.obj.selectedIndex != null)
                    {
                        value = escape(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;
    }
}



//register onload
EventManager.Add(window, 'load', register_onload);
