function display(oText)
{
  if (oText.createTextRange){
	   var range = document.selection.createRange();
       var str = range.text;
	   return str;
	}
  else if (oText.getSelection){
	var str = document.getSelection();
      if (window.RegExp) {
        var regstr = unescape("%20%20%20%20%20");
        var regexp = new RegExp(regstr, "g");
        str = str.replace(regexp, "");
      }
	return str;}
  else
  	str = "";
	return str;
}

function storeCaret(oText)
{
  if (oText.createTextRange){
  	oText.caretPos = document.selection.createRange().duplicate();
	}
  else if (oText.getSelection){
  	oText.caretPos = document.getSelection();
    }
  else
  	return;
}
  
function insertField(oText, oSel)
{
  text=oSel.value;
  if (oText.createTextRange && oText.caretPos){
  	var caretPos = oText.caretPos;
	caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) 
== ' ' ? text + ' ' : text;  
	}
   else if (oText.getSelection && oText.caretPos){
  	var caretPos = oText.caretPos;
	caretPos.text = caretPos.text.charat(caretPos.text.length - 1)  
== ' ' ? text + ' ' : text;
	}
  else {
  	oText.value=text;
  	}

    oSel.selectedIndex = 0;
    oText.focus();
}

function insertMailSexText(oText, inputDlgFile)
{
    arrParams =new Array();
    arrParams[0] = "kk";
    arrParams[1] = "kvk";

    arrRet = window.showModalDialog(inputDlgFile,arrParams,"dialogWidth:260px;dialogHeight:220px; resizable: no; scroll: no;");

    if (!arrRet)
      return -1;
        
      if (arrRet[0] == "")
          return -1;

      text="&[kyn:" + arrRet[0] + ","+ arrRet[1] +"]";
      if (oText.createTextRange && oText.caretPos){
        var caretPos = oText.caretPos;
        caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) 
    == ' ' ? text + ' ' : text;  
        }
       else if (oText.getSelection && oText.caretPos){
        var caretPos = oText.caretPos;
        caretPos.text = caretPos.text.charat(caretPos.text.length - 1)  
    == ' ' ? text + ' ' : text;
        }
      else {
        oText.value=text;
        }					
}

function setText(oCombo, aArray, oTextField1, oTextField2)
{
  var oSel;
  oSel = document.userAdd.zipID;
  /*if (oSel.selectedIndex != 0)
  {*/
	oTextField1.value = aArray[oSel.selectedIndex];
	oTextField2.value = aArray[oSel.selectedIndex];
  /*}
  else
  {
	oTextField.value = "";
  }*/
}
function changeState(theBox)
{
  alert(theBox.name);
}

function ShowHideElement(id,bHide)
{
  if ( ! bHide)
  {
    if (document.all) 
      document.all[''+id+''].style.visibility = "visible"
    else if (document.getElementById) 
      document.getElementById(''+id+'').style.visibility = "visible"
  }
  else
  {
	  if (document.all) 
      document.all[''+id+''].style.visibility = "hidden"
	  else if (document.getElementById) 
      document.getElementById(''+id+'').style.visibility = "hidden"
  }
}

function ShowLayerFromSel(oSelect, oID, valueHide, valueShow)
{
    sel = oSelect.selectedIndex;

    if(oSelect[sel].value ==  valueHide)
    {
	    if (document.all) 
        document.all[''+oID+''].style.visibility = "hidden"
	    else if (document.getElementById) 
        document.getElementById(''+oID+'').style.visibility = "hidden"
    }
    else 
    {
      if (document.all) 
        document.all[''+oID+''].style.visibility = "visible"
      else if (document.getElementById) 
        document.getElementById(''+oID+'').style.visibility = "visible"
    }
}

function ShowLayerFromSelEx(oSelect, oID, valueHide, valueShow, valueShow2)
{
    sel = oSelect.selectedIndex;

    if(oSelect[sel].value ==  valueHide)
    {
	if (document.all) 
            document.all[''+oID+''].style.visibility = "hidden"
	else if (document.getElementById) 
            document.getElementById(''+oID+'').style.visibility = "hidden"
    }
    else 
    {
      if (document.all) 
        document.all[''+oID+''].style.visibility = "visible"
      else if (document.getElementById) 
        document.getElementById(''+oID+'').style.visibility = "visible"
    }
}

function ShowLayerFromSelEx(oSelect, oID, valueHide, valueShow, valueShow2, valueHide2)
{
    sel = oSelect.selectedIndex;

    if(oSelect[sel].value == valueHide || oSelect[sel].value == valueHide2)
    {
	if (document.all) 
            document.all[''+oID+''].style.visibility = "hidden"
	else if (document.getElementById) 
            document.getElementById(''+oID+'').style.visibility = "hidden"
    }
    else 
    {
      if (document.all) 
        document.all[''+oID+''].style.visibility = "visible"
      else if (document.getElementById) 
        document.getElementById(''+oID+'').style.visibility = "visible"
    }
}

