var status = {
	'true': 'open',
	'false': 'close'
};

function getRootDir()
{
	return "http://dealflow.dumontventure.de/";
}

function sendByAjaxStr()
{
	return "&sendByAjax=1";
}

function getGlobalParameter(prefix){
	prefix = prefix == null ? '' : prefix;
	param = "";
	param += prefix + "type=" + document.getElementById("gloablType").value;
	param += "&";
	param += prefix + "id=" + document.getElementById("gloablId").value;
	param += "&";
	
	return param;
}


function getGlobalCommentsParameter(prefix){
	
	if(document.getElementById("gloablCommentsId") != null)
	{
		prefix = prefix == null ? '' : prefix;
		param = "";
		param += prefix + "type=" + document.getElementById("gloablCommentsType").value;
		param += "&";
		param += prefix + "id=" + document.getElementById("gloablCommentsId").value;
		param += "&";
	}
	else
	{
		return getGlobalParameter(prefix);
	}
	
	return param;
}
//////////////////////////////////////////////////////////////
//Archive	
//////////////////////////////////////////////////////////////
var loadList = function(page, containerID, type, search, sortby, sortDirection) {		
		if (sortby == undefined || sortby == ''){
			//sortSting = '&scol=time_modified';	
			sortSting = '';	
			sortby = '';
		}
		else{
			sortSting = "&scol=" + sortby;
		}

		if (sortDirection == undefined || sortDirection == ''){
			//sortDirectionSting = '&sdir=asc';	
			sortDirectionSting = '';	
			sortDirection = '';
		}
		else{
			sortDirectionSting = "&sdir=" + sortDirection;
		}		
		
		
		if (search == undefined) search = '';
		
		$(containerID + "_current_page").value = page;
		
		var container = $(containerID);	
		var url;
		var searchData = ""
	
		if (containerID == 'my_open_deals'){
			url = "deal/showmyopen";
			//url = "deal/showmycontacts";
		}
		if (containerID == 'my_pused_deals'){
			url = "deal/showmypaused";
		}						
		if (containerID == 'my_investments'){
			url = "deal/showmyinvestments";
		}			
		if (containerID == 'my_projects'){
			url = "project/showmylistshort";
		}
		if (containerID == 'deals_archive'){
			url = "/deal/showfinished";
		}		
		if (containerID == 'new_deals'){
			url = "/deal/shownew";
		}	
		if (containerID == 'my_new_deals'){
			url = "/deal/showmynew";
		}			
		if (containerID == 'open_deals'){
			url = "/deal/showopen";
			//url = "deal/showcontacts";
		}
		if (containerID == 'pused_deals'){
			url = "/deal/showpaused";
		}						
		if (containerID == 'investments'){
			url = "/deal/showinvestments";
		}			
		if (containerID == 'projects'){
			if(type == 'projects')
			{
				url = "/project/showlistlong";
			}
			else if(type == 'projects_archive')
			{
				url = "/project/showarchive";
			}			
			else
			{
				url = "/project/showlistshort";
			}
		}
		
		if (containerID == 'contacts'){
			url = "/deal/showcontacts";
		}			
		
		if (search != ''){
			searchData = "type=" + type + "&" + "searchString=" + search + "&";
		}		
		
		if (search != '' && type == "deals"){
			url = "../search/getdealssearch";
		}								
		
		if (search != '' && type == "projects"){
			url = "../search/getprojectssearch";
		}										

		if (search != '' && type == "contacts"){
			url = "../search/getdealcontactssearch";
		}									

		if (type != "investments" && type != "deals_archive" && type != "projects_archive" && type != "contacts"){
			var itemProPage = 10;
		}
		else{
			var itemProPage = 10000;
		}
		var itemProPageString = "&psize=" + itemProPage + "&";
		
		clearList(container);	
		var request = new Request.JSON({
			data: searchData + "pind="+(page.toInt() - 1) + sendByAjaxStr() + sortSting + sortDirectionSting + itemProPageString,
			url: url,
			method: 'get',
			onComplete: function(jsonObj) {
				if 	(jsonObj == null ){			
					return 0;
				}
				showList(jsonObj.list, container, type, search, sortby, sortDirection);
			}
		
		}).send();
}	
	
