//-----------------------------------------------
//	CautaMedic.ro
//	Copyright (c) 2007 - 2008 Sysmart S.R.L.
//	
//	This code is the property of Sysmart S.R.L.
//	Do not copy, distribute, or modify this code. 
//	Un-licensed use of this code is prohibited.
//-----------------------------------------------

function srch_toggleForm(adv){
	var o = getById('searchType');
	if(!o)
		return;
	
	o.value = adv;
	
	var objs = null;
	
	if(adv == 1){
		objs = getElementsByClassName(document, '*', 'srch_adv');
		for(i=0; i<objs.length; i++)
		{	
			o = objs[i];
			o.style.display = '';
		}	
		
		objs = getElementsByClassName(document, '*', 'srch_simp');
		for(i=0; i<objs.length; i++)
		{	
			o = objs[i];
			o.style.display = 'none';
		}		
	}
	
	else		
	if(adv == 0){
		objs = getElementsByClassName(document, '*', 'srch_simp');
		for(i=0; i<objs.length; i++)
		{	
			o = objs[i];
			o.style.display = '';
		}	
		
		objs = getElementsByClassName(document, '*', 'srch_adv');
		for(i=0; i<objs.length; i++)
		{	
			o = objs[i];
			o.style.display = 'none';
		}		
	}
}

function reg_showCC(c){
	var els = getElementsByClassName(document, 'li', 'reg_cc');
	for(i=0; i<els.length; i++)
	{	
		o = els[i];
		o.style.display = c ? "" : "none";		
	}	
}


function reg_showTicket(c){
	var els = getElementsByClassName(document, 'li', 'reg_ticket');
	for(i=0; i<els.length; i++)
	{	
		o = els[i];
		o.style.display = c ? "" : "none";		
	}	
}

function reg_showCCorTicket(){
	reg_showCC(getById('reg_type1').checked);
	reg_showTicket(getById('reg_type2').checked);	
}


function dynRegion(oRegion, oCity, forSearch){	
	var idRegion = comboGetSelectedValue(getById(oRegion));
	
	makeJSONCall('dyn/getCities.php', '', [ idRegion , forSearch ? SEARCH_ONLY_EXISTING_CITIES : 0],
			function(ret) {				
				if (typeof(ret) == 'string' && ret.substr(0, 1) == '*'){			
					alert(ret.substr(1));
				}
				else{
					// Get drop
					var o = getById(oCity);
					
					if(!o)
						return;
						
					// Remove all options except the first one 
					while (o.options.length > 1)
						o.remove(o.options.length-1);	
					
					// Populate drop with new options	
					for (var i = 0; i < ret.length; i++){
						var opt = document.createElement('OPTION');
						o.options.add(opt);
						opt.value = ret[i].idCity;
						if(LIST_IMPORTANT_CITIES['x' + opt.value])
							opt.className = 'important';
						opt.innerHTML = htmlEntities(ret[i].name);						
					}					
				}
			},
		false);	
}

function medicardEdit_delSpecialtyRow(id)
{
	var oItem = getById('specialtyItem_'+id.toString());
	var oContainer = getById('ulSpecialty');

	oContainer.removeChild(oItem);
	
	// Also remove from link hidden field list 
	var oList = getById('specialtyItems');	
	var allIds = oList.value.split(',');
	var newVal = '';
	for (var i = 0; i < allIds.length; i++)
	{
		if (allIds[i] == '' || allIds[i] == id)
			continue;
		if (newVal != '')
			newVal += ',';
		newVal += allIds[i];
	}
	oList.value = newVal;		 
}

function medicardEdit_addSpecialtyRow()
{
	// Get list of ids
	var oList = getById('specialtyItems');	
	var arrIds = oList.value.split(',');	
	
	// Get max id used
	var maxId = 0;
	for (var i = 0; i < arrIds.length; i++)
	{
		if (parseInt(arrIds[i]) > maxId)
			maxId = parseInt(arrIds[i]);
	}	
	// Next id 						
	maxId++;	
	
	var callback = function(responseText)
	{
		// Get the container to insert into
		var oContainer = getById('ulSpecialty');				
		
		// Create new LI
		var oNewItem = document.createElement("LI");
		oNewItem.id = "specialtyItem_"+maxId.toString();
		
		// Find place to insert new item
		var oInsertBefore = getTagAncestor(document.getElementById('addSpecialtyButton'), 'LI');
		oContainer.insertBefore(oNewItem, oInsertBefore);
		
		setAndExecute(oNewItem, responseText);
		
		// Concatenate to list 
		if (oList.value == '')
			oList.value = maxId.toString();
		else
			oList.value += ','+maxId.toString();					
	}						
	
	var queryString = 'dyn/specialty.php?id='+maxId.toString();						
	makeHttpGetRequest(queryString, callback, false);
}

