// JavaScript Document
function addform()
{
	$('form')[0].reset();
	$('div#collapsedbox').slideDown("slow");
	$('#action').val("Add");		
	$('#act_btn').val("Add");
	$('#form_heading').html("<a href=\"#\" onclick=\"addform(); return false\">Add New "+$('#form_head').val()+"</a>");
	$("#err_box").css("display","none");
	return false;
}

function addformrights()
{
	$('form')[0].reset();
	$("input[@type='checkbox']").each(function() {
			this.checked	= false;
		});
	$('div#collapsedbox').slideDown("slow");
	$('#action').val("Add");		
	$('#act_btn').val("Add");
	$('#form_heading').html("<a href=\"#\" onclick=\"addformrights(); return false\">Add New "+$('#form_head').val()+"</a>");
	$("#err_box").css("display","none");
	return false;
}


function addnameform()
{
	$('form')[0].reset();
	$('div#collapsedbox').slideDown("slow");
	$('#action').val("Add");		
	$('#act_btn').val("Add");
	$('#form_heading').html("<a href=\"#\" onclick=\"addform(); return false\">Add New "+$('#form_head').val()+"</a>");
	$("#err_box").css("display","none");
	return false;
}

function getClassName(tblid)
{
	var table 	= document.getElementById(tblid);
	var tds 	= table.getElementsByTagName('tr');
	var rows	= (tds.length-1);
	if((rows%2) != 0)
		{
			return " class=\"tr-2\"";
		}
	else
		{
			return " class=\"tr-1\"";
		}
}


function tab_sttings()
{
	$('div.tabbed div').hide();
	$('div.t1').show();
	$('div.tabbed ul.tabs li a').removeClass('tab-current');
	$("#tab1").addClass('tab-current');
}

function hide_form(formHead)
{
	//$('err_box').css("display","none");
	$('form')[0].reset();
	$('div#collapsedbox').slideUp("slow");
	//$('#form_heading').html("<a href=\"#\" onclick=\"addform(); return false\">Add New "+formHead+"</a>");
	//$("#err_box").css("display","none");
}

function cancel_form(formHead)
{
	//$('err_box').css("display","none");
	$('form')[0].reset();
	$('div#collapsedbox').slideUp("slow");
	//$('#form_heading').html("<a href=\"#\" onclick=\"addform(); return false\">Add New "+formHead+"</a>");
	$("#err_box").css("display","none");
}
function show_msg(status,dbid)
{
	if(status=="add")
		{
			$("#err_box").html("<li>Record successfully Added.</li>");
		}
	else if(status=="update")
		{
			$("#err_box").html("<li>Record successfully Updated.</li>");
		}
	else if(status=="delete")
		{
			$("#err_box").html("<li>Record successfully Deleted.</li>");
		}
	else if(status=="sortedit")
		{
			$("#err_box").html("<li>Sort Order successfully Upadted.</li>");
		}
	else if(status=="Activate")
		{
			$("#err_box").html("<li>User successfully Activated.</li>");
		}	
	else if(status=="Deactivate")
		{
			$("#err_box").html("<li>User successfully Deactivated.</li>");
		}
	else if(status=="search")
		{
			$("#err_box").html("<li>Search results Found.</li>");
		}
	else if(status=="useradd")
		{
			$("#err_box").html("<li>User successfully added to the team.</li>");
		}
	else if(status=="approval")
		{
			$("#err_box").html("<li>Changes are pending for superuser approval.</li>");	
		}
	else if(status=="approved")
		{
			$("#err_box").html("<li>Master record updated sucessfully.</li>");	
		}
	else if(status=="unapporved")
		{
			$("#err_box").html("<li>Change unapproved.</li>");	
		}
	else if(status=="pending")
		{
			$("#err_box").html("<li>Change has moved to pending list.</li>");	
		}
	$("#err_box").highlightFade({color:'#e8fdd3',speed:1000,iterator:'exponential'});
	if(dbid!="NoEffect")
		{
			$("#rec_"+dbid).highlightFade({color:'#e8fdd3',speed:1000,iterator:'exponential'});
		}
	//$("#err_box").fadeIn("slow");
	/*$("#err_box").css("background","#E9E9E9");*/
	$("#err_box").css("display","block");
	$("#err_box").addClass("success_box");
	/*$("#err_box").css("color","#009900");*/
}
function form_loading(errId)
{
	$("#"+errId).css("background","#fff");
	$("#"+errId).html("<center><img src=\"images/loading.gif\" /></center>");
	$("#"+errId).css("display","block");
	
}
function disableText()
{
	$('input[@type=text]').each(function()
	{
		$(this).attr("readonly","readonly");
	});
}