var showList = function(list, container, type, search, sortby, sortDirection) {
	var items = list.items;
	
	if 	(items == null ){			
		noItems(container);
		return 0;
	}
	
	var item;

	container.empty();
	items.each(
		function(items){			
			item = new Element("div", {"class": "item"});
				if(type == "contacts"){
					var name = new Element("div", {"html": items.last_name + "&#160;" ,"class": "name"}).inject(item);		
					var first_name = new Element("div", {"html": items.first_name + "&#160;","class": "firstName"}).inject(name, "after");		
					var phone = new Element("div", {"html": items.normal_phone_number + "&#160;" + "&#160;","class": "phoneNumber"}).inject(first_name, "after");							
					var email = new Element("div", {"html": items.email+ "&#160;","class": "email"}).inject(phone, "after");							
					var card_container = new Element("div", {"class": "vcard"}).inject(email, "after");													
					var card_link = new Element("a", {"html": "Herunterladen...","href": "/vcard?id=" + items.id}).inject(card_container);											
					var name_container = new Element("div", {"class": "name"}).inject(card_container, "after");													
					var name_link = new Element("a", {"html": items.deal_name.value,"href": items.deal_name.url}).inject(name_container);	
					var clearer = new Element("div", {"class" : "clearer"}).inject(name_container, "after");
					item.inject(container);
					
					return 0;
				}
				
				if (type.indexOf("projects") == -1 ){
					var id = new Element("div", {"html": items.id,"class": "id"}).inject(item);					
				}
				if(type == "deals_new"){				
					var start_date = new Element("div", {"html": items.start_date,"class": "date"});														
				}
				else{
					var start_date = new Element("div", {"html": items.time_modified,"class": "date"});														
				}
				if (type.indexOf("deals") != -1 || type == "investments") {
					start_date.inject(id, "after");
				}
				if (type == "projects" || type == "projects_archive" || type == "my_projects"){
					start_date.inject(item);
				}
				
				var name_container = new Element("div", {"class": "name"});				
					if (type != "my_projects") {
						name_container.inject(start_date, "after");
					}
					else{
						name_container.inject(item);
					}				
					var name_link = new Element("a", {"html": items.name.value,"href": items.name.url}).inject(name_container);				
				if (type.indexOf("projects") == -1){
					var category = new Element("div", { "html": items.category, "class": "category" }).inject(name_container, "after");						
					if (type != "deals_new"){
						var rating_container = new Element("div", {"class": "rating"}).inject(category, "after");				
							if(type != "deals" && type != "deals_archive"){
									var rating_value = new Element("span", {"html": "&#160;"}).inject(rating_container);
							}
							else{
								var rating_minus = new Element("a", {"html": " - ", "href": "javascript: setRating(" + items.id + "," + (items.rating - 1) + " );", "id": "rating_minus" + items.id}).inject(rating_container);
								var rating_value = new Element("span", {"html": items.rating, "id": "rating" + items.id}).inject(rating_minus, "after");
								var rating_plus = new Element("a", {"html": " + ", "href": "javascript: setRating(" + items.id + "," + (items.rating + 1) + ");", "id": "rating_plus" + items.id}).inject(rating_value, "after");									
							}							
					}
					var owner = new Element("div", {"class": "owner", "html": items.owner });		
					if (type != "deals_new"){
						owner.inject(rating_container, "after");
					}
					else{
						owner.inject(category, "after");
					}										
				}			
				
				if (type != "deals_new"){
					if (type != "deals_archive") {
						var todos = new Element("div", {
							"html": items.todos,
							"class": "todos"
						});
						if (type.indexOf("projects") == -1) {
							todos.inject(owner, "after");
						}
						else {
							todos.inject(name_container, "after");
						}
					}
					else{
						var owner = new Element("div", {
							"html": items.owner_name,
							"class": "owner"
						}).inject(owner, "after");					
					}						
				}
				
				
				if (type == "projects" || type == "projects_archive") {
						var owner = new Element("div", {
							"html": items.owner,
							"class": "owner"
						}).inject(name_container, "after");		
						
						var status = new Element("div", {
							"html": items.status,
							"class": "status"
						}).inject(owner, "after");								
						
						var endDate = new Element("div", {
							"html": items.end_date,
							"class": "endDate"
						}).inject(status, "after");							
				}						
				var clearer = new Element("div", {"class" : "clearer"});				
				if (type == "deals_archive"){
					clearer.inject(owner, "after");
				}
				else if (type == "deals_new"){
					clearer.inject(owner, "after");					
				}							
				else{
					clearer.inject(todos, "after");
				}
			item.inject(container);
		}		
	);	

	if (list.general_info.total_pages > 1){
		var pageingContainer = new Element("div", {"class" : "pager"}).inject(container);
			var pageInfo = new Element("div", {"class": "pageInfo", "html": "Seite " +  list.general_info.current_page + " von " + list.general_info.total_pages}).inject(pageingContainer);
			var paging = new Element("div", {"class": "paging"}).inject(pageInfo, "after");
			var clearer = new Element("div", {"class" : "clearer"}).inject(paging, "after");
	
			buildPaging(paging, list.general_info.current_page, list.general_info.total_pages, "loadList", new Array(container.id, type, search, sortby, sortDirection));		
	}
};

var clearList = function(object){
	object.empty();
	var div = new Element("div", {"class":"center"});
	var img = new Element("img", {"src": "/img/loading.gif"}).inject(div);
	
	div.inject(object);
}

var noItems = function(object){
	object.empty();
	var div = new Element("div", {"class":"noItems", "html": "keine Einträge gefunden"});
	
	div.inject(object);
}

var sortList = function(containerID, type, search, sortby, aObj){		
	aObjTempClass = aObj.className;
	
	aElements = aObj.parentNode.parentNode.getElementsByTagName('a');	

	for(i = 0; i < aElements.length; i++){
		aElements[i].className = "";
	}	
	
	var sortDirection;
	if (aObjTempClass == "" || aObjTempClass == "sortDown"){
		sortDirection = "asc";
		aObj.className = "sortUp";
	}
	else{
		sortDirection = "desc";
		aObj.className = "sortDown";		
	}	

	page = $(containerID + "_current_page").value;
	loadList(page, containerID, type, search, sortby, sortDirection);
}
//////////////////////////////////////////////////////////////
//Error Handling
//////////////////////////////////////////////////////////////

var errorHandling = function(errors, type, contactIndex){
	errors.each(
			function(error){	
				errorHandlingSingle(error, type, contactIndex);
			}
	);
}

var errorHandlingSingle = function(error, type, contactIndex){
	if (type == "contact"){
		if	(document.getElementsByName("contacts[" + contactIndex + "][" + error.field + "]")[0]){
			document.getElementsByName("contacts[" + contactIndex + "][" + error.field + "]")[0].className = "error";
		}
	}
	else{
		if(document.getElementsByName(error.field)[0]){
			document.getElementsByName(error.field)[0].className = "error";	
		}
	}
}


//////////////////////////////////////////////////////////////
//Rating
//////////////////////////////////////////////////////////////
var setRating = function(id, rate) {
		$("rating_plus" + id).style.visibility = "hidden";
		$("rating_minus" + id).style.visibility = "hidden";

		var request = new Request.JSON({
			data: "id=" + id + "&rating=" + rate + "&editRatingSubmit=1" + sendByAjaxStr(),
			url: getRootDir() + "deal/edit",
			onComplete: function(jsonObj) {
				if(jsonObj.errors){				
					/*Test*/
					alert("Rating nicht erlaubt");
					$("rating_plus" + id).style.visibility = "visible";
					$("rating_minus" + id).style.visibility = "visible";						
					return
				}
				chageRatingById(jsonObj.rating, id);
			}
		}).send();
}	

var chageRatingById	= function (rating, id){
	$("rating_minus" + id).href = "javascript: setRating(" + id + "," + (parseInt(rating) - 1) + " );";
	$("rating" + id).innerHTML = rating;
	$("rating_plus" + id).href = "javascript: setRating(" + id + "," + (parseInt(rating) + 1) + " );";
	
	$("rating_plus" + id).style.visibility = "visible";
	$("rating_minus" + id).style.visibility = "visible";	
}

