var ajax_url = "/includes/ajax.php";
var testimonialsTimer = false;
var currentTestimonial = false;

function getDim()
{
    var result = {width:0, height:0};

    if (typeof window.innerWidth != 'undefined')
    {
        result.width = window.innerWidth,
        result.height = window.innerHeight
    }
    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth !='undefined' && document.documentElement.clientWidth != 0)
    {
        result.width = document.documentElement.clientWidth,
        result.height = document.documentElement.clientHeight
    }
    
    return result;
    
    //return document.viewport.getDimensions();  // standard Prototype solution stoped working properly at the new FF ver.
}

function sendContactUs(event)
{
    var errors = validate('contact_form');
    
    if (errors)
    {
        var html = "";
        Event.stop(event);
        
        $('message').firstDescendant().innerHTML = "Please fix the errors in your form";
        $('message').addClassName('error_box');
        
        errors.each(function(item){html += "<li>"+item+"</li>"}); 
        
        $('errors').innerHTML = html;              
    } 
    else
    {
        var data = $('contact_form').serialize(true);

        data['f'] = 'sendContactUs';
        
        new Ajax.Request(ajax_url, 
        {
            method: 'post',
            parameters: data,
            onSuccess: function(req) 
            {  
            }
        });
        
        $('contact_box').innerHTML = "<div style='font-weight:bold'>Thank you.</div><br>Your request was sent succesfuly." ;    
    }   
}

function sendLead()
{
    /*var data = $('contact_form').serialize(true);

    data['f'] = 'sendContactUs';
    
    new Ajax.Request(ajax_url, 
    {
        method: 'post',
        parameters: data,
        onSuccess: function(req) 
        {  
        }
    }); */
    
    $('signup_box').innerHTML = "<div style='font-weight:bold'>Thank you.</div><br>Your request was sent succesfuly."
}

function validate(form)
{
    var elements = $(form).getElements();
    var err_stack = [];
    var err_index = {};
    
    var email_tpl = /^[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)$/i;
    var url_tpl = /^(https?:\/\/)?(([\w!~*\'().&=+$%-]+: )?[\w!~*\'().&=+$%-]+@)?((\d{1,3}\.){3}\d{1,3}|([\w!~*\'()-]+\.)*([\w^-][\w-]{0,61})?[\w]\.[a-z]{2,6})(:\d{1,4})?((\/*)|(\/+[\w!~*\'().;?:@&=+$,%#-]+)+\/*)$/i;
    var phone_tpl = /^[0-9\-\+]+$/i;
    var number_tpl = /^\d+(\.\d+)?$/i;
    var date_tpl = /^(0[1-9]|[12][0-9]|3[01])([\-\.\/\\])(0[1-9]|1[012])\2(19|20\d\d)$/;
    var month_tpl = /^0[1-9]|1[012]$/;
    var year_tpl = /^1[0-9]$/;
    var cb_tpl = /[1]/;
    var id_tpl = /^[0-9]{8,9}$/;
    var cvv_tpl = /^[0-9]{3}$/;
    
    function unify(element)
    {
        if (!err_index[element.name])
        {
            element.addClassName('validation_error');
            err_stack.push(element.getAttribute('err'));
            err_index[element.name] = true;
        }    
    }
    
    elements.each(function(element)
    {
        if (element.hasClassName('_validate'))
        {
            var value = element.value;
            
            element.removeClassName('validation_error');
            
            if (element.hasClassName('_require') && (((element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') && value.blank()) || (element.tagName == 'SELECT' && value == 0))) unify(element);        
            else if (element.hasClassName('_require_cb') &&  !cb_tpl.exec(value)) unify(element);   
            else if (element.hasClassName('_phone') && !phone_tpl.exec(value)) unify(element); 
            else if (element.hasClassName('_email') && !email_tpl.exec(value)) unify(element); 
            else if (element.hasClassName('_number') && !number_tpl.exec(value)) unify(element); 
            else if (element.hasClassName('_id') && !id_tpl.exec(value)) unify(element); 
            else if (element.hasClassName('_cvv') && !cvv_tpl.exec(value)) unify(element); 
            else if (element.hasClassName('_month') && !month_tpl.exec(value)) unify(element); 
            else if (element.hasClassName('_year') && !year_tpl.exec(value)) unify(element); 
        }
    });
    return (err_stack.length > 0) ? err_stack : false;    
}

function sendAdvertizersSignUp(form, event)
{
    var errors = validate(form);
    
    if (errors)
    {
        var html = "";
        Event.stop(event);
        
        $('message').firstDescendant().innerHTML = "Please fix the errors in your form";
        $('message').addClassName('error_box');
        
        errors.each(function(item){html += "<li>"+item+"</li>"}); 
        
        $('errors').innerHTML = html;              
    } 
    else
    {
        var data = $('signup_form').serialize(true);

        data['f'] = 'sendAdvertizersSignUp';
        
        new Ajax.Request(ajax_url, 
        {
            method: 'post',
            parameters: data,
            onSuccess: function(req) 
            {  
            }
        });
        $('signup_box').innerHTML = "<div style='font-weight:bold'>Thank you.</div><br>Your request was sent succesfuly."        
    }   
}

function fieldEffect(element, focus)
{
    var defaultValue = element.getAttribute('default')
    
    if (focus)
    {    
        if (element.value==defaultValue)
        {
            element.value = "";
            element.style.color = "#444444";
        }
    }
    else
    {
        if (element.value=="")
        {
            element.value = defaultValue;
            element.style.color = "#A8C1CF";    
        }    
    }
}

function login()
{
    $('message').firstDescendant().innerHTML = "The <strong>email</strong> or <strong>password</strong> use suplied is incorrect. <br><br>Please try again!";
    $('message').addClassName('error_box');    
}

function rotateTestimonials()
{
    testimonialsTimer = setInterval(function()
    {
        currentTestimonial = (currentTestimonial < testimonials.length-1) ? currentTestimonial+1 : 0;
        
        //console.log(currentTestimonial);
        animateTestimonials();                                                            
    }, 9000);     
}

function animateTestimonials()
{
    var item = $('testimonial'); 
    
    new Effect.Parallel(
    [
      new Effect.Move(item, { sync: true, x: -100 }), 
      new Effect.Opacity(item, { sync: true, from: 1, to: 0 }) 
    ], 
    {
        duration: 0.3,
        afterFinish: function()
        {                               
            $('testimonial_text').innerHTML = testimonials[currentTestimonial].text; 
            $('testimonial_name').innerHTML = testimonials[currentTestimonial].name; 
            item.style.left = '100px';
            
            new Effect.Parallel(
            [
              new Effect.Move(item, { sync: true, x: -100 }), 
              new Effect.Opacity(item, { sync: true, from: 0, to: 1 }) 
            ], {duration: 0.3});    
        }
    });    
}

window.onload = function()
{
    
}


