﻿function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
		return pair[1];
		}
	} 

}

function validateEmailAddress(email)
{
	var emailPattern = /^.+@.+\..{2,3}$/;
	return emailPattern.test(email);
}

function showCommonToolTips() {
	try {
		setTitle('saveButton',			'Texta vantar'); 
		setTitle('saveCloseButton',		'Texta vantar');
		setTitle('editButton',			'Texta vantar');
		setTitle('cancelButton',		'Texta vantar');
					
		setTitle('StatusDropDown',		'Texta vantar');
		setTitle('publishing',			'Texta vantar');
		setTitle('publishingDate',		'Texta vantar');
		setTitle('CommentHistory',		'Texta vantar');
		
		displayFjoldabirt();
		
	} catch (Exception) {}
	
}

function checkRegistration() {

	// Athuga Birtingardagsetningar
	if (!Validate_ProcessInfo())
	{
		document.body.style.cursor='none';
		return false;
	}
		
	// Byrja greiðsludæmi
	if (!Validate_billingInfo())
	{
		document.body.style.cursor='none';
		return false;
	}
	
	
	//enda greiðslusíðu		
	return true;
}


function fieldFocus(objName) {
	try {
		objName.focus();
	} catch (Exception) {}
}


function setFocus(id) {
	try {
	
		if (document.getElementById) {
			elem = document.getElementById(id);
			elem.focus();
		}

	}catch(Exception){}
}
//Validate Kennitala
function Modulus11(arguments)
{
	var l1, l2, l3, l4, l5, l6, l7, l8, lTemp, lSum, lMod, lRest;
	//fá kennitölu
	
	var mySSN = arguments.value.replace("-","");
	//Byrja að reikna
	l1 = mySSN.substr(0, 1) * 3;
	l2 = mySSN.substr(1, 1) * 2;
	l3 = mySSN.substr(2, 1) * 7;
	l4 = mySSN.substr(3, 1) * 6;
	l5 = mySSN.substr(4, 1) * 5;
	l6 = mySSN.substr(5, 1) * 4;
	l7 = mySSN.substr(6, 1) * 3;
	l8 = mySSN.substr(7, 1) * 2;

	//leggja allt saman
	lSum = l1 + l2 + l3 + l4 + l5 + l6 + l7 + l8;
	//deila með 11
	lTemp = lSum / 11;
	//Rounda niður
	lTemp = Math.round(lTemp-0.5);
	//Margfalda roundaða tölu með 11 og draga síðan frá 
	lRest = lSum - (11 * lTemp);
	//Draga síðan 11 frá
	var xMod;
	xMod = 11 - lRest;
	//Sértilfelli ef útkoma er 11
	if(xMod == 11)
		xMod = 0;
	//athuga hvort vartala stemmi við innslátt
	if(xMod == mySSN.substr(8, 1))
		return true;
	else
	{
		return false;
	}
}
function formatIDNumber(numerid)
{
	if (numerid.length == 10)
		return numerid.substring(0,6)+"-"+numerid.substring(6,numerid.length);
	else
	    return numerid;
}



			
function isDate(dateStr) {

	var datePat = /^(\d{2})(\/|-|.)(\d{2})(\/|-|.)(\d{4})$/;
	var matchArray = dateStr.match(datePat);

	if (matchArray == null) {
		return false;
	}

	day = matchArray[1];
	month = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) {
		return false;
	}

	if (day < 1 || day > 31) {
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		return false;
	}

	if (month == 2) {
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
			return false;
		}
	}
	return true;
}

			
			
function checkDate(dateValue) {
	nowDate = new Date();
	arrDates = dateValue.split('.')
	
	inDate = new Date(arrDates[2],(arrDates[1]-1),arrDates[0]);
	nowDate.setHours(0, 0, 0, 0);
	
	if (inDate < nowDate) {
		return false;
	}
	return true;
}

function getToday()
{
	var now = new Date();
	now.setHours(0, 0, 0, 0);
	return now;
}

function getFutureDate(day, futureadd)
{
	arrDates = day.split('.')
	inDate = new Date(arrDates[2],(arrDates[1]-1),arrDates[0]);
	retDate = new Date(inDate.getTime() + futureadd*24*60*60*1000);
	
	nowDate = new Date();
	nowDate.setHours(0, 0, 0, 0);

	if (retDate < nowDate) {
		return false;
	}
	
	return true;
}
			
function IsNumeric(sText)  {

	var ValidChars = "0123456789.";
	var IsNumber = true;
	var Char = "";
	for (i = 0; i < sText.length && IsNumber == true; i++) 	{
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1)
			IsNumber = false;
	}
	return IsNumber;
}
	

function getUnElement(id) {
	try {
	
		if (document.getElementById) {
			return document.getElementById(id);
		}

	}catch(Exception){return null;}
}

function formatNumber_thousund(numerid)
{
    var numer = numerid.toString()
    numer = numer.replace('.', '');
    numer = numer.replace(',', '');
    
    var formatted = "";
    for (i = 1; i <= numer.length; i++) {
       formatted = formatted+numer.substring(i-1,i);
       var truepos = (numer.length-i);
       if (truepos%3 == 0 && truepos>0) formatted = formatted+".";
    }
    return formatted;
}

function setTitle(id, text) {
	try {
	
		if (document.getElementById) {
			document.getElementById(id).title = text;
		}

	}catch(Exception){}
}


//============================================================================
// Removes leading and trailing space from a string
//============================================================================
function Trim(inputString) {
	var retValue = inputString;
	var ch = retValue.substring(0, 1);
	
		// Check for spaces at the beginning of the string
	while (ch == " ")
	{
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}
	
	// Check for spaces at the end of the string
	ch = retValue.substring(retValue.length-1, retValue.length);
	while (ch == " ")
	{ 
		retValue = retValue.substring(0, retValue.length-1);
		ch = retValue.substring(retValue.length-1, retValue.length);
	}
	
	// Return the trimmed string back to the user
	return retValue;
}			

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function Help(id)
{
	window.showModalDialog("./Lbl/Help.aspx?id="+id, 'help', 'dialogHeight: 260px; dialogWidth: 380px; center: Yes; help: No; resizable: No; status: No;');
}

function PrintAdvert(print)
{
	window.top.PrintSelectedAdvert("+print+");
}
//
// Checks the lengt of a multiline textbox and stops input if length is more then max length.
// This function is outdated, because do not work correct
function CheckInputLength(input,max)
{
	if(input.length > max)
	{
		alert("Sviðið má ekki innihalda fleiri en " + max + " stafi.");
		input = input.substring(0,max);
	}
	return input;
}