//////////////////////////////////////////////////////////////
//EditableEntry
//////////////////////////////////////////////////////////////
var togglerEditableEntry = function(blockId, itemId, action, type){ 
	if (action == "cancel") {
		if (itemId == "") {
			if (type == "todo") {
				deleteTodo(blockId, itemId);				
			}
			if (type == "contact") {
				deleteContact(blockId, itemId);				
			}
			if (type == "comment") {
				deleteComment(blockId, itemId);				
			}			
			return;
		}
	}
	if (action == "save") {
		togglerEditableView(blockId, "loading", type, false);
	}

	var containerId;
	if (type == "deal"){
		containerId = "vertical_slide_" + blockId;
	}	
	if (type == "contact"){
		containerId = "infoContact_" + blockId;
	}		
	if (type == "comment"){
		containerId = "infoComment_" + blockId;
	}		
	if (type == "todo"){
		containerId = "infoTodo_" + blockId;
	}
	if (type == "project"){
		containerId = "vertical_slide_" + blockId;
	}	
	valueContainer = getElementsByAttribute($(containerId), "div", "title", "value_" + blockId);		
	
	
	var updateFields = new Array();	

	for(i = 0; i < valueContainer.length; i++){		
		if (action == "edit" && valueContainer[i].className != "functions") {
			valueContainer[i].firstChild.style.display = "none";
			valueContainer[i].lastChild.style.display = "block";			
		}
		if ( action == "cancel" && valueContainer[i].className != "functions") {
			valueContainer[i].lastChild.style.display = "none";
			valueContainer[i].firstChild.style.display = "block";
		}
		
		if ( action == "save" && valueContainer[i].className != "functions") {
			valueContainer[i].lastChild.className = "";
		}		
		
		if ( (action == "save" || action == "cancel") && valueContainer[i].className != "functions" ){					
			if (action == "cancel"){
				if (valueContainer[i].lastChild.nodeName == "INPUT"){
					if (valueContainer[i].firstChild.className.indexOf("link") == -1){
						valueContainer[i].lastChild.value = valueContainer[i].firstChild.innerHTML;
					}
					else{
						valueContainer[i].lastChild.value = valueContainer[i].firstChild.firstChild.innerHTML;
					}
				}				
				if (valueContainer[i].lastChild.nodeName == "TEXTAREA"){					
					valueContainer[i].lastChild.value = valueContainer[i].firstChild.innerHTML.replace(/<br>/gi, "\n");
				}
				if (valueContainer[i].lastChild.nodeName == "SELECT"){
					var optionValue;
					for(j = 0; j < valueContainer[i].lastChild.childNodes.length; j++){						
						//if(type == "todo" && (valueContainer[i].className.indexOf("status") != -1) ){
						if(false){						
							if ( (valueContainer[i].firstChild.className ).substr(7) == valueContainer[i].lastChild.childNodes[j].innerHTML.toLowerCase().replace(/ /, "")){
								optionValue = valueContainer[i].lastChild.childNodes[j].value;
								break;								
							}							
						}
						else{
							if (valueContainer[i].firstChild.innerHTML == valueContainer[i].lastChild.childNodes[j].innerHTML){
								optionValue = valueContainer[i].lastChild.childNodes[j].value;
								break;
							}							
						}
					}
					valueContainer[i].lastChild.value = optionValue;
				}					
			}
			
			if (action == "save"){
				updateFields.push(new Array (2));
				
				if (valueContainer[i].lastChild.nodeName == "INPUT"){
					updateFields[i][0] = valueContainer[i].lastChild.name;
					updateFields[i][1] = valueContainer[i].lastChild.value;
					if (valueContainer[i].firstChild.className.indexOf("link") == -1){
						valueContainer[i].firstChild.innerHTML = valueContainer[i].lastChild.value;
					}			
					else{
						if (valueContainer[i].firstChild.className.indexOf("url") != -1){
							valueContainer[i].firstChild.firstChild.href = valueContainer[i].lastChild.value;
						}					
						valueContainer[i].firstChild.firstChild.innerHTML = valueContainer[i].lastChild.value;
					}
					
				}				
				if (valueContainer[i].lastChild.nodeName == "TEXTAREA"){
					updateFields[i][0] = valueContainer[i].lastChild.name;
					updateFields[i][1] = valueContainer[i].lastChild.value;

					valueContainer[i].firstChild.innerHTML = valueContainer[i].lastChild.value.replace(/\n/gi, "<br />");
				}
				if (valueContainer[i].lastChild.nodeName == "SELECT"){
					var optionValue = valueContainer[i].lastChild.value;
					for(j = 0; j < valueContainer[i].lastChild.childNodes.length; j++){
							if (optionValue == valueContainer[i].lastChild.childNodes[j].value){
								var optionText = valueContainer[i].lastChild.childNodes[j].innerHTML;
								break;
							}											
					}
					updateFields[i][0] = valueContainer[i].lastChild.name;
					updateFields[i][1] = valueContainer[i].lastChild.value;									
					//if((type == "todo") && (valueContainer[i].className.indexOf("status") != -1) ){						
					if(false){											
						valueContainer[i].firstChild.className = "status " + optionText.toLowerCase().replace(/ /, "");
					}
					else{
						valueContainer[i].firstChild.innerHTML = optionText;						
					}
				}					
			}			
			
			
		}		
	}
	if (action == "edit") {
		if ($("content").className != "new"){
			$(type + "_save_" + blockId).style.display = "inline";
		}
		$(type + "_cancel_" + blockId).style.display = "inline";
		$(type + "_edit_" + blockId).style.display = "none";
	}
	if (action == "cancel") {			
		$(type + "_save_" + blockId).style.display = "none";
		$(type + "_cancel_" + blockId).style.display = "none";
		$(type + "_edit_" + blockId).style.display = "inline";

	}
	if (action == "save"){
		$(type + "_save_" + blockId).style.display = "none";
		$(type + "_cancel_" + blockId).style.display = "none";
		$(type + "_edit_" + blockId).style.display = "none";		
		
		editData(updateFields, blockId, itemId, type);					
	}
	
			
}