function show_error(error,errid)
{
		var output	= error.split("#");
		
		//$("#err_box").css("background","#FFF5F1");
		$("#"+errid).removeClass("success_box");
		$("#"+errid).addClass("error_box");
		$("#"+errid).html(output[1]);
		$("#"+errid).fadeIn("slow");
		$("#"+errid).css("display","block");
		//$("#err_box").css("color","#fff");
		$("#"+errid).highlightFade({color:'#f3cbcb',speed:1000,iterator:'exponential'});
		
}


/** Show error message for the admin ***/
/*function showErrorMessage(formId,msg){
	
	var errorData,i,errVal;
	errorData=msg.split('#');
	
	$("#"+formId+" :input").each(function(){
		var id= this.id;			
		$("#"+id).removeClass("error");
		$("#li_"+id).removeClass("bg_error");
	});
	
	$("#"+formId+" div").each(function(){									   
		if(this.id.indexOf("error_") != "-1")
		{			
			this.style.display="none";
		}
	});
	
	var counter		= 0;
	var first_error	= "";
	
	for(i=1; i<errorData.length; i++)
		{
			errVal=errorData[i].split('@@');		
			
			$("#"+errVal[0]).addClass("error");
			$("#li_"+errVal[0]).addClass("bg_error");
			$("#error_"+errVal[0]).css("display","block");			
			$("#error_"+errVal[0]).html("<p>"+errVal[1]+"</p>");
			if(counter==0 && errVal[0]!="")
				{
					counter 	= 1;	
					first_error	= errVal[0]; 
				}
			
	}
	$.scrollTo($("#"+first_error), 800 );
	alert("scroll to wworking"+first_error+counter);
} */

function showErrorMessage(formId,msg){
	var errorData,i,errVal;
	errorData=msg.split('#');
	
	$("#"+formId+" :input").each(function(){
		var id= this.id;			
		if(id)
		{
			$("#"+id).removeClass("error");
			$("#li_"+id).removeClass("bg_error");
		}
	});
	
	$("#"+formId+" div").each(function(){									   
		if(this.id.indexOf("error_") != "-1")
		{			
			this.style.display="none";
		}
	});
	first_msg = 0;
	
	var counter		= 0;
	var first_error	= "";
	
	for(i=1; i<errorData.length; i++){			
		errVal = errorData[i].split('@@');			
		if(errVal[0]	!=	"")
		{
			$("#"+errVal[0]).addClass("error");
			$("#li_"+errVal[0]).addClass("bg_error");
			$("#error_"+errVal[0]).css("display","block");			
			$("#error_"+errVal[0]).html("<p>"+errVal[1]+"</p>");
			if(first_error == "")
			{				
				counter 	= 1;
				first_error	= errVal[0];
				if(first_error	==	'common')
				{
					first_error	=	'error_'+first_error;
				}
					 
			}
		}
	}
	
	/*if(first_error !="")
	{
		$.scrollTo($("#"+first_error), 800 );
	}	*/
}

function liststate(country)
{
	if(country=="106")
		{
			$.ajax({
					type	: "POST",
					url		: "ajaxforms/SelectState.php",
					data	: "Rval=106",
					success	: function(msg)
							{
								$('#state_span').html(msg);
							}
					
					});
			
		}
	else
		{
			$("#state_span").html("");
			$("#state_span").html('<input type="text" name="state" id="state" size="25" class="inputArea" title="Enter your state">');
		}
}