function medicardEdit_delLanguageRow(id)
{
	var oItem = getById('languageItem_'+id.toString());
	var oContainer = getById('ulLanguage');

	oContainer.removeChild(oItem);
	
	// Also remove from link hidden field list 
	var oList = getById('languageItems');	
	var allIds = oList.value.split(',');
	var newVal = '';
	for (var i = 0; i < allIds.length; i++)
	{
		if (allIds[i] == '' || allIds[i] == id)
			continue;
		if (newVal != '')
			newVal += ',';
		newVal += allIds[i];
	}
	oList.value = newVal;		 
}

function medicardEdit_addLanguageRow()
{
	// Get list of ids
	var oList = getById('languageItems');	
	var arrIds = oList.value.split(',');	
	
	// Get max id used
	var maxId = 0;
	for (var i = 0; i < arrIds.length; i++)
	{
		if (parseInt(arrIds[i]) > maxId)
			maxId = parseInt(arrIds[i]);
	}	
	// Next id 						
	maxId++;	
	
	var callback = function(responseText)
	{
		// Get the container to insert into
		var oContainer = getById('ulLanguage');				
		
		// Create new LI
		var oNewItem = document.createElement("LI");
		oNewItem.id = "languageItem_"+maxId.toString();
		
		// Find place to insert new item
		var oInsertBefore = getTagAncestor(document.getElementById('addLanguageButton'), 'LI');
		oContainer.insertBefore(oNewItem, oInsertBefore);
		
		setAndExecute(oNewItem, responseText);
		
		// Concatenate to list 
		if (oList.value == '')
			oList.value = maxId.toString();
		else
			oList.value += ','+maxId.toString();					
	}						
	
	var queryString = 'dyn/language.php?id='+maxId.toString();						
	makeHttpGetRequest(queryString, callback, false);
}

function medicardEdit_delPhoneRow(id)
{
	var oItem = getById('phoneItem_'+id.toString());
	var oContainer = getById('ulContact');

	oContainer.removeChild(oItem);
	
	// Also remove from link hidden field list 
	var oList = getById('phoneItems');	
	var allIds = oList.value.split(',');
	var newVal = '';
	for (var i = 0; i < allIds.length; i++)
	{
		if (allIds[i] == '' || allIds[i] == id)
			continue;
		if (newVal != '')
			newVal += ',';
		newVal += allIds[i];
	}
	oList.value = newVal;		 
}

function medicardEdit_addPhoneRow()
{
	// Get list of ids
	var oList = getById('phoneItems');	
	var arrIds = oList.value.split(',');	
	
	// Get max id used
	var maxId = 0;
	for (var i = 0; i < arrIds.length; i++)
	{
		if (parseInt(arrIds[i]) > maxId)
			maxId = parseInt(arrIds[i]);
	}	
	// Next id 						
	maxId++;	
	
	var callback = function(responseText)
	{
		// Get the container to insert into
		var oContainer = getById('ulContact');				
		
		// Create new LI
		var oNewItem = document.createElement("LI");
		oNewItem.id = "phoneItem_"+maxId.toString();
		
		// Find place to insert new item
		var oInsertBefore = getTagAncestor(document.getElementById('addPhoneButton'), 'LI');
		oContainer.insertBefore(oNewItem, oInsertBefore);
		
		setAndExecute(oNewItem, responseText);
		
		// Concatenate to list 
		if (oList.value == '')
			oList.value = maxId.toString();
		else
			oList.value += ','+maxId.toString();					
	}						
	
	var queryString = 'dyn/phone.php?id='+maxId.toString();						
	makeHttpGetRequest(queryString, callback, false);
}