var editData = function(updateFields, blockId, itemId, type) {
		contactIndex = '';
		
		if (type == "deal"){			
			var updateFieldsString = getGlobalParameter();
		}
		if (type == "contact"){
			if (itemId == ''){
				var updateFieldsString = getGlobalParameter() + "contact_id=" + itemId + "&addContactSubmit=1&";
			}
			else
			{
				var updateFieldsString = getGlobalParameter() + "contact_id=" + itemId + "&editContactSubmit=1&";
			}
			contactIndex = $("contact_" + blockId).firstChild.value
		}
		
		if (type == "comment"){
			if (itemId == ''){
				var updateFieldsString = getGlobalCommentsParameter('parent_') + "comment_id=" + itemId + "&addSubmit=1&"  + "parent_sup_type=" + document.getElementById("gloablType").value + "&";
			}
			else
			{
				var updateFieldsString = getGlobalCommentsParameter('parent_') + "id=" + itemId + "&editSubmit=1&"  + "parent_sup_type=" + document.getElementById("gloablType").value + "&";
			}
				
		}		
		
		if (type == "project"){
			var updateFieldsString = "id=" + itemId + "&" + "editSubmit=1&";
		}									
		
		if (type == "todo"){
			if (itemId == ''){
				var updateFieldsString = getGlobalParameter('parent_') + "id=" + itemId + "&addSubmit=1&";
			}
			else{
				var updateFieldsString = getGlobalParameter('parent_') + "id=" + itemId + "&editSubmit=1&";
			}
			
		}
		
		for(i = 0; i < updateFields.length; i++){
			updateFieldsString += encodeURIComponent(updateFields[i][0]) + "=" + encodeURIComponent(updateFields[i][1])+ "&";
		}
		if (type == "deal"){
			if(updateFieldsString.indexOf("&market") > -1)
			{
				updateFieldsString += "editAdditionalSubmit=1&"
			}
			else
			{
				updateFieldsString += "editMainSubmit=1&"
			}
			
			var url = "deal/edit";
		}
		if (type == "contact"){
			if (itemId == ''){
				var url = "deal/edit";						
			}
			else{						
				var url = "deal/edit";						
			}

		}
		
		if (type == "comment"){
			if (itemId == ''){
				var url = getRootDir() + "comments/add";						
			}
			else{
				var url = getRootDir() + "comments/edit";
			}

		}		
		
		if (type == "todo"){
			if (itemId == ''){
				var url = "todos/add";						
			}
			else{
				var url = getRootDir() + "todos/edit";
			}

		}		
		
		if (type == "project"){
			var url = "project/edit";
		}							
		
		var request = new Request.JSON({
			url: url,
			data: updateFieldsString + sendByAjaxStr(),
			onComplete: function(jsonObj) {
				if(jsonObj.errors){				
					errorHandling(jsonObj.errors, type, contactIndex);
				}
				editDataComplete(jsonObj, blockId, itemId, type);
			}
		}).send();
}

function editDataComplete(response, blockId, itemId, type){			
		if(response.errors){	
				
				$(type + "_save_" + blockId).style.display = "inline";
				$(type + "_cancel_" + blockId).style.display = "inline";
				$(type + "_edit_" + blockId).style.display = "none";				
		
				togglerEditableView(blockId, "", type, true);
		}
		else{
			if (response.state = "success"){
				$(type + "_save_" + blockId).style.display = "none";
				$(type + "_cancel_" + blockId).style.display = "none";
				$(type + "_edit_" + blockId).style.display = "inline";				
				
				var containerId;
				if (type == "deal"){
					containerId = "vertical_slide_" + blockId;
				}	
				if (type == "contact"){
					containerId = "infoContact_" + blockId;
				}		
				if (type == "comment"){
					containerId = "infoComment_" + blockId;
				}		
				if (type == "todo"){
					containerId = "infoTodo_" + blockId;
				}
				if (type == "project"){
					containerId = "vertical_slide_" + blockId;
				}	
				
				var valueContainer = getElementsByAttribute($(containerId), "div", "title", "value_" + blockId);		

				for(i = 0; i < valueContainer.length; i++){		
					if ( valueContainer[i].className != "functions") {				
						valueContainer[i].lastChild.style.display = "none";
						valueContainer[i].firstChild.style.display = "block";
					}
					if (itemId == ''){
						if (valueContainer[i].firstChild.className.indexOf("link") != -1){
							valueContainer[i].firstChild.firstChild.href = response.new_url;			
						}																												
					}
				}	

				if (itemId == ''){
					$(type + "_save_" + blockId).href = "javascript:togglerEditableEntry(" + blockId + ", '" + response.new_id + "', 'save', '" + type + "')";
					$(type + "_cancel_" + blockId).href = "javascript:togglerEditableEntry(" + blockId + ", '" + response.new_id + "', 'cancel', '" + type + "')";
					$(type + "_edit_" + blockId).href = "javascript:togglerEditableEntry(" + blockId + ", '" + response.new_id + "', 'edit', '" + type + "')";							
					if(type == "contact"){
						$(type + "_delete_" + blockId).href = "javascript:deleteContact(" + blockId + ", " + response.new_id + ")";					
						$("vcard_" + blockId).href += response.new_id;
						$("vcard_" + blockId).style.display = "block";
					}
					if(type == "todo"){
						$(type + "_delete_" + blockId).href = "javascript:deleteTodo(" + blockId + ", " + response.new_id + ")";					
					}			
					if(type == "comment"){
						$(type + "_delete_" + blockId).href = "javascript:deleteComment(" + blockId + ", " + response.new_id + ")";					
					}								
					
				}				
				
				togglerEditableView(blockId, "", type, false);		
			}
		}
}	

var togglerEditableView = function(blockId, action, type, error){
	if (type == 'deal'){
		if(action == "loading"){
			$("infoTable_" + blockId).style.display = "none";
			$("loading_" + blockId).style.display = "block";
		}
		else{
			$("infoTable_" + blockId).style.display = "block";
			$("loading_" + blockId).style.display = "none";
		}			
	}
	
	if (type == 'contact'){
		if(action == "loading"){
			$("contact_" + blockId).style.display = "none";
	
			var div = new Element("div", {"class":"center loading", "id": "loading_contact" + blockId});
			var img = new Element("img", {"src": "img/loading.gif"}).inject(div);
			
			div.inject($("contact_" + blockId), "after");
			
		}
		else{
				$("loading_contact" + blockId).parentNode.removeChild($("loading_contact" + blockId));
				$("contact_" + blockId).style.display = "block";
		}			
	}	
	
	
}

//////////////////////////////////////////////////////////////
//Contact
//////////////////////////////////////////////////////////////
var loadContacts = function(blockId) {
		var request = new Request.JSON({
			url: "deal/getcontacts",
			data: getGlobalParameter() + "submitGetContacts=1&" + sendByAjaxStr(),
			method: 'get',
			onComplete: function(jsonObj) {
				if 	(jsonObj == null ){			
					return 0;
				}
				showListContacts(jsonObj.contacts, blockId);
			}
		}).send();
}


var showListContacts = function(contacts, blockId){
	if 	(contacts == null ){			
		return 0;
	}
	contacts.each(
		function(contacts){
			 buildContact(blockId, contacts);			 
		}
	);
}

