/**
 * $Id: main.js 343 2009-10-13 13:58:37Z shrey $
 * 
 * This file can not be redistributed to anyone without the permission of Giga Promoters
 * Copyright 2005 - 2008, Giga Promoters
 * 						  608 Aggarwal Cyber Plaza
 *						  Netaji Subhash Place, Delhi - 110034
 *						  www.gigapromoters.com
 * 
 *
 * This is parent of parent class of a controller and should not be edited ever without permission.
 * @version 			$Rev: 343 $
 * @lastModifiedBy		$LastChangedBy: shrey $
 * @lastChanged			$Date: 2009-10-13 19:28:37 +0530 (Tue, 13 Oct 2009) $
 * @copyright 			Giga Promoters (www.gigapromoters.com)
 * 
 */

// global js file
$(document).ready(function(){
    swapValues = [];
    $(".swap_value").each(function(i){
		$(this).addClass('inactiveSwap');
        swapValues[i] = $(this).val();
        $(this).focus(function(){
			$(this).removeClass('inactiveSwap');
            if ($(this).val() == swapValues[i]) {
                $(this).val("");
            }
        }).blur(function(){
            if ($.trim($(this).val()) == "") {
				$(this).addClass('inactiveSwap');
                $(this).val(swapValues[i]);
            }
        });
    });
	// activate primary ajax indicator
	$("#progressIndicator").ajaxStart(function(){
	   $(this).show();
	 });
	$("#progressIndicator").ajaxStop(function(){
	   $(this).hide();
	 });

	 // secondary ajax indicator(s)
	$(".progressIndicator").ajaxStart(function(){
	   $(this).show();
	 });
	$(".progressIndicator").ajaxStop(function(){
	   $(this).hide();
	 });
	
	// supports single field per page only
	$(".addAnotherField").click(function(){
		if(!window.fieldHtml)
		{
			window.fieldHtml = $(this).parent().children(":not(a.addAnotherField, label)");
			//console.log(window.fieldHtml);
		}
		window.fieldHtml.clone().addClass('childInput').clearForm().insertBefore(this);
	});
	
	// check if console is available, if not, define it
	
	if(typeof console == "undefined")
	{
		//$(document).append('<div id="jsDebug"></div>');
		window.console = {
			log: function(message)
			{
				if($("#jsDebug"))
					$("#jsDebug").append(message+"<br>");
			}
		};
		console.log("using alternate js debugging");
	}
});


/*
 * Jonathan Howard
 *
 * jQuery Pause
 * version 0.2
 *
 * Requires: jQuery 1.0 (tested with svn as of 7/20/2006)
 *
 * Feel free to do whatever you'd like with this, just please give credit where
 * credit is do.
 *
 *
 *
 * pause() will hold everything in the queue for a given number of milliseconds,
 * or 1000 milliseconds if none is given.
 *
 *
 *
 * unpause() will clear the queue of everything of a given type, or 'fx' if no
 * type is given.
 */

$.fn.pause = function(milli,type) {
	milli = milli || 1000;
	type = type || "fx";
	return this.queue(type,function(){
		var self = this;
		setTimeout(function(){
			$.dequeue(self);
		},milli);
	});



};

$.fn.clearQueue = $.fn.unpause = function(type) {
	return this.each(function(){
		type = type || "fx";
		if(this.queue && this.queue[type]) {
			this.queue[type].length = 0;
		}
	});
};

// function to toggle selected checkboxes
function toggleCheckboxes(checkboxes)
{
	checkem = false;
	if($('#checkAll').attr("checked"))
	{
		checkem = true;
	}
	checkboxTog = $('#checkAll').attr("checked");
	checkboxes.each( function(){
		$(this).attr("checked",checkem);
	});
	//checkboxTog = !checkboxTog;

}
function redirect(url)
{
	window.location = url;
	return true;
}

// clears all form inputs: $('form').clearForm()
$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
 else
	 return $(this).children().clearForm();
  });
};

function addToolTip(selector,style,parentSelector)
{

	$(selector).each(function(){
		if(parentSelector==null)
			var qtipSelector = $(this).parent();
		else
			var qtipSelector = $(this).parent(parentSelector);
		var tip = $(this).text();
		qtipSelector  = qtipSelector.qtip({'content':tip,'style':{name:style}})
	
	})
}
 function applyAutoComplete(selector,url,options)
 {
	 try
	 {
		return $(selector).autocomplete(url,options);	
	 }
	 catch (e)
	 {
		console.log(e);
	 }
 }

 function autoCompleteformatItem(row) {
	if(row[1] == undefined) {
		return row[0];
	}
	else {
		return row[0];
	}
}


function insertAtCursor(myField, myValue) {
//IE support
	if (document.selection) {
	myField.focus();
	sel = document.selection.createRange();
	sel.text = myValue;
}
//MOZILLA/NETSCAPE support
else if (myField.selectionStart || myField.selectionStart == '0') {
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
}
// outerHTML() to grab full html of selected element
(function($) {
	 // Returns whether or not a result set has results in it
	 $.fn.outerHTML = function() {
	   return $('<div>').append( this.eq(0).clone() ).html();
	 };
})(jQuery);

function makeHelpTextBox(selector,style)
{
	if(selector==null)
		selector = '.helpBox,.helpbox';
	$(selector).each(function(){
		$(this).css('display','none');
		var content = $(this).text();
		var elem = $('<span class="help_text_marker">(?)</span>');
		$(this).replaceWith(elem);
		elem.qtip({'content':content,'style':{name:style}});
	});
}
	//ucisrt equivalent
    String.prototype.ucFirst = function()
   {
      return this.charAt(0).toUpperCase() + this.substring(1);
	}