function medicardEdit_delFaxRow(id)
{
	var oItem = getById('faxItem_'+id.toString());
	var oContainer = getById('ulContact');

	oContainer.removeChild(oItem);
	
	// Also remove from link hidden field list 
	var oList = getById('faxItems');	
	var allIds = oList.value.split(',');
	var newVal = '';
	for (var i = 0; i < allIds.length; i++)
	{
		if (allIds[i] == '' || allIds[i] == id)
			continue;
		if (newVal != '')
			newVal += ',';
		newVal += allIds[i];
	}
	oList.value = newVal;		 
}

function medicardEdit_addFaxRow()
{
	// Get list of ids
	var oList = getById('faxItems');	
	var arrIds = oList.value.split(',');	
	
	// Get max id used
	var maxId = 0;
	for (var i = 0; i < arrIds.length; i++)
	{
		if (parseInt(arrIds[i]) > maxId)
			maxId = parseInt(arrIds[i]);
	}	
	// Next id 						
	maxId++;	
	
	var callback = function(responseText)
	{
		// Get the container to insert into
		var oContainer = getById('ulContact');				
		
		// Create new LI
		var oNewItem = document.createElement("LI");
		oNewItem.id = "faxItem_"+maxId.toString();
		
		// Find place to insert new item
		var oInsertBefore = getTagAncestor(document.getElementById('addFaxButton'), 'LI');
		oContainer.insertBefore(oNewItem, oInsertBefore);
		
		setAndExecute(oNewItem, responseText);
		
		// Concatenate to list 
		if (oList.value == '')
			oList.value = maxId.toString();
		else
			oList.value += ','+maxId.toString();					
	}						
	
	var queryString = 'dyn/fax.php?id='+maxId.toString();						
	makeHttpGetRequest(queryString, callback, false);
}

function medicardEdit_delEmailRow(id)
{
	var oItem = getById('emailItem_'+id.toString());
	var oContainer = getById('ulContact');

	oContainer.removeChild(oItem);
	
	// Also remove from link hidden field list 
	var oList = getById('emailItems');	
	var allIds = oList.value.split(',');
	var newVal = '';
	for (var i = 0; i < allIds.length; i++)
	{
		if (allIds[i] == '' || allIds[i] == id)
			continue;
		if (newVal != '')
			newVal += ',';
		newVal += allIds[i];
	}
	oList.value = newVal;		 
}

function medicardEdit_addEmailRow()
{
	// Get list of ids
	var oList = getById('emailItems');	
	var arrIds = oList.value.split(',');	
	
	// Get max id used
	var maxId = 0;
	for (var i = 0; i < arrIds.length; i++)
	{
		if (parseInt(arrIds[i]) > maxId)
			maxId = parseInt(arrIds[i]);
	}	
	// Next id 						
	maxId++;	
	
	var callback = function(responseText)
	{
		// Get the container to insert into
		var oContainer = getById('ulContact');				
		
		// Create new LI
		var oNewItem = document.createElement("LI");
		oNewItem.id = "emailItem_"+maxId.toString();
		
		// Find place to insert new item
		var oInsertBefore = getTagAncestor(document.getElementById('addEmailButton'), 'LI');
		oContainer.insertBefore(oNewItem, oInsertBefore);
		
		setAndExecute(oNewItem, responseText);
		
		// Concatenate to list 
		if (oList.value == '')
			oList.value = maxId.toString();
		else
			oList.value += ','+maxId.toString();					
	}						
	
	var queryString = 'dyn/email.php?id='+maxId.toString();						
	makeHttpGetRequest(queryString, callback, false);
}

function medicardEdit_delURLRow(id)
{
	var oItem = getById('urlItem_'+id.toString());
	var oContainer = getById('ulURL');

	oContainer.removeChild(oItem);
	
	// Also remove from link hidden field list 
	var oList = getById('urlItems');	
	var allIds = oList.value.split(',');
	var newVal = '';
	for (var i = 0; i < allIds.length; i++)
	{
		if (allIds[i] == '' || allIds[i] == id)
			continue;
		if (newVal != '')
			newVal += ',';
		newVal += allIds[i];
	}
	oList.value = newVal;		 
}