/*Function for showing Activate and Dectivate*/
function show_status(dbid,status)
	{
		if(status=="Activate")
			{
				$('#sts_'+dbid).html('Yes');
				$('#sts_'+dbid).removeClass('red');
				$('#sts_'+dbid).fadeIn("slow");
				$('#del_'+dbid).attr("src","images/active.gif");
				//$('#rec_'+dbid).highlightFade({color:'e8fdd3',speed:1000,iterator:'exponential'});
				
			}
		else
			{
				$('#sts_'+dbid).html('No');
				$('#sts_'+dbid).addClass('red');
				$('#sts_'+dbid).fadeIn("slow");
				$('#del_'+dbid).attr("src","images/inactive.gif");
				//$('#del_'+dbid).html("Activate");
				//$('#rec_'+dbid).highlightFade({color:'f3cbcb',speed:1000,iterator:'exponential'});
			}
	}

function ued_encode(arr,current_index) {
	var query = ""
	if(typeof current_index=='undefined') current_index = '';

	if(typeof(arr) == 'object') {
		var params = new Array();
		for(key in arr) {
			var data = arr[key];
			var key_value = key;
			if(current_index) {
				key_value = current_index+"["+key+"]"
			}

			if(typeof(data) == 'object') {
				if(data.length) { //List
					for(var i=0;i<data.length; i++) {
						params.push(key_value+"[]="+ued_encode(data[i],key_value)); //:RECURSION:
					}
				} else { //Associative array
					params.push(ued_encode(data,key_value)); //:RECURSION:
				}
			} else { //String or Number
				params.push(key_value+"="+encodeURIComponent(data));
			}
		}
		query = params.join("&");
	} else {
		query = encodeURIComponent(arr);
	}

	return query;
}

/* thick Box related functions*/
var jThickboxNewLink;
function tb_remove_open(reloadLink){
jThickboxReloadLink	=	reloadLink;
tb_remove();
setTimeout("jThickboxNewLink();",500);
return false;
}

function tb_open_new(jThickboxNewLink){
tb_show(null,jThickboxNewLink,null);
}

function tb_remove(parent_func_callback) {
$("#TB_imageOff").unbind("click");
$("#TB_closeWindowButton").unbind("click");
$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
$("#TB_load").remove();
if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
$("body","html").css({height: "auto", width: "auto"});
$("html").css("overflow","");
}

if(parent_func_callback != undefined)
eval("window."+parent_func_callback);
document.onkeydown = "";
document.onkeyup = "";
return false;
}
 function format_date(format, date)
	{
		if (format == "mdy")
		{
			var arrdate = date.split("-");
			format_date = arrdate[1]+"-"+arrdate[2]+"-"+arrdate[0];		
		}
		return format_date;
	}


function check_file_type(file)
{
	if(file)
		{
			gtePos= file.lastIndexOf(".");
			str1 = file.substring((gtePos + 1),file.length);
			  if(str1 == 'jpg' || str1 == 'png' || str1 == 'gif' || str1 == 'jpeg')
				return true;
			else
				return false;	
		}
	else
		{
			return true;
		}
}

/* Tiny MCE Functions start */
	function setup()
	{
		tinyMCE.init({			
			mode : "textareas",
			theme : "advanced",
			editor_selector : "mceEditor",
			editor_deselector : "mceNoEditor",
			theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|formatselect,fontselect,fontsizeselect,|insertimage",
			//plugins : "bbcode",
			skin : "o2k7", 
			skin_variant : "silver",									
			add_unload_trigger : true,
			remove_linebreaks : true,
			inline_styles : false,
			convert_fonts_to_spans : false,
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
			tab_focus : ':prev,:next',
			file_browser_callback : "tinyBrowser"
		});
	}
	
	function closeMCE(){
		if (typeof tinyMCE != "undefined") {
			for(id in tinyMCE.editors){
				tinyMCE.execCommand('mceRemoveControl', false, id);
			}
		}else{
		
		}
	}
/* Tiny MCE Functions end*/
