/*
//Example Usage:
From now on, please use this JS library to validate inputs, and also you may add more regex pattern if you so wish.

To use it:
>> var objVal = new Validation();
>> objVal.validateInput(obj.value, objVal.regex_sku )
*/

function Validation() 
{	
	this.regex_numeric_int = /^[0-9]+$/gim;
	this.regex_email = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/gim;
	this.regex_text_all = /./gim;
	this.regex_numeric_float = /^[-+]?([0-9]*\.[0-9]+|[0-9]+)$/gim;
	this.regex_alpha = /^[a-zA-Z]+$/gim;
	
	this.regex_sku = /^[a-zA-Z0-9_\-\s"]+$/gim;

	this.validateInput = validateInput;
}

function validateInput(inputStr, regexStr)
{		
	var re = new RegExp(regexStr);
  	var m = re.exec(inputStr);
	
  	if (m == null){retval = false;} 
	else{retval = true;}
	return retval;
}


function IsAlphaNumeric(strString)
{
   var strValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   for (i = 0; i < strString.length && blnResult == true; i++)
   {
	strChar = strString.charAt(i);
      	if (strValidChars.indexOf(strChar) == -1)
        {
        	blnResult = false;
        }
   }  	
	return blnResult;
}

function IsPhone(strString)
{
   var strValidChars = "0123456789.-+()";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   for (i = 0; i < strString.length && blnResult == true; i++)
   {
	strChar = strString.charAt(i);
      	if (strValidChars.indexOf(strChar) == -1)
        {
        	blnResult = false;
        }
   }  	
	return blnResult;
}


function GetNewString(strString)
{
   var strValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_";
   var strChar;
   var blnResult = true;
   var output= "";

   if (strString.length == 0) output="";

   for (i = 0; i < strString.length && blnResult == true; i++)
   {
	strChar = strString.charAt(i);
      	if (strValidChars.indexOf(strChar) != -1)
        {
            output += strChar;
        }
   }  	
   return output;
}



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;
}

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;
      }
   }
}