function buildContact(blockID, contactInfo){
		checkSliderSize(blockID, 233);
		var namePrefix = "contacts[" + getContactIndex() + "][";	
	
	
		if (contactInfo == ""){
			var contactInfo = {				 
					 				"id": "",
					 				"first_name": "",
					 				"last_name": "",
					 				"position": "",
					 				"address": "",
					 				"email": "",
					 				"normal_phone_number": "",									
					 				"mobile_phone_number": ""																
								}
		}
	
		var itemID = parseInt($("currentContact").value) + 1;		
		$("currentContact").value = itemID;		
		
		var container = $("contactContainer");
	
		//alert(container);
		var item = new Element("div", {"class": "item", "id": "contact_" + itemID});		
			var contactIndex = new Element("input", {"type": "hidden", "value": getContactIndex() }).inject(item);																				
			var functions = new Element("div", {"class": "functions"}).inject(contactIndex, "after");
				var link1 = new Element("a", {"id": "contact_edit_" + itemID , "html": "Bearbeiten...", "class": "edit", "href": "javascript:togglerEditableEntry(" + itemID + ", '" + contactInfo.id + "', 'edit', 'contact')"}).inject(functions);			
				var link1span = new Element("span", {"html": " "}).inject(link1, "after");
				var link2 = new Element("a", {"id": "contact_cancel_" + itemID ,"html": "Abbrechen", "class": "cancel", "href": "javascript:togglerEditableEntry(" + itemID + ", '" + contactInfo.id + "', 'cancel', 'contact')"}).inject(link1span, "after");			
				var link2span = new Element("span", {"html": " "}).inject(link2, "after");
				var link3 = new Element("a", {"id": "contact_save_" + itemID ,"html": "Speichern", "class": "save", "href": "javascript:togglerEditableEntry(" + itemID + ", '" + contactInfo.id + "', 'save', 'contact')"}).inject(link2span, "after");			
				var link3span = new Element("span", {"html": " "}).inject(link3, "after");
				var link4 = new Element("a", {"id": "contact_delete_" + itemID ,"html": "Löschen", "href": "javascript:deleteContact(" + itemID + ", '" + contactInfo.id + "')"}).inject(link3span, "after");			
				
			var contact = new Element("div", {"class": "infoTable", "id": "infoContact_" + itemID}).inject(functions, "after");
				var line1 = new Element("div", {"class": "line twoCol text"}).inject(contact);
					var line1label1 = new Element("div", {"class": "label", "html": "Name"}).inject(line1);
					var line1value1 = new Element("div", {"class": "value", "title": "value_" + itemID}).inject(line1label1, "after");					
						var line1value1text = new Element("div", {"title": "name", "html": contactInfo.last_name}).inject(line1value1);					
						var line1value1input = new Element("input", {"type": "text", "value": contactInfo.last_name , "name": namePrefix + "last_name]"}).inject(line1value1text, "after");																	
						
					var line1label2 = new Element("div", {"class": "label", "html": "E-Mail"}).inject(line1value1, "after");
					var line1value2 = new Element("div", {"class": "value", "title": "value_" + itemID}).inject(line1label2, "after");					
						var line1value2text = new Element("div", {"title": "email", "html": contactInfo.email}).inject(line1value2);					
						var line1value2input = new Element("input", {"type": "text", "value": contactInfo.email , "name": namePrefix + "email]"}).inject(line1value2text, "after");																							
					var clearer = new Element("div", {"class" : "clearer"}).inject(line1value2, "after");	
						
				var line2 = new Element("div", {"class": "line twoCol text"}).inject(line1, "after");
					var line2label1 = new Element("div", {"class": "label", "html": "Vorname"}).inject(line2);
					var line2value1 = new Element("div", {"class": "value", "title": "value_" + itemID}).inject(line2label1, "after");					
						var line2value1text = new Element("div", {"title": "name", "html": contactInfo.first_name }).inject(line2value1);					
						var line2value1input = new Element("input", {"type": "text", "value": contactInfo.first_name , "name": namePrefix + "first_name]"}).inject(line2value1text, "after");																	
						
					var line2label2 = new Element("div", {"class": "label", "html": "Telefon"}).inject(line2value1, "after");
					var line2value2 = new Element("div", {"class": "value", "title": "value_" + itemID}).inject(line2label2, "after");					
						var line2value2text = new Element("div", {"title": "email", "html": contactInfo.normal_phone_number }).inject(line2value2);					
						var line2value2input = new Element("input", {"type": "text", "value": contactInfo.normal_phone_number , "name": namePrefix + "normal_phone_number]"}).inject(line2value2text, "after");																							
					var clearer = new Element("div", {"class" : "clearer"}).inject(line2value2, "after");							
						
				var line3 = new Element("div", {"class": "line twoCol text"}).inject(line2, "after");
					var line3label1 = new Element("div", {"class": "label", "html": "Position"}).inject(line3);
					var line3value1 = new Element("div", {"class": "value", "title": "value_" + itemID}).inject(line3label1, "after");					
						var line3value1text = new Element("div", {"title": "name", "html": contactInfo.position }).inject(line3value1);					
						var line3value1input = new Element("input", {"type": "text", "value": contactInfo.position , "name": namePrefix + "position]"}).inject(line3value1text, "after");																	
						
					var line3label2 = new Element("div", {"class": "label", "html": "Mobil"}).inject(line3value1, "after");
					var line3value2 = new Element("div", {"class": "value", "title": "value_" + itemID}).inject(line3label2, "after");					
						var line3value2text = new Element("div", {"title": "email", "html": contactInfo.mobile_phone_number }).inject(line3value2);					
						var line3value2input = new Element("input", {"type": "text", "value": contactInfo.mobile_phone_number , "name": namePrefix + "mobile_phone_number]"}).inject(line3value2text, "after");																																			
					var clearer = new Element("div", {"class" : "clearer"}).inject(line3value2, "after");							

				var line4 = new Element("div", {"class": "line twoCol text"}).inject(line3, "after");
					var line4label1 = new Element("div", {"class": "label", "html": " "}).inject(line4);
					var line4value1 = new Element("div", {"class": "value", "html": " "}).inject(line4label1, "after");					
						
					var line4label2 = new Element("div", {"class": "label", "html": "Vcard"}).inject(line4value1, "after");
					var line4value2 = new Element("div", { "class": "value" }).inject(line4label2, "after");					
						var line4value2text = new Element("a", {"class": "vcard", "id": "vcard_" + itemID, "href": getRootDir() + "vcard?id=" + contactInfo.id,"html": "Herunterladen..."}).inject(line4value2);					
						if(contactInfo.id == ""){
							line4value2text.style.display = "none";
						}
					var clearer = new Element("div", {"class" : "clearer"}).inject(line4value2, "after");							
					
				var line5 = new Element("div", {"class": "line oneCol text"}).inject(line4, "after");
					var line5label1 = new Element("div", {"class": "label", "html": "Adresse"}).inject(line5);
					var line5value1 = new Element("div", {"class": "value", "title": "value_" + itemID}).inject(line5label1, "after");					
						var line5value1text = new Element("div", {"title": "address", "html": contactInfo.address.replace(/\n/gi, "<br />") }).inject(line5value1);					
						var line5value1input = new Element("textarea", {"value": contactInfo.address, "name": namePrefix + "address]"}).inject(line5value1text, "after");																						
				var clearer = new Element("div", {"class" : "clearer"}).inject(line5value1, "after");																		
		item.inject(container, "top");
		
		if (contactInfo.id == ""){
			togglerEditableEntry(itemID, '', 'edit', 'contact');			
		}
		
		setContactIndex(parseInt( getContactIndex() ) + 1);
		
}

