function checkFieldEmpty(value, picID)
{
	
	// Set picID
	var picID = picID + "Validate";
	if(value)
	{
		document.getElementById(picID).setAttribute("src", "include/images/ok.png");
		
		return true;
	}
	else
	{
		document.getElementById(picID).setAttribute("src", "include/images/no.png");
		
		return false;
	}
	
}

function checkFieldsEqual(field1, field2, picID)
{
	// Set picID
	var picID = picID + "Validate";
	
	if(field1 == field2)
	{
		document.getElementById(picID).setAttribute("src", "include/images/ok.png");
		
		return true;
	}
	else
	{
		document.getElementById(picID).setAttribute("src", "include/images/no.png");
		
		return false;
	}
}

function AJAXCheckUsername(username)
{

  var HTTPRequest = makeHTTPRequestWait('checkUsername=1' + '&action=AGeneral', 	'username=' + username);

	return HTTPRequest.responseText;
}

function AJAXCheckEmail(email)
{
  var HTTPRequest = makeHTTPRequestWait('checkEmail=1' + '&action=AGeneral', 	'email=' + email);

	
	return HTTPRequest.responseText;
}

function AJAXCheckUsernameEdit(username, userID)
{
  var HTTPRequest = makeHTTPRequestWait('checkUsernameEdit=1' + '&action=AGeneral', 	'username=' + username +
																																									'&userID=' + userID);


	return HTTPRequest.responseText;
}

function AJAXCheckCoursename(coursename) 
{

  var HTTPRequest = makeHTTPRequestWait('checkCoursename=1' + '&action=AGeneral', 	'coursename=' + coursename);

	return HTTPRequest.responseText;
}
	
function AJAXCheckCategorycoursename(categoryname)
{

  var HTTPRequest = makeHTTPRequestWait('checkCategorycoursename=1' + '&action=AGeneral', 	'categoryname=' + categoryname);
 
	return HTTPRequest.responseText;
}
	
	
/*
		Functions (class in JS) for errorhandling
*/
function ErrorMessageHandler()
{
	this.errorMessages = new Array();
}

function changeErrorMessagesState(fieldNr, messageNr, value)
{
	// Check if this error message exists, else create it
	if(!this.errorMessages[fieldNr])
	{
		this.errorMessages[fieldNr] = new Array();;
		this.errorMessages[fieldNr][1] = messageNr;
		this.errorMessages[fieldNr][2] = 0;
	}
	
	// change state
	this.errorMessages[fieldNr][2] = value;
	
}

function printErrorMessages()
{
	showErrorBox = 0;
	
	// Array that contains each error messages and how many times it should be displayed (0 meands hidden and all values higher than 0 means visible
	showErrorMessages = new Array();
				
	for(curErrorMessage in this.errorMessages)
	{
		
		// Cur showErrorMessage is null, set to 0
		if(showErrorMessages[this.errorMessages[curErrorMessage][1]] == null)
		{
			showErrorMessages[this.errorMessages[curErrorMessage][1]] = 0;
		}
		
		if(this.errorMessages[curErrorMessage][2] != null)
		{
			
			if(this.errorMessages[curErrorMessage][2] > 0)
			{
				showErrorBox++;
				showErrorMessages[this.errorMessages[curErrorMessage][1]]++;
			}
			else
			{
				// showErrorMessages[this.errorMessages[curErrorMessage][1]]--;
			}
		}
	}
	
	// If showErrorBox is bigger than 0, show
	if(showErrorBox < 1)
	{
		// Hide errorbox
		document.getElementById("error").style.visibility = "hidden";
	}
	else
	{
		// Show errorbox
		document.getElementById("error").style.visibility = "visible";
	}
	
	// Show/hide errors
	// Loop through showErrorMessages
	for(curErrorMessage in showErrorMessages)
	{
		if(showErrorMessages[curErrorMessage] > 0)
		{
			// Show specific error
			document.getElementById("errorNr" + curErrorMessage).style.visibility = "visible";
			document.getElementById("errorNr" + curErrorMessage).style.position = "relative";
		}
		else
		{
			// Hide specific error
			document.getElementById("errorNr" + curErrorMessage).style.visibility = "hidden";
			document.getElementById("errorNr" + curErrorMessage).style.position = "absolute";
		}
	}
}




