var ie  =  document.all
var ns6 =  document.getElementById&&!document.all
var horizontal_offset = "6px"; //horizontal offset of hint box from anchor link
var vertical_offset   = "-10px"; //horizontal offset of hint box from anchor link. No need to change.
/*
$(document).ajaxStart(function(){
	$('#systemWorking').show();
}).ajaxStop(function(){
	$('#systemWorking').hide();
}).ajaxError(function(a, b, e) {
	throw e;
});

*/
/** Check box for delete record displayed against each record **/
function checkBoxSelect(opt){
	var ids = $('#depIdCon').val();
	var idsArr = ids.split(',');
	for (var i = 0;i < idsArr.length ; i++ )
	{
		if (opt == 1)
		{
         	$('input[id=del_'+idsArr[i]+']').attr('checked', true);
         	setDel(idsArr[i]);
		}
		else{
			$('input[id=del_'+idsArr[i]+']').attr('checked', false);
		}
	}
}
function setDel(id){
 document.getElementById('tr_'+id).onclick = '';
}
/**  Get FckEditor content  **/
function getEditorValue(editorName)
{
	var inst = FCKeditorAPI.GetInstance(editorName);
	var sValue = inst.GetHTML();
	return sValue;
}

function showErrorMessage(msg,divId)
{
	var body = '<table align="center" width="100%" border="0" cellspacing="1" cellpadding="5">'
	+'	<tr>'
	+'		<td align="left" width="" class="" valign="middle" style="background-color:#e2e3e9; border:1px dotted #1e1e42;">'
	+'			 <table align="center" width="100%" border="0" cellspacing="1" cellpadding="5">'
	+'				<tr>'
	+'					<td align="left" width="" class="" valign="middle" style="padding-left:15px"><span class="errorHead">The following error occurred during saving record.</span></td>'
	+'				</tr>'
	+'				<tr>'
	+'					<td align="left" width="" class="normalError" valign="middle" style="padding-left:60px">'+msg+'</td>'
	+'				</tr>'
	+'			 </table>'
	+'		</td>'
	+'	</tr>'
	+'</table>';
	$(divId).innerHTML = body;
	$(divId).style.display = "";

}
function setPage(ele, to_show, sortField, sort, listFunctionName){
	if (ele.value == 1)
	{
		eval(listFunctionName + "(0, sortField, sort)");
	}
	else
	{
		var str = (ele.value - 1) * to_show;
		eval(listFunctionName + "(str, sortField, sort)");
	}
}
/*==================================================================================================================#
# * Function for adding a Filter to an Input Field																	#
# * @param  : [filterType  ] Type of filter 0=>Alpha, 1=>Num, 2=>AlphaNum											#
# * @param  : [evt         ] The Event Object																		#
# * @param  : [allowDecimal] To allow Decimal Point set this to true												#
# * @param  : [allowCustom ] Custom Characters that are to be allowed												#
#																													#
# * usage1  : onKeyPress="return filterInput(1, event)"   (No Decimal point)										#
# * usage2  : onKeyPress="return filterInput(1, event, true)"    (With Decimal point)								#
# * usage3  : onKeyPress="return filterInput(0, event)"    (Only Alphabets)											#
# * usage4  : onKeyPress="return filterInput(2, event)"    (Alphabets and Numeric)									#
# * usage5  : onKeyPress="return filterInput(2, event, false, '@_-.')"     (With Custom characters [@ _ .])			#
#===================================================================================================================*/
function filterInput(filterType, evt, allowDecimal, allowCustom){
    var keyCode, Char, inputField, filter = '';
    var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var num   = '0123456789';
    // Get the Key Code of the Key pressed if possible else - allow
    if(window.event){
        keyCode = window.event.keyCode;
        evt = window.event;
    }else if (evt)keyCode = evt.which;
    else return true;
    // Setup the allowed Character Set
    if(filterType == 0) filter = alpha;
    else if(filterType == 1) filter = num;
    else if(filterType == 2) filter = alpha + num;
    if(allowCustom)filter += allowCustom;
    if(filter == '')return true;
    // Get the Element that triggered the Event
    inputField = evt.srcElement ? evt.srcElement : evt.target || evt.currentTarget;
    // If the Key Pressed is a CTRL key like Esc, Enter etc - allow
    if((keyCode==null) || (keyCode==0) || (keyCode==8) || (keyCode==9) || (keyCode==13) || (keyCode==27) )return true;
    // Get the Pressed Character
    Char = String.fromCharCode(keyCode);
    // If the Character is a number - allow
    if((filter.indexOf(Char) > -1)) return true;
    // Else if Decimal Point is allowed and the Character is '.' - allow
    else if(filterType == 1 && allowDecimal && (Char == '.') && inputField.value.indexOf('.') == -1)return true;
    else return false;
}

//The Plugin Script
(function($) {

    $.fn.fieldclone = function(options) { 
    
		//==> Options <==//
		var settings = {
			newid_ : 0,
			target_: $(this),
			insert_: "before",
			limit_: 0
		};
        if (options) $.extend(settings, options);           

		if( (settings.newid_ <= (settings.limit_+1)) || (settings.limit_==0) ){	//Check the limit to see if we can clone

			//==> Clone <==//
			var fieldclone = $(this).clone();
			var node = $(this)[0].nodeName;
			var classes = $(this).attr("class");

			//==> Increment every input id <==//
			var srcid = 1;
			$(fieldclone).find(':input').each(function(){
				var s = $(this).attr("name"); 			
				$(this).attr("name", s.replace(eval('/_'+srcid+'/ig'),'_'+settings.newid_)); 
				$(this).attr("id", s.replace(eval('/_'+srcid+'/ig'),'_'+settings.newid_)); 
			});

			//==> Locate Target Id <==//
			var targetid = $(settings.target_).attr("id");
			if(targetid.length<=0){
				targetid = "clonetarget";
				$(settings.target_).attr("id",targetid);
			}		

			//==> Insert Clone <==//
			var newhtml = $(fieldclone).html().replace(/\n/gi,"");
			newhtml = '<'+node+' class="'+classes+'">'+newhtml+'</'+node+'>';
			
			eval("var insertCall = $('#"+targetid+"')."+settings.insert_+"(newhtml)"); 
		}
    };

})(jQuery);
