this.validations = new Array();

function register_onload()
{
    Event.observe('register-btn', 'click', register_click);
    //Event.observe('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("country", "NotEmpty", "Your country"));
    validations.push(new ValidationObject("company", "NotEmpty", "Your company name"));
    validations.push(new ValidationObject("email", "Email", "Your email address"));
    //validations.push(new ValidationObject("accept", "Checked", "Agree to terms and conditions"));
}

function sendRegistration()
{
    if(!userData.isRegistered)
	{
	    var o = null;
	    var v = null;
	    var arr = $H([]);
	    var fields = ["name", "firstname", "lastname", "email", "position", "company", "location", "country", "email-summary", "sms-alert", "mobile-area", "mobile"];
    	fields.each(function(field){
    	    v = getValue(field);
    	    if(v != null)
    	    {
    	        arr[field] = 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();
        Event.observe(img, 'load', reportSent.bindAsEventListener(this));
        Event.observe(img, 'error', reportSent.bindAsEventListener(this));
        
        var url = reportUrl + "t=r&" + "cid=" + userData.id + "&" + arr.toQueryString()+ "&i=" + new Date().getTime()+Math.random()*1000;
        img.src = url;
	}
}

function reportSent(e)
{
    if(Cookies.accept)
    {
        if(Cookies.getCookie("isRegistered") != "true")
        {
            Cookies.setCookie("isRegistered",true, 365);
            userData.isRegistered = true;	    
            window.location.reload(true);
        } 
    }
}

function getValue(id)
{
    var value = null;
    var o = null;
    if(typeof(id)=="string")
        o = $(id);
    else
        o = id;
    
    if(o != null)
    {
        try
        {
            if(o.value != null && o.value != "" && (o.type == "text" || o.type == "textbox" || o.type == "textarea"))
            {
                value = o.value.strip();
            }
            else if(o.type == "check" || o.type == "checkbox")
            {
                value = o.checked;
            }
            else if(o.options != null)
            {
                value = 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":
                        case "textarea":
                        {
                            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;
    }
}

//register onload
Event.observe(window, 'load', register_onload, false);
