var focuser;
;(function($) {
    
        $(document).ready(function() {
        	
        $('#name').each(function()
		{
			var default_value = this.value;
			$(this).focus(function() { if(this.value == default_value) { this.value = ''; } });
			$(this).blur(function() { if(this.value == '') { this.value = default_value;  } });
		});
		$('#pl_email').each(function()
		{
			var default_value = this.value;
			$(this).focus(function() { if(this.value == default_value) { this.value = ''; } });
			$(this).blur(function() { if(this.value == '') { this.value = default_value;  } });
		});
		$('#phone').each(function()
		{
			var default_value = this.value;
			$(this).focus(function() { if(this.value == default_value) { this.value = ''; } });
			$(this).blur(function() { if(this.value == '') { this.value = default_value;  } });
		});
		
		$('#country').each(function()
		{
			var default_value = this.value;
			$(this).focus(function() { if(this.value == default_value) { this.value = ''; } });
			$(this).blur(function() { if(this.value == '') { this.value = default_value;  } });
		});
		
        $('#firstname').each(function()
		{
			var default_value = this.value;
			$(this).focus(function() { if(this.value == default_value) { this.value = ''; } });
			$(this).blur(function() { if(this.value == '') { this.value = default_value;  } });
		});
		$('#email').each(function()
		{
			var default_value = this.value;
			$(this).focus(function() { if(this.value == default_value) { this.value = ''; } });
			$(this).blur(function() { if(this.value == '') { this.value = default_value;  } });
		});
		
		
		
        $(document).keypress(function(e) {
            if (e.keyCode == 13 && $('div#light_box,div#overlay').size()) {
                hidebox();
            }
        })  
        
        $('div#banner div.become_aff').click(function() {
           show_contact_form(); 
        });  
        
        
        if (is_first_visit == '1'){
        	//show_newsletter_signup_form();
        }
   });
})(jQuery);   
jQuery.fn.ip_validate = function() {
                    var $ = jQuery;
                    var name = $(this).attr('submit_name');
                    var inputs = $(this).find(':input');
                    var errors = new Array();
                    var i = 0;
                    var arrPDValues = ["NAME*","EMAIL*"]; // literal array
                    
                    $(inputs).each(function() {
                        var error = 0;
                         var error_message = $(this).attr('error_message');
                        var ip_validations = $(this).attr('ip_validations') ?  $(this).attr('ip_validations').split(' ') : new Array();
                        var value = get_element_value($(this));       
                         var name = $(this).attr('name');
                        
                        if (error_message && value == '' || js_inarray(value, arrPDValues)) {
                            errors.push(error_message);
                            if (i==0) focuser = $(this);
                            error = 1;
                            i++;
                        }
                        
                        if (error == 0) {
                            for (j=0; j < ip_validations.length;j++) {
                                var return_val = validations[ip_validations[j]](value, $(this));
                                if (typeof return_val == 'string' || validations[ip_validations[j]](value, $(this)) != true) {
                                    var e_message = '';
                                    if (typeof return_val == 'string') {
                                        e_message = return_val;
                                    }
                                    else {
                                        var valid_message = $(this).attr('error_valid_message');
                                        e_message = (!valid_message) ? get_valid_message(error_message) : valid_message; 
                                    }
                                    if (i==0) focuser = $(this);  
                                    error = 1;
                                    errors.push(e_message); 
                                    i++;
                                    break;
                                }                     
                                
                            }
                        }
                    });
                    
                    if(errors.length > 0) {
                        showbox({name:'Please correct or check the following:', msg: errors});
                        return false;    
                    }
                    else { 
                        return true;
                    }
                };
                
