function showRelated(temp)
{
	if(temp != null && temp != "") {
		var relatedTasks = document.getElementById('projhdn');
		relatedTasks.value = temp;
		doChangeFilter(relatedTasks,document.getElementById("olist"),14);
	}
}

function ReplaceTags(xStr)
{
	var regExp = /<\/?[^>]+>/g;
	xStr = xStr.replace(regExp,"");
	return xStr.replace(/^\s+|\s+$/g,'');
}

var filterStore = new Array();

function IsNumeric(textval)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	
	for (i = 0; i < textval.length && IsNumber == true; i++) 
	{ 
		Char = textval.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	if(textval.length==0) 
	IsNumber = false;
	return IsNumber;
}

function replace(statusval)
{			
	var text1=statusval.replace("&nbsp;",'');
	var text2=text1.replace("%",'');			
	return text2;
}

/***
Function: doChangeFilter
Summary: Calls a function that filters the table when user types in a key in the input, then creates the cookie with the values
****/

function doChangeFilter(sender,table,cell)
{
	doChangeFilters(sender.value,table,cell,"table");
	//createCookies();
}

function doChangeAddressFilter(sender,table,cell)
{
	doChangeFilters(sender.value,table,cell,"table");
}

/***
Function: createCookies
Summary: Reads the values from the filters and creates a cookie with the values
****/

function createCookies(checkValue,cell)
{
	var tableHeaders = new Array("type","priority","state","client","task","pm","tl","start","deadline","update","hours","status");
	
	var cookieContent = "";

	for (i = 0; i < tableHeaders.length; i++)
	{
		var checkValue = document.getElementById(tableHeaders[i] + "x").value;
		cookieContent = cookieContent + tableHeaders[i] + ":" + checkValue + "|";
	}
	
	if(cookieContent != "")
	{
		createCookie("filter",cookieContent,1);
	}
}

/***
Function: checkCookies
Summary: Checks if a filter cookie exists, and if it does, it fills in the filters and calls a function to filter the table
****/

function checkCookies()
{
	var cookieContent = readCookie("filter");
	
	if(cookieContent)
	{
		var cookieValues = cookieContent.split("|");

		for(i = 0; i < cookieValues.length-1; i++)
		{
			var columnFilter = cookieValues[i].split(":");
			
			if(columnFilter.length > 1)
			{
				var columnName = columnFilter[0];
				var columnValue = columnFilter[1];
				
				if(columnValue != "")
				{
					document.getElementById(columnName + "x").value = columnValue;
					doChangeFilters(columnValue,document.getElementById('olist'),i,"cookie");
				}
			}
		}
	}
}

function replaceSpecialCharacters(text) {
	var text = replaceAll(text, "á","a");	
	text = replaceAll(text, "å","a");
	text = replaceAll(text, "ä","a");
	text = replaceAll(text, "í","i");
	text = replaceAll(text, "ó","o");
	text = replaceAll(text, "ö","o");
	text = replaceAll(text, "ú","u");
	text = replaceAll(text, "é","e");
	text = replaceAll(text, "æ","ae");
	text = replaceAll(text, "þ","th");
	text = replaceAll(text, "ð","d");
	
	return text;
}

function replaceAll(oldStr,findStr,repStr) {
	// srchNdx will keep track of where in the whole line of oldStr are we searching.
	var srchNdx = 0;  
	
	// newStr will hold the altered version of oldStr.
	var newStr = "";  

	// As long as there are strings to replace, this loop will run.
	while (oldStr.indexOf(findStr,srchNdx) != -1)
	{
		newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
		newStr += repStr;
		srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
	}
	
	newStr += oldStr.substring(srchNdx,oldStr.length);
	return newStr;
}

