/**
 * File:          $Workfile: general.js $"
 * Changed:       $Date: 03-11-05 9:44 $"
 * Modified:      $Modtime: 03-11-05 9:44 $"
 * Changed by:    $Author: Thomasm $"
 * 
 *
 *				
 * @author		Thomas Malm (TM)
 * @version		$Revision: 24 $
 *  
 * Date			Author			Changes
 * 2002-11-21	TM				Created
 */
 
 /*
 Check if browser is valid
 */
 function isValidBrowser() {
	var validBrowser = false;
	
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		a_Temp = navigator.appVersion.split("MSIE");
		browserVersion = parseFloat(a_Temp[1]);
		
		if (browserVersion >= 5.5)
			validBrowser = true;
	}
		
	return validBrowser;
}

/*
 Set innerHTML property in a layer
*/
function setInnerHTML(sLayerId, sText) {
	if (document.getElementById(sLayerId)) {
		document.getElementById(sLayerId).innerHTML = sText;
	}
}

/*
 Set innerHTML property in a layer - från event propertychange för att kunna varna om antalet tecken
*/
function setInnerHTMLChar(sLayerId, sText) {
	if (document.getElementById(sLayerId)) {
		document.getElementById(sLayerId).innerHTML = sText;
	}
	
	if (sText<0)
	{
		if (document.forms[0]!=null)
		{
			if (document.forms[0].ErrToManyChar!=null)
			{
				alert(document.forms[0].ErrToManyChar.value);		
			}	
		}
	}
}

/*
Used to control the max length for text areas.
*/
function imposeMaxLength(obj, maxLength) {
	maxLength--;
	if (obj.value.length > maxLength) {
		obj.value = obj.value.substring(0, maxLength);
	}
	else {
		if (document.getElementById("cnt" + obj.id)) {
			document.getElementById("cnt" + obj.id).innerHTML = maxLength - obj.value.length;
		}
	}
}

/*
Open preview window 

IN: Page URL
*/ 
function openWindowPreview(sURL) {
	window.open(sURL, 'winPreview', 'width=800, height=400, scrollbars=yes');
} 

/*
Open print preview window

IN: Page URL
*/ 
function openWindowPrintPreview(sURL) {
	window.open(sURL, 'winPrintPreview', 'width=675, height=500, toolbar=no, menubar=yes, scrollbars=yes');
}

/*
Open print preview window (landscape format)

IN: Page URL
*/ 
function openWindowPrintPreviewLandscape(sURL) {
	window.open(sURL, 'winPrintPreview', 'width=1004, height=500, toolbar=no, menubar=yes, scrollbars=yes');
}

/*
Open print preview window (landscape format)
IN: Page URL
*/ 
function openWindowReport(sURL) {
	window.open(sURL, 'report', 'width=675, height=500, toolbar=no, menubar=yes, scrollbars=yes, resizable=yes');
}