function medicardEdit_addURLRow()
{
	// Get list of ids
	var oList = getById('urlItems');	
	var arrIds = oList.value.split(',');	
	
	// Get max id used
	var maxId = 0;
	for (var i = 0; i < arrIds.length; i++)
	{
		if (parseInt(arrIds[i]) > maxId)
			maxId = parseInt(arrIds[i]);
	}	
	// Next id 						
	maxId++;	
	
	var callback = function(responseText)
	{
		// Get the container to insert into
		var oContainer = getById('ulURL');				
		
		// Create new LI
		var oNewItem = document.createElement("LI");
		oNewItem.id = "urlItem_"+maxId.toString();
		
		// Find place to insert new item
		var oInsertBefore = getTagAncestor(document.getElementById('addURLButton'), 'LI');
		oContainer.insertBefore(oNewItem, oInsertBefore);
		
		setAndExecute(oNewItem, responseText);
		
		// Concatenate to list 
		if (oList.value == '')
			oList.value = maxId.toString();
		else
			oList.value += ','+maxId.toString();					
	}						
	
	var queryString = 'dyn/url.php?id='+maxId.toString();						
	makeHttpGetRequest(queryString, callback, false);
}

function medicardEdit_delAddressRow(id)
{
	var oItem = getById('addressItem_'+id.toString());
	var oContainer = getById('ulAddress');

	oContainer.removeChild(oItem);
	
	// Also remove from link hidden field list 
	var oList = getById('addressItems');	
	var allIds = oList.value.split(',');
	var newVal = '';
	for (var i = 0; i < allIds.length; i++)
	{
		if (allIds[i] == '' || allIds[i] == id)
			continue;
		if (newVal != '')
			newVal += ',';
		newVal += allIds[i];
	}
	oList.value = newVal;		 
}

function medicardEdit_addAddressRow()
{
	// Get list of ids
	var oList = getById('addressItems');	
	var arrIds = oList.value.split(',');	
	
	// Get max id used
	var maxId = 0;
	for (var i = 0; i < arrIds.length; i++)
	{
		if (parseInt(arrIds[i]) > maxId)
			maxId = parseInt(arrIds[i]);
	}	
	// Next id 						
	maxId++;	
	
	var callback = function(responseText)
	{
		// Get the container to insert into
		var oContainer = getById('ulAddress');				
		
		// Create new LI
		var oNewItem = document.createElement("LI");
		oNewItem.id = "addressItem_"+maxId.toString();
		
		// Find place to insert new item
		var oInsertBefore = getTagAncestor(document.getElementById('addAddressButton'), 'LI');
		oContainer.insertBefore(oNewItem, oInsertBefore);
		
		setAndExecute(oNewItem, responseText);
		
		// Concatenate to list 
		if (oList.value == '')
			oList.value = maxId.toString();
		else
			oList.value += ','+maxId.toString();					
	}						
	
	var queryString = 'dyn/address.php?id='+maxId.toString();						
	makeHttpGetRequest(queryString, callback, false);
}

function medicardEdit_delLocationRow(id)
{
	var oItem = getById('locationItem_'+id.toString());
	var oContainer = getById('ulLocation');

	oContainer.removeChild(oItem);
	
	// Also remove from link hidden field list 
	var oList = getById('locationItems');	
	var allIds = oList.value.split(',');
	var newVal = '';
	for (var i = 0; i < allIds.length; i++)
	{
		if (allIds[i] == '' || allIds[i] == id)
			continue;
		if (newVal != '')
			newVal += ',';
		newVal += allIds[i];
	}
	oList.value = newVal;		 
}

function medicardEdit_addLocationRow()
{
	// Get list of ids
	var oList = getById('locationItems');	
	var arrIds = oList.value.split(',');	
	
	// Get max id used
	var maxId = 0;
	for (var i = 0; i < arrIds.length; i++)
	{
		if (parseInt(arrIds[i]) > maxId)
			maxId = parseInt(arrIds[i]);
	}	
	// Next id 						
	maxId++;	
	
	var callback = function(responseText)
	{
		// Get the container to insert into
		var oContainer = getById('ulLocation');				
		
		// Create new LI
		var oNewItem = document.createElement("LI");
		oNewItem.id = "locationItem_"+maxId.toString();
		
		// Find place to insert new item
		var oInsertBefore = getTagAncestor(document.getElementById('addLocationButton'), 'LI');
		oContainer.insertBefore(oNewItem, oInsertBefore);
		
		setAndExecute(oNewItem, responseText);
		
		// Concatenate to list 
		if (oList.value == '')
			oList.value = maxId.toString();
		else
			oList.value += ','+maxId.toString();					
	}						
	
	var queryString = 'dyn/location.php?id='+maxId.toString();						
	makeHttpGetRequest(queryString, callback, false);
}
