Window_Head_Move_Left=0;
Window_Head_Move_Top=0;
Window_Head_Move_Width=0;
Window_Head_Move_Height=0;
var Account_Activated;
var Reset_Pass;
var ListCatsFirst;

function loadfunc() {
	//$('a').attr('href','test');
	$("body").css('overflow','hidden');
	document.getElementById('WindowBg').style.width=$(document).width()+'px';
	$("body").css('overflow','');
	document.getElementById('WindowBg').style.height=$(document).height()*2+'px';
	$('#WindowBg').css('opacity','0.75');
	document.getElementById('WindowBox').style.left=(($(document).width()-352)/2)+'px';
	document.getElementById('WindowBox').style.top=(($(window).height()-130)/2)+'px';
	
	$("#Window_Head").mousedown(function(e){
		$(this).css('cursor','move');
		$('#WindowBox').animate({opacity:0.5},{ duration: 'fast', queue: false });
		Window_Head_Move_Top=e.clientY-$('#WindowBox').offset().top;
		Window_Head_Move_Left=e.clientX-$('#WindowBox').offset().left;
		Window_Head_Move_Width=$('#WindowTbl').width();
		Window_Head_Move_Height=$('#WindowTbl').height();
		Window_Head_Move_WinWidth=$(document).width();
		Window_Head_Move_WinHeight=$(window).height();
		return false;
	});
	$("#WindowBg,#WindowTbl").mouseup(function(e){
		if($("#Window_Head").css('cursor')=='move'){
			$("#Window_Head").css('cursor','default');
			$('#WindowBox').animate({opacity:1},{ duration: 'fast', queue: false });
		}
	});

	$("#WindowBg,#WindowTbl").mousemove(function(e){
		if($("#Window_Head").css('cursor')=='move'){
			Top=e.clientY-Window_Head_Move_Top;
			if(Top<0)Top=0;
			if(Top>(Window_Head_Move_WinHeight-Window_Head_Move_Height))Top=(Window_Head_Move_WinHeight-Window_Head_Move_Height);
			Left=e.clientX-Window_Head_Move_Left;
			if(Left<0)Left=0;
			if(Left>(Window_Head_Move_WinWidth-Window_Head_Move_Width))Left=(Window_Head_Move_WinWidth-Window_Head_Move_Width);
			$('#WindowBox').css('top',(Top)+'px');
			$('#WindowBox').css('left',(Left)+'px');
		}
	});

	if(ListCatsFirst=='y'){
		WindowBoxShow("GetContacts");
	}

	$('#ChangeLang').hover(
		function(){;
			$('#ChangeLang').animate({width:'85px',opacity:1}, { duration: "slow", queue: false });
		},
		function(){
			$('#ChangeLang').animate({width:'30px',opacity:0.5}, { duration: "slow", queue: false });
		});
	$('#window_close').hover(
		function(){;
			$('#window_close').animate({opacity:1}, { duration: 500, queue: false });
		},
		function(){
			$('#window_close').animate({opacity:0.5}, { duration: 500, queue: false });
		});

	DocLoad();
	
	if(Account_Activated==1) {
		WindowBoxShow("Activation",false);
	} else if (Account_Activated==2) {
		WindowBoxShow("Activation",true);
	}
	
	if(Reset_Pass==1) {
		WindowBoxShow("Reset_Pass",false);
	} else if (Reset_Pass==2) {
		WindowBoxShow("Reset_Pass",true);
	}

	$('#TopDivs').css('width',($('#MainTbl').width()-25)+'px');
	
};
	

function Cat_Expand(CID) {
	i=1;
	display='';
	if (document.getElementById('cat'+CID+'expand').className=='Cat_Opened' || document.getElementById('cat'+CID+'expand').className=='Cat_Opened_e') {
		display='none';
		if(SubCategory[CID])document.getElementById('cat'+CID+'expand').className='Cat_Closed';
		else document.getElementById('cat'+CID+'expand').className='Cat_Closed_e';
		$('#cat'+CID+'_').animate({left:'-=200'},10); 
		$('#cat'+CID+'_').hide(600);
	} else {
		if(SubCategory[CID])document.getElementById('cat'+CID+'expand').className='Cat_Opened';
		else document.getElementById('cat'+CID+'expand').className='Cat_Opened_e';
		$('#cat'+CID+'_').animate({left:'-=200'},10); 
		$('#cat'+CID+'_').show(600);
	}
}

function Cat_Add(Parent) {
	WindowBoxShow('AddCat',Parent);
}

function Cat_Edit(CID,OriginalID) {
	WindowBoxShow('RenameCat',CID,OriginalID);
}

function Cat_Delete(CID,OriginalID) {
	WindowBoxShow('DeleteCat',CID,OriginalID);
}

SelectedCat='-2';
SelectedCatID=0;
function Cat_Select(CID,OriginalID) {
	if(SelectedCat!='-2')$('#cat'+SelectedCat).attr('class','');
	$('#cat'+CID).attr('class','Cat_Selected');
	document.getElementById('cat'+CID).style.backgroundColor='';
	SelectedCat=CID;
	SelectedCatID=OriginalID;
	ListContacts(OriginalID);
}


