function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function popup(url,width,height) {
	newwindow=window.open(url,'name','height='+height+',width='+width+'');
	if (window.focus) {newwindow.focus()}
	window.print();
	return false;
}

function popupWind(path,w,h,title) {
	var wid=parseInt(w);
	var hei=parseInt(h);
	var leftVal=(screen.width / 2) - parseInt(w/2);
	var topVal = (screen.height / 2) - parseInt(h/2);
	window.open(path,'','toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes, width='+wid+', height='+hei+', left = '+leftVal+', top='+topVal+' ');
}


var letter_obj = null;
var last_tab='tab';
var selected_contacts=new Array();
var rss_feed = null; //first feed in list by default
var rss_title = null; //first title from frist feed list by default

function doSearchContacts(userID,letter,type,page) {

	selected_contacts=new Array();
	update_number();
	if(letter_obj == null) {
		letter_obj=$("letter_");
	}
	remove_selection(letter_obj);
	select_tab($("letter_"+letter));

	var ajax = new Ajax.Request (
			"lib/handler.php",
			{
				parameters:"&action_name=show_contacts&user_id="+userID
							+"&letter="+letter
							+"&type="+type
							+"&page="+page, 
				method:'post', 
				
				onFailure: function (t){ 
					alert("ajax communication error occured"); 					
				},
				onLoading: function(t){
					//Loading
					$("item_contacts").innerHTML="<div align='center' style='padding:10px' class='common_text'>"
												+"<img src='img/loader.gif' align='top'>&nbsp;<b>Loading...</b>"
				},
				onComplete: function (t) {
				
					$("item_contacts").innerHTML=t.responseText;
				}
			} 
	 );				
}


function doShowContactDetails(userID)
{
	var ajax = new Ajax.Request (
			"lib/handler.php",
			{
				parameters:"&action_name=show_contact_details&user_id="+userID, 
				method:'post', 
				
				onFailure: function (t){ 
					alert("ajax communication error occured"); 					
				},
				onLoading: function(t){
					//Loading
					$("contact_details").innerHTML="<div align='center' style='padding:10px' class='common_text'>"
												+"<img src='img/loader.gif' align='top'>&nbsp;<b>Loading...</b>"
				},
				onComplete: function (t) {
				
					$("contact_details").innerHTML=t.responseText;
					//$("contact_details").innerHTML=userID;
				}
			} 
	 );				
}

function doAddContact(userID)
{
	var ajax = new Ajax.Request (
			"lib/handler.php",
			{
				parameters:"&action_name=add_contact&user_id="+userID, 
				method:'post', 
				
				onFailure: function (t){ 
					alert("ajax communication error occured"); 					
				},
				onLoading: function(t){
					//Loading
					$("page_content").innerHTML="<div  align='center' style='padding:10px' class='common_text'>"
												+"<img src='img/loader.gif' align='top'>&nbsp;<b>Loading...</b>"
				},
				onComplete: function (t) {
				
					$("page_content").innerHTML=t.responseText;
					//$("contact_details").innerHTML=userID;
				}
			} 
	 );				
}

function doSaveContact(userID)
{
	var ajax = new Ajax.Request (
			"lib/handler.php",
			{
				parameters:"&action_name=save_contact&user_id="+userID
							+"&form="+escape(Form.serialize($("contact_form"))), 
				method:'post', 
				
				onFailure: function (t){ 
					alert("ajax communication error occured"); 					
				},
				onLoading: function(t){
					//Loading
					$("frm_content").innerHTML="<div  align='center' style='padding=10px' class='common_text'>"
												+"<img src='img/loader.gif' align='top'>&nbsp;<b>Saving Contact...</b>"
				},
				onComplete: function (t) {
				
					$("frm_content").innerHTML=t.responseText;
					//$("contact_details").innerHTML=userID;
				}
			} 
	 );				
}