/*
Check if e-mail address is valid

IN: E-mail address
*/
function isEmailAddressValid(emailAddress) {
	//Bindestreck OCH punkt ska tillåtas innan @ (cq 32144)
	if (emailAddress != null && emailAddress.search(/^\w+[a-zA-Z0-9_\-.]+@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
		return true;
	else
		return false;
}

/*
Disable/Enable links

IN: Window object
*/
function toggleLinks(oWin) {
	for (var i = 0; i < oWin.document.links.length; i++) {
		toggleLink(oWin.document.links[i]);
	}
}

function centerLayer(oLayer, iLayerWidth, iLayerHeight) {
	var iWindowWidth = document.body.offsetWidth;
	var iWindowHeight = document.body.offsetHeight;
	
	window.scroll(0, 0);
	
	if (document.all) //IE 
	{
		document.all[oLayer].style.left = 400 - (iLayerWidth / 2);
		document.all[oLayer].style.top = 60;
		document.all[oLayer].style.visibility = 'visible';
	} else if(document.getElementById)  //NS6
	{
		document.getElementById(oLayer).style.left = 400 - (iLayerWidth / 2);
		document.getElementById(oLayer).style.top = 60;
		document.getElementById(oLayer).style.visibility = 'visible';
	}
}

function cancelLink() {
  return false;
}

function disableLink(link) {
	if (link.onclick)
		link.oldOnClick = link.onclick;
	
	link.onclick = cancelLink;
	
	if (link.style)
		link.style.cursor = 'default';
}

function enableLink(link) {
	link.onclick = link.oldOnClick ? link.oldOnClick : null;
	
	if (link.style)
		link.style.cursor = document.all ? 'hand' : 'pointer';
}

function enableLinkById(linkId) {
	for (var i = 0; i < document.links.length; i++) {
		if (document.links[i].id = linkId)
			enableLink(document.links[i]);
	}
}

function toggleLink(link) {
	if (link.disabled) 
		enableLink(link)
  else 
    disableLink(link);
  link.disabled = !link.disabled;
}

/*
Disable elements

IN: elementType - element type to disable
IN: element - element not to disable (null if non)
*/
function disableElements(elementType, element) {
	var form = document.forms[0];
	var elementName = "";
	
	if (element != null)
		elementName = element.Name; 
	
	for (var i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type == elementType && form.elements[i].name != elementName) {
				form.elements[i].setAttribute("disabled", "disabled");
		}		
	}
}

/*
* General submit funtion that views the page loading layer.
*/
function doSubmit() {
	// Disable all links in document while submitting the page, to prevent errors.
	for (var e = 0; e < document.links.length; e++) {
		toggleLink(document.links[e]);
	}
	
	centerLayer('loading', 277, 37);
	document.forms[0].submit();
}

function addField (form, fieldName, fieldValue) {
  if (document.getElementById) {
    var input = document.createElement('INPUT');
      if (document.all) { // what follows should work 
                          // with NN6 but doesn't in M14
        input.type = "hidden";
        input.name = fieldName;
        input.value = fieldValue;
      }
      else if (document.getElementById) { // so here is the
                                          // NN6 workaround
        input.setAttribute('type', "hidden");
        input.setAttribute('name', fieldName);
        input.setAttribute('value', fieldValue);
      }
    form.appendChild(input);
  }
}

function submitForm(formNumber, typeOfChoice) {
	var obj = eval("document.forms[" + formNumber + "]");
	
	addField(obj, "Search", typeOfChoice);
	
	for (var e = 0; e < document.links.length; e++) {
		toggleLink(document.links[e]);
	}
	
	centerLayer('loading', 277, 37);
	obj.submit();	
}

function submitFormByKey(e, formNumber) {
	if (e.keyCode == 13) {
		submitForm(formNumber);
	}
}

function submitNameForm(formName)
{
	// Disable all links in document while submitting the page, to prevent errors.
	for (var e = 0; e < document.links.length; e++) {
		toggleLink(document.links[e]);
	}
	
	centerLayer('loading', 277, 37);
	document.forms[formName].submit();
}

function submitNameFormByKey(e, formName) {
	if (e.keyCode == 13) {
		submitNameForm(formName);
	}
}

/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our string.
   IN: str - the string we want to LTrim
*/
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
==================================================================
RTrim(string) : Returns a copy of a string without trailing spaces.
==================================================================
*/
function RTrim(str)
/*
   PURPOSE: Remove trailing blanks from our string.
   IN: str - the string we want to RTrim

*/
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}

/*
=============================================================
Trim(string) : Returns a copy of a string without leading or trailing spaces
=============================================================
*/
function Trim(str)
/*
   PURPOSE: Remove trailing and leading blanks from our string.
   IN: str - the string we want to Trim

   RETVAL: A Trimmed string!
*/
{
   return RTrim(LTrim(str));
}
/*
=============================================================
replaceAll(str,replacement) : Ersätter alla tecken i en sträng (Kolla bo_decentralized.js för exempel)
=============================================================
*/
function replaceAll( str, replacements ) {
  for ( i = 0; i < replacements.length; i++ ) {
    var idx = str.indexOf( replacements[i][0] );
      while ( idx > -1 ) {
        str = str.replace( replacements[i][0], replacements[i][1] ); 
        idx = str.indexOf( replacements[i][0] );
     	}
  }
  return str;
}