
// Custom jQuery functions
$.fn.exists = function(){return this.length>0;}

function validateDate(strValue){
	var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
	
	//check to see if in correct format
	if(!objRegExp.test(strValue))
		return false; //doesn't match pattern, bad date
	else{
		var strSeparator = strValue.substring(2,3) 
		var arrayDate = strValue.split(strSeparator); 
		//create a lookup for months not equal to Feb.
		var arrayLookup = { '01' : 31,'03' : 31, 
							'04' : 30,'05' : 31,
							'06' : 30,'07' : 31,
							'08' : 31,'09' : 30,
							'10' : 31,'11' : 30,'12' : 31}
		var intDay = parseInt(arrayDate[1],10); 
		
		//check if month value and day value agree
		if(arrayLookup[arrayDate[0]] != null) {
			if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0) {
				return true; //found in lookup table, good date
			} 				
		}
	
		//check for February (bugfix 20050322)
		//bugfix  for parseInt kevin
		//bugfix  biss year  O.Jp Voutat
		var intMonth = parseInt(arrayDate[0],10);
		if (intMonth == 2) { 
			var intYear = parseInt(arrayDate[2]);
			if (intDay > 0 && intDay < 29) {
				return true;
			}
			else if (intDay == 29) {
				if ((intYear % 4 == 0) && (intYear % 100 != 0) || 
					(intYear % 400 == 0)) {
					// year div by 4 and ((not div by 100) or div by 400) ->ok
					return true;
				}   
			}
		}
	}  
	return false; //any other values, bad date
}

function validateCode(zip) {
	var reg = /^\d{5}([\-]\d{4})?$/;
	return reg.test(zip);
}

function validateEmail(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	return reg.test(email);
}

function validatePhone(phone) {
	var reg = /^\D*\d{3}\D*\d{3}\D*\d{4}\D*$/;
	return reg.test(phone);
}

function validateZip(zip) {
	var reg = /^\d{5}$|^\d{5}-\d{4}$/;
	return reg.test(zip);
}

function trim(str){
	var	str = str.replace(/^\s\s*/,''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}	

function loadDialog(ttl, html) {
	$('#UIModalContent').dialog('destroy').html(html).dialog({
		title: ttl, modal: true, width: 450, height:300, resizable: false,
		buttons:{"Close": function() { $(this).dialog("close"); }},
		open: function(event, ui) {
			$(this).parent().css("left",($(window).width()-$(this).width())/2+"px");
			$(this).parent().css("top",($(window).height()-$(this).height())/2-50+"px");
			$(this).attr("uimtop",$(this).parent().offset().top-$(window).scrollTop()).attr("uimleft",$(this).parent().offset().left);
		},
		dragStop: function(event, ui){
			$(this).attr("uimtop",$(this).parent().offset().top-$(window).scrollTop()).attr("uimleft",$(this).parent().offset().left);
		}
	});
}

function loadBlockDialog(ttl, html) {
	$('#UIModalContent').dialog('destroy').html(html).dialog({
		title: ttl, modal: true, width: 450, height:300, resizable: false, closeOnEscape: false,
		open: function(event, ui) {
			$(this).parent().children().children('.ui-dialog-titlebar-close').hide(); 
			$(this).parent().css("left",($(window).width()-$(this).width())/2+"px");
			$(this).parent().css("top",($(window).height()-$(this).height())/2-50+"px");
			$(this).attr("uimtop",$(this).parent().offset().top-$(window).scrollTop()).attr("uimleft",$(this).parent().offset().left);
		},
		dragStop: function(event, ui){
			$(this).attr("uimtop",$(this).parent().offset().top-$(window).scrollTop()).attr("uimleft",$(this).parent().offset().left);
		}
	});
}

function ajax(argurl, div, functionName) {
	$.ajax({
		url:argurl,
		cache:false,
		success: function(data){
			$('#'+div).html(data);
			if (functionName) {
				eval(functionName+"(data)");	
			}
		}
	})
}

function getCheckedValue(myOption) {
	myValue = $("input[name='"+myOption+"']:checked").val();
	if (myValue == undefined) {
		return "";	
	} else {
		return myValue;
	}
}

var currentBanner = "banner_1"
function swapBanner(bannerID){
	if (bannerID != currentBanner) {
		var $active = $('#'+currentBanner);
		var $next = $('#'+bannerID);
		currentBanner = bannerID;
		$active.fadeOut(600);
		$next.fadeIn(600, function(){
			$active.removeClass('active');
			$next.addClass('active');
		});
	}
}