function deleteContact(blockID , itemId){
	if (itemId != ''){
		var request = new Request.JSON({
			url: "deal/edit",
			data: "id=" + itemId + "&deleteContactSubmit=1&" + sendByAjaxStr(),
			onComplete: function(jsonObj) {
				//alert(jsonObj.state);
			}
		}).send();		
	}
	
	//setContactIndex(parseInt( getContactIndex() ) - 1);
	
	var containerBlockIdName = $('contact_' + blockID).parentNode.parentNode.id;	
	var containerBlockId = containerBlockIdName.substr(containerBlockIdName.lastIndexOf("_") + 1);
	
	var additionalHeight;
	
	if (itemId == '') {
		additionalHeight = -233;
	}
	else{
		additionalHeight = -146;
	}
		
	checkSliderSize(containerBlockId, additionalHeight);		
	$("contact_" + blockID).parentNode.removeChild($("contact_" + blockID));

}

function setContactIndex(index){
	$('indexContact').value = index;
}

function getContactIndex(){
	return $('indexContact').value;
}

function setTodoIndex(index){
	$('indexTodo').value = index;
}

function getTodoIndex(){
	return $('indexTodo').value;
}

//////////////////////////////////////////////////////////////
//ToDOs
//////////////////////////////////////////////////////////////
var initToDos = function() {
		var request = new Request.JSON({
			async: false,
			data: getGlobalParameter() + sendByAjaxStr(),
			url: getRootDir() + "dataservice/todos",
			onComplete: function(jsonObj) {
				if 	(jsonObj == null ){			
					return 0;
				}
				initSelectToDos(jsonObj.selects);
			}
		}).send();
}

var initSelectToDos = function(obj){
	todoSelect = obj;
}

function bildSelectField( obj, name, param ){
	var mainName = name.substr(name.lastIndexOf("[") + 1);	
	mainName = mainName.substr(0, mainName.length - 1);		
	if(obj != null){
		for(i = 0; i < obj.length; i++){
			if (obj[i].name == mainName){
				var selectValue = 1;
				var select = new Element("select", {"name": name});
					var option;
					for(j = 0; j < obj[i].value.length; j++){
						option = new Element("option", {"html": obj[i].value[j].value, "value": obj[i].value[j].id}).inject(select);															
						if(obj[i].value[j].value == param){
							select.value = obj[i].value[j].id;
						}
					}
				return select;
			}
		}
		return null;	
	}
	else{
		var select = new Element("select", {"name": name});
		return select;
	}
}

var loadToDos = function(blockID, search) {
		if (search == undefined) 
		{
			var	data = getGlobalParameter();
			var url_string = getRootDir() + "todos/showlist";
		}
		else
		{
			var data = "searchString=" + search;
			var url_string = getRootDir() + "search/gettodossearch";
		}
				
		var request = new Request.JSON({
			async: false,
			url: url_string,
			data: data + sendByAjaxStr(),
			method: 'get',
			onComplete: function(jsonObj) {
				showListToDos(jsonObj.todos, blockID);
			}
		}).send();
}

var showListToDos = function(todos, blockID, search){
	if( todos == null){
		return 0;
	}
	
	
	if(todos.length > 0){
		$("list_todo").style.display = "block";
	}
	todos.each(
		function(todos){
			 buildToDo(blockID, todos, search);			 
		}
	);
}