function doShowNames(userID) {

	var ajax = new Ajax.Request (
			"lib/handler.php",
			{
				parameters:"&action_name=show_names&user_id="+userID, 
				method:'post', 
				
				onFailure: function (t){ 
					alert("ajax communication error occured"); 					
				},
				onLoading: function(t){
					//Loading
					$("page_content").innerHTML="<div  align='center' style='padding:10px' class='common_text'>"
												+"<img src='img/loader.gif' align='top'>&nbsp;<b>Getting contacts...</b>"
				},
				onComplete: function (t) {
				
					$("page_content").innerHTML=t.responseText;
					select_tab($("letter_"));
					//$("contact_details").innerHTML=userID;
				}
			} 
	 );				
}


function deleteContact(userID,type) {

	for(i=0;i<selected_contacts.length;i++) {

		var ajax = new Ajax.Request (
				"lib/handler.php",
				{
					parameters:"&action_name=delete_contacts&user_id="+userID
								+"&type="+type
								+"&cid="+selected_contacts[i], 
					method:'post', 

					onFailure: function (t){ 
						alert("ajax communication error occured"); 					
					},
					onLoading: function(t){
						//Loading
						$("item_contacts").innerHTML="<div align='center' style='padding:10px' class='common_text'>"
													+"<img src='img/loader.gif' align='top'>&nbsp;<b>Loading...</b>"
					},
					onComplete: function (t) {
						$("item_contacts").innerHTML=t.responseText;
					}
				}
		 );

	}

}


function select_tab(obj_tab) {

	if(obj_tab.className == "tab_last") {
		last_tab="tab_last";
	} else {
		last_tab="tab";
	}
	obj_tab.className="tab_open";
//	obj_tab.style.backgroundColor='1px solid #ffffff;'
	letter_obj=$("letter_"+(obj_tab.innerHTML.toLowerCase()));

}


function remove_selection(obj_tab) {

	obj_tab.className=last_tab;	

}


function doSelectCard(obj) {

	id=obj.id.replace("contact_","");
	if(!is_duplicate(id)) {
		selected_contacts.push(id);
		obj.style.backgroundColor='#EFF4FC';
		/*card=$("card_"+id);
		card.innerHTML="<input type='checkbox' readonly='yes' checked>";*/
	} else {
		selected_contacts=remove_selected_contact(id);
		//unSelectCard(obj);
		obj.style.backgroundColor='#ffffff';
	}
	update_number();
	//alert(selected_contacts);
	//alert(obj.id);

}


function unSelectCard(obj) {
	id=obj.id.replace("contact_","");
	card=$("card_"+id);
	card.innerHTML="&nbsp;";	
}


function doSelectGrid(obj_id) {

	obj=$(obj_id)
	id=obj.id.replace("grid_","");
	if(!is_duplicate(id)) {
		selected_contacts.push(id);
		obj.style.backgroundColor='#EFF4FC';
	} else {
		selected_contacts=remove_selected_contact(id);
		obj.style.backgroundColor='#ffffff';
	}
	update_number();
	//alert(selected_contacts);
	//alert(obj.id);
}


function is_duplicate(id) {

	flag=false;
	for(i=0;i<selected_contacts.length;i++) {
		if(selected_contacts[i]==id) {
			flag=true;
		}
	}
	return flag;

}


function remove_selected_contact(id) {

	tmp=new Array();
	for(i=0;i<selected_contacts.length;i++) {
		if(selected_contacts[i]!=id) {
			tmp.push(selected_contacts[i]);
		}
	}
	return tmp;

}


function update_number() {

	$("sel_contacts").innerHTML=selected_contacts.length;

}


function addEvent( obj, type, fn ) {

	if(obj.attachEvent) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	} else {
        obj.addEventListener( type, fn, false );
	}

}


function removeEvent( obj, type, fn ) {

	if ( obj.detachEvent ) {
		obj.detachEvent( 'on'+type, obj[type+fn] );
		obj[type+fn] = null;
	} else {
		obj.removeEventListener( type, fn, false );
 	}

}
