var secCount = 0;
var qFormShown = false;
var qFormHello = '<p>Hello,<br />What can I help you with today?</p>';

function adjustBg(){
	var width = $(window).width();
	var l = Math.ceil( ( width - 1192 ) / 2 );
	
	var dif = l - 408;
	
	$("#logo-container")
		.css( "background-image", "url(/images/head_back_adjust.jpg)" )
		.css( "background-position", dif + "px 0px" )
		.css( "background-repeat", "no-repeat" );
	$("body")
		.css( "background-image", "url(/images/page_container_back.jpg)" )
		.css( "background-position", dif + "px 0px" )
		.css( "background-repeat", "repeat-y" );
}

$(window).load( function(){

	adjustBg();
		
	$(window).resize( function(){
		adjustBg();
	});

	$('img#we-are-here-pic').click( function(){

	    quickContactForm();

	});

	//checkSec();

	$('form#contact-form').submit(function(){

	    var phone = $('form#contact-form input#phone').val();

	    var email = $('form#contact-form input#email').val();

	    if ( phone.length > 0 && !isPhoneNum( phone ) ){

		alert( 'Please type in a valid 10-digit phone number.' );
		$('form#contact-form input#phone').focus();
		$('form#contact-form input#phone').val("");
		return false;
		
	    }

	    if ( email.length > 0 && !isEmail( email ) ){

		alert( 'Please type in a valid email address.' );
		$('form#contact-form input#email').focus();
		$('form#contact-form input#email').val("");
		return false;
		
	    }

	    if ( !isPhoneNum( phone ) && !isEmail( email ) ){
		alert( "In order to contact you, we need at least a telepnone number or an email address.\nPlease provide at least one of the two.\nThank you.");
		$('form#contact-form input#phone').focus();
		return false;
	    }

	    return TRUE;
	});

	$('form#contact-form textarea#desc').focus();
	
});

function quickContactForm(){

    Shadowbox.open({

	content:    '<div id="we-are-here-form">' + '<div><img style="float:left;" src="http://www.amourgis.com/images/cut_serv_small.jpg" alt="" />' + qFormHello +
		    '<p>One of our attorneys is always here for you. Please fill out the following form and we will get back to you ASAP</p><div style="clear:left;"></div></div><hr />' +
		    '<form action=""><table style="width:80%; margin: auto;">' +
		    '<tr><td><label>Your Name:</label></td></tr><tr><td><input type="text" name="name" style="width:100%;" /></td></tr>' +
		    '<tr><td><label>Your Phone #</label></td></tr><tr><td><input type="text" name="phone" style="width:100%;" /></td></tr>' +
		    '<tr><td><label>Your Email Address</label></td></tr><tr><td><input type="text" name="email" style="width:100%;" /></td></tr>' +
		    '<tr><td><label>Your case has to do with</label></td></tr><tr><td><input name="regarding" style="width:100%;" /></td></tr>' +
		    '<tr><td><p style="text-align:center;"><input type="submit" name="quick_contact" value="Send" /></p></td></tr>' +
		    '</table></form>' +
		    '<br /></div>',
	player:     "html",
	title:      "We are always here for you!",
	animate: false,
	width:	350,
	height:	550
	
    });

    qFormShown = true;

}

function quickContactFormPop(){
    if ( qFormShown == false ){
	quickContactForm();
    }
}

function checkSec(){

    secCount++;

    if ( secCount < 60 ){
	//setTimeout('checkSec()', 1000 );
    } else if ( secCount == 60 ) {
	//quickContactFormPop();
    }

}


function isPhoneNum( str ){

    str = str.replace(/ /g, "");
    str = str.replace(/\(/g, "");
    str = str.replace(/\)/g, "");
    str = str.replace(/-/g, "");
    str = str.replace(/\./g, "");

    var patt = /^(1)?\d{10}$/;

    if ( patt.exec(str) ){
	return true;
    }

    return false;

}

function isEmail( str ){
    var patt = /^([a-zA-Z0-9\+_\-]+)(\.[a-zA-Z0-9\+_\-]+)*@([a-zA-Z0-9\-]+\.)+[a-zA-Z]{2,6}$/;

    if ( patt.exec(str) ){
	return true;
    }

    return false;
}
