var timerID;
var openwindowname = "";

// Standert Funtion for contactDetails page. Will close window.
function loadaddcontact()
{
	loadbooleansforaddcontactpage();
	gobacktocontactpage('false');
	trackImprint();	
}

// Standert Funtion for contactDetails page. Will give a close message.
function loadaddcontactmessage()
{
	loadbooleansforaddcontactpage();	
	getthankyoupage();
	trackImprint();	
}

function loadcategoriesforaddcontactpage()
{
	var categoriesobj 		= document.getElementsByName("cbCategories");
	
	if(categoriesobj.length > 0)
	{
		for(index=0;index<categoriesobj.length;index++)
		{
			var id = categoriesobj[index].id.substring(10, categoriesobj[index].id.length);
			
			if(hasSelectedCategoryId(id))
			{
				categoriesobj[index].checked = true;
			}
			else
			{
				categoriesobj[index].checked = false;
			}
		}		
	}
}

function addremovecategory(eventObject, categoryId)
{
	pageEdit();
	
	eventObject = eventObject || event ; //acquire the event object
	var element = eventObject.target ||  eventObject.srcElement ;
	
	var addCategoriesStringObj 		= document.getElementsByName("addCategories")[0];
	var removeCategoriesStringObj 	= document.getElementsByName("removeCategories")[0];
	
	if(element.checked)
	{
		addIdToString(addCategoriesStringObj, categoryId);
		removeIdFromString(removeCategoriesStringObj, categoryId);
	}
	else
	{
		addIdToString(removeCategoriesStringObj, categoryId);
		removeIdFromString(addCategoriesStringObj, categoryId);
	}	
}

function addIdToString(categoriesObj, categoryId)
{
	var exists = false;
	var	categories = categoriesObj.value.split(",");
	
	for(var index = 0;index < categories.length;index++)
	{	
		if(categories[index] != null && categories[index] == categoryId)
		{
			exists = true;
		}
	}
	
	if(!exists)
	{
		categoriesObj.value = categoriesObj.value + categoryId + ","; 
	}
}

function removeIdFromString(categoriesObj, categoryId)
{
	var exists = false;
	var	categories = categoriesObj.value.split(",");
	var newIds = "";
	
	for(var index = 0;index < categories.length;index++)
	{	
		if(categories[index] != null && categories[index] != categoryId && categories[index] > 0)
		{
			newIds = newIds + categories[index] + ",";
		}
	}
	
	categoriesObj.value = newIds;
}

function hasSelectedCategoryId(id)
{
	var inExistingCategories 		= document.getElementsByName("inExistingCategory");
	
	for(var index = 0;index < inExistingCategories.length;index++)
	{
		if(inExistingCategories[index].value != null && inExistingCategories[index].value != "" && inExistingCategories[index].value == id)
		{
			return true;
		}
	}
	
	return false;
}

function getthankyoupage()
{
	var actionobj 		= document.getElementsByName("action");
	
	if(actionobj[0].value == "has_saved_contact")
	{
		if(ignorechanges())
		{
			var trcontentObj 		= document.getElementById("trcontent1");
			
			if(trcontentObj != null)
			{
				trcontentObj.style.display = "none"; 
			}
			
			trcontentObj 		= document.getElementById("trcontent2");
			
			if(trcontentObj != null)
			{
				trcontentObj.style.display = "none"; 
			}
			
			trcontentObj 		= document.getElementById("trcontent3");
			
			if(trcontentObj != null)
			{
				trcontentObj.style.display = "none"; 
			}
			
			var trthankyouObj 		= document.getElementById("trthankyou");
			trthankyouObj.style.display = "block"; 
		}		
	}
	else
	{
		var trthankyouObj 		= document.getElementById("trthankyou");
		trthankyouObj.style.display = "none"; 
	}
}

function savecontact()
{
	savebooleansforaddcontactpage();
	
	doSubmitForm('save_contact');
}

function startClock()
{
   // Initial call
   timerID = window.setTimeout("showClock()", 2000); // 1-second interval
}