function showbox(pop_options){
    var $ = jQuery;
    // Calculate the top and left position of the lightbox
        var body_width = $('body').width() - 80;
        var body_height = $(document).height();
        var d_height = $('body').height();
        var left = (body_width / 2) -  (350 / 2);
        var top = (window.pageYOffset || document.documentElement.scrollTop || 0) + 300;
        var the_height = pop_options.msg.length * 20;
    // ------------------------------------------
    
    /*var class_schedules_text = '';
    class_schedules.each(function(){
        class_schedules_text =  class_schedules_text + $(this).text() + '<br/>';
    });
    class_schedules_text = '<div style="width: 280px; margin-bottom: 10px; float: left; margin-left: 5px;">'+class_schedules_text+'</div>';
    */
    if (!pop_options) pop_options =    {};
    if (!pop_options.name) pop_options.name = 'Alert';
    var the_message = "<ul>";
    for (i=0;i<pop_options.msg.length ;i++ ) {
        the_message += "<li>" + pop_options.msg[i] + "</li>";
    }
    the_message += "</ul>";


    html = "<div class='name' style='background:url("+unijj_url+"heading.jpg) repeat-x scroll left top transparent;border:1px solid #FFFFFF;color:#0A1F62;font-size:15px;line-height:36px;'><img src='"+unijj_url+"error.png' class='error_icon'/>"+pop_options.name+"</div><div class='desc'>"+the_message+"</div>";    
    var box_content = '';
    overlay = "<div id='overlay' style='height:"+the_height+"px'></div>    <div id='light_box'><div id='box_content'>"+html+"<p class='close_button'><input type='button' name='close' value='OK'/> </p></div></div>";
    
    $('div#light_box,div#overlay').remove();
    $('body').prepend(overlay);
    $('body').prepend(box_content);
    $('div#overlay').height(body_height);
    $('div#light_box').css('left', left);
    $('div#light_box').css('top', top);
    if (pop_options.pop_button_action && typeof pop_options.pop_button_action == 'function') {
        $('div#light_box div#box_content .pop_button').unbind('click');
        $('div#light_box div#box_content .pop_button').bind('click', pop_options.pop_button_action);
    }
    $('div#light_box div#box_content .pop_image').css('padding-bottom', function() { return 240-parseInt($(this).height());});
    $('div#light_box').fadeIn('slow');
    $(".close_button input").click(function(){
        hidebox();
    });
    $(".close_button input").focus();
   
    
}

function hidebox() {
    var $ = jQuery;
        $('div#light_box,div#overlay').fadeOut('slow', function() {$(this).remove();           
                if (focuser) { 
                if ($(focuser).hasClass('editor')) {
                    id = $(focuser).attr('id').replace(/^text_area/, 'editor');
                    $('#' +id).focus();
                    setCaretToEnd($('#' +id)[0]);
                }
                else {
                    $(focuser).focus();
                }
                focuser = null;
            }});
}

function get_element_value(object) {
    var $ = jQuery;
    var value = '';
    var type = $(object).attr('type');
    var form = $(object).parents('form');
    switch (type) {
        case 'text':
        case 'password':
        case 'textarea':
            value = $(object).val();
        break;
        case 'checkbox':
        case 'radio':
            var name = $(object).attr('name');
            value = $(form).find('input[name='+name+']:checked').size();
            value = (value > 0) ? 'yes' : '';
        break;
        case 'select-one':
            value = $(object).val();
        break;   
        case 'image':
            value = '';
        break;
    
    }
    
    return value;
} 


validations = {};
validations['email'] = function(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email) 
}
validations['number'] = function(number) { 
    return !isNaN(number) 
}

validations['phone'] = function(number) { 
    var re =  /^[\(\)\ 0-9\-\.]+$/;
    return re.test(number); 
}

validations['zip'] = function(zip_code) { 
    var re = /^\d{5}([\-]\d{4})?$/;
    return re.test(zip_code); 
}

validations['url'] = function(url) { 
    var re = /^((http|https):\/\/).*$/;
    return re.test(url); 
}

validations['password'] = function(value, object) { 
    var $ = jQuery;
    if ($(object).size()) {
        var name = $(object).attr('name');
        var confirm_object = $(object).parents('form').find('input[name='+name+'_confirm]');
        
        if ($.trim(value) == '') return true;
        if (value.length < 6) return "Password needs to be minimum of 6 Characters";
        
        if ($.trim($(confirm_object).val()) == '') return "Please confirm your password";
        if ($(confirm_object).val() != value) return "Passwords do not match";
    }
    
    
    return true; 
}