function buildToDo(blockID, todoInfo, search){
		checkSliderSize(blockID, 26);
		var namePrefix = "todos[" + getTodoIndex() + "][";	
					
		if(todoInfo == ""){
			var currentTime = new Date();
			var month = currentTime.getMonth() + 1; 
			if (month.toString().length == 1){
				month = "0" + month
			}
			var day = currentTime.getDate();
			if (day.toString().length == 1){
				day = "0" + day
			}		
			var year = currentTime.getFullYear();
			
			full_day = day + "." + month + "." + year;					
			
			todoInfo = {				 
			 				"id": "",
			 				"start_date": full_day ,
			 				"category": "",
			 				"description": "",
			 				"owner": "",
			 				"status": "",	
							"url": ""
		 				}
		}
		
		var itemID = parseInt($("currentTodo").value) + 1;		
		$("currentTodo").value = itemID;		
		
		//var container = $("contactContainer");		
		
		if($("list_todo").style.display != "block"){
			$("list_todo").style.display = "block";
		}		
		
		var item = new Element("div", {"class": "item"});
			var todoIndex = new Element("input", {"type": "hidden", "value": getTodoIndex() }).inject(item);	
			var itemInfo = new Element("div", {"class": "infoTable ", "id": "infoTodo_" + itemID}).inject(item);
	
				var start_dateConainer = new Element("div", {"class": "date", "title": "value_" + itemID}).inject(itemInfo);					
					var start_dateInput = new Element("input", {"type": "text", "disabled":"disabled", "value": todoInfo.start_date , "name": namePrefix + "date]"}).inject(start_dateConainer);																	
					var start_date = new Element("div", {"html": todoInfo.start_date,"class": "date"}).inject(start_dateInput, "before");				
	
				var categoryConainer = new Element("div", {"class": "category", "title": "value_" + itemID}).inject(start_dateConainer, "after");					
					var categoryInput = bildSelectField(todoSelect, namePrefix + "category]", todoInfo.category).inject(categoryConainer);																							
					var category = new Element("div", {"html": todoInfo.category,"class": "category"}).inject(categoryInput, "before");								

				var descriptionConainer = new Element("div", {"class": "description", "title": "value_" + itemID}).inject(categoryConainer, "after");					
					var descriptionInput = new Element("input", {"value": todoInfo.description, "name": namePrefix + "description]"}).inject(descriptionConainer);																						
					var description = new Element("div", {"class": "description link"}).inject(descriptionInput, "before");								
						var description_link = new Element("a", {"html": todoInfo.description, "href": todoInfo.url } ).inject(description);								

				var owner_nameConainer = new Element("div", {"class": "owner", "title": "value_" + itemID}).inject(descriptionConainer, "after");					
					var owner_nameInput = bildSelectField(todoSelect, namePrefix + "user]", todoInfo.owner).inject(owner_nameConainer);																	
					var owner_name = new Element("div", {"html": todoInfo.owner,"class": "owner"}).inject(owner_nameInput, "before");				
	
				var statusConainer = new Element("div", {"class": "status", "title": "value_" + itemID}).inject(owner_nameConainer, "after");					
					var statusInput = bildSelectField(todoSelect, namePrefix + "status]", todoInfo.status).inject(statusConainer);																									
					//var status = new Element("div", {"html": "&#160;", "class": "status " + todoInfo.status.toLowerCase().replace(/ /, "")}).inject(statusInput, "before");															
					var status = new Element("div", {"html": todoInfo.status, "class": "status " }).inject(statusInput, "before");																				
				
				var functionsConainer = new Element("div", {"class": "functions", "title": "value_" + itemID}).inject(statusConainer, "after");					
					var link1 = new Element("a", {"id": "todo_edit_" + itemID , "class": "edit", "href": "javascript:togglerEditableEntry(" + itemID + ", '" + todoInfo.id + "', 'edit', 'todo')"}).inject(functionsConainer);			
					var link2 = new Element("a", {"id": "todo_cancel_" + itemID , "class": "cancel", "href": "javascript:togglerEditableEntry(" + itemID + ", '" + todoInfo.id + "', 'cancel', 'todo')"}).inject(link1, "after");			
					var link3 = new Element("a", {"id": "todo_save_" + itemID , "class": "save", "href": "javascript:togglerEditableEntry(" + itemID + ", '" + todoInfo.id + "', 'save', 'todo')"}).inject(link2, "after");			
					var link4 = new Element("a", {"id": "todo_delete_" + itemID ,"class": "delete", "href": "javascript:deleteTodo(" + itemID + ", '" + todoInfo.id + "')"}).inject(link3, "after");			
					var clearer = new Element("div", {"class" : "clearer"}).inject(link4, "after");								
				
				var clearer = new Element("div", {"class" : "clearer"}).inject(functionsConainer, "after");
			var clearer = new Element("div", {"class" : "clearer"}).inject(itemInfo, "after");

		item.inject($('content_todo'));
		if (todoInfo.id == "") {
			togglerEditableEntry(itemID, '', 'edit', 'todo');
		}
		
		setTodoIndex(parseInt( getTodoIndex() ) + 1);
}

function createDefaultTodos(blockID, owner){
	createDefaultTodoSingle(blockID, "Eingangsmail", owner);
	createDefaultTodoSingle(blockID, "Sichten", owner);
	createDefaultTodoSingle(blockID, "Absage", owner);
}

function createDefaultTodoSingle(blockID, category, owner){
			var currentTime = new Date();
			var month = currentTime.getMonth() + 1; 
			if (month.toString().length == 1){
				month = "0" + month
			}
			var day = currentTime.getDate();
			if (day.toString().length == 1){
				day = "0" + day
			}		
			var year = currentTime.getFullYear();
			
			full_day = day + "." + month + "." + year;		
			
			todoInfo = {				 
			 				"id": "",
			 				"start_date": full_day ,
			 				"category": category,
			 				"description": "kein Kommentar",
			 				"owner": owner,
			 				"status": "",	
							"url": ""
		 				}	
			buildToDo(blockID, todoInfo, "");
}

function deleteTodo(blockID , itemId){
	if (itemId != ''){
		var request = new Request.JSON({
			url: "todos/delete",
			data: getGlobalParameter('parent_') + "deleteSubmit=1&id=" + itemId + sendByAjaxStr(),
			onComplete: function(jsonObj) {
				//alert(jsonObj.state);
			}
		}).send();		
	}
	
	var containerBlockIdName = $('infoTodo_' + blockID).parentNode.parentNode.parentNode.parentNode.id;	
	var containerBlockId = containerBlockIdName.substr(containerBlockIdName.lastIndexOf("_") + 1);
	
	var additionalHeight;
	
	additionalHeight = -26;
	
	checkSliderSize(containerBlockId, additionalHeight);		
	$("infoTodo_" + blockID).parentNode.parentNode.removeChild($("infoTodo_" + blockID).parentNode);

}

//////////////////////////////////////////////////////////////
//Comments
//////////////////////////////////////////////////////////////
var loadComments = function(blockId) {
		var request = new Request.JSON({
			url: "../comments/showlist",
			data: getGlobalCommentsParameter() + sendByAjaxStr() + "&parent_sup_type=" + document.getElementById("gloablType").value ,
			method: 'get',
			onComplete: function(jsonObj) {
				if 	(jsonObj == null ){			
					return 0;
				}
				showListComments(jsonObj.comments, blockId);
			}
		}).send();
}


var showListComments = function(comments, blockId){
	if 	(comments == null ){			
		return 0;
	}
	comments.each(
		function(comments){
			 buildComment(blockId, comments);			 
		}
	);
}