/*
		END ERROR HANDLING
*/



function showErrorMessage(message)
{
	// Show errorbox
	document.getElementById("error").style.visibility = "visible";
	document.getElementById("error").style.position = "relative";
	
	// Get innerhtml
	currentErrorMessage = document.getElementById("errormessage").innerHTML;
	
	// Set new message
	currentErrorMessage += "<p class='errortext'>" + message + "</p>";
	document.getElementById("errormessage").innerHTML = currentErrorMessage;
}

function hideErrorMessage()
{
	// Hide errorbox
	document.getElementById("error").style.visibility = "hidden";
	document.getElementById("error").style.position = "absolute";
	
	// Set empty innerhtml
	document.getElementById("errormessage").innerHTML = "";

}

function getYearsInSelect(name, startYear)
{

	if(!startYear)
	{
		var d = new Date();
		startYear = d.getFullYear(); 
	}
	
	html = "<select id='" + name + "' name='" + name + "'>";

	for(i = startYear; i <= (startYear + 1); i++)
	{
		html += "<option value='" + i + "'>" + i + "</option>";
	}
	
	html += "</select>";
	
	return html;
}

function getUsersInSelect(htmlID, rights, selected, class2)
{
	if(!rights)
	{
		rights = 0;
	}

	if(!selected)
	{
		selected = "";	
	}
	 
  var HTTPRequest = makeHTTPRequestWait('getUsers=1' + '&action=AGeneral', 	'htmlID=' + htmlID +
																																											'&rights=' + rights +
																																											'&selected=' + selected +
																																											'&class=' + class2);
	  
  HTTPRequest.onreadystatechange = function() {
    if (HTTPRequest.readyState == 4) {
      if (HTTPRequest.status == 200) {
        result = HTTPRequest.responseText;
				return result;
      } 
    }
  };
	return HTTPRequest.responseText;
}