function stopClock()
{
	window.clearTimeout(timerID);	
   
	if(openwindowname == "addcontact")
	{
		doSubmitForm('added_contact');  
	}
	else if(openwindowname == "addcategory")
	{
		openwindowname = "addcontact";
		
		doSubmitForm('added_category');  
	}
	else if(openwindowname == "selectcategory")
	{
		doSubmitForm('added_parentcategory'); 
	}
	else if(openwindowname == "uploadfile")
	{
		var includeFileObj 	= document.getElementsByName("includeFile")[0];
		
		if(includeFileObj.value != "")
		{
			doSubmitForm('import_contacts'); 
		}
	}
}

function showClock()
{
   if(ChildWindow == null)
   {
	   stopClock();
   }
   else
   {
   		if(ChildWindow.closed)
	   	{
	   		stopClock();
		}
		else
		{
	   		timerID = window.setTimeout("showClock()", 1000); // repeat call
	   	}
   }
}

function loadbooleansforaddcontactpage()
{
	var cbAllowContactObj 		= document.getElementById("cbAllowContact");
	var cbAllowThirdPartyObj 	= document.getElementById("cbAllowThirdParty");
	var cbAllowNewsObj 		= document.getElementById("cbAllowNews");
	var cbAllowOffersObj 		= document.getElementById("cbAllowOffers");

	if(cbAllowContactObj != null)
	{
		var allowContactObj 	= document.getElementsByName("allowContact")[0];

		if(allowContactObj.value == "true")
		{
			cbAllowContactObj.checked = true;
		}
		else
		{
			cbAllowContactObj.checked = false;
		}
	}

	if(cbAllowThirdPartyObj != null)
	{
		var allowThirdPartyObj 	= document.getElementsByName("allowThirdParty")[0];

		if(allowThirdPartyObj.value == "true")
		{
			cbAllowThirdPartyObj.checked = true;
		}
		else
		{
			cbAllowThirdPartyObj.checked = false;
		}
	}

	if(cbAllowNewsObj != null)
	{
		var allowNewsObj 	= document.getElementsByName("allowNews")[0];

		if(allowNewsObj.value == "true")
		{
			cbAllowNewsObj.checked = true;
		}
		else
		{
			cbAllowNewsObj.checked = false;
		}
	}

	if(cbAllowOffersObj != null)
	{
		var allowOffersObj 	= document.getElementsByName("allowOffers")[0];

		if(allowOffersObj.value == "true")
		{
			cbAllowOffersObj.checked = true;
		}
		else
		{
			cbAllowOffersObj.checked = false;
		}
	}
}

function savebooleansforaddcontactpage()
{
	var cbAllowContactObj 		= document.getElementById("cbAllowContact");
	var cbAllowThirdPartyObj 	= document.getElementById("cbAllowThirdParty");
	var cbAllowNewsObj 			= document.getElementById("cbAllowNews");
	var cbAllowOffersObj 		= document.getElementById("cbAllowOffers");

	if(cbAllowContactObj != null)
	{
		var allowContactObj 	= document.getElementsByName("allowContact")[0];

		if(cbAllowContactObj.checked)
		{
			allowContactObj.value = "true";
		}
		else
		{
			allowContactObj.value = "false";
		}
	}

	if(cbAllowThirdPartyObj != null)
	{
		var allowThirdPartyObj 	= document.getElementsByName("allowThirdParty")[0];

		if(cbAllowThirdPartyObj.checked)
		{
			allowThirdPartyObj.value = "true";
		}
		else
		{
			allowThirdPartyObj.value = "false";
		}
	}

	if(cbAllowNewsObj != null)
	{
		var allowNewsObj 	= document.getElementsByName("allowNews")[0];

		if(cbAllowNewsObj.checked)
		{
			allowNewsObj.value = "true";
		}
		else
		{
			allowNewsObj.value = "false";
		}
	}

	if(cbAllowOffersObj != null)
	{
		var allowOffersObj 	= document.getElementsByName("allowOffers")[0];

		if(cbAllowOffersObj.checked)
		{
			allowOffersObj.value = "true";
		}
		else
		{
			allowOffersObj.value = "false";
		}
	}
}