function WindowBoxShow(Box,var1,var2) {
	if(Box=='RenameCat') {
		$("#window_title").html('Rename Category: '+document.getElementById('cat'+var1+'title').innerHTML.replace('<small','<small style="display:none"'));
		$("#window_body").html(Lang_Old_Name+': <b>'+document.getElementById('cat'+var1+'title').innerHTML.replace('<small','<small style="display:none"')+'</b><br><label for="CatNewName">'+Lang_New_Name+': </label><input type="text" name="CatNewName" id="CatNewName" size="20" />');
		$("#window_body").append('<p style="padding:10px 0 0 0;margin:0" align="center" dir="ltr"><img id="OkButton" src="_mycontacts/Themes/Simple/Images/window/button_ok.gif" style="cursor:pointer" /> &nbsp;<img src="_mycontacts/Themes/Simple/Images/window/button_cancel.gif" style="cursor:pointer" onclick="WindowBoxHide()" /></p>');
		$("#CatNewName").keypress(function (e) {
			if(e.which==13)$("#OkButton").click();
		});
		$("#OkButton").click(function () {  
			document.getElementById("CatNewName").disabled=true;
			$("#WindowBox").fadeOut(300, 
								function(){
									UseAjax('action=catrename&id='+var2+'&newname='+document.getElementById('CatNewName').value);
									$("#window_title").html('Loading...');
									$("#window_body").html(LoadingHTML);
									$("#WindowBox").fadeIn(600);
								} );
		}); 
	} else if(Box=='AddCat') {
		$("#window_title").html('Add New Category');
		$("#window_body").html('<label for="CatNewName">'+Lang_Cat_Name+': </label><input type="text" name="CatNewName" id="CatNewName" size="20" />');
		$("#window_body").append('<p style="padding:10px 0 0 0;margin:0" align="center" dir="ltr"><img id="OkButton" src="_mycontacts/Themes/Simple/Images/window/button_ok.gif" style="cursor:pointer" /> &nbsp;<img src="_mycontacts/Themes/Simple/Images/window/button_cancel.gif" style="cursor:pointer" onclick="WindowBoxHide()" /></p>');
		$("#CatNewName").keypress(function (e) {
			if(e.which==13)$("#OkButton").click();
		});
		$("#OkButton").click(function () {  
			document.getElementById("CatNewName").disabled=true;
			$("#WindowBox").fadeOut(300, 
								function(){
									UseAjax('action=newcat&parentid='+var1+'&name='+document.getElementById('CatNewName').value);
									$("#window_title").html('Loading...');
									$("#window_body").html(LoadingHTML);
									$("#WindowBox").fadeIn(600);
								} );
		}); 
	} else if(Box=='DeleteCat') {
		$("#window_title").html('Delete Category: '+document.getElementById('cat'+var1+'title').innerHTML);
		$("#window_body").html(Lang_Delete_Cat);
		$("#window_body").append('<p style="padding:10px 0 0 0;margin:0" align="center" dir="ltr"><img id="OkButton" src="_mycontacts/Themes/Simple/Images/window/button_ok.gif" style="cursor:pointer" /> &nbsp;<img src="_mycontacts/Themes/Simple/Images/window/button_cancel.gif" style="cursor:pointer" onclick="WindowBoxHide()" /></p>');
		$("#OkButton").click(function () {  
			UseAjax('action=catdelete&id='+var2);
			$("#WindowBox").fadeOut(600, 
								function(){
									$("#window_title").html('Loading...');
									$("#window_body").html(LoadingHTML);
									$("#WindowBox").fadeIn(600);
								} );
		});
	} else if(Box=='Activation') {
		$("#window_title").html('Account Activation');
		if (var1) $("#window_body").html(Lang_Activation1);
		else $("#window_body").html(Lang_Activation2);
		$("#window_body").append('<p style="padding:10px 0 0 0;margin:0" align="center" dir="ltr"><img id="OkButton" src="_mycontacts/Themes/Simple/Images/window/button_ok.gif" style="cursor:pointer" /></p>');
		$("#OkButton").click(function () {  
			WindowBoxHide();
		}); 
	} else if(Box=='Reset_Pass') {
		$("#window_title").html('Reset Password');
		if (var1) $("#window_body").html(Lang_ResetPass1);
		else $("#window_body").html(Lang_ResetPass2);
		$("#window_body").append('<p style="padding:10px 0 0 0;margin:0" align="center" dir="ltr"><img id="OkButton" src="_mycontacts/Themes/Simple/Images/window/button_ok.gif" style="cursor:pointer" /></p>');
		$("#OkButton").click(function () {  
			WindowBoxHide();
		}); 
	} else if(Box=='Signout') {
		$("#window_title").html('Logout');
		$("#window_body").html(Lang_Signout);
		$("#window_body").append('<p style="padding:10px 0 0 0;margin:0" align="center" dir="ltr"><img id="OkButton" src="_mycontacts/Themes/Simple/Images/window/button_ok.gif" style="cursor:pointer" /> &nbsp;<img src="_mycontacts/Themes/Simple/Images/window/button_cancel.gif" style="cursor:pointer" onclick="WindowBoxHide()" /></p>');
		$("#OkButton").click(function () {  
			UseAjax('action=signout');
			$("#WindowBox").fadeOut(600, 
								function(){
									$("#window_title").html('Loading...');
									$("#window_body").html(LoadingHTML);
									$("#WindowBox").fadeIn(600);
								} );
		}); 
	} else if(Box=='GetContacts') {
		UseAjax('action=getcontacts');
		$("#window_title").html('Loading...');
		$("#window_body").html(LoadingHTML);
	} else if(Box=='ChangeColumns') {
		$("#window_title").html('Columns');
		$("#window_body").html('<p align="center"><select multiple="multiple" name="SelectColumns" id="SelectColumns" size="15" style="width:150px;float:center;overflow:hidden;font-weight:bold">'+
														'<option'+(ShowColumn[3]=='y'?' selected="selected"':'')+' value="3">'+Lang_Column_FirstName+'</option>'+
														'<option'+(ShowColumn[4]=='y'?' selected="selected"':'')+' value="4">'+Lang_Column_LastName+'</option>'+
														'<option disabled="disabled" style="font-weight:normal"><i>'+Lang_Column_PhoneNumbers+'</i></option>'+
														'<option'+(ShowColumn[5]=='y'?' selected="selected"':'')+' value="5"> &nbsp &nbsp '+Lang_Column_Mobile+'</option>'+
														'<option'+(ShowColumn[6]=='y'?' selected="selected"':'')+' value="6"> &nbsp &nbsp '+Lang_Column_Home+'</option>'+
														'<option'+(ShowColumn[7]=='y'?' selected="selected"':'')+' value="7"> &nbsp &nbsp '+Lang_Column_Business+'</option>'+
														'<option'+(ShowColumn[8]=='y'?' selected="selected"':'')+' value="8"> &nbsp &nbsp '+Lang_Column_Fax+'</option>'+
														'<option'+(ShowColumn[9]=='y'?' selected="selected"':'')+' value="9">'+Lang_Column_Email+'</option>'+
														'<option'+(ShowColumn[10]=='y'?' selected="selected"':'')+' value="10">'+Lang_Column_Website+'</option>'+
														'<option'+(ShowColumn[11]=='y'?' selected="selected"':'')+' value="11">'+Lang_Column_Company+'</option>'+
														'<option'+(ShowColumn[12]=='y'?' selected="selected"':'')+' value="12">'+Lang_Column_Job+'</option>'+
														'<option'+(ShowColumn[13]=='y'?' selected="selected"':'')+' value="13">'+Lang_Column_Address+'</option>'+
														'<option'+(ShowColumn[14]=='y'?' selected="selected"':'')+' value="14">'+Lang_Column_Birthday+'</option>'+
														'<option'+(ShowColumn[15]=='y'?' selected="selected"':'')+' value="15">'+Lang_Column_Notes+'</option>'+
														'<option'+(ShowColumn[16]=='y'?' selected="selected"':'')+' value="16">'+Lang_Column_CreationDate+'</option>'+
												'</select></p>');
		$("#window_body").append('<p style="padding:10px 0 0 0;margin:0" align="center" dir="ltr"><img id="OkButton" src="_mycontacts/Themes/Simple/Images/window/button_ok.gif" style="cursor:pointer" /> &nbsp;<img src="_mycontacts/Themes/Simple/Images/window/button_cancel.gif" style="cursor:pointer" onclick="WindowBoxHide()" /></p>');
		$("#OkButton").click(function () {  
			for(j=3;j<=16;j++){
				ShowColumn[j]='n';
			}
			ShowColumnTxt='';
			$("#SelectColumns option:selected").each(function () { 
				ShowColumn[$(this).attr('value')]='y';
				if(ShowColumnTxt!='')ShowColumnTxt+=',';
				ShowColumnTxt+=$(this).attr('value');
				}); 
			UseAjax('action=savecolumns&columns='+ShowColumnTxt);
			Cat_Select(SelectedCat,SelectedCatID);
			WindowBoxHide("LoadingBox");
		}); 
	} else if(Box=='ContactDelete') {
		$("#window_title").html('Delete Contact');
		$("#window_body").html(Lang_Delete_Contact);
		$("#window_body").append('<p style="padding:10px 0 0 0;margin:0" align="center" dir="ltr"><img id="OkButton" src="_mycontacts/Themes/Simple/Images/window/button_ok.gif" style="cursor:pointer" /> &nbsp;<img src="_mycontacts/Themes/Simple/Images/window/button_cancel.gif" style="cursor:pointer" onclick="WindowBoxHide()" /></p>');
		$("#OkButton").click(function () {  
			UseAjax('action=contactdelete&id='+var1);
			$("#WindowBox").fadeOut(600, 
								function(){
									$("#window_title").html('Loading...');
									$("#window_body").html(LoadingHTML);
									$("#WindowBox").fadeIn(600);
								} );
		}); 
	} else if(Box=='SaveContact') {
		$("#window_title").html('Loading...');
		$("#window_body").html(LoadingHTML);
		UseAjax('action=editcontact&id='+var2+'&category='+($('#Contact'+var1+' #fe1 option:selected').attr('value').replace('_','>').replace(/\n/g,'%26'))+
											'&firstname='+($('#Contact'+var1+' #fe3').attr('value').replace(/\n/g,'%26'))+
											'&lastname='+($('#Contact'+var1+' #fe4').attr('value').replace(/\n/g,'%26'))+
											'&mobile='+($('#Contact'+var1+' #fe5').attr('value').replace(/"/g,'&quot;').replace(/\n/g,'"').replace(/\n/g,'%26'))+
											'&home='+($('#Contact'+var1+' #fe6').attr('value').replace(/"/g,'&quot;').replace(/\n/g,'"').replace(/\n/g,'%26'))+
											'&work='+($('#Contact'+var1+' #fe7').attr('value').replace(/"/g,'&quot;').replace(/\n/g,'"').replace(/\n/g,'%26'))+
											'&fax='+($('#Contact'+var1+' #fe8').attr('value').replace(/"/g,'&quot;').replace(/\n/g,'"').replace(/\n/g,'%26'))+
											'&email='+($('#Contact'+var1+' #fe9').attr('value').replace(/"/g,'&quot;').replace(/\n/g,'"').replace(/\n/g,'%26'))+
											'&website='+($('#Contact'+var1+' #fe10').attr('value').replace(/"/g,'&quot;').replace(/\n/g,'"').replace(/\n/g,'%26'))+
											'&company='+($('#Contact'+var1+' #fe11').attr('value').replace(/\n/g,'%26'))+
											'&job='+($('#Contact'+var1+' #fe12').attr('value').replace(/\n/g,'%26'))+
											'&address='+($('#Contact'+var1+' #fe13').attr('value').replace(/"/g,'&quot;').replace(/\n/g,'"').replace(/\n/g,'%26'))+
											'&birthday='+($('#Contact'+var1+' #fe14').attr('value').replace(/\n/g,'%26'))+
											'&notes='+($('#Contact'+var1+' #fe15').attr('value').replace(/\n/g,'%26'))
			);
	} else if(Box=='AddNewContact') {
		$("#window_title").html('Add New Contact');
		$("#window_body").html('<label for="NewFirstName">'+Lang_Column_FirstName+': <input type="text" name="NewFirstName" id="NewFirstName" size="20" /></label><br><label for="NewLastName">'+Lang_Column_LastName+': </label><input type="text" name="NewLastName" id="NewLastName" size="20" /></label><br>'+Lang_Category+': <select id="NewContactCat">'+CatsOptions.replace('0_'+SelectedCatID+'"','0_'+SelectedCatID+'" selected')+'</select>');
		$("#window_body").append('<p style="padding:10px 0 0 0;margin:0" align="center" dir="ltr"><img id="OkButton" src="_mycontacts/Themes/Simple/Images/window/button_ok.gif" style="cursor:pointer" /> &nbsp;<img src="_mycontacts/Themes/Simple/Images/window/button_cancel.gif" style="cursor:pointer" onclick="WindowBoxHide()" /></p>');
		$("#OkButton").click(function () {  
			UseAjax('action=addcontact&category='+($('#NewContactCat option:selected').attr('value').replace('_','>'))+
											'&firstname='+($('#NewFirstName').attr('value'))+
											'&lastname='+($('#NewLastName').attr('value')) ); 
			$("#WindowBox").fadeOut(600, 
								function(){
									$("#window_title").html('Loading...');
									$("#window_body").html(LoadingHTML);
									$("#WindowBox").fadeIn(600);
									})
								} );
	} else {
		$("#window_title").html('Loading...');
		$("#window_body").html(LoadingHTML);
	}
	$("#WindowTbl").css('display','');
	window.scrollTo(0,0);
	$("body").css('overflow','hidden');
	document.getElementById('WindowBox').style.left=(($(document).width()-352)/2)+'px';
	document.getElementById('WindowBox').style.top=(($(window).height()-130)/2)+'px';
	$("#WindowBg").fadeIn(260);
	$("#WindowBox").fadeIn(600);
}
function WindowBoxHide() {
	function oncomp(){
						$("#WindowTbl").css('display','none');
						$("#WindowBg").fadeOut(160,
											function(){$("#WindowBg").css("display","none");$("body").css('overflow','');}
											);}
	$("#WindowTbl").fadeOut(600,function(){oncomp()});
}

ajaxobject='';
function UseAjax(queries){
	ajaxobject=$.ajax({ 
		type: "POST", 
		cache: false, 
		dataType: "script" ,
		data: "ajax=true&"+queries, 
		success: function(msg){ 
			if(msg.indexOf("</b> on line <b>")>0)alert( "Server-Side Error.\nPlease try again.\nIf problem persists contact Administrator. (E-Mail:Ardalan.Naghshineh"+"@"+"gmail.com)");//+"\n"+msg); 
		} 
	});
}

var CatsOptions='';
function ListCats() {
	CatsOptions='<option value="0_0">'+Lang_Uncategorized+'</option>';
	ret='<tr id="cat-3"><td class="CatIcon" onclick="Cat_Select(-3,-3)"></td><td class="Cat_Title" id="cat-1title" onclick="Cat_Select(-3,-3)" colspan="2">'+Lang_All+' <small>('+CatContacts(-3)+')</small></td><td></td><td></td></tr>';
	ret=ret+'<tr id="cat-1"><td></td><td class="CatIcon" onclick="Cat_Select(-1,-1)"></td><td class="Cat_Title" id="cat-1title" onclick="Cat_Select(-1,-1)">'+Lang_Uncategorized+' <small>('+CatContacts(-1)+')</small></td><td></td><td></td></tr>';
	for(i=0;Category[i];i++){
		CatsOptions+='<option value="0_'+CategoryID[i]+'">'+Category[i]+'</option>\n';
		if(SubCategory[i]){
			ret=ret+('<tr id="cat'+i+'"><td class="Cat_Closed" onclick="Cat_Expand(\''+i+'\')" id="cat'+i+'expand"></td><td class="CatIcon" onclick="Cat_Select('+i+','+CategoryID[i]+')"></td><td class="Cat_Title" id="cat'+i+'title" onclick="Cat_Select('+i+','+CategoryID[i]+')" nowrap>'+Category[i]+' <small>('+CatContacts(CategoryID[i])+')</small></td><td class="Cat_Edit" onclick="Cat_Edit('+i+','+CategoryID[i]+')"></td><td onclick="Cat_Delete('+i+','+CategoryID[i]+')" class="Cat_Delete"></td></tr>');
			ret=ret+('<tr><td colspan="5" width="100%" style="padding:0;margin:0" class="SubParrentTd"><table cellpadding="0" cellspacing="0" id="cat'+i+'_" style="display:none;width:100%;padding:0;margin:0">');
			for(j=0;SubCategory[i][j];j++){
				ret=ret+('<tr id="cat'+i+'_'+j+'"><td class="SubCat"></td><td class="SubCat_end"></td><td class="CatIcon" onclick="Cat_Select(\''+i+'_'+j+'\','+SubCategoryID[i][j]+')"></td><td class="SubCat_Title" id="cat'+i+'_'+j+'title" onclick="Cat_Select(\''+i+'_'+j+'\','+SubCategoryID[i][j]+')" nowrap>'+SubCategory[i][j]+' <small>('+CatContacts(SubCategoryID[i][j])+')</small></td><td class="Cat_Edit" onclick="Cat_Edit(\''+i+'_'+j+'\','+SubCategoryID[i][j]+')"></td><td class="Cat_Delete" onclick="Cat_Delete(\''+i+'_'+j+'\','+SubCategoryID[i][j]+')"></td></tr>\n');
				CatsOptions+='<option value="'+CategoryID[i]+'_'+SubCategoryID[i][j]+'">'+Category[i]+' > '+SubCategory[i][j]+'</option>\n';
			}
			ret=ret+('<tr id="cat'+i+'_Add" onclick="Cat_Add('+CategoryID[i]+')"><td class="SubCat_Last"></td><td class="SubCat_end"></td><td class="CatIcon_Add"></td><td class="SubCat_Add_Title" id="cat'+i+'_Addtitle" nowrap>'+Lang_AddNewCat+'</td><td></td><td></td></tr>');
			ret=ret+('</table></td></tr>');
		} else {
			ret=ret+('<tr id="cat'+i+'"><td class="Cat_Closed_e" onclick="Cat_Expand(\''+i+'\')" id="cat'+i+'expand"></td><td class="CatIcon" onclick="Cat_Select('+i+','+CategoryID[i]+')"></td><td class="Cat_Title" id="cat'+i+'title" onclick="Cat_Select('+i+','+CategoryID[i]+')" nowrap>'+Category[i]+' <small>('+CatContacts(CategoryID[i])+')</small></td><td class="Cat_Edit" onclick="Cat_Edit('+i+','+CategoryID[i]+')"></td><td onclick="Cat_Delete('+i+','+CategoryID[i]+')" class="Cat_Delete"></td></tr>');
			ret=ret+('<tr><td colspan="5" width="100%" style="padding:0;margin:0"><table cellpadding="0" cellspacing="0" id="cat'+i+'_" style="display:none;width:100%;padding:0;margin:0">');
			ret=ret+('<tr id="cat'+i+'_Add" onclick="Cat_Add('+CategoryID[i]+')"><td class="SubCat_Last"></td><td class="SubCat_end"></td><td class="CatIcon_Add"></td><td class="SubCat_Add_Title" id="cat'+i+'_Addtitle" style="width:143px" nowrap>'+Lang_AddNewCat+'</td></tr>');
			ret=ret+('</table></td></tr>');
		}
	}
	ret=ret+('<tr id="cat-2" onclick="Cat_Add(0)"><td colspan="2" class="CatIcon_Add" style="border-bottom:1px #000000 solid"></td><td colspan="3" class="SubCat_Add_Title" id="cat-2title" style="border-bottom:1px #000000 solid;padding-top:3px;padding-bottom:4px">'+Lang_AddNewCat+'</td></tr>');
	$('#Categories').html(ret);
}

function ListContacts(CatID) {
	DatatableSource='<table cellpadding="0" cellspacing="0" border="0" class="display" id="datatable" style="min-width:490px">';
	DatatableSource+='<thead>';
	DatatableSource+='<tr><th rowspan="2" style="display:none">ID</th>';
	if(ShowColumn[3]=='y')DatatableSource+='<th rowspan="2">'+Lang_Column_FirstName+'</th>';
	if(ShowColumn[4]=='y')DatatableSource+='<th rowspan="2">'+Lang_Column_LastName+'</th>';
	PhoneN=0;
	if(ShowColumn[5]=='y')PhoneN++;
	if(ShowColumn[6]=='y')PhoneN++;
	if(ShowColumn[7]=='y')PhoneN++;
	if(ShowColumn[8]=='y')PhoneN++;
	if(PhoneN>1)DatatableSource+='<th colspan="'+PhoneN+'">'+Lang_Column_PhoneNumbers+'</th>';
	else if(PhoneN==1){
		if(ShowColumn[5]=='y')DatatableSource+='<th>'+Lang_Column_Mobile+'</th>';
		if(ShowColumn[6]=='y')DatatableSource+='<th>'+Lang_Column_Home+'</th>';
		if(ShowColumn[7]=='y')DatatableSource+='<th>'+Lang_Column_Business+'</th>';
		if(ShowColumn[8]=='y')DatatableSource+='<th>'+Lang_Column_Fax+'</th>';
	}
	if(ShowColumn[9]=='y')DatatableSource+='<th rowspan="2">'+Lang_Column_Email+'</th>';
	if(ShowColumn[10]=='y')DatatableSource+='<th rowspan="2">'+Lang_Column_Website+'</th>';
	if(ShowColumn[11]=='y')DatatableSource+='<th rowspan="2">'+Lang_Column_Company+'</th>';
	if(ShowColumn[12]=='y')DatatableSource+='<th rowspan="2">'+Lang_Column_Job+'</th>';
	if(ShowColumn[13]=='y')DatatableSource+='<th rowspan="2">'+Lang_Column_Address+'</th>';
	if(ShowColumn[14]=='y')DatatableSource+='<th rowspan="2">'+Lang_Column_Birthday+'</th>';
	if(ShowColumn[15]=='y')DatatableSource+='<th rowspan="2">'+Lang_Column_Notes+'</th>';
	if(ShowColumn[16]=='y')DatatableSource+='<th rowspan="2">'+Lang_Column_CreationDate+'</th>';
	DatatableSource+='</tr>';
	if(PhoneN>1){
		DatatableSource+='<tr>';
		if(ShowColumn[5]=='y')DatatableSource+='<th>'+Lang_Column_Mobile+'</th>';
		if(ShowColumn[6]=='y')DatatableSource+='<th>'+Lang_Column_Home+'</th>';
		if(ShowColumn[7]=='y')DatatableSource+='<th>'+Lang_Column_Business+'</th>';
		if(ShowColumn[8]=='y')DatatableSource+='<th>'+Lang_Column_Fax+'</th>';
		DatatableSource+='</tr>';
	}
	DatatableSource+='</thead>';
	DatatableSource+='<tbody>';
	
	for(i3=0;Contacts[i3];i3++){
		if(CatID!=-3 && (Contacts[i3][1]!=CatID && Contacts[i3][2]!=CatID) && !(CatID==-1 && Contacts[i3][1]==0 && Contacts[i3][2]==0))continue;
		DatatableSource+='<tr class="gradeU" id="cont'+i3+'tr">';
		DatatableSource+='<td style="display:none" id="cont'+i3+'">'+i3+'</td>';
		for(j3=3;j3<=16;j3++){
			if(ShowColumn[j3]!='y')continue;
			DatatableSource+='<td'+((j3==14 || j3==16)?' align="center"':'')+((5<=j3 && j3<=10)?' dir="ltr"':'')+'>'+Contacts[i3][j3].replace(/"/g,'<br>')+'</td>';
		}
		DatatableSource+='</tr>';
	}
	
	DatatableSource+='</tbody>';
	DatatableSource+='<tfoot>';
	DatatableSource+='<tr><th style="display:none">ID</th>';
	if(ShowColumn[3]=='y')DatatableSource+='<th>'+Lang_Column_FirstName+'</th>';
	if(ShowColumn[4]=='y')DatatableSource+='<th>'+Lang_Column_LastName+'</th>';
	if(PhoneN>1)DatatableSource+='<th colspan="'+PhoneN+'">'+Lang_Column_PhoneNumbers+'</th>';
	else if(PhoneN==1){
		if(ShowColumn[5]=='y')DatatableSource+='<th>'+Lang_Column_Mobile+'</th>';
		if(ShowColumn[6]=='y')DatatableSource+='<th>'+Lang_Column_Home+'</th>';
		if(ShowColumn[7]=='y')DatatableSource+='<th>'+Lang_Column_Business+'</th>';
		if(ShowColumn[8]=='y')DatatableSource+='<th>'+Lang_Column_Fax+'</th>';
	}
	if(ShowColumn[9]=='y')DatatableSource+='<th>'+Lang_Column_Email+'</th>';
	if(ShowColumn[10]=='y')DatatableSource+='<th>'+Lang_Column_Website+'</th>';
	if(ShowColumn[11]=='y')DatatableSource+='<th>'+Lang_Column_Company+'</th>';
	if(ShowColumn[12]=='y')DatatableSource+='<th>'+Lang_Column_Job+'</th>';
	if(ShowColumn[13]=='y')DatatableSource+='<th>'+Lang_Column_Address+'</th>';
	if(ShowColumn[14]=='y')DatatableSource+='<th>'+Lang_Column_Birthday+'</th>';
	if(ShowColumn[15]=='y')DatatableSource+='<th>'+Lang_Column_Notes+'</th>';
	if(ShowColumn[16]=='y')DatatableSource+='<th>'+Lang_Column_CreationDate+'</th>';
	DatatableSource+='</tr>';
	DatatableSource+='</tfoot>';
	DatatableSource+='</table>';
	$("#MainTD").html('');
	$("#MainTD").html(DatatableSource);
	DrawContactsTable();
}

function CatContacts(CatID) {
	CatContactsCount=0;
	if(CatID==-3){
		for(i2=0;Contacts[i2];i2++){
			CatContactsCount++;
		}
	} else if(CatID==-1){
		for(i2=0;Contacts[i2];i2++){
			if(Contacts[i2][2]==0 && Contacts[i2][1]==0)CatContactsCount++;
		}
	} else {
		for(i2=0;Contacts[i2];i2++){
			if(Contacts[i2][2]==CatID || Contacts[i2][1]==CatID)CatContactsCount++;
		}
	}
	return CatContactsCount;
}

function ShowDetails(ContID) {
	ret='<form id="Contact'+ContID+'" onsubmit="return false;"><table cellspacing="0" cellpadding="0" border="0" style="width:100%"><tr><td valign="top"><h1>'+Lang_ContactDetails+'</h1>';
	if(Contacts[ContID][3]!='')ret+=Lang_Column_FirstName+': <span id="f3">'+Contacts[ContID][3]+'</span><br>';
	else ret+='<div style="display:none">'+Lang_Column_FirstName+': <span id="f3"></span><br></div>';
	if(Contacts[ContID][4]!='')ret+=Lang_Column_LastName+': <span id="f4">'+Contacts[ContID][4]+'</span><br>';
	else ret+='<div style="display:none">'+Lang_Column_LastName+': <span id="f4"></span><br></div>';
	if(Contacts[ContID][11]!='')ret+=Lang_Column_Company+': <span id="f11">'+Contacts[ContID][11]+'</span><br>';
	else ret+='<div style="display:none">'+Lang_Column_Company+': <span id="f11"></span><br></div>';
	if(Contacts[ContID][12]!='')ret+=Lang_Column_Job+': <span id="f12">'+Contacts[ContID][12]+'</span><br>';
	else ret+='<div style="display:none">'+Lang_Column_Job+': <span id="f12"></span><br></div>';
	if(Contacts[ContID][14]!='')ret+=Lang_Column_Birthday+': <span id="f14">'+Contacts[ContID][14]+'</span><br>';
	else ret+='<div style="display:none">'+Lang_Column_Birthday+': <span id="f14"></span><br></div>';
	if(Contacts[ContID][15]!='')ret+=Lang_Column_Notes+': <span id="f15">'+Contacts[ContID][15]+'</span><br>';
	else ret+='<div style="display:none">'+Lang_Column_Notes+': <span id="f15"></span><br></div>';
	ret+=Lang_Column_CreationDate+': <span>'+Contacts[ContID][16]+'</span><br>';
	ret+='<div style="display:none">'+Lang_Category+': <span id="f1"></span><br></div>';
	ret+='<br></td><td valign="top" style="padding: 7px 0 7px 0" rowspan="2">';
	
	PhoneN=0;
	if(Contacts[ContID][5]!='')PhoneN++;
	if(Contacts[ContID][6]!='')PhoneN++;
	if(Contacts[ContID][7]!='')PhoneN++;
	if(Contacts[ContID][8]!='')PhoneN++;
	
	ret+='<table cellspacing="0" cellpadding="0" border="0" style="min-width:200px;padding:5px 15px 15px 15px;float:'+(Dir=='ltr'?'right':'left')+'" class="coninfo">';
	if(PhoneN>0)ret+='<tr><td style="color:#009cff"><h3 style="padding-top:8px;margin:0">'+Lang_Column_PhoneNumbers+'</h3></td></tr><tr><td><ul>';
	else ret+='<tr><td style="color:#009cff"><div style="display:none"><h3 style="padding-top:8px;margin:0">'+Lang_Column_PhoneNumbers+'</h3></div></td></tr><tr><td><ul>';
	if(Contacts[ContID][5]!='')ret+='<li>'+Lang_Column_Mobile+'<br><ul id="f5"><li dir="ltr">'+Contacts[ContID][5].replace(/"/g,'</li><li dir="ltr">')+'</li></ul></li>';
	else ret+='<div style="display:none"><li>'+Lang_Column_Mobile+'<br><ul id="f5"><li dir="ltr"></li></ul></li></div>';
	if(Contacts[ContID][6]!='')ret+='<li>'+Lang_Column_Home+'<br><ul id="f6"><li dir="ltr">'+Contacts[ContID][6].replace(/"/g,'</li><li dir="ltr">')+'</li></ul></li>';
	else ret+='<div style="display:none"><li>'+Lang_Column_Home+'<br><ul id="f6"><li dir="ltr"></li></ul></li></div>';
	if(Contacts[ContID][7]!='')ret+='<li>'+Lang_Column_Business+'<br><ul id="f7"><li dir="ltr">'+Contacts[ContID][7].replace(/"/g,'</li><li dir="ltr">')+'</li></ul></li>';
	else ret+='<div style="display:none"><li>'+Lang_Column_Business+'<br><ul id="f7"><li dir="ltr"></li></ul></li></div>';
	if(Contacts[ContID][8]!='')ret+='<li>'+Lang_Column_Fax+'<br><ul id="f8"><li dir="ltr">'+Contacts[ContID][8].replace(/"/g,'</li><li dir="ltr">')+'</li></ul></li>';
	else ret+='<div style="display:none"><li>'+Lang_Column_Fax+'<br><ul id="f8"><li dir="ltr"></li></ul></li></div>';
	ret+='</ul></td></tr>';
	if(Contacts[ContID][9]!='')ret+='<tr><td style="color:#009cff"><h3 style="padding-top:8px;margin:0">'+Lang_Column_Email+'</h3></td></tr><tr><td><ul id="f9"><li dir="ltr">'+Contacts[ContID][9].replace(/"/g,'</li><li dir="ltr">')+'</li></ul></td></tr>';
	else ret+='<tr><td style="color:#009cff"><div style="display:none"><h3 style="padding-top:8px;margin:0">'+Lang_Column_Email+'</h3></div></td></tr><tr><td><div style="display:none"><ul id="f9"><li dir="ltr"></li></ul></div></td></tr>';
	if(Contacts[ContID][10]!='')ret+='<tr><td style="color:#009cff"><h3 style="padding-top:8px;margin:0">'+Lang_Column_Website+'</h3></td></tr><tr><td><ul id="f10"><li dir="ltr">'+Contacts[ContID][10].replace(/"/g,'</li><li dir="ltr">')+'</li></ul></td></tr>';
	else ret+='<tr><td style="color:#009cff"><div style="display:none"><h3 style="padding-top:8px;margin:0">'+Lang_Column_Website+'</h3></div></td></tr><tr><td><div style="display:none"><ul id="f10"><li dir="ltr"></li></ul></div></td></tr>';
	if(Contacts[ContID][13]!='')ret+='<tr><td style="color:#009cff"><h3 style="padding-top:8px;margin:0">'+Lang_Column_Address+'</h3></td></tr><tr><td><ul id="f13"><li>'+Contacts[ContID][13].replace(/"/g,'</li><li>')+'</li></ul></td></tr>';
	else ret+='<tr><td style="color:#009cff"><div style="display:none"><h3 style="padding-top:8px;margin:0">'+Lang_Column_Address+'</h3></div></td></tr><tr><td><div style="display:none"><ul id="f13"><li dir="ltr"></li></ul></div></td></tr>';
	ret+='</table>';
	ret+='</td></tr><tr><td valign="bottom" style="padding:0 0 6px 0"><input class="button2" id="ContactEdit" name="ContactEdit" value="'+Lang_Edit+'" type="submit" style="height:20px;padding-bottom:3px" onclick="Edit_Contact('+ContID+');return false;"> &nbsp <input class="button2" id="DeleteContact" name="DeleteContact" value="'+Lang_Delete+'" type="button" style="height:20px;padding-bottom:3px" onclick="WindowBoxShow(\'ContactDelete\',\''+Contacts[ContID][0]+'\')">';
	ret+='</td></tr></table></form>';
	return ret;
}

function Edit_Contact(ContactID) {
	if(ContactID!=-3){
		$('form#Contact'+ContactID+' span , form#Contact'+ContactID+' ul').each(function() {
			if($(this).attr('id')!=''){
				tempid=$(this).attr('id').replace('f','');
				if(5<=tempid && tempid<=10){
					$(this).after('<textarea dir="ltr" id="fe'+tempid+'" cols="32" rows="2">'+Contacts[ContactID][tempid].replace(/"/g,'\n')+'</textarea>');
				} else if(tempid==1){
					$(this).after('<select id="fe'+tempid+'">'+CatsOptions.replace(Contacts[ContactID][1]+'_'+Contacts[ContactID][2]+'"',Contacts[ContactID][1]+'_'+Contacts[ContactID][2]+'" selected')+'</select>');
				} else if(tempid==14){
					$(this).after('<input type="text" id="fe14" value="'+Contacts[ContactID][14]+'" maxlength="10" />');
				} else if(tempid==15 || tempid==13){
					$(this).after('<textarea id="fe'+tempid+'" cols="32" rows="2">'+Contacts[ContactID][tempid].replace(/"/g,'\n')+'</textarea>');
				} else {
					$(this).after('<input type="text" id="fe'+tempid+'" value="'+Contacts[ContactID][tempid]+'" maxlength="90" />');
				}
				$('#'+$(this).attr('id')).remove();
			}
		});
		$('form#Contact'+ContactID+' div').each(function() {
			$(this).css('display','');
		});
		$("#ContactEdit").attr('value',Lang_Save);
		$("#ContactEdit").removeAttr('onclick');
		$("#Contact"+ContactID).removeAttr('onsubmit');
		$("#Contact"+ContactID).submit(function(){WindowBoxShow('SaveContact',ContactID,Contacts[ContactID][0]);return false;});
		$("#DeleteContact").attr('value',Lang_Cancel);
		$("#DeleteContact").removeAttr('onclick');
		$("#DeleteContact").click(function(){$('#cont'+ContactID).click();$('#cont'+ContactID).click(); });

	} else {
	
	}
}