// --------------------------------
// Load
// --------------------------------

window.onload = function()
{
	LoadTextSize();
	document.getElementById('Production').style.display = 'none';
}

// --------------------------------
// Print
// --------------------------------

function PrintPage()
{
	window.print();
}

// --------------------------------
// Cookie
// --------------------------------

function SetCookie(name,value,expires,path,domain,secure)
{
    document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
}

function GetCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
	{
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
	else
	{
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
	{
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function DeleteCookie(name,path,domain)
{
    if (GetCookie(name))
	{
        document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

// --------------------------------
// Menu
// --------------------------------

function MenuOver(obj)
{
	if(obj.className != 'Active')
	{
		obj.className = 'Over';		
	}
}

function MenuOut(obj)
{
	if(obj.className != 'Active')
	{
		obj.className = '';	
	}
}

// --------------------------------
// AJAX
// --------------------------------

function AjaxUpdateLatestNews(intLocationId)
{
	new Ajax.Updater('LatestNewsPanel', 'Inc/Ajax.asp', { method: 'get', parameters: { Action: 'UpdateLatestNews', LocationId: intLocationId } });
}

function AjaxUpdateLatestCalendar(intLocationId)
{
	new Ajax.Updater('LatestCalendarPanel', 'Inc/Ajax.asp', { method: 'get', parameters: { Action: 'UpdateLatestCalendar', LocationId: intLocationId } });
}

// --------------------------------
// Email
// --------------------------------

function WriteEmailAddress(a,x,y)
{
	b = '@';
	c = '.';
	var output = '';
	output += a;
	output += b;
	output += x;
	output += c;
	output += y;
	document.write('<a href=\"mailto:' + output + '\" title=\"' + output + '\">' + output + '</a>');
}

// --------------------------------
// Text size
// --------------------------------

function SetTextSize(direction)
{
	if(!GetCookie('FontSize'))
	{
		SetCookie('FontSize','Medium');
		document.body.style.fontSize='62.5%';	
	}
	if(direction=='Larger')
	{
		if('Small'==GetCookie('FontSize'))
		{
			SetCookie('FontSize','Medium');
			document.body.style.fontSize='62.5%';
		}
		else if('Medium'==GetCookie('FontSize'))
		{
			SetCookie('FontSize','Large');
			document.body.style.fontSize='70%';	
		}
	}
	else
	{
		if('Large'==GetCookie('FontSize'))
		{
			SetCookie('FontSize','Medium');
			document.body.style.fontSize='62.5%';	
		}
		else if('Medium'==GetCookie('FontSize'))
		{
			SetCookie('FontSize','Small');
			document.body.style.fontSize='55%';	
		}
	}
}

function LoadTextSize()
{
	if('Small'==GetCookie('FontSize')||'Medium'==GetCookie('FontSize')||'Large'==GetCookie('FontSize'))
	{
		var strSize = GetCookie('FontSize');
		if(strSize=='Small')
		{
			document.body.style.fontSize='55%';
		}
		else if(strSize=='Large')
		{
			document.body.style.fontSize='70%';
		}
		else
		{
			document.body.style.fontSize='62.5%';
		}
	}
}

// --------------------------------
// Form
// --------------------------------

function ActivateFormField(objField)
{
	if(objField.type == "text")
	{
		objField.className = "FormInputTextActive";
	}
	else if(objField.type == "textarea")
	{
		objField.className = "FormTextareaActive";
	}
	document.getElementById("Title"+objField.id).className = "FormTitleActive";
}

function DeactivateFormField(objField)
{
	if(objField.type == "text")
	{
		objField.className = "FormInputText";
	}
	else if(objField.type == "textarea")
	{
		objField.className = "FormTextarea";
	}
	document.getElementById("Title"+objField.id).className = "FormTitle";
}

function HighlightButton(objButton)
{
	objButton.className = "FormButtonOver";
}

function DownlightButton(objButton)
{
	objButton.className = "FormButton";
}

// --------------------------------
// Search
// --------------------------------

function InitMarkSearch()
{
	var strReferrer = document.referrer;
	if (strReferrer != '' && strReferrer != null)
	{
		var strQueryPrefix = "SearchString=";
		strQueryPrefix = strQueryPrefix.toLowerCase();
		var intStartPosition = strReferrer.toLowerCase().indexOf(strQueryPrefix);
		if ((intStartPosition < 0) || (intStartPosition+strQueryPrefix.length == strReferrer.length))
		{
			//Error
		}
		else
		{
			var intEndPosition = strReferrer.indexOf("&",intStartPosition);
			if (intEndPosition < 0)
			{
				intEndPosition = strReferrer.length;
			}
			var strQueryString = strReferrer.substring(intStartPosition+strQueryPrefix.length,intEndPosition);
			strQueryString = strQueryString.replace(/%20/gi, " ");	// fix the space characters
			strQueryString = strQueryString.replace(/\+/gi, " ");
			strQueryString = strQueryString.replace(/%22/gi, "");	// remove the quotes (if you're really creative, you could search for the
			strQueryString = strQueryString.replace(/\"/gi, "");	// terms within the quotes as phrases, and everything else as single terms)
			MarkSearchValue(strQueryString);
		}
	}
}

function MarkSearchValue(strValue)
{
	if (strValue != '')
	{
		var boolTreatAsPhrase = false;
		var boolErrorWarning = false;
		var strTextColor = '#ffffff';
		var strBackgroundColor = '#acacac';
		var strHighlightBeginTag = "<font style='color:"+ strTextColor+"; background-color:"+strBackgroundColor+";'>";
		var strHighlightEndTag = "</font>";
		return FindSearchValue(strValue,boolTreatAsPhrase,boolErrorWarning,strHighlightBeginTag, strHighlightEndTag);
	}
	else
	{
		return false;
	}
}

function FindSearchValue(strValue,boolTreatAsPhrase,boolErrorWarning,strHighlightBeginTag,strHighlightEndTag)
{
	var objTargetObject = 'CenterColumn';
	if (boolTreatAsPhrase)
	{
		arrSearchArray = [strValue];
	}
	else
	{
		arrSearchArray = strValue.split(" ");
	}
	if (document.getElementById(objTargetObject).innerHTML == 'undefined' || document.getElementById(objTargetObject).innerHTML == null)
	{
		if (boolErrorWarning)
		{
			alert("Ett fel inträffade, ditt sökvärde kunde inte markeras.");
		}
	return false;
	}
	var strContentText = document.getElementById(objTargetObject).innerHTML;
	for (var i = 0; i < arrSearchArray.length; i++)
	{
		strContentText = HighlightSearchValue(strContentText,arrSearchArray[i],strHighlightBeginTag,strHighlightEndTag);
	}
	document.getElementById(objTargetObject).innerHTML = strContentText;
	return true;
}

function HighlightSearchValue(strContentText,strSearchTerm,strHighlightBeginTag,strHighlightEndTag) 
{
	var strNewContent = "";
	var i = -1;
	var strLowerSearchTerm = strSearchTerm.toLowerCase();
	var strLowerContentText = strContentText.toLowerCase();
	
	while (strContentText.length > 0)
	{
		i = strLowerContentText.indexOf(strLowerSearchTerm,i+1);
		if (i < 0)
		{
			strNewContent += strContentText;
			strContentText = "";
		}
		else
		{
			if (strContentText.lastIndexOf(">", i) >= strContentText.lastIndexOf("<", i))
			{
				if (strLowerContentText.lastIndexOf("/script>", i) >= strLowerContentText.lastIndexOf("<script", i))
				{
					strNewContent += strContentText.substring(0, i) + strHighlightBeginTag + strContentText.substr(i, strSearchTerm.length) + strHighlightEndTag;
					strContentText = strContentText.substr(i + strSearchTerm.length);
					strLowerContentText = strContentText.toLowerCase();
					i = -1;
				}
			}
		}
	}
	return strNewContent;
}

// --------------------------------
// Validations
// --------------------------------

function ValidateSearchForm()
{
	if(document.getElementById('SearchString').value == "")
	{
		alert('Sökord är obligatoriskt');
		document.getElementById('SearchString').focus();
	}
	else
	{
		document.getElementById('SearchForm').submit();
	}
}

function ValidateContactForm()
{
	if(document.getElementById('Firstname').value == "")
	{
		alert('Förnamn är obligatoriskt');
		document.getElementById('Firstname').focus();
	}
	else if(document.getElementById('Lastname').value == "")
	{
		alert('Efternamn är obligatoriskt');
		document.getElementById('Lastname').focus();
	}
	else if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.getElementById('Email').value) != true || document.getElementById('Email').value == "")
	{
		alert('E-postadress är obligatoriskt och måste vara en giltig adress');
		document.getElementById('Email').focus();
	}
	else if(document.getElementById('Message').value == "")
	{
		alert('Meddelande är obligatoriskt');
		document.getElementById('Message').focus();
	}
	else if(document.getElementById('Code').value == "")
	{
		alert('Verifikationskod är obligatoriskt');
		document.getElementById('Code').focus();
	}
	else
	{
		document.getElementById('ContactForm').submit();
	}
}