function show_contact_form() {
    var $ = jQuery
    var body_width = $('body').width() - 80;
    var body_height = $(document).height();
    var d_height = $('body').height();
    var left = (body_width / 2) -  ($('div#contact_form').width() / 2)+40;
    var top = (window.pageYOffset || document.documentElement.scrollTop || 0) + 225;

    $('div#light_box_contact,div#overlay_contact').remove();  
    
    var overlay = "<div id='overlay_contact' style='height:"+body_height+"px'></div>";
    $('body').prepend(overlay);  
    $('div#contact_form').css('left', left);
    $('div#contact_form').find(':input').find()
    $('div#contact_form').css('top', top); 
    $('div#contact_form').find('form')[0].reset();
    $('div#contact_form').find('div.contact_content_thank_you').hide();
    $('div#contact_form').find('div.contact_content').show();
    
    $('div#contact_form').fadeIn('slow', function() {
    	$(this).find('input[name=name]').focus();
    });     
    $('div#contact_form').find('div.close_button').unbind('click').bind('click', function() {
        if ($('div#overlay').size() == 0) {
            $('div#overlay_contact').remove();
            $('div#contact_form').fadeOut();
        }
    });
     $('div#contact_form').find('input#thank_you_close').unbind('click').bind('click', function() {
        if ($('div#overlay').size() == 0) {
            $('div#overlay_contact').remove();
            $('div#contact_form').fadeOut();
        }
    });
    
    $('div#contact_form img.submit_btn').unbind('click').bind('click', function() {
        if ($('div#contact_form form').ip_validate()) {
            var data = $('div#contact_form form').serialize();
            $.post(unijj_ajax_url, {action:'send_mail', data: data}, function(response) {
                  $('div#contact_form').find('div.contact_content').hide(); 
                  $('div#contact_form').find('div.contact_content_thank_you').show();
                
            });
        }  
    })
}

function show_newsletter_signup_form() {
    var $ = jQuery
    var body_width = $('body').width() - 80;
    var body_height = $(document).height();
    var d_height = $('body').height();
    var left = (body_width / 2) -  ($('div#newsletter_signup').width() / 2)+40;
    var top = (window.pageYOffset || document.documentElement.scrollTop || 0) + 225;
         
    $('div#light_box_contact,div#overlay_newsletter').remove();  
    
    var overlay = "<div id='overlay_newsletter' style='height:"+body_height+"px'></div>";
    $('body').prepend(overlay);  
    $('div#newsletter_signup').css('left', left);
    $('div#newsletter_signup').find(':input').find()
    $('div#newsletter_signup').css('top', top); 
    $('div#newsletter_signup').find('form')[0].reset();
    $('div#newsletter_signup').find('div.newsletter_content_thank_you').hide();
    $('div#newsletter_signup').find('div.newsletter_content').show();
    
    $('div#newsletter_signup').fadeIn('slow', function() {
    	$(this).find('input[name=firstname]').focus();
 	
    });     
    $('div#newsletter_signup').find('div.close_button').unbind('click').bind('click', function() {
        if ($('div#overlay').size() == 0) {
            $('div#overlay_newsletter').remove();
            $('div#newsletter_signup').fadeOut();
        }
    });
     $('div#newsletter_signup').find('input#thank_you_close').unbind('click').bind('click', function() {
        if ($('div#overlay').size() == 0) {
            $('div#overlay_newsletter').remove();
            $('div#newsletter_signup').fadeOut();
        }
    });
    
    $('div#newsletter_signup img.submit_btn').unbind('click').bind('click', function() {
        if ($('div#newsletter_signup form').ip_validate()) 
        {
			if (check_cdfs(document.survey)) {
				//window.open('','signup','resizable=1,scrollbars=0,width=300,height=150');
                $('div#newsletter_signup').find('div.newsletter_content').hide(); 
                $('div#newsletter_signup').find('div.newsletter_signup_thank_you').show();
				return true;
			}
			else 
			{  alert('false');
				return false; 
			}
        }  
    })
}

function check_cdfs(form) {	return true; }

function js_inarray(strValue, arrDataHolder)
{
	for (var i=0;i<arrDataHolder.length;i++)
	{
		if (strValue == arrDataHolder[i])
		{
			return 1
		}
	}
	return 0;
}
