/************************************************
 *
 *  File     :  init.js
 *  Version  :  v1.0
 *  Website  :  How to Stage Your Own Home
 *  Author   :  Kevin Birch
 *  
 *  jQuery / Site initialization
 *
 ************************************************/

$(document).ready(function(){

    // Confirmation prompt for all delete functions
    $("a.del").click(function(e) {
        e.preventDefault();
        confirmDel( $(this).attr('href'), $(this).attr('title') );
    });

    // Coming soon alert for in progress features
    $(".soon").click(function(e) {
        e.preventDefault();
        alert( 'Maintenance in progress...' );
    });

    // Initialize form controls
    jQuery.initforms();
    
    jQuery.initNewsletterForm();

});

jQuery.initforms = function() {

    // Form validation 
    $("form").submit(function(e) {
        var trigger = false;
        var emailre = /^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i;
        $(".valtip").remove();
        $("input").blur();

        $(".required",this).each ( function() {
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");
            
            if($(this).attr("value") == '') {
                $(listItem).addClass("invalid focused");
                // $(listItem).append('<div class="valtip"><span>&laquo;<span>&laquo;</span></span><p>Please fill this in</p></div>');
                trigger = true;
            }
            else {
                $(listItem).removeClass("invalid");
            }
        });

        $(".email",this).each ( function() {
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");

            if((!$(this).attr("value").match(emailre)) && ($(this).attr("value") != '')) {
                $(listItem).addClass("invalid focused");
                $(listItem).append('<div class="valtip">Invalid e-mail, try again</div>');
                trigger = true;
            } 
        });        
        
        $(".number",this).each ( function() {
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");
            if((!$(this).attr("value").match(/^\d*$/)) && ($(this).attr("value") != '')) {
                $(listItem).addClass("invalid focused");
                $(listItem).append('<div class="valtip">Input must be numbers only</div>');
                trigger = true;
            } 
        });        

        if( $(this).attr("id") == "affiliate_form" ) {
		if( $("#email" ).attr( "value" ) != $("#email-confirm" ).attr( "value" ) ) {
			alert('Email and email confirmation do not match.' );
			trigger = true;
		}
		
		if( $("#password" ).attr( "value" ) != $("#password-confirm" ).attr( "value" ) ) {
			alert('Password and password confirmation do not match.' );
			trigger = true;
		}
		}

        if(trigger ) {
            e.preventDefault();
        }

        $(".invalid > .required:first").focus();
    });

}

jQuery.initNewsletterForm = function() {

	$(".clearable").focus(function() {
		if( this.value == this.defaultValue ) {
			this.value = "";
		}
	}).blur(function() {
		if( !this.value.length ) {
			this.value = this.defaultValue;
		}
	});

	// Form validation 
    $("#newsletterform").submit(function(e) {
 
    	var trigger = false;
        var emailre = /^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i;

        if( $("#nl-name").attr( "value" ) == '' || $("#nl-name").attr( "value" ) == 'Name' ) {
        	alert( "Please enter your name.");
        	trigger = true;
        } else if( $("#nl-email").attr( "value" ) == '' ) {
        	alert( "Please enter a valid email address.");
        	trigger = true;
        } else if( !$("#nl-email").attr("value").match(emailre) ) {
        	alert( "Please enter a valid email address." );
            trigger = true;
        } 
        
        if(trigger ) {
            e.preventDefault();
        }

    });
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}