function FlipVisible(id,checkBox) { //

// Layer visible
if (checkBox.checked) { //
    // only for IE and NS 6
 // if (document.layers) document.layers[''+id+''].visibility = "show"
  if (document.all) document.all[''+id+''].style.visibility = "visible"
  else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
  }
// Layer hidden
else { //
    // only for IE and NS 6
  //if (document.layers) document.layers[''+id+''].visibility = "hide"
  if (document.all) document.all[''+id+''].style.visibility = "hidden"
  else if (document.getElementById)
  	document.getElementById(''+id+'').style.visibility = "hidden";
  }
}

function FileEdit(HideId,ShowId)
{
	HideId.style.display = "none";

	document.all[''+ShowId+''].style.display = "block";
	document.all[''+ShowId+''].focus();
	document.all[''+ShowId+''].select();
}


// ####################### VALIDATION FUNCTIONS ###########################

function isAllEmpty(aBox,customString)
{
	empty = 0;

	for (i=0 ; i < aBox.length ; i++)
	{
		value = aBox[i].value;
		if ( value == "" || trimBlanks( value ) == "")
			empty++;
	}

	return ( empty == aBox.length ) ? isEmpty(aBox[0],customString) : false;

}

function isAnyEmpty(aBox,customString)
{

	for (i=0 ; i < aBox.length ; i++)
	{
		if ( isEmpty(aBox[i],customString) )
			return true;
	}
	return false;
}

function isSelected(box,errorString)
{
	if (box.selectedIndex == 0)
	{
  		alert(errorString);
		return(false);
	}

	return (true);

}

function alertBox(box, errorMsg)
{
	alert(errorMsg);
	box.focus();
	box.select();
}

function IsStringOK(box, compareString,customString)
{
	if (customString)
	{
		if (isEmpty(box,customString))
			return false;
	}
	else
	{
		if (isEmpty(box,"Svæðið er tómt.\nVinsamlega settu inn gild tákn."))
			return false;
	}


	var allValid = true;
	var checkStr = box.value;


	for (i = 0;  i < checkStr.length;  i++)
	{
	  ch = checkStr.charAt(i);

	  for (j = 0;  j < compareString.length;  j++)
	    if (ch == compareString.charAt(j))
	      break;

	  if (j == compareString.length)
	  {
		//alert("validation failed");
		if (customString)
			alertBox(box,customString);
		else
			alertBox(box, "Vinsamlega settu aðeins gild tákn í svæðið.\nEf um tölusvæði er að ræða á bara að setja tölur.");
	    allValid = false;
	    break;
	  }

	}
	return allValid;
 }


function isNumeric(box,customString)
{
	return IsStringOK(box, "0123456789",customString);
}

function isSsnFormatted(box,customString)
{
	return IsStringOK(box, "0123456789-",customString);
}

function isFloat(box,customString)
{
	return IsStringOK(box, "0123456789,.",customString);
}

