function isRadioButtonSelected(form, inputName)
{
    var check = false;
    element = form.elements[inputName]
    for(var i = 0;i < element.length;i++)
    {
        if(element[i].checked == true)
        {
            check = true;
            break;
        }            
    }
    
    return check;
}

function isEmptyField(form, inputName) {
	if (form.elements[inputName] == null) {
		alert('ERROR, cannot find field: ' + inputName);
		return true;
	}
	if (form.elements[inputName].type == 'select-one') {
		if (form.elements[inputName].selectedIndex == 0) {
			return true;
		}		
	} else {
		if (form.elements[inputName].value == '') {
			return true;
		}
	}
	return false;
}


function checkEmail(frm, inputName, emptyFound){
	if (!(frm.elements[inputName].value == null || frm.elements[inputName].value == "")) {
		var str=frm.elements[inputName].value;
		//var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2,})?)$/i
        var filter=/^[a-z0-9A-Z](([-_.~]|(\.\.))?[a-z0-9A-Z])*@[a-z0-9A-Z]([-.]?[a-z0-9A-Z])*\.([A-Za-z]{2,})$/i
		emptyFound = (filter.test(str)) ? emptyFound :  emptyFound + ReturnResponse(frm, inputName)+'\n';
	} else {
		emptyFound = emptyFound + ReturnResponse(frm, inputName)+'\n';
	}
	return emptyFound;
}


function InvalidEmail(frm, inputName) {
	var str=frm.elements[inputName].value;
	var result;
	if (str == null || str == "") { // if field is empty
		result = true;
	} else { // if we have somthing
        var filter=/^[a-z0-9A-Z](([-_.~]|(\.\.))?[a-z0-9A-Z])*@[a-z0-9A-Z]([-.]?[a-z0-9A-Z])*\.([A-Za-z]{2,})$/i
		//var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2,})?)$/i
		result = (filter.test(str)) ? false : true;
	}
	return result;
}


function checkCheckBox(frm, inputName, emptyFound) {
  if (frm.elements[inputName] == null)
   alert('Error with field: '+inputName);

  if (!frm.elements[inputName].checked)
  {
    emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
    return emptyFound;
  }
  return emptyFound;
}

// check if form field is empty and return true(==1)
// also add field name to emptyField
function checkField(frm, inputName, emptyFound) {
 if (frm.elements[inputName] == null)
   alert('Error with field: '+inputName);
 if (frm.elements[inputName].type == 'select-one') {
  if (frm.elements[inputName].selectedIndex == 0) {
    emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
    return emptyFound;
   }
 } else {
  if (trimSpace(frm.elements[inputName].value) == '') {
    emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
    return emptyFound;
  }
 } // else type if statement
    return emptyFound;
 }  // end checkField


 function checkFieldSym(frm, inputName, emptyFound) {
  if (frm.elements[inputName].value.length < 3) {
    emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
    return emptyFound;
  }
    return emptyFound;
 }  // end checkField

// this is to check for validate addresses
 function checkAddress(frm, inputName, emptyFound) {
  if(frm.elements[inputName + 'postal1'] != undefined) {
    frm.elements[inputName + 'postal'].value
      = frm.elements[inputName + 'postal1'].value + frm.elements[inputName + 'postal2'].value
  }
  emptyFound = checkField(frm, inputName + 'address', emptyFound);
  emptyFound = checkField(frm, inputName + 'city', emptyFound);
  emptyFound = checkField(frm, inputName + 'province', emptyFound);
  emptyFound = checkPostal(frm, inputName + 'postal', emptyFound);
  return emptyFound;
 }

