//------------------------------------------------------------------------------
jQuery.fn.extend({
	tabs: function(options) {
		var defaults = {
			active: 1,
			duration: 0,
			navClass: "ui-tabs",
			selectedClass: "selected"
		};
		return this.each(function() {
			var obj = $(this);
			var tabDivs = $("div", obj);
			var link = $("ul li a", obj);
			tabDivs.hide();
			var o = $.extend(defaults, options);
			obj.addClass(o.navClass);	//apply class to the entire tab content
			
			tabDivs.eq(o.active-1).show(o.duration);
			for(i=0;i<link.length;i++) {
				if(i != o.active)
					link.eq(i).removeClass(o.selectedClass);
				link.eq(o.active-1).addClass(o.selectedClass);
			}

			link.click(function() {
				var index = link.index(this);
				$('li a',obj).each(function() {
					$(this).removeClass(o.selectedClass);
					tabDivs.hide(o.duration);
				});
				$(this).addClass(o.selectedClass);
				tabDivs.eq(index).show(o.duration);
			});
		});
	}
});
//------------------------------------------------------------------------------
jQuery.extend({
	paging: function(url, page, totalPages) {
		if(!this.isNumeric(page)) {
			alert('Invalid page no'); page.focus(); return false;
		}
		pageNo = $.trim(page.value);
		if(pageNo > totalPages || pageNo < 1) {
			alert('Invalid Page'); page.focus(); return false;
		}
		this.goTo(url+pageNo);
		return false;
	},

	loadPage: function(url, objAnchor, rh) {
		this.controlLbLayer("block");
		if ( objAnchor != "" ) {
			var anchorParent = objAnchor.parentNode.parentNode.parentNode.id;
			rh  = "process_"+anchorParent;
		} else {
			rh	=	"process_"+rh;
		}
		$.ajax( {type:"GET", url:url, success: eval(rh)} );
		return false;
	},

	controlLbLayer: function(mode) {
		if(mode == "block") { $('select').hide(); $('#lb_layer').show(); }
		else { $('select').show(); $('#lb_layer').hide(); }
	},

	goBack: function(url) {
		if( url == undefined )
			history.go(-1);
		else
			window.location	= url;
	},

	goTo: function(url) {
		window.location	=	url;
	},
	
	delAlert: function(url, msg) {
		msg = msg ? msg : "Are you sure you wish to delete?";
		if ( confirm(msg) ) {
			this.goTo(url);
		} else {
			return false;
		}
	},

	getURL: function() {
		url = document.location.href;
		x   = url.indexOf(".");
		x	= url.indexOf("/", x);
		return url.substring(0,x);
	},

	getParam: function(key) {
		var strQS = window.location.search;
		var param = strQS.split("&");
		for ( var i = 0; i < param.length; i++ ) {
			if ( param[i].indexOf(param + "=") > -1 ) {
				var aParam = aQueryString[iParam].split("=");
				var keyValue = aParam[1];
				break;
			}
		}
		return keyValue;
	},
	
	popUp: function(url, win_name, width, height, resize, scroll, top, left) {
		popWin = window.open(url, win_name, "toolbar=no, location=no, directories=no, status=no, menubar=no, resizable="+(!resize ? "yes" : resize)+", copyhistory=no, scrollbars="+(!scroll ? "yes" : scroll)+", width="+(!width ? "400" : width)+", height="+(!height ? "300" : height)+", top="+(!top ? "50" : top)+", left="+(!left ? "50" : left));
		popWin.focus();
	},

	openTarget: function(form, windowName, width, height, resize, scroll, top, left) {
		form.target = windowName;
		this.popUp('', windowName, width, height, resize, scroll, top, left);
	},
	
	//random bet'n 0 and x
	random: function(x) {
	    return Math.floor(x * (Math.random() % 1));
	},

	randomBetween: function(minV, maxV) {
	  return minV + this.random(maxV - minV + 1);
	},

	round: function(number, x) {
		x = (!x ? 2 : x);
		return Math.round(number*Math.pow(10,x))/Math.pow(10,x);
	},

	isEmpty: function(objElt) {
		data =	$.trim(objElt.value);
		if ( data.length > 0 ) { return false; }
		return true;
	},

	isAlpha: function(objElt) {
		return	/^[a-zA-Z]+$/.test(objElt.value);
	},
	
	isAlpha1: function(objElt) {
		return	/^[a-zA-Z ]+$/.test(objElt.value);
	},
	
	isNumeric: function(objElt) {
		return	/^[0-9]+$/.test(objElt.value);
	},
	
	isAlphaNumeric: function(objElt) {
		return	/^[a-zA-Z0-9 ]+$/.test(objElt.value);
	},
	
	isPhone: function(objElt) {
		return	/^[0-9\-()+ ]+$/.test(objElt.value);
	},
	
	isEmail: function(objElt) {
		return	/^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,4}(\.[a-z]{2}){0,2})$/i.test(objElt.value);
	},
	
	isAmount: function(objElt) {
		return	/^[0-9]+(.){0,1}([0-9]*)$/i.test(objElt.value);
	},

	isPositiveInt: function(objElt) {
		if(parseInt(objElt.value)>0) {	return true; }
		return false;
	},

	isPositiveFloat: function(objElt) {
		return this.isNotNegativeFloat(objElt) && (parseFloat(objElt.value)>0);	
	},

	isNotNegativeFloat: function(objElt) {
		return /^[0-9]*[.]{0,1}[0-9]*$/.test(objElt.value);
	},

	isNotNegativeInt: function(objElt) {
		return /^[0-9]*$/.test(objElt.value);
	},

	isZip: function(objElt, noOfChars) {
		patZip	=	eval("/^[0-9]{"+noOfChars+"}$/");
		return patZip.test(objElt.value);
	},

	isPeriod: function(objElt) {
		return /^[1-9][0-9]{2}$/.test(objElt.value);
	},

	isFloat: function(objElt) {
		return /^[\-\+]{0,1}[0-9]*[.]{0,1}[0-9]*$/.test(objElt.value);
	},

	isInt: function(objElt) {
		i = parseInt(objElt.value);
		if(i>0 ||i==0 || i<0) { return true; }
		return false;
	},
	
	isSpace: function(objElt) {
		return /^[ ]+$/.test(objElt.value);
	},

	itemsChecked: function(type, eltName, form) {
		a = 0;
		for(var i=0; i<form.elements.length; i++) {
			var e = form.elements[i];
			if(e.type == type && e.name == eltName && e.checked) { a++; }
		}
		return a;
	},

	itemSelected: function(objElt) {
		if ( objElt.options.selectedIndex == 0 || objElt.options.selectedIndex == -1 )
			return false;
		return true;
	},

	itemsSelected: function(objElt) {
		a = 0;
		for(i=0; i<objElt.options.length; i++)
		{
		  if(objElt.options[i].selected)
			a++;
		}
		return a;
	},

	itemSelectedData: function(objElt, mode) {
		return (mode == "value") ? objElt.value : objElt.options[objElt.selectedIndex].text;
	},

	radioValue: function(objElt) {
		var x = null;
		for(i=0; i<objElt.length; i++)
		{
			if (objElt[i].checked) { x = objElt[i].value; break; }
		}
		return x;
	},

	//onclick="$.checkAll(this, this.form, 'cbox[]');
	checkAllOld: function(chk, form, eltname) {
		for(i=0; i<form.elements.length; i++) {
			var e = form.elements[i];
			if(e.type == "checkbox" && e.name == eltname) { e.checked = chk.checked; }
		 }
	},

	//onclick="$.checkAll(this, this.form.elements['cbox[]']);
	checkAll: function(chk, objElt) {
		for(i=0; i<objElt.length; i++)
			objElt[i].checked = chk.checked;
	}, 

	transfer: function(fromId, toId) {
		return !$('#'+fromId).children('option:selected').remove().appendTo($('#'+toId)); 
	},

	populateDD: function(url, srcDD, destDD) {
		qdata	=	$("select#"+srcDD).val();
		if ( qdata == null || qdata == "" )
			$("select#"+destDD).html('');

		$.getJSON(url, "qd="+qdata, function(j) {
			if ( $("select#"+destDD).attr("multiple") )
				var optx = '<option value="0">All</option>';
			else
				var optx = '<option value="">Select</option>';
			for (var i = 0; i < j.length; i++) {
				optx += '<option value="' + j[i].ov + '">' + j[i].od + '</option>';
			}
			$("select#"+destDD).html(optx);
			//$("option:first", "select#"+destDD).attr("selected","selected");
			//$("select#"+destDD+" option:first").attr('selected', 'selected');
			//$("select#"+destDD).attr("value","1");
		})
	},

	validateCaptcha: function(chkUrl, cCode) {
		qdata	=	$("#"+cCode).val();
		return	$.ajax({ type: "GET", url: chkUrl, data: "qd="+qdata, cache: false, async: false}).responseText;
	},

	//<input type=button value='Move Up' style="width:150px"  onclick="$.moveUp(this.form.sequence)"><p>
	//<input type=button value='Move Down' style="width:150px" onclick="$.moveDown(this.form.sequence)"><p>
	moveUp: function(obj) {
		sI = obj.selectedIndex;
		if(sI>0)
		{
			tmpVal	= obj.options[sI-1].value;
			tmpText = obj.options[sI-1].text;
			obj.options[sI-1].value = obj.options[sI].value;
			obj.options[sI-1].text	= obj.options[sI].text;
			obj.options[sI].value	= tmpVal;
			obj.options[sI].text	= tmpText;
			obj.selectedIndex -= 1;
		}
		else
		{
			alert("Please select an item");
			obj.focus();
		}
	},

	moveDown: function(obj) {
		sI = obj.selectedIndex;
		if(sI>=0)
		{	
			if(sI < obj.options.length-1)
			{
				tmpVal = obj.options[sI+1].value;
				tmpText = obj.options[sI+1].text;
				obj.options[sI+1].value = obj.options[sI].value;
				obj.options[sI+1].text = obj.options[sI].text;
				obj.options[sI].value = tmpVal;
				obj.options[sI].text = tmpText;
				obj.selectedIndex +=1;
			}
		}
		else
		{
			alert("Please select an item");
			obj.focus();
		}
	},

	formatMoney: function(amount) {
		var i = parseFloat(amount);
		if(isNaN(i)) { i = 0.00; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		s = new String(i);
		if(s.indexOf('.') < 0) { s += '.00'; }
		if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
		s = minus + s;
		return s;
	},

	dateCompare: function (startDate,endDate)
	 {

		  var startArray = startDate.split("-");
		  var endArray = endDate.split("-");

		  var startDay = parseInt(startArray[0]);
		  var startMonth= parseInt(startArray[1]);
		  var startYear = parseInt(startArray[2]);
		  var endDay = parseInt(endArray[0]);
		  var endMonth = parseInt(endArray[1]);
		  var endYear = parseInt(endArray[2]);

		  if(endYear < startYear)
		  {
		       return false;
		  } 
		  if(endYear ==  startYear && endMonth < startMonth)
		  {
		    return false;
		  }
		  if((endYear == startYear) && (endMonth == startMonth) &&(endDay <startDay ))
		  {
		     return false;
		  }
          return true;

        },
	
	setCookie: function(cookieName, cookieValue, lifeTime, path, domain, isSecure) {
		if ( !cookieName ) { return false; }
		life_time	=	lifeTime * 24 * 60 * 60 * 1000;
		document.cookie = encodeURIComponent(cookieName) + "=" + encodeURIComponent(cookieValue) + (lifeTime ? ";expires=" + ( new Date( ( new Date() ).getTime() + (life_time) )).toGMTString() : "") + (path ? ";path=" + path : "") + (domain ? ";domain=" + domain : "") + (isSecure ? ";secure" : "");
	},

	getCookie: function(cookieName) {
		var cookieJar = document.cookie.split(";");
		for(var x = 0; x < cookieJar.length; x++) {
			var arrCookie = cookieJar[x].split("=");
			if(arrCookie[0] == decodeURIComponent(cookieName) ) { return decodeURIComponent(arrCookie[1]); }
		}
		return null;
	},
	
	pad: function(str, len, pad, dir) {
		if (typeof(len) == "undefined") { var len = 0; }
		if (typeof(pad) == "undefined") { var pad = ' '; }
		if (typeof(dir) == "undefined") { var dir = "left"; }

		if (len + 1 >= str.length) {
			switch (dir) {
				case "left":
					str = Array(len + 1 - str.length).join(pad) + str;
					break;

				case "both":
					var right = Math.ceil((padlen = len - str.length) / 2);
					var left = padlen - right;
					str = Array(left+1).join(pad) + str + Array(right+1).join(pad);
					break;

				case "right":
					str = str + Array(len + 1 - str.length).join(pad);
					break;
			}
		}
		return str;
	},

	//var prelod = ['image1.gif', 'image2.gif'];
	//$.preloadImages(preload);
	//$.preloadImages('image1.gif', 'image2.gif');
	preloadImages: function() {
		var a = (typeof arguments[0] == 'object') ? arguments[0] : arguments;
		for(var i=a.length-1; i>=0; i--) {
			jQuery("<img>").attr("src", a[i]);
		}
	},
	//$.correctPNG();

	correctPNG: function() {
		
	   var arVersion = navigator.appVersion.split("MSIE")
	   var version = parseFloat(arVersion[1])
	   if ((version >= 5.5) && (document.body.filters)) 
	   {
		  for(var i=0; i<document.images.length; i++)
		  {
			 var img = document.images[i]
			 var imgName = img.src.toUpperCase()
			 if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			 {
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
				img.outerHTML = strNewHTML
				i = i-1
			 }
		  }
	   }
	},

	navHover: function() {
		$("#navmenu-h li").hover(
			function() { $(this).addClass('iehover'); },
			function() { $(this).removeClass('iehover'); }
		);
	},

	checkFriendsEmail: function(obj,num,oname){
		var firstelem = '';
		var len = obj.elements.length;
		for(i=0;i<len;i++)
		{
			var e = obj.elements[i];
			if (e.name == oname)
			{
				if (firstelem == '')
				{
					firstelem = e;
				}
				var e2 = obj.elements[i+1];

				if($.isEmpty(e) && !$.isEmpty(e2))
				{
					alert("Please enter friend name");e.focus();
					return false;
				}
				
				if(!$.isEmpty(e) && $.isEmpty(e2))
				{
					alert("Please enter email");e2.focus();
					return false;
				}
				if(!$.isEmpty(e) && !$.isEmpty(e2))
				{
					if (!$.isEmail(e2))
					{
						alert("Please enter valid email");
						e2.focus();e2.select();
						return false;
					}
					else
					{
						num++;
					}
				}
			}
		}
		if (num == 0)
		{
			alert("Please enter atleast one recipient name and email");
			firstelem.focus();
			return false;
		}
		return true;
	}
});

if ( $.browser.msie && parseInt($.browser.version) <= 6 ) {
	if (window.attachEvent) window.attachEvent("onload", $.navHover);

}
