/**
 * Copyright 2006 Olivier Clavel
 *
 * This file is part of Site LCJ-Conseil software.
 *
 * Site LCJ-Conseil is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License,
 * or any later version.
 *
 * Site LCJ-Conseil is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MediaNetLive portal software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @author Olivier Clavel <contact AT retiz DOT com>
 */
/**
 * Some general usage functions used elsewhere
 */

/**
 * checks for valid numeric strings
 */
function isNumeric(strString) {
  var strValidChars = "0123456789.-";
  var strChar;
  var blnResult = true;

  if (strString.length == 0) return false;

  //  test strString consists of valid characters listed above
  for (i = 0; i < strString.length && blnResult == true; i++)
    {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
	{
	  blnResult = false;
	}
    }
  return blnResult;
}

/**
 * Valid email check function
 * Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
function emailCheck(str) {
  var at="@";
  var dot=".";
  var lat=str.indexOf(at);
  var lstr=str.length;
  var ldot=str.indexOf(dot);
  if (str.indexOf(at)==-1){
    return false;
  }

  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
    return false;
  }
  
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
    return false;
  }

  if (str.indexOf(at,(lat+1))!=-1){
    return false;
  }

  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
    return false;
  }

  if (str.indexOf(dot,(lat+2))==-1){
    return false;
  }

  if (str.indexOf(" ")!=-1){
    return false;
  }

  return true;
}

/**
 * A fucntion which removes textNodes which are 
 * direct children of a given note
 */
function cleanupTextNodes (theNode) {
  var maxIndex = theNode.childNodes.length - 1;
  for (i=maxIndex; i>=0; i--) {
    /* 3 is the value of Node.TEXTNODE constant but IE eats that */
    if (theNode.childNodes[i].nodeType == 3) {
      theNode.removeChild(theNode.childNodes[i]);
    }
  }
}

/**
 * Adds options to a select element. These option are numeric,
 * with an increment of 1, starting at 'start' up/down to 'end'
 * @param HTMLSelect selectField The select field to add options to
 * @param int start The starting point for options
 * @param int end The end point for options
 */
function makeNumericOptions (selectField, start, end) {
  selectField.appendChild(document.createElement('option'));
  if (start < end) {
    for (i=start; i<=end; i++) {
      theOption = document.createElement('option');
      theOption.setAttribute('value', i);
      theOption.appendChild(document.createTextNode(i));
      selectField.appendChild(theOption);
    }
  } else {
    for (i=start; i>=end; i--) {
      theOption = document.createElement('option');
      theOption.setAttribute('value', i);
      theOption.appendChild(document.createTextNode(i));
      selectField.appendChild(theOption);
    }
  }
}

/**
 * Check for presence of a selected value in a
 * (multiple) select field and returns a confirmation
 * message if not (preceeded with "Etes-vous certain de "
 *
 * @param HtmlSelect selectNode The select node to check
 * @param string tailMessage The message to append to confirm box
 */
function optionsSelectedOrConfirm (selectNode, tailMessage) {

  var smthSelected = false;

  for (i=0; i<selectNode.options.length; i++) {
    if (selectNode.options[i].selected) {
      smthSelected = true;
      break;
    }
  }
  if (!smthSelected) {
    return confirm('Etes-vous certain de ' + tailMessage);
  } else {
    return true;
  }
}

/**
 * Returns a P wrapper element for form fields with a label
 */
function makeWrapper (text) {
  var label = document.createElement('label');
  label.appendChild(document.createTextNode(text));
  var wrapper = document.createElement('p');
  wrapper.appendChild(label);
  return wrapper;
}

/**
 * Adds an OPTION element with value and text
 * to SELECT element
 */
function addOption (opval, optext, opselect) {
  var myoption = document.createElement('option');
  myoption.value = opval;
  myoption.appendChild(document.createTextNode(optext));
  opselect.appendChild(myoption);
}


/**
 * Safely creates a named element that will still work in IE
 *
 * @param string tagName The tagname of the element
 * @param string name The name of the element
 */
function createNamedElement(tagName, name) {
   var element = null;
   // Try the IE way; this fails on standards-compliant browsers
   try {
      element = document.createElement('<'+tagName+' name="'+name+'">');
   } catch (e) {
   }
   if (!element || element.nodeName != tagName.toUpperCase()) {
      // Non-IE browser; use canonical method to create named element
      element = document.createElement(tagName);
      element.name = name;
   }
   return element;
}