// this check is specifically for phone number fields
// it checks the area code, prefix and suffix
 function checkPhone(frm, inputName, emptyFound) {
  var telArea = frm.elements[inputName + 'Area'].value;
  var telNum1 = frm.elements[inputName + '1'].value;
  var telNum2 = frm.elements[inputName + '2'].value;
  if (trimSpace(telArea) == '') {
    emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
    return emptyFound;
  }
  if (trimSpace(telNum1) == '') {
    emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
    return emptyFound;
  }
  if (trimSpace(telNum2) == '') {
    emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
    return emptyFound;
  }
  // validate if number has only digits.
	var emptyPhoneErr = '';
	emptyPhoneErr = checkNumeric(frm, inputName + 'Area', emptyPhoneErr);
	if (emptyPhoneErr == '')
		emptyPhoneErr = checkNumeric(frm, inputName + '1', emptyPhoneErr);
	if (emptyPhoneErr == '')
		emptyPhoneErr = checkNumeric(frm, inputName + '2', emptyPhoneErr);
	if (emptyPhoneErr == '')
		emptyPhoneErr = checkFieldLength(frm, inputName+'Area', emptyPhoneErr, 3);
	if (emptyPhoneErr == '')
		emptyPhoneErr = checkFieldLength(frm, inputName+'1', emptyPhoneErr, 3);
	if (emptyPhoneErr == '')
		emptyPhoneErr = checkFieldLength(frm, inputName+'2', emptyPhoneErr, 4);
	emptyFound = emptyFound + emptyPhoneErr;
  return emptyFound;
 } // end checkPhone

  //This is a more rigorous check and validates that the phone number fields
  //contain digits only and are of proper length.
  function checkPhone2(frm, inputName, emptyFound) {
    var empty = '';
    empty = checkFieldLength(frm, inputName+'Npa',empty,3);
    empty = checkFieldLength(frm, inputName+'Nnx',empty,3);
    empty = checkFieldLength(frm, inputName+'Line',empty,4);
    if (empty != '') 
      emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
    
    //emptyFound = checkExtension(frm, inputName, emptyFound);
    
    return emptyFound;
  }

 function check16Digit(frm, inputName, emptyFound) {
   var empty = '';
   empty = checkFieldLength(frm, inputName,empty,16);
   if (empty != '') emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
   return emptyFound;
 }

function checkAlpha(frm, inputName, emptyFound) {
  var empty = '';
  var objValue = frm.elements[inputName].value;
  var regex = "[^A-Za-z\-_\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C6\u00C7\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D2\u00D3\u00D4\u00D5\u00D6\u00D9\u00DA\u00DB\u00DC\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u00E6\u00E7\u00E8\u00E9\u00EA\u00EB\u00EC\u00ED\u00EE\u00EF\u00F2\u00F3\u00F4\u00F5\u00F6\u00F9\u00FA\u00FB\u00FC\' ]";
  var re = new RegExp(regex);
  if (!(objValue == null || objValue.length == 0)) {
    if (objValue.match(re)) { 
      emptyFound = emptyFound + ReturnResponse(frm, inputName)+'\n'; 
    } 			
  } else {
    emptyFound = emptyFound + ReturnResponse(frm, inputName)+'\n';
  }

  return emptyFound;
}

function checkAlphaNumeric(frm, inputName) {
  var objValue = frm.elements[inputName].value;
  var regex = "[^0-9A-Za-z]";// "[^A-Za-z0-9\-_\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C6\u00C7\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF\u00D2\u00D3\u00D4\u00D5\u00D6\u00D9\u00DA\u00DB\u00DC\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u00E6\u00E7\u00E8\u00E9\u00EA\u00EB\u00EC\u00ED\u00EE\u00EF\u00F2\u00F3\u00F4\u00F5\u00F6\u00F9\u00FA\u00FB\u00FC\' ]";
  var re = new RegExp(regex);
  if (!(objValue == null || objValue.length == 0) && !objValue.match(re)) {
    return true;
  }
  else 
    return false;
}


function checkNumeric(frm, inputName, emptyFound) {
  var empty = '';
  var objValue = frm.elements[inputName].value;

  if (!(objValue == null || objValue == '')) {
    var charpos = objValue.search("[^0-9]"); 
    if (objValue.length > 0 && charpos >= 0 ) { 
      emptyFound = emptyFound + ReturnResponse(frm, inputName)+'\n'; 
    } 			
  } else {
    emptyFound = emptyFound + ReturnResponse(frm, inputName)+'\n';
  }

  return emptyFound;
}