function buildComment(blockID, commentInfo){
		checkSliderSize(blockID, 163);
	
	
		if (commentInfo == ""){
			var commentInfo = {				 
					 				"id": "",
				 					"owner": "",
									"comment": ""														
								}
		}
	
		var itemID = parseInt($("currentComment").value) + 1;		
		$("currentComment").value = itemID;		
		
		var container = $("commentContainer");
	
		//alert(container);
		var item = new Element("div", {"class": "item", "id": "comment_" + itemID});
			var functions = new Element("div", {"class": "functions"}).inject(item);
				var link1 = new Element("a", {"id": "comment_edit_" + itemID , "html": "Bearbeiten...", "class": "edit", "href": "javascript:togglerEditableEntry(" + itemID + ", '" + commentInfo.id + "', 'edit', 'comment')"}).inject(functions);			
				var link1span = new Element("span", {"html": " "}).inject(link1, "after");
				var link2 = new Element("a", {"id": "comment_cancel_" + itemID ,"html": "Abbrechen", "class": "cancel", "href": "javascript:togglerEditableEntry(" + itemID + ", '" + commentInfo.id + "', 'cancel', 'comment')"}).inject(link1span, "after");			
				var link2span = new Element("span", {"html": " "}).inject(link2, "after");
				var link3 = new Element("a", {"id": "comment_save_" + itemID ,"html": "Speichern", "class": "save", "href": "javascript:togglerEditableEntry(" + itemID + ", '" + commentInfo.id + "', 'save', 'comment')"}).inject(link2span, "after");			
				var link3span = new Element("span", {"html": " "}).inject(link3, "after");
				var link4 = new Element("a", {"id": "comment_delete_" + itemID ,"html": "Löschen", "href": "javascript:deleteComment(" + itemID + ", '" + commentInfo.id + "')"}).inject(link3span, "after");			
				
			var comment = new Element("div", {"class": "infoTable", "id": "infoComment_" + itemID}).inject(functions, "after");
				var line1 = new Element("div", {"class": "line oneCol text"}).inject(comment);
					var line1label1 = new Element("div", {"class": "label", "html": "Kommentar"}).inject(line1);
					var line1value1 = new Element("div", {"class": "value", "title": "value_" + itemID}).inject(line1label1, "after");					
						var line1value1text = new Element("div", {"title": "comment", "html": commentInfo.comment.replace(/\n/gi, "<br />") }).inject(line1value1);					
						var line1value1input = new Element("textarea", {"html": commentInfo.comment, "name": "comment"}).inject(line1value1text, "after");																						
				var clearer = new Element("div", {"class" : "clearer"}).inject(line1value1, "after");	
				if(commentInfo.owner != ""){
					var line2 = new Element("div", {"class": "line oneCol text"}).inject(line1, "before");
						var line2label1 = new Element("div", {"class": "label", "html": "Autor"}).inject(line2);
						var line2value1 = new Element("div", {"class": "value", "title": "value_" + itemID}).inject(line2label1, "after");					
							var line2value1text = new Element("div", {"title": "owner", "html": commentInfo.owner }).inject(line2value1);					
					var clearer = new Element("div", {"class" : "clearer"}).inject(line2value1, "after");																		
				}
		item.inject(container);
		
		if (commentInfo.id == ""){
			togglerEditableEntry(itemID, '', 'edit', 'comment');			
		}
		
}

function deleteComment(blockID , itemId){
	if (itemId != ''){
		var request = new Request.JSON({
			url: "../comments/delete",
			data: getGlobalCommentsParameter('parent_') + "id=" + itemId + "&deleteSubmit=1" + sendByAjaxStr() + "&parent_sup_type=" + document.getElementById("gloablType").value,
			onComplete: function(jsonObj) {
				//alert(jsonObj.state);
			}
		}).send();		
	}
	
	var containerBlockIdName = $('comment_' + blockID).parentNode.parentNode.id;	
	var containerBlockId = containerBlockIdName.substr(containerBlockIdName.lastIndexOf("_") + 1);
	
	var additionalHeight;
	
	if (itemId == '') {
		additionalHeight = -233;
	}
	else{
		additionalHeight = -146;
	}
		
	checkSliderSize(containerBlockId, additionalHeight);		
	$("comment_" + blockID).parentNode.removeChild($("comment_" + blockID));

}

//////////////////////////////////////////////////////////////
//Paging
//////////////////////////////////////////////////////////////

function buildPaging(container, currentPage, totalPages, jsFunction, jsParam){
	if (totalPages > 1){
		if (currentPage	>= 4 ){
			buildSinglePage(1, container, jsFunction, jsParam);
			if (currentPage	> 4 ){
				buildPageSeparator(container);
			}
		}
		buildNextPreviousPages(currentPage, totalPages, container,jsFunction, jsParam);
		if (totalPages - currentPage >=   3){
			if (totalPages - currentPage >   3){
				buildPageSeparator(container);				
			}
			buildSinglePage(totalPages, container, jsFunction, jsParam);			
		}		
	}
}

function buildSinglePage(pageIndex, container, jsFunction, jsParam){
	var jsParamString = ",";
	for (t = 0; t < jsParam.length; t++){
		jsParamString += "'" + jsParam[t] + "'";
		if (t != jsParam.length - 1){
			jsParamString += ", ";
		}
	}
	
	new Element("a", {"html": pageIndex, "href": "javascript: " + jsFunction + " (" + pageIndex + jsParamString +")"}).inject(container);
}

function buildPageSeparator(container){
	new Element("span", {"html": "..."}).inject(container);
}

function buildNextPreviousPages(pageIndex, totalPages, container, jsFunction, jsParam){
	for (i = pageIndex - 2; i <= parseInt(pageIndex) + 2; i++){
		if(i > 0 && i <= totalPages){
			buildSinglePage(i, container, jsFunction, jsParam);
		}
	}
}


//////////////////////////////////////////////////////////////
//Utilities
//////////////////////////////////////////////////////////////

function getSelectFieldValueName(id){
	var optionValue = $(id).value;
	for(j = 0; j < $(id).childNodes.length; j++){
		if (optionValue == $(id).childNodes[j].value){
			return $(id).childNodes[j].innerHTML;
		}
	}	
}

function checkSliderSize(blockId, additionalHeight){
	if( $("vertical_slide_" + blockId).parentNode.style.height != ""){
		$("vertical_slide_" + blockId).parentNode.style.height = (parseInt($("vertical_slide_" + blockId).parentNode.style.height) + additionalHeight) + "px";
	}
}

function getElementsByClassName(class_name)
{
  var all_obj,ret_obj=new Array(),j=0;
  if(document.all)all_obj=document.all;
  else if(document.getElementsByTagName && !document.all)all_obj=document.getElementsByTagName("*");
  for(i=0;i<all_obj.length;i++)
  {
    if(all_obj[i].className==class_name)
    {
      ret_obj[j]=all_obj[i];
      j++
    }
  }
  return ret_obj;
}  

/*
	Copyright Robert Nyman, http://www.robertnyman.com
	Free to use if this text is included
*/

function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
	var oCurrent;
	var oAttribute;
	for(var i=0; i<arrElements.length; i++){
		oCurrent = arrElements[i];
		oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
		if(typeof oAttribute == "string" && oAttribute.length > 0){
			if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
				arrReturnElements.push(oCurrent);
			}
		}
	}
	return arrReturnElements;
}