function getCalendar(htmlID, year, month, day, location, oldDates)
{
	var todayColor, clickedColor, backgroundColor, textColor, dayColor, nextOrBeforeColor;

	if(!oldDates)
	{
		oldDates = 0;
	}

	todayColorImg = "include/images/redr.png";
	clickedColor = "#d4cfcf";
	backgroundColor = "#f3f4f4";
	textColor = "#54636e";
	dayColor = "#f3f4f4";
	nextOrBeforeColor = "#cdc9c4";
	
 var temp = 1;
 var j = 1;
	
	
	var monthArray = new Array();
	monthArray[1] = "Januari";
	monthArray[2] = "Februari";
	monthArray[3] = "Mars";
	monthArray[4] = "April";
	monthArray[5] = "Maj";
	monthArray[6] = "Juni";
	monthArray[7] = "Juli";
	monthArray[8] = "Augusti";
	monthArray[9] = "September";
	monthArray[10] = "Oktober";
	monthArray[11] = "November";
	monthArray[12] = "December";


	
	var date = new Date();
	
	// Used to only make todays date and forward clickable, se line 373 and forward
	var tempDate = date;
	tempDate.setDate(tempDate.getDate()-1);
	
	// Ifall datum inte är satt, sätt till dagens
	if(year == "")
	{
		year = date.getFullYear();
	}

	if(month === "")
	{
		month = date.getMonth();
	}
	
	if(day == "")
	{
		day = date.getDate();
	}

	var userDate = new Date(year, month, day);
	startDate = new Date(userDate.getFullYear(), userDate.getMonth(), 1);
	var lastDateInMonth = new Date(userDate.getFullYear(), userDate.getMonth() + 1, 0);
	var endDate = new Date(userDate.getFullYear(), userDate.getMonth() + 1, 0);
	var nextMonth = new Date(userDate.getFullYear(), userDate.getMonth(), 1);
	var previousMonth = new Date(userDate.getFullYear(), userDate.getMonth(), 1);
	
	var todayDate = new Date();
	
	// Set nextMonth
	nextMonth.setMonth(nextMonth.getMonth() + 1);

	// Set previous month
	previousMonth.setMonth(previousMonth.getMonth() - 1);

	// Get startdate, always a monday
	while(startDate.getDay() !== 1)
	{
		startDate.setDate(startDate.getDate() - 1);
	}
	
	// Get enddate, always a sunday
	while(endDate.getDay() !== 0)
	{
		endDate.setDate(endDate.getDate() + 1);
	}


	// Table var, that are returned later on
	table = "<table height='226' width='185' border='0' style='color :" +textColor +"; border: solid 1px #000; background-color: " + backgroundColor+"' cellspacing='0' cellpadding='0' >";
	table += "<tr ><td colspan='3' valign='top' style = 'height: 25px; padding-left: 8px; font-weight: bold; font-family: tahoma; font-size: 12px; color :" +textColor +"; text-align: left; border-bottom: solid 1px;'>Kalender&aring;r "+userDate.getFullYear()+"</td></tr>";
	table += "<tr> <td style='height: 22px; border-bottom: solid 1px;'><a style='margin-left:5px;' onclick = \"getCalendar('"+htmlID+"', '" + (previousMonth.getFullYear()) + "', '" + (previousMonth.getMonth()) + "', 1, '"+ location + "') \">&laquo;&laquo;</<a></td><td style = 'border-bottom: solid 1px; font-family: tahoma; font-size: 14px; font-weight: bold; color :" +textColor +"; text-align: center'>	"+ monthArray[(userDate.getMonth() + 1)] + "	</td>				<td style='border-bottom: solid 1px;' align='right'>		<a  style='margin-right:5px;' onclick = \"getCalendar('"+htmlID+"', '" + nextMonth.getFullYear() + "', '" + (nextMonth.getMonth()) + "', 1, '"+ location + "') \">&raquo;&raquo;</<a>							</td>	</tr>";
	table += "<tr>";
	table += "<td valign='top' colspan='3'>";
	// Loop through calendar
	while((Date.UTC(startDate.getFullYear(), startDate.getMonth(), startDate.getDate())) < (Date.UTC(endDate.getFullYear(), endDate.getMonth(), endDate.getDate())) )
	{

		table += "<div style='margin-left: 4px; margin-right: 4px; clear: both;'>";
		
		temp = 1;
		for(i = 1; i < 8; i++)
		{
			if(startDate.getFullYear() == todayDate.getFullYear() && startDate.getMonth() == todayDate.getMonth() && startDate.getDate() == todayDate.getDate())
			{
				innerTD = " background-image:url("+todayColorImg+")";
				curTextColor = "#FFF";
			}
			else if(startDate.getFullYear() == year && startDate.getMonth() == month && startDate.getDate() == day)
			{
				innerTD = " background-color:"+clickedColor;
				curTextColor = "#000";
			}
			else
			{
				curTextColor = textColor;
				if (temp == 2)
				{
					innerTD = " background-color:"+dayColor;
				}
				else
				{
					innerTD = " background-color: #FFFFFF";
				}
			}
			
			if (temp == 2)
			{
				temp=1;
			}
			else
			{
				temp=2;
			}
			
			table += "<div style=' float:left; padding-top: 4px; " + innerTD + "; height: 19px; width: 23px; margin-top: 4px; font-size: 12px; font-weight: bold;  border:solid 1px; vertical-align:middle;'><center>";
			
			curOperators = "&year=" + startDate.getFullYear() + "&month="+ startDate.getMonth() + "&day="+startDate.getDate();

			if(oldDates == 0)
			{
				if(startDate > tempDate)
				{
					table += "<a  style = 'text-decoration : none; color :" +curTextColor +"' href=' "+ location + curOperators + "' >" + startDate.getDate() + "</a>";			
				}
				else
				{
					table += startDate.getDate();			
				}
			}
			else if(oldDates == 1)
			{
				table += "<a  style = 'text-decoration : none; color :" +curTextColor +"' href=' "+ location + curOperators + "' >" + startDate.getDate() + "</a>";	
			}
				
			table += "</center></div>";
			
			startDate.setDate(startDate.getDate() + 1);
		}
		table += "</div>";
	}
	table += "</tr>";
	table += "</td>";
	table += "</table>";

	document.getElementById(htmlID).innerHTML = table;

}