function checkBankAccountNumber(frm, inputName, emptyFound) {
  var empty = '';
  var objValue = frm.elements[inputName].value;

  if (!(objValue == null || objValue == '')) {
    var charpos = objValue.search("[^0-9]"); 
    if (objValue.length < 7 || charpos >= 0 ) { 
      emptyFound = emptyFound + ReturnResponse(frm, inputName)+'\n'; 
    } 			
  } else {
    emptyFound = emptyFound + ReturnResponse(frm, inputName)+'\n';
  }

  return emptyFound;
}


//This function checks the Customer Phone Number  
function checkPhone3(frm, inputName, emptyFound) {

  if (!(frm.elements[inputName].value == null))
  {
    var patternPhone = /\d{3}[ ]\d{3}[-]\d{4}/;
    var phoneNumber = frm.elements[inputName].value;

    var inputStr = phoneNumber.match(patternPhone);
    
    if ((inputStr == null)) {
      emptyFound = emptyFound + ReturnResponse(frm, inputName)+'\n';
    }
  }
  else
  {
    emptyFound = emptyFound + ReturnResponse(frm, inputName)+'\n';
  }

  return  emptyFound;
}

// check if form field is empty and return true(==1)
// also add field name to emptyField
function checkFieldLength(frm, inputName, emptyFound, fieldSize) {
  var patternSize
  
  if (fieldSize == 1)
    patternSize = /\d{1}/;
  else if (fieldSize == 2)
    patternSize = /\d{2}/;
  else if (fieldSize == 3)
    patternSize = /\d{3}/;
  else if (fieldSize == 4)
    patternSize = /\d{4}/;
  else if (fieldSize == 5)
    patternSize = /\d{5}/;
  else if (fieldSize == 8)
    patternSize = /\d{8}/;
  else if (fieldSize == 15)
    patternSize = /\d{15}/;
  else if (fieldSize == 16)
    patternSize = /\d{16}/;
  else
    patternSize = /\d{20}/;
  
  if (frm.elements[inputName] == null)
    alert('Error with field: '+inputName);

  if (frm.elements[inputName].value == null) {
    emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
    return emptyFound;
  }

  var inputStr = trimSpace(frm.elements[inputName].value);
  inputStr = inputStr.match(patternSize);
  if (frm.elements[inputName].type == 'select-one') {
    if (frm.elements[inputName].selectedIndex == 0) {
      emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
      return emptyFound;
    }
  } else {
    if (inputStr == null) {
      emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
      return emptyFound;
    }
  } // else type if statement
  return emptyFound;
}  // end checkField


/*jumps to the next control if the maximum length is reached*/
function doAutoTab(e, maxLength, objNext) {
  var charCode
  var source
  
  if (document.all) {
    // Get the character code of the key pressed
    charCode = window.event.keyCode
    // Get a handle on the element (control) that raised the event
    source = window.event.srcElement
  } else {
    // Get the character code of the key pressed
    charCode = e.which
    // Get a handle on the element (control) that raised the event
    source = e.target
  }
  
  // If the key was a TAB or BACKTAB, then select all
  //if (charCode == 9 || charCode == 16) {
  //  source.select()
  //}
  // Check If the key is a valid alphanumeric key
  if ( (charCode >= 48 && charCode <= 57) ||    // 0..9
       (charCode >= 96 && charCode <= 122) ||   //a..z
       (charCode >= 65 && charCode <= 90)       //A..Z
     ) {
        // If maxLength is exceeded, set focus to next control
        if (source.value.length >= maxLength) {
            objNext.focus()
            // If next control is a textbox, select all text inside
            if (objNext.type == "text") {
                    objNext.select()
            }
        }
  }
}


//This function checks the postal code  
//for empty and format (AlphaNumAlpha NumAlphaNum)
function checkPostal(frm, inputName1, inputName2, emptyFound) {

  if (!((frm.elements[inputName1] == null) || (frm.elements[inputName2] == null)))
  {
    var patternPostal = /[A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9]/;
    var postalCode = frm.elements[inputName1].value + " " + frm.elements[inputName2].value;
  
    postalCode = postalCode.toUpperCase().replace(" ","");
    //postalCode = postalCode.substring(0,3) + " " + postalCode.substring(3,6);
    //frm.elements[inputName].value = postalCode;
    
    var inputStr = postalCode.match(patternPostal);
    
    if ( (inputStr == null) || (postalCode.length!=6)) {
      emptyFound = emptyFound + ReturnResponse(frm, inputName1)+'\n';
    }
  }
  else
  {
    emptyFound = emptyFound + ReturnResponse(frm, inputName1)+'\n';
  }

  return  emptyFound;
}

