/* ------------------------------------------------------------------------------------------------ */
/* tablet_sort																						*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_sort (by)
{
	oForm = document.getElementById("tabletSortForm");

	if (oForm.sortBy.value == by)
	{
		if (oForm.sortDir.value == "asc")
			tablet_sortDown (by);
		else
			tablet_sortUp   (by);
	}
	else
		tablet_sortUp (by);
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_sortUp																					*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_sortUp (by)
{
	oForm = document.getElementById("tabletSortForm");

	oForm.sortBy.value  = by;
	oForm.sortDir.value = "asc";

	oForm.submit ();
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_sortDown																					*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_sortDown (by)
{
	oForm = document.getElementById("tabletSortForm");

	oForm.sortBy.value  = by;
	oForm.sortDir.value = "desc";

	oForm.submit ();
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_overRow																					*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_overRow (id)
{
	className = document.getElementById("tabletRow" + id).className;

	if (className.indexOf("_open") == -1)
		document.getElementById("tabletRow" + id).className += "_open";
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_outRow																					*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_outRow (id)
{
	className = document.getElementById("tabletRow" + id).className;

	if (document.getElementById("details"   + id).style.display	== "none" && className.indexOf("_open") != -1)
		document.getElementById("tabletRow" + id).className		= className.substr(0,className.length-5);
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_openCloseRow																				*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_openCloseRow (id)
{
	className = document.getElementById("tabletRow" + id).className;

	if (document.getElementById("details"   + id).style.display	== "none")
	{
		if (className.indexOf("_open") == -1)
			document.getElementById("tabletRow" + id).className += "_open";
		document.getElementById("details"   + id).style.display	= "";
	}
	else
	{
		if (className.indexOf("_open") != -1)
			document.getElementById("tabletRow" + id).className	= className.substr(0,className.length-5);
		document.getElementById("details"   + id).style.display	= "none";
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_openRow																					*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_openRow (id)
{
	document.getElementById("details"   + id).style.display	= "";

	className = document.getElementById("tabletRow" + id).className;

	if (className.indexOf("_open") == -1)
		document.getElementById("tabletRow" + id).className += "_open";
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_closeRow																					*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_closeRow (id)
{
	document.getElementById("details"   + id).style.display	= "none";

	var className = document.getElementById("tabletRow" + id).className;

	document.getElementById("tabletRow" + id).className		= className.substr(0,className.length-5);
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_markAll																					*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_markAll ()
{
	oTbl	= document.getElementById ("tabletTable").getElementsByTagName("tbody")[0];

	for (i=1; i<oTbl.rows.length-1; i++)
	{
		oTbl.rows[i].cells[0].childNodes[0].checked = true;
		i = i+2;
	}

	document.getElementById("markAll").style.display   = "none";
	document.getElementById("unmarkAll").style.display = "";
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_unmarkAll																					*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_unmarkAll ()
{
	oTbl	= document.getElementById ("tabletTable").getElementsByTagName("tbody")[0];

	for (i=1; i<oTbl.rows.length-1; i++)
	{
		oTbl.rows[i].cells[0].childNodes[0].checked = false;
		i = i+2;
	}

	document.getElementById("markAll").style.display   = "";
	document.getElementById("unmarkAll").style.display = "none";
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_printSelected																				*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_printSelected (type, tabletId)
{
	// find selected
	itemIds = "";

	oTbl	= document.getElementById ("tabletTable").getElementsByTagName("tbody")[0];

	for (i=1; i<oTbl.rows.length-1; i++)
	{
		if (oTbl.rows[i].cells[0].childNodes[0].checked)
		{
			if (itemIds != "") itemIds += ",";

			itemIds += oTbl.rows[i].id.substr(9,oTbl.rows[i].id.length);;
		}
		i = i+2;
	}

	if (itemIds == "")
	{
		alert ("יש לבחור לפחות מודעה אחת להדפסה");
	}
	else
	{
		if (type == "nadlan")
			window.open ("index2.php?id=23&tabletId=" + tabletId + "&itemIds=" + itemIds);
		else
			window.open ("index2.php?id=24&tabletId=" + tabletId + "&itemIds=" + itemIds);
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_printThisItem																				*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_printThisItem (type, tabletId, itemId)
{
	if (type == "nadlan")
		window.open ("index2.php?id=23&tabletId=" + tabletId + "&itemIds=" + itemId);
	else
		window.open ("index2.php?id=24&tabletId=" + tabletId + "&itemIds=" + itemId);
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_showPhone																					*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_showPhone (itemType, itemId, phoneField)
{
	xml = "<data>" +
				"<command>private.getTabletItemPhone</command>"			   	+
				"<itemType>" 	+ itemType			+ "</itemType>"			+
				"<itemId>"	 	+ itemId	      	+ "</itemId>" 			+
				"<phoneField>"	+ phoneField	    + "</phoneField>" 		+
		  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "tablet_showPhone_response");
	return false;
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_showPhone_response																		*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_showPhone_response ()
{
	xmlRequest.init(commonDecode(asyncHttpObj.responseText));
						
	try
	{
		var itemId  	= xmlRequest.getValue("itemId");
		var phoneField  = xmlRequest.getValue("phoneField");
		var phoneNumber = xmlRequest.getValue("phoneNumber");
	}
	catch (e)
	{
		alert ("AJAX Error");
		return false;
	}

	document.getElementById(phoneField + "Link_" + itemId).style.display = "none";
	document.getElementById(phoneField + "_" + itemId).innerHTML = phoneNumber;
}

var currPic = 1;

/* ------------------------------------------------------------------------------------------------ */
/* tablet_showNextPic																				*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_showNextPic (itemType, itemId)
{
	currPic = currPic+1;
	if (currPic > document.getElementById("tabletItemNumPics_" + itemId).innerHTML)
	{
		currPic = 1;	
	}

	tablet_getPic (itemType, itemId);
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_showPrevPic																				*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_showPrevPic (itemType, itemId)
{
	currPic = currPic-1;
	if (currPic == 0)
	{
		currPic = document.getElementById("tabletItemNumPics_" + itemId).innerHTML;	
	}

	tablet_getPic (itemType, itemId);
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_getPic																					*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_getPic (itemType, itemId)
{
	xml = "<data>" +
				"<command>private.getTabletItemPic</command>"			   	+
				"<itemType>" 	+ itemType			+ "</itemType>"			+
				"<itemId>"	 	+ itemId	      	+ "</itemId>" 			+
				"<picPos>"	 	+ currPic	      	+ "</picPos>" 			+
		  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "tablet_getPic_response");
	return false;
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_getPic_response																			*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_getPic_response ()
{
	xmlRequest.init(commonDecode(asyncHttpObj.responseText));
						
	try
	{
		var itemId 	 = xmlRequest.getValue("itemId");
		var fileName = xmlRequest.getValue("fileName");
	}
	catch (e)
	{
		return false;
	}

	var picBigFileName = fileName.split("size1").join("size0");

	document.getElementById("tabletItemViewPicBig_" + itemId).href = picBigFileName;

	var newImage = new Image();
	newImage.src = fileName;
	document.getElementById("tabletItemSmallPic_" + itemId).src = newImage.src;

	document.getElementById("tabletItemCurrPicNum_" + itemId).innerHTML = currPic;
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_onSelectArea																				*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_onSelectArea (formName)
{
	var oForm = document.getElementById(formName);

	xml = "<data>" +
				"<command>private.getCities</command>"			   	+
				"<areaId>"   + oForm.areaId.value  	+ "</areaId>" 	+
		  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "tablet_onSelectArea_response");
	return false;
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_onSelectArea_response																		*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_onSelectArea_response ()
{
	xmlRequest.init(commonDecode(asyncHttpObj.responseText));
						
	try
	{
		htmlData = xmlRequest.getValue("htmlData");
		document.getElementById("cityContainer").innerHTML = htmlData;
	}
	catch (e)
	{
		alert ("AJAX Error");
		return false;
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_onSelectCarType																			*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_onSelectCarType ()
{
	var oForm = document.getElementById("tabletSearchForm");

	xml = "<data>" +
				"<command>private.getManufacturers</command>"	   	+
				"<carType>"   + oForm.carType.value  + "</carType>"	+
		  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "tablet_onSelectCarType_response");
	return false;
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_onSelectCarType_response																	*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_onSelectCarType_response ()
{
	xmlRequest.init(commonDecode(asyncHttpObj.responseText));
						
	try
	{
		htmlData = xmlRequest.getValue("htmlData");
		document.getElementById("manufacturerContainer").innerHTML = htmlData;
	}
	catch (e)
	{
		alert ("AJAX Error");
		return false;
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_submitAgentsForm																			*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_submitAgentsForm ()
{
	oForm = document.getElementById("agentsForm");

	if (oForm.agentName.value == "")
	{
		alert ("יש להזין מספר סוכן");
		oForm.agentName.focus ();
		return false;
	}

	tablet_uniqueAgent ();

	return false;
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_uniqueAgent																				*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_uniqueAgent ()
{
	oForm = document.getElementById("agentsForm");

	xml = "<data>" +
				"<command>private.uniqueAgent</command>"			   		+
				"<agentId>"    + oForm.agentId.value  	+ "</agentId>"		+
				"<tablet>"     + oForm.tablet.value  	+ "</tablet>"		+
				"<areaId>"     + oForm.areaId.value  	+ "</areaId>" 		+
				"<cityId>"     + oForm.cityId.value  	+ "</cityId>" 		+
				"<type>"       + oForm.type.value 	 	+ "</type>" 		+
				"<reportMeTo>" + oForm.reportMeTo.value	+ "</reportMeTo>"	+
		  "</data>";

	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "tablet_uniqueAgent_response");
	return false;
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_uniqueAgent_response																		*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_uniqueAgent_response ()
{
	xmlRequest.init(commonDecode(asyncHttpObj.responseText));
						
	try
	{
		success = xmlRequest.getValue("success");
	}
	catch (e)
	{
		alert ("AJAX Error");
		return false;
	}
	
	if (success == "0")
	{
		oForm = document.getElementById("agentsForm");
		oForm.submit();
	}
	else if (success == "1")
	{
		alert ("יש סוכן עם פרטים דומים , אנא מלא סוכן חדש");
	}
		
	return false;
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_deleteAgent																				*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_deleteAgent (ids)
{
	if (confirm("אשר מחיקת הסוכן"))
	{
		location.href = "privateAgentActions.php?action=delete&ids=" + ids;
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* tablet_onSelectTablet																			*/
/* ------------------------------------------------------------------------------------------------ */
function tablet_onSelectTablet ()
{
	if (document.getElementById("agentsForm").tablet.value.indexOf("nadlan_") != -1)
	{
		document.getElementById("nadlanFieldTitle").style.display 	= "";
		document.getElementById("carFieldTitle").style.display    	= "none";

		document.getElementById("nadlanFieldOptions").style.display = "";
		document.getElementById("carFieldOptions").style.display 	= "none";
	}
	else
	{
		document.getElementById("nadlanFieldTitle").style.display 	= "none";
		document.getElementById("carFieldTitle").style.display    	= "";

		document.getElementById("nadlanFieldOptions").style.display = "none";
		document.getElementById("carFieldOptions").style.display 	= "";
	}
}