function isAlphaNumeric(box,customString)
{
return IsStringOK(box,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",customString);
}

function isAlphabetic(box,customString)
{
return IsStringOK(box,":;.,ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789- \t\r\n\f",customString);
}


function isTooLong(box, length,customString)
{
	if (box.value.length > length)
	{
		if (customString)
			alertBox(box,customString);
		else
			alert("Vinsamlega settu ekki fleiri en "+length+" stafi í svæðið.");
		box.focus();
		box.select();
		return false;
	}
	return true;
}

function isTooShort(box, length,customString)
{
	if (box.value.length < length)
	{
		if (customString)
			alertBox(box,customString);
		else
			alertBox(box,"Vinsamlegast settu ekki færri en "+length+" stafi í svæðið.");
		box.focus();
		box.select();
		return true;
	}
	return false;
}

function checkSSN(box,customString)
{
    if ( isTooShort(box, 10,customString) )
        return false;

    if ( !isSsnFormatted(box,"Rangt form kennitölu") )
        return false;

    theValue = box.value;
	var1 = theValue.substring(0, 1);
	var2 = theValue.substring(1, 2);
	var3 = theValue.substring(2, 3);
	var4 = theValue.substring(3, 4);
	var5 = theValue.substring(4, 5);
	var6 = theValue.substring(5, 6);

    if ( theValue.length == 10 )
    {
        var7 = theValue.substring(6, 7);
        var8 = theValue.substring(7, 8);
    	var9 = theValue.substring(8, 9);
    	var10 = theValue.substring(9, 10);
    }
    else
    {
        var7 = theValue.substring(7, 8);
    	var8 = theValue.substring(8, 9);
    	var9 = theValue.substring(9, 10);
        var10 = theValue.substring(10, 11);
    }

	var ending = parseInt(var10);
	if ( ending && (0 < ending) && (ending < 9))
	{
		alertBox (box, "Kennitalan er ekki lögleg, hún verður að enda á 0 eða 9." );
		return false;
	}

 	var allTheSame = (var1 == var2) && (var2 == var3) && (var3 == var4) && (var4 == var5)
        && (var5 == var6) && (var6 == var7) && (var7 == var8) && (var8 == var9 );
    var checksum = (3*var1) + (2*var2) + (7*var3) + (6*var4) + (5*var5) + (4*var6) + (3*var7) + (2*var8) + (1*var9);
    if (checksum % 11 > 0 || allTheSame )
	{
		alertBox (box, "Kennitalan er ekki lögleg");
		return false;
	}
	return true;
}

function trimBlanks(theString, repChar)
{
	//this function replaces the spaces in a string with the provided character
	trimString="";

	for ( i = 0; i < theString.length; i++)
	{
		theChar=theString.substring ( i, i+1);
		if ( theChar == " ")
		{
			trimString += repChar
		}
		else
		{
			trimString+=theString.substring ( i, i+1);
		}
	}
	return (trimString);
}

function isAllCheckBoxEmpty(checkbox, errorString)
{  
	if(checkbox.length > 1)
	{
		for (i=0; i<checkbox.length; i++)
		{
		   if(checkbox[i].checked)
		   {
				return false;
		   }
		}
	}
	else
	{
		if(checkbox.checked)
		{
			return false;
		}
	}
    alert(errorString);
	return true;
}

function isEmpty(box, errorString)
{
	//Hreinsa öll space úr strengi
	trimField = trimBlanks( box.value );

	//Ef strengur er tómur eða trimaður strengur er bara space þá skila true
	if (box.value == "" || trimField == "")
	{
		alertBox(box,errorString);
		return true;
	}

	return false;
}

function emailCheck (box)
{
	var emailStr=box.value;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var firstChars=validChars;
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom="(" + firstChars + validChars + "*" + ")";
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	var matchArray=emailStr.match(emailPat);
	if (matchArray==null)
	{
		//Of mikið af @ eða öðru, ekki lögleg addressa.
		alertBox(box,"Netfangið er ólöglegt, athugaðu @ merkja- og punkta- fjölda.");
		return false;
	}

	var user=matchArray[1];
	var domain=matchArray[2];

	// Tékka hvort user sé löglegur
	if (user.match(userPat)==null)
	{
		// Notandi ekki löglegur
		alertBox(box,"Ólöglegt nafn í netfangi.");
		return false
	}

	// Tékka hvort ip-adressa sé notuð í maili, ef svo er tékka hvort ip-talan sé lögleg
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null)
	{
		// ip-address
		for (var i=1;i<=4;i++)
		{
			if (IPArray[i]>255)
			{
				alertBox(box,"IP-talan er ólögleg.")
				return false
			}
		}
		return true
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null)
	{
		alertBox(box,"Nafinið á léninu í netfanginu virðist ekki vera rétt.")
		return false
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3)
	{
		alertBox(box,"Netfangið verður að enda á þriggja-stafa léni eða tveggja-stafa landskóda.")
		return false
	}

	if (domArr[domArr.length-1].length==3 && len<2)
	{
		var errStr="Netfangið skortir lénsnafn."
		alertBox(box,errStr)
		return false
	}

	return true;
}

/*
<!-- Original:  Sandeep V. Tamhankar (stamhankar@hotmail.com) -->
<!-- Minor changes: Finnur Geir -->
Tek frekar inn input objectið sjálft (til að setja focus) og custom skilaboð

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
*/
function isValidDate(theBox,msg)
{
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
	// Also separates date into month, day, and year variables
	//var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
	// nýtt pattern frá finni
	var datePat = /^(\d{1,2})(.)(\d{1,2})\2(\d{2,4})$/;
	// To require a 4 digit year entry, use this line instead:
	// var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})\2(\d{4})$/;
	var dateStr = theBox.value;

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		if (msg)
			alertBox(theBox,msg);
		else
			alertBox(theBox,"Dagsetning er ekki rétt formuð.");
	return false;
	}
	//month = matchArray[1]; // parse date into variables
	month = matchArray[3];


	if (month.charAt(0) == "0" )
		month = month.substr(1);

	//day = matchArray[3];
	day =  matchArray[1];
	year = matchArray[4];
	//alert(matchArray);
	if (month < 1 || month > 12) { // check month range
		if (msg)
			alertBox(theBox,msg);
		else
			alertBox(theBox,"Mánuðir verða að vera milli 1 og 12.");
	return false;
	}
	if (day < 1 || day > 31) {
		if (msg)
			alertBox(theBox,msg);
		else
			alertBox(theBox,"Dagar verða að vera milli 1 og 31.");
	return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	var sMonth;
	switch(month)
	{
		case "4":
			sMonth = "Apríl";
		break;
		case "6":
			sMonth = "Júní";
		break;
		case "9":
			sMonth = "September";
		break;
		case "11":
			sMonth = "Nóvember";
		break;
	}
	if (msg)
		alertBox(theBox,msg);
	else
		alertBox(theBox,sMonth +" hefur ekki 31 dag.");
	return false
	}
	if (month == 2) { // check for february 29th
	var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	if (day>29 || (day==29 && !isleap)) {
		if (msg)
			alertBox(theBox,msg);
		else
			alertBox(theBox,"Febrúar hefur ekki " + day + " daga árið " + year);
	return false;
	   }
	}
	return true;  // date is valid
}