function addoreditcontact(contactId)
{
	var Width = window.screen.Width;
	var Height = window.screen.Height;
	pTop  = Height/2 - 500/2;
	pLeft = Width/2 - 750/2;
		
	openwindowname = "addcontact";
	
	if(ChildWindow == null)
	{					
		ChildWindow = window.open('../actions/addcontact.do?action=load&allowVerify=false&contactId=' + contactId, '_blank', "height=500,width=750,status=no, toolbar=no, titlebar=no, resizable=no, menubar=no,scrollbars=no, location=no, left=" + pLeft + ", top=" + pTop);
		startClock();
	}
	else
	{
		if(ChildWindow.closed)
		{						
			ChildWindow = null;
			ChildWindow = window.open('../actions/addcontact.do?action=load&allowVerify=false&contactId=' + contactId, '_blank', "height=500,width=750,status=no, toolbar=no, titlebar=no, resizable=no, menubar=no,scrollbars=no, location=no, left=" + pLeft + ", top=" + pTop);
			startClock();
		}					
	}
}

function confirmdelist(firstname, surname, emailaddress, id)
{				
	if(confirm("Contact:\n\r\n\rName: " + firstname + " " + surname + "\n\rEmail address: " + emailaddress + "\n\r\n\rAre you sure you want to delete this contact?")) 
	{
		var selectedContactId = document.getElementsByName("contactId");										
		selectedContactId[0].value = id;
		
		doSubmitForm('delete_contact'); 				
	}				
}

function generateacontactheader()
{
	var headerObj		= document.getElementById("header");
	var contactIdObj 	= document.getElementsByName("contactId")[0];
	
	if(headerObj != null)
	{
		if(contactIdObj.value > 0)
		{
			headerObj.innerHTML = "Edit contact";
		}
		else
		{
			headerObj.innerHTML = "Create contact";
		}
	}
}

function addcategory()
{
	savebooleansforaddcontactpage();
	
	var contactIdObj 	= document.getElementsByName("contactId")[0];
	
	if(contactIdObj.value > 0)
	{
		openselectcategory(contactIdObj.value);
	}	
	else
	{
		if(confirm("To add a category to this new contact we need to save  this contact first.\r\n\r\nDo you want to continue?"))
		{
			doSubmitForm('save_contact_select_category');
		}
	}
}

function openselectcategory(contactId)
{
	if(ignorechanges())
	{	
		openwindowname = "addcategory";
		
		var Width = window.screen.Width;
		var Height = window.screen.Height;
		pTop  = Height/2 - 500/2;
		pLeft = Width/2 - 350/2;
			
		if(ChildWindow == null)
		{					
			ChildWindow = window.open('../actions/selectcategory.do?action=load&contactId=' + contactId, '_blank', "height=500,width=350,status=no, toolbar=no, titlebar=no, resizable=no, menubar=no,scrollbars=no, location=no, left=" + pLeft + ", top=" + pTop);
			startClock();
		}
		else
		{
			if(ChildWindow.closed)
			{						
				ChildWindow = null;
				ChildWindow = window.open('../actions/selectcategory.do?action=load&contactId=' + contactId, '_blank', "height=500,width=350,status=no, toolbar=no, titlebar=no, resizable=no, menubar=no,scrollbars=no, location=no, left=" + pLeft + ", top=" + pTop);
				startClock();
			}					
		}
	}
}

function gobacktocontactpage(force)
{
	var actionobj 		= document.getElementsByName("action");
	
	if(actionobj[0].value == "has_saved_contact" || force == "true" || actionobj[0].value == "has_selected_category")
	{
		if(ignorechanges())
		{
			window.close();
		}		
	}
	else if(actionobj[0].value == "has_saved_contact_select_category")
	{
		addcategory();
	}
}

function newcategory()
{
	var nameobj 			= document.getElementsByName("name")[0];
	var parentCategoryIdObj = document.getElementsByName("parentCategoryId")[0];
	var categoryIdObj 		= document.getElementsByName("categoryId")[0];
	var contactareaObj 		= document.getElementById("contactarea")
	
	nameobj.value 				= "";
	parentCategoryIdObj.value 	= 0;
	categoryIdObj.value 		= 0;
	contactareaObj.className 	= "requiredfieldoff";
}

function chooseparentcategory()
{
	var categoryIdObj 	= document.getElementsByName("categoryId")[0];
	
	if(validatepage("categories"))
	{
		if(categoryIdObj.value > 0)
		{
			openselectparentcategory(categoryIdObj.value)
		}	
		else
		{		
			if(confirm("In order to choose a parent category you need to save this category.\r\n\r\nDo you want to continue?"))
			{
				doSubmitForm('save_category_to_choose_parent');
			}		
		}
	}
}

