var defaultEmptyOK = false
var reAlphabetic = /^[a-zA-Z]+$/
var reAlphanumeric = /^[a-zA-Z0-9]+$/

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function isAlphabetic (s) {   
	var i;
    if (isEmpty(s)) {
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    } else {
       return reAlphabetic.test(s)
    }
}

function isAlphanumeric (s)

{   var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    else {
       return reAlphanumeric.test(s)
    }
}

function n3_swapClass(theID,cssStyle,cssStyleOther) {
	if(!document.getElementById){return};
	var theElement = document.getElementById(theID);
	if (theElement.className == cssStyle) {
		theElement.className = cssStyleOther;
	} else {
		theElement.className = cssStyle;
	}
}

function NewWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',resizable,scrollbars'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function


function getSelectedButton(buttonGroup){
	for (var i = 0; i < buttonGroup.length; i++) {
		if (buttonGroup[i].checked) {
			return i
		}
	}
	return 0
}

function getSelectedButton1(buttonGroup){
	alert(buttonGroup.length);
	for (var i = 0; i < buttonGroup.length; i++) {
		if (buttonGroup[i].checked) {
			return i
		}
	}
	return 99
}


function CurrencyFormatted(amount, langue, pays)
{

	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	if (pays == "US") {
		devise = "USD"
	} else {
		devise = "CAD"
	}
	if (langue == 'EN') {
		s = minus + s + ' ' +devise
	}
	if (langue == 'FR') {
		s = minus + s + ' ' +devise
	}
	//s = minus + ' $ ' + s;
	return s;
}  // function CurrencyFormatted()


function emptyField(textObj)
{
	if (textObj.value.length == 0) return true;
	for (var i=0; i<textObj.value.length; ++i) {
		var ch = textObj.value.charAt(i);
		if (ch != ' ' && ch != '\t') return false;
	}
	return true;
}


function validateDate( strValue ) {
/************************************************
DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy


PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS:
   Avoids some of the limitations of the Date.parse()

   method such as the date separator character.
*************************************************/
  var objRegExp = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/

  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return 1; //doesn't match pattern, bad date
  else{
    var strSeparator = strValue.substring(4,5) //find date separator
    var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[2]);

    //check if month value and day value agree
    if(arrayLookup[arrayDate[1]] != null) {
      if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0)

        return 99; //found in lookup table, good date
    }

    //check for February
    var intYear = parseInt(arrayDate[0]);
    var intMonth = parseInt(arrayDate[1]);
    if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
      return 99; //Feb. had valid number of days
  }
  return 2; //any other values, bad date
}

function isEmailAddr(email)
{
  var result = false
  var theStr = new String(email)
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}

function isPosInteger (inputVal) {
	inputStr = inputVal.toString()
	for (var i=0; i<inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9") {
			return false;
		}
	}
	return true;
}

function GeneratePassword(length) {

    if (parseInt(navigator.appVersion) <= 3) {
        alert("Sorry this only works in 4.0+ browsers");
        return true;
    }

    var sPassword = "";

    var noPunction = true;
    for (i=0; i < length; i++) {

        numI = getRandomNum();
        if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }

        sPassword = sPassword + String.fromCharCode(numI);
    }


    return sPassword;
}

function getRandomNum() {

    // between 0 - 1
    var rndNum = Math.random()

    // rndNum from 0 - 1000
    rndNum = parseInt(rndNum * 1000);

    // rndNum from 33 - 127
    rndNum = (rndNum % 94) + 33;

    return rndNum;
}

function checkPunc(num) {

    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }

    return false;
}

function validateMultipleSelect(listObject) {

	// "listObject" is the object input array for the select 
	// list, referenced in the function call as "this.name", where
	// "name" is the name of the select list.
	// "numToSelect" is the number of items you allow to be selected.

	var listName = listObject.name;
	var optionsSelected = new Array;
	var allOptions = "";
	var j = 0;

	for (var i = 0; i < listObject.length; i++) {
		if (listObject.options[i].selected) {
			optionsSelected[j] = listObject.options[i].value;
			j++;
		}
	}
	if (optionsSelected.length <= 0) {
		alert('stef');
	} else {
		alert('stephane');
	}
}

//-->

