/*
function:   function setValueIfNotNull(obj, new_content)
parameters: obj, new_content
descrip.:   sætter værdien af f.eks. et tekstfelt til new_content hvis new_content ikke er null.
note:       generic
example:    setValueIfNotNull(document.form1.link,"new value")
*/
function setValueIfNotNull(obj, new_content){
  if( new_content!=null){
    obj.value = new_content;
  }  
}

/*
function:   function PopUpWin(url,window_name,left,scrX,top,scrY,tb,loca,dir,sta,menu,scroll,resize,width,height)
parameters: url,window_name,left,scrX,top,scrY,tb,loca,dir,sta,menu,scroll,resize,width,height
descrip.:   pop up window 100% variable
note:       generic
example:    <a href="javascript:PopUpWin('url', 'popupx', 100, 100, 100, 100, 0, 1, 0, 0, 0, 0, 0, 250, 250)">
*/
function PopUpWin(url,window_name,left,scrX,top,scrY,tb,loca,dir,sta,menu,scroll,resize,width,height) {

  new_window = window.open(url,window_name,"left="+left+",screenX="+scrX+",top="+top+",screenY="+scrY+",toolbar="+tb+",location="+loca+",directories="+dir+",status="+sta+",menubar="+menu+",scrollbars="+scroll+",resizable="+resize+",width="+width+",height="+height);
}


/*
function    set_innerHtml(obj, new_content)
parameter:  obj = id of object to change
parameter:  new_content = content to insert
descrip.:   sets the innnerHtml of an object (id)
note:       generic
*/
function set_innerHtml(obj, new_content){
  document.all(obj).innerHTML = new_content;
}


/*
function:   getSelectedIndex(select_obj)
parameter:  select_obj - select object
descrip.:   returns the selected index of a non multiple select tag
note:       generic
*/
function getSelectedIndex(select_obj){
  var value = select_obj.selectedIndex;
  return(value);
}

// matches a regular expression against a string
// returns true if found, else false
// re = regular expression
// str = string to match against
function matchMe(re, str) {
  var pattern = new RegExp (re,'ig');
  var found = pattern.test(str);
  if (found) { return (true); }
  else { return (false) }
}

/* checks for a valid email */
function isValidEmail(emailAddress) {
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return re.test(emailAddress)
}

/*
function:   checkRequired(f)
parameter:  f - The form to check
descrip.:   Tjekker om brugeren har udfyldt form felter markeret med required. Hvis ikke vises den tekst der skrives ud for required="error".
eks:        <INPUT type="text" name="overskrift" required="Titel">
note:       generic
*/
function checkRequired(f) {
  var strError = "";
  for (var intLoop = 0; intLoop<f.elements.length; intLoop++){
    // Tjekker om dato er udfyldt rigtigt
    if (null!=f.elements[intLoop].getAttribute("date")) {
      if(!isEmpty(f.elements[intLoop].value)){
        if (!matchMe('^[0-9]{2}-[0-9]{2}-[0-9]{4}$', f.elements[intLoop].value)){
          strError += " - " + f.elements[intLoop].getAttribute("date") + "\n";
        }
      }
    }
    // Tjekker Required.
    if (null!=f.elements[intLoop].getAttribute("required")) {
      if (isEmpty(f.elements[intLoop].value)) {
        if(isEmpty(f.elements[intLoop].getAttribute("required"))){
          strError += " - " + f.elements[intLoop].name + "\n";
        }else{
          strError += " - " + f.elements[intLoop].getAttribute("required") + "\n";
        }
      }
    }
  }
  
  if ("" != strError) {
    alert("Udfyld venligst:\n" + strError);
    return false;
  }
  return true;
}
/*
function:   isEmpty(str)
parameter:  str - tekstfelt, der skal tjekkes. 
descrip.:   Tjekker om et tekstfelt indeholder tekst forskellig fra " "
note:       generic
*/
function isEmpty(str) {
// Check whether string is empty.
  for (var intLoop = 0; intLoop < str.length; intLoop++)
    if (" " != str.charAt(intLoop))
      return false;
    return true;
}
/*
function:   confirmDeletion(URL,Warning) 
parameter:  URL - URL der redirectes til, hvis man trykker "JA"
parameter:  Warning - TEkst der vises i en alert boks.
descrip.:   Viser en Alert boks med en advarsel, hvis man klikker "JA" redirectes til en url. Eks Er du sikker på at det skal slettes?
note:       generic
*/
function confirmDeletion(URL,Warning) {
  var agree=confirm(Warning);
  	if (agree) {
		document.location.href=URL; }
}


// returns the value of a radio button or radio button group
function getRadioValue (radioButtonOrGroup) {
  var value = null;
  if (radioButtonOrGroup.length) { // group 
    for (var b = 0; b < radioButtonOrGroup.length; b++)
      if (radioButtonOrGroup[b].checked)
        value = radioButtonOrGroup[b].value;
  }
  else if (radioButtonOrGroup.checked)
    value = radioButtonOrGroup.value;
  return value;
}

function selectColorDialog(sInitColor){
  if (sInitColor == null){ 
	  //display color dialog box
	  var sColor = dlgHelper.ChooseColorDlg();
  }else{
	  var sColor = dlgHelper.ChooseColorDlg(sInitColor);
	}
  //change decimal to hex
	sColor = sColor.toString(16);
	//add extra zeroes if hex number is less than 6 digits
  if (sColor.length < 6) {
  	var sTempString = "000000".substring(0,6-sColor.length);
  	sColor = sTempString.concat(sColor);
  }
	//change color of the selected text
	return sColor;
  //document.execCommand(sColorType, false, sColor);
	//sInitColor = sColor;
	//oDiv.focus();
}




