if(document.domain == 'localhost')
	var BASE_URL = 'http://localhost/scottdisick.com';
else
	var BASE_URL = 'http://www.scottdisick.com';
$(document)
	.ready(function(){
		//focus on first element of the page
		$('body').find('input[type="text"]:first').select();
		//numbers only fields
		$('.numbers-only').keyup(function () { 
			this.value = this.value.replace(/[^0-9\.]/g,'');
		});
		//phone fields auto focus
		$('.phone-fields').find(':input').keyup(function(){
			name = $(this).attr('name');
			if(name.charAt(name.length-1) != 3 && this.value.length == 3)
				$(this).next().focus();
		})
	});
	
function isValidEmail(emailAddress) {
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(emailAddress);
}
	
function isValidURL(url){ 
    var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
    var bool = false;
	if(RegExp.test(url))
        bool = true; 
	return bool;
}
	
function refreshChat(limit){
	setTimeout("refreshChat();",60000);
	$.get(BASE_URL+'/online.php',{key: Math.random(), limit: limit},
	function(data){
		if(data.result == 1)
			$('.chat-list').html(data.msg);
		else
			$('.chat-list').html('');
	},"json");
	return false;
}

function refreshNotification(){
	setTimeout("refreshNotification();",15000);
	$.get(BASE_URL+'/notifications.php',{key: Math.random()},
	function(data){
		if(data.result == 1)
			$('.user-notification-container').html(data.msg);
		else
		$('.user-notification-container').html('');
	},"json");
	return false;
}

function submitForm(value,form){
	if(value != '' && value.length > 0)
		form.submit();		
}
	
(function($) {
  $.fn.breakWords = function() {
    this.each(function() {
      if(this.nodeType !== 1) { return; }

      if(this.currentStyle && typeof this.currentStyle.wordBreak === 'string') {
        //Lazy Function Definition Pattern, Peter's Blog
        //From http://peter.michaux.ca/article/3556
        this.runtimeStyle.wordBreak = 'break-all';
      }
      else if(document.createTreeWalker) {

        //Faster Trim in Javascript, Flagrant Badassery
        //http://blog.stevenlevithan.com/archives/faster-trim-javascript

        var trim = function(str) {
          str = str.replace(/^\s\s*/, '');
          var ws = /\s/,
          i = str.length;
          while (ws.test(str.charAt(--i)));
          return str.slice(0, i + 1);
        };

        //Lazy Function Definition Pattern, Peter's Blog
        //From http://peter.michaux.ca/article/3556

        //For Opera, Safari, and Firefox
        var dWalker = document.createTreeWalker(this, NodeFilter.SHOW_TEXT, null, false);
        var node,s,c = String.fromCharCode('8203');
        while (dWalker.nextNode()) {
          node = dWalker.currentNode;
          //we need to trim String otherwise Firefox will display
          //incorect text-indent with space characters
          s = trim( node.nodeValue ).split('').join(c);
          node.nodeValue = s;
        }
      }
    });

    return this;
  };
})(jQuery);

function showAjaxPreloader(opt){
	count_style = opt.style.length;
	style = '';
	if(count_style > 0){
		for(i=0;i<count_style;i++){
			style += opt.style[i]+' ';
		}
	}
	ht = '<div class="'+style+'"><img src="'+opt.dir+opt.name+'.gif" /></div>';
	$(opt.elem).css('display','block');
	$(opt.elem).html(ht);
}

function hideAjaxPreloader(elem){
	$(elem).css('display','none');
	$(elem).html('');
}

function changeState(value,page,elem){
	var url = BASE_URL;
	$('td#'+elem).html('Loading states...');
	$.post(page,{ajax:true, a:'displayState','country': value},		  
	function(data){	
		changeCity(0,url+'/includes/ajax_callback.php','city_holder');
		$('td#'+elem).html(data);
	});
}

function changeCity(zone,page,elem){
	$('td#'+elem).html('Loading cities...');
	$.post(page,{ajax:true, a:'displayCity','country': $('#country').val(), 'state':zone},
    function(data){
		$('td#'+elem).html(data);
    });
}