function openselectparentcategory(categoryId)
{
	openwindowname = "selectcategory";
		
	var Width = window.screen.Width;
	var Height = window.screen.Height;
	pTop  = Height/2 - 500/2;
	pLeft = Width/2 - 350/2;
		
	if(ChildWindow == null)
	{					
		ChildWindow = window.open('../actions/selectcategory.do?action=load&toCategoryId=' + categoryId, '_blank', "height=500,width=350,status=no, toolbar=no, titlebar=no, resizable=no, menubar=no,scrollbars=no, location=no, left=" + pLeft + ", top=" + pTop);
		startClock();
	}
	else
	{
		if(ChildWindow.closed)
		{						
			ChildWindow = null;
			ChildWindow = window.open('../actions/selectcategory.do?action=load&toCategoryId=' + categoryId, '_blank', "height=500,width=350,status=no, toolbar=no, titlebar=no, resizable=no, menubar=no,scrollbars=no, location=no, left=" + pLeft + ", top=" + pTop);
			startClock();
		}					
	}
}

function selectcategory(categoryId)
{
	var categoryIdObj 	= document.getElementsByName("categoryId")[0];
	
	categoryIdObj.value = categoryId;
	
	doSubmitForm('select_category');
}

function openonloadselectcategory()
{
	var actionObj 	= document.getElementsByName("action")[0];
	
	if(actionObj.value == "has_saved_category_choose_parent")
	{
		chooseparentcategory();
	}
}

function confirmdelistcategory()
{			
	var selectedCategoryId = document.getElementsByName("categoryId")[0];	
	
	if(selectedCategoryId.value > 0)
	{
		var name = document.getElementsByName("name")[0].value;	
			
		if(confirm("Are you sure you want to delete the category '" + name + "'?")) 
		{
			doSubmitForm('delete_category'); 				
		}			
	}
	else
	{
		alert("There is not a category selected!");
	}	
}

function confirmremovecategory(categoryId)
{
	if(categoryId > 0)
	{
		if(confirm("Are you sure you want to remove the category from this contact?")) 
		{
			var removeCategoriesStringObj 	= document.getElementsByName("removeCategories")[0];
			
			removeCategoriesStringObj.value = categoryId;
			
			doSubmitForm('remove_category');
		}
	}
}

function importcontacts()
{
	var Width = window.screen.Width;
	var Height = window.screen.Height;
	Height = Height/2 - 120/2;
	Width = Width/2 - 400/2;
	
	openwindowname = "uploadfile";
	
	if(ChildWindow == null)
	{					
		ChildWindow = window.open("../actions/uploadfile.do?mode=2&returnField=includeFile", '_blank', "height=120,width=400,status=no, toolbar=no, titlebar=no, resizable=no, menubar=no,scrollbars=no, location=no, left=" + Width + ", top=" + Height);
		startClock();
	}
	else
	{
		if(ChildWindow.closed)
		{						
			ChildWindow = null;
			ChildWindow = window.open("../actions/uploadfile.do?mode=2&returnField=includeFile", '_blank', "height=120,width=400,status=no, toolbar=no, titlebar=no, resizable=no, menubar=no,scrollbars=no, location=no, left=" + Width + ", top=" + Height);
			startClock();
		}					
	}
}

function exportcontacts()
{
	doSubmitForm('export_contacts');
}

function createalternatedcontacttablerow()
{
	var logtable = document.getElementById("tablelist");
				
	if(logtable != null)
	{
		var trlist 	= logtable.getElementsByTagName("tr");
		var even	= true;
		
		for(i=1;i<trlist.length;i++)
		{
			var tdlist = trlist[i].getElementsByTagName("td");
			
			for(i2=0;i2<tdlist.length;i2++)
			{
				if(!even)
				{
					if(i2 == 0)
					{
						tdlist[i2].className= "contactcellalternated";
					}
					else
					{
						tdlist[i2].className="logtextalternated";
					}
				}
				else
				{
					if(i2 == 0)
					{
						tdlist[i2].className="contactcell";
					}
					else
					{
						tdlist[i2].className="logtext";
					}
				}
			}
				
			if(!even)
			{
				even = true;
			}
			else
			{
				even = false;
			}					
		}	
	}
}