// This function check the postal code for one field for empty and fromat
function checkPostalCode(frm, inputName1, emptyFound) {

  if (!(frm.elements[inputName1] == null))
  {
    var patternPostal = /[A-Za-z][0-9][A-Za-z][0-9][A-Za-z][0-9]/;
    var postalCode = frm.elements[inputName1].value;
  
    postalCode = postalCode.toUpperCase().replace(" ","");
    //postalCode = postalCode.substring(0,3) + " " + postalCode.substring(3,6);
    //frm.elements[inputName].value = postalCode;
    
    var inputStr = postalCode.match(patternPostal);
    
    if ( (inputStr == null) || (postalCode.length!=6)) {
      emptyFound = emptyFound + ReturnResponse(frm, inputName1)+'\n';
    }
  }
  else
  {
    emptyFound = emptyFound + ReturnResponse(frm, inputName1)+'\n';
  }

  return  emptyFound;
}


//This function checks the phone extension for empty and length
function checkExtension(frm, inputName, emptyFound) {

  var extError = '';
  if ( (frm.elements[inputName+'Ext']) && (frm.elements[inputName+'Ext'].value.length >0) ){
    extError = checkFieldLength(frm, inputName+'Ext', extError, frm.elements[inputName+'Ext'].value.length);
  }
  if (extError!='') {
    emptyFound = emptyFound + ReturnResponse(frm, inputName)+'\n';
  }
  
  return  emptyFound;
}

//This function is used for ?????
function setField(frm, field1, field2) {
  frm.elements[field1].value = frm.elements[field2].value;
}

// checkPhoneAllorNone
// if anything is placed in the phone number field then it validates that it's all
// digits, also if an extension field exists it validates that too.

 function checkPhoneAllorNone(frm, inputName, emptyFound) {
  var phoneError='';
  var returnMessage = '';
  var patternSize = /\D/;
  if ( (frm.elements[inputName+'Area'].value.length!=0) || (frm.elements[inputName+'1'].value.length!=0) || (frm.elements[inputName+'2'].value.length!=0) ) {
    phoneError = checkFieldLength(frm, inputName+'Area', phoneError, 3);
    phoneError = checkFieldLength(frm, inputName+'1', phoneError, 3);
    phoneError = checkFieldLength(frm, inputName+'2', phoneError, 4);
  }
  if (phoneError != '') {
   emptyFound = emptyFound + ReturnResponse(frm, inputName)+'\n';
  }
  return emptyFound;
 }

// trim method.  
// This method will strip the whitespaces before and after the string.
 function trimSpace(strToTrim) {
	// Remove leading spaces
	var trimmedString = strToTrim.replace(/^\s+/g, "")
	// Remove trailing spaces
	trimmedString = trimmedString.replace(/\s+$/g, "")
	return trimmedString 
 }

    // This function checks the select field without a please select one option
     function checkField1(frm, inputName, emptyFound) {
	if (frm.elements[inputName] == null)
	    alert('Error with field: '+inputName);
        if (frm.elements[inputName].value == '') {
	    emptyFound = emptyFound + ReturnResponse(frm, inputName) + '\n';
	    return emptyFound;
        }
        return emptyFound;
     }  // end checkField

function InvalidSympaticoEmail(form, inputName) {
	if (form.elements[inputName].value == null) {
		alert("ERROR " + inputName + " is null!");
		return true;
	}
    var emailStr = form.elements[inputName].value.split("@");
    var patternEmail=/([a-zA-Z0-9._-]+@sympatico+\.ca+)/gi
    var emailStr2 = form.elements[inputName].value.match(patternEmail);
	if(!emailStr2) {
		return true;
	}
	return false;
}