function doChangeFilters(sender,table,cell,type)
{	

	var sText;			     
	
	sText = sender.toLowerCase();

	table = document.getElementById('olist');					
	var textval;
	var type = new Array();     
	var val = sText;
	
	filterStore[cell] = sText;			     
				
	var hideRow;
	var counter = 1;
	
	for (var i=2;i < table.rows.length;i++)
	{
		hideRow = false;

		var oRow = table.rows[i];
			
		var cels = oRow.getElementsByTagName('td');
		
		if(cels.length > 0 && oRow.getAttribute('class')!='dontinclude')
		{        
			for (var j=0;j<filterStore.length;j++) 
			{	
				if (filterStore[j]) 
				{
					var isAltFilterExact = cels[j].getAttribute('filterexactalt');
					var val2 = '';
					
					if (!isAltFilterExact)
						val2 = ReplaceTags(cels[j].innerHTML.toLowerCase());

					var testVal = (!isAltFilterExact ? val2 : isAltFilterExact);
					
					if(j==0 || j==11 || j ==13)
					{
						var count=0;
						commaseperated = filterStore[j].split(",");
						
						for(z=0;z<commaseperated.length;z++)
						{
							hyphenseperated = commaseperated[z].split("-");
							if(hyphenseperated.length == 2)
							{ 
								if(trimAll(hyphenseperated[0])!='' && trimAll(hyphenseperated[1])!='')
								{
									from = trimAll(hyphenseperated[0]);
									to = trimAll(hyphenseperated[1]);
									if(from<=to)
									{
										for(x=from;x<=to;x++)
										{
											/*
											backup: cels[j].innerHTML.toLowerCase().indexOf(x) >= 0
											
											cels[j].innerHTML.toLowerCase() == x
											
											Changed the code from indexOf to equals (==)
											so I only get type 1 and not type 1 and 10 and 11 and 12 etc.
											*/
											
											if(testVal == x)
											{
												//alert(testVal+" "+x);											
												count++;																														
											}									
										}
									}
								}
							}														
							
							if(trimAll(commaseperated[z])!='')
							{
								/*
								backup: cels[j].innerHTML.toLowerCase().indexOf(commaseperated[z]) >= 0
								cels[j].innerHTML.toLowerCase() == commaseperated[z]
								
								Changed the code from indexOf to equals (==)
								so I only get type 1 and not type 1 and 10 and 11 and 12 etc.
								*/
							
							/*
								if(testVal == commaseperated[z])
								{
									alert(testVal+" "+commaseperated[z]);
									count++;																														
								}												
								*/
								
								
								if(testVal.indexOf(commaseperated[z])!=-1)
								{
									//alert(testVal+" "+commaseperated[z]);
									count++;																														
								}
							}
						}
						if(count == 0)
						{
							hideRow = true
						}
					}														
					else  
					{
						if(isAltFilterExact) {
							if (!(isAltFilterExact+''==filterStore[j]+'')) {
							//alert(isAltFilterExact+' '+filterStore[j]);
								hideRow = true;
							}
						} else  if (replaceSpecialCharacters(val2).indexOf(replaceSpecialCharacters(filterStore[j])) < 0) {
						//alert(filterStore[j]);
							hideRow = true;
						}
						//break;
					}
				}    
			}
		}
			
		if (hideRow) {
			oRow.style.display = 'none';
		} else {
			oRow.style.display = '';
		}
	}
}

function trimAll(sString) 
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	while (sString.substring(0,1) == '%')
	{
		sString = sString.substring(1, sString.length);
	}
	
	while (sString.substring(sString.length-1, sString.length) == '%')
	{
		sString = sString.substring(0,sString.length-1);
	}
	
	return sString;
}

function cal(date)
{
	var url_new;
	url = document.location.href;

	if (url.indexOf('&') > 0)
	{
		url_new = url.split('&');
		url_new = url_new[0].split('=');
	}
	url = url.split('&');
	
	if ((url[1] == 'date=desc') || (document.location.href.indexOf('&') < 0) || (date != url_new[1]))
		document.location.href = '?'+'change='+date+'&amp;date=asce';
	else
		if (url[1] == 'date=asce')
			document.location.href = '?'+'change='+date+'&amp;date=desc';
}

function getprojectid()
{
	document.getElementById('projhdn').value = document.getElementById('projectid').value;
}
