function validateCheckboxes(p_oCheckboxes, p_sFormId, p_sAction){
  var nCount = 0;
  for(var i=0; i < p_oCheckboxes.length; i++){
	if(p_oCheckboxes[i].checked)
	nCount++;
  }
  
  if(nCount==0){
    alert("Please select a line.");
  }else{
    document.getElementById(p_sFormId).action=p_sAction;
    document.getElementById(p_sFormId).submit();
  }
}

function validateCsvDownload(p_oCheckboxes, p_sFormId, p_sAction, p_sDialogId, p_sResonseDivId){
  var nCount = 0;
  for(var i=0; i < p_oCheckboxes.length; i++){
	if(p_oCheckboxes[i].checked)
	nCount++;
  }
  
  if(nCount==0){
    alert("Please select a line.");
  }else{
    dijit.byId(p_sDialogId).show();
    dojoRequestPost(p_sFormId, p_sAction, p_sResonseDivId);
  }
}

/*
 * Use with dojo tabs only.  If multiple forms on on a page,
 * the dom id's must be different.
 */
function validateTabSearch(p_sContainerId){
  var bValid = false;
  var bSelected = false;
  
  if(document.getElementById('atgsearch'+p_sContainerId) != null){
    if(document.getElementById('atgsearch'+p_sContainerId).checked){
      bValid = true;
	  bSelected = true;
	  if(document.getElementById('txtAtgSearch'+p_sContainerId).value == null){
	    bValid = false;
	  }
	
	  if(document.getElementById('txtAtgSearch'+p_sContainerId).value == ""){
	    bValid = false;
	  }
	
	  if(!bValid){
	    alert('Please enter a gene name.');	
	  }
    }
  }
  
  if(document.getElementById('linenamesearch'+p_sContainerId)!=null){
    if(document.getElementById('linenamesearch'+p_sContainerId).checked){
	  bValid = true;
	  bSelected = true;
	  if(document.getElementById('txtLineNameSearch'+p_sContainerId).value == null){
	    bValid = false;
	  }
	
	  if(document.getElementById('txtLineNameSearch'+p_sContainerId).value == ""){
	    bValid = false;
	  }
	
	  if(!bValid){
	    alert('Please enter a line name.');	
	  }
	}
  }
  
  if(document.getElementById('traysearch'+p_sContainerId) != null){
    if(document.getElementById('traysearch'+p_sContainerId).checked){
	  bValid = true;
	  bSelected = true;
	  if(document.getElementById('txtTrayIdOnly'+p_sContainerId).value == null){
	    bValid = false;
	  }
	
	  if(document.getElementById('txtTrayIdOnly'+p_sContainerId).value == ""){
	    bValid = false;
	  }
	
	  if(!bValid){
	    alert('Please enter a tray number.');	
	  }
	}
  }
  
  if(document.getElementById('catalogsearch'+p_sContainerId) != null){	 
    if(document.getElementById('catalogsearch'+p_sContainerId).checked){	 
      //Add validation to catalog later...
      bValid = true;
	  bSelected = true;
	}
  }
  
  if(document.getElementById('packetsearch'+p_sContainerId) != null){	  
    if(document.getElementById('packetsearch'+p_sContainerId).checked){	  
      bValid = true;
	  bSelected = true;
	  if(document.getElementById('packetNumber'+p_sContainerId).value == null){
	    bValid = false;
	  }
	
	  if(document.getElementById('packetNumber'+p_sContainerId).value == ""){
	    bValid = false;
	  }
	
	  if(!bValid){
	    alert('Please enter a packet number.');	
	  }
	}
  }
  
  if(document.getElementById('runsearch'+p_sContainerId) != null){
    if(document.getElementById('runsearch'+p_sContainerId).checked){
	  bValid = true;
	  bSelected = true;
	  if(document.getElementById('txtRunIdOnly'+p_sContainerId).value == null){
	    bValid = false;
	  }
	
	  if(document.getElementById('txtRunIdOnly'+p_sContainerId).value == ""){
	    bValid = false;
	  }
	
	  if(!bValid){
	    alert('Please enter a run number.');	
	  }
	}
  }
  
  if(document.getElementById('ordersearch'+p_sContainerId) != null){
    if(document.getElementById('ordersearch'+p_sContainerId).checked){
	  bValid = true;
	  bSelected = true;
	  if(document.getElementById('txtOrderIdOnly'+p_sContainerId).value == null){
	    bValid = false;
	  }
	
	  if(document.getElementById('txtOrderIdOnly'+p_sContainerId).value == ""){
	    bValid = false;
	  }
	
	  if(!bValid){
	    alert('Please enter a order number.');	
	  }  
    }
  }
  
  if(bValid){
    document.getElementById('frmBasicSearch'+p_sContainerId).submit();
  }else{
	if(!bSelected){
	  alert('Please select a radio button and enter a search term.');	
	}
  }
}

/*
 * Validation for a single form. (No tabs)
 */
function validateSearch(){
  var bValid = false;
  var bSelected = false;
  
  if(document.getElementById('atgsearch') != null){
    if(document.getElementById('atgsearch').checked){
      bValid = true;
	  bSelected = true;
	  if(document.getElementById('txtAtgSearch').value == null){
	    bValid = false;
	  }
	
	  if(document.getElementById('txtAtgSearch').value == ""){
	    bValid = false;
	  }
	
	  if(!bValid){
	    alert('Please enter a gene name.');	
	  }
    }
  }
  
  if(document.getElementById('linenamesearch')!=null){
    if(document.getElementById('linenamesearch').checked){
	  bValid = true;
	  bSelected = true;
	  if(document.getElementById('txtLineNameSearch').value == null){
	    bValid = false;
	  }
	
	  if(document.getElementById('txtLineNameSearch').value == ""){
	    bValid = false;
	  }
	
	  if(!bValid){
	    alert('Please enter a line name.');	
	  }
	}
  }
  
  if(document.getElementById('traysearch') != null){
    if(document.getElementById('traysearch').checked){
	  bValid = true;
	  bSelected = true;
	  if(document.getElementById('txtTrayIdOnly').value == null){
	    bValid = false;
	  }
	
	  if(document.getElementById('txtTrayIdOnly').value == ""){
	    bValid = false;
	  }
	
	  if(!bValid){
	    alert('Please enter a tray number.');	
	  }
	}
  }
  
  if(document.getElementById('catalogsearch') != null){	 
    if(document.getElementById('catalogsearch').checked){	 
      //Add validation to catalog later...
      bValid = true;
	  bSelected = true;
	}
  }
  
  if(document.getElementById('packetsearch') != null){	  
    if(document.getElementById('packetsearch').checked){	  
      bValid = true;
	  bSelected = true;
	  if(document.getElementById('packetNumber').value == null){
	    bValid = false;
	  }
	
	  if(document.getElementById('packetNumber').value == ""){
	    bValid = false;
	  }
	
	  if(!bValid){
	    alert('Please enter a packet number.');	
	  }
	}
  }
  
  if(document.getElementById('runsearch') != null){
    if(document.getElementById('runsearch').checked){
	  bValid = true;
	  bSelected = true;
	  if(document.getElementById('txtRunIdOnly').value == null){
	    bValid = false;
	  }
	
	  if(document.getElementById('txtRunIdOnly').value == ""){
	    bValid = false;
	  }
	
	  if(!bValid){
	    alert('Please enter a run number.');	
	  }
	}
  }
  
  if(document.getElementById('ordersearch') != null){
    if(document.getElementById('ordersearch').checked){
	  bValid = true;
	  bSelected = true;
	  if(document.getElementById('txtOrderIdOnly').value == null){
	    bValid = false;
	  }
	
	  if(document.getElementById('txtOrderIdOnly').value == ""){
	    bValid = false;
	  }
	
	  if(!bValid){
	    alert('Please enter a order number.');	
	  }  
    }
  }
  
  if(bValid){
    document.getElementById('frmBasicSearch').submit();
  }else{
	if(!bSelected){
	  alert('Please select a radio button and enter a search term.');	
	}
  }
}

function validateCreateTypedet(p_sFormId, p_sAction, p_sResultDiv){
  var bValid = true;
  
  var sValue = document.getElementById('txtName');
  if(sValue == null){
    bValid = false;
    alert('Please enter name.');
  }else if(sValue.value == ''){
    bValid = false;
    alert('Please enter name.');
  }
  
  var sDescript = document.getElementById('txtDescription');
  if(sDescript == null){
    bValid = false;
    alert('Please enter description.');
  }else if(sDescript.value == ''){
    bValid = false;
    alert('Please enter description.');
  }
  
  if(bValid){
    dojoRequestPost(p_sFormId, p_sAction, p_sResultDiv);
  }
}

function validateEditTypedet(p_sFormId, p_sAction, p_sResultDiv){
  var bValid = true;
  
  var sValue = document.getElementById('txtName');
  if(sValue == null){
    bValid = false;
    alert('Please enter name.');
  }else if(sValue.value == ''){
    bValid = false;
    alert('Please enter name.');
  }
  
  var sDescript = document.getElementById('txtDescription');
  if(sDescript == null){
    bValid = false;
    alert('Please enter description.');
  }else if(sDescript.value == ''){
    bValid = false;
    alert('Please enter description.');
  }
  
  var sTydetId = document.getElementById('txtTydetId');
  if(sTydetId == null){
    bValid = false;
    alert('Type detail not selected.');
  }else if(sTydetId.value == ''){
    bValid = false;
    alert('Type detail not selected.');
  }
  
  if(bValid){
    dojoRequestPost(p_sFormId, p_sAction, p_sResultDiv);
  }
}

function validateCreateAtomic(p_sFormId, p_sAction, p_sResultDiv){
  var bValid = true;
  
  var number = document.getElementById('txtAtomicNum');
  if(number == null){
    bValid = false;
    alert('Please enter atomic number.');
  }else if(number.value == ''){
    bValid = false;
    alert('Please enter atomic number.');
  }
  
  var symbol = document.getElementById('txtSymbol');
  if(symbol == null){
    bValid = false;
    alert('Please enter symbol.');
  }else if(symbol.value == ''){
    bValid = false;
    alert('Please enter symbol.');
  }
  
  var mass = document.getElementById('txtAtomicMass');
  if(mass == null){
    bValid = false;
    alert('Please enter mass.');
  }else if(mass.value == ''){
    bValid = false;
    alert('Please enter mass.');
  }
  
  if(bValid){
    dojoRequestPost(p_sFormId, p_sAction, p_sResultDiv);
  }
}

function validateEditAtomic(p_sFormId, p_sAction, p_sResultDiv){
  var bValid = true;
  
  var an = document.getElementById('txtAtomicId');
  if(an == null){
    bValid = false;
    alert('Please select element.');
  }else if(an.value == ''){
    bValid = false;
    alert('Please select element.');
  }
  
  var number = document.getElementById('txtAtomicNum');
  if(number == null){
    bValid = false;
    alert('Please enter atomic number.');
  }else if(number.value == ''){
    bValid = false;
    alert('Please enter atomic number.');
  }
  
  var symbol = document.getElementById('txtSymbol');
  if(symbol == null){
    bValid = false;
    alert('Please enter symbol.');
  }else if(symbol.value == ''){
    bValid = false;
    alert('Please enter symbol.');
  }
  
  var mass = document.getElementById('txtAtomicMass');
  if(mass == null){
    bValid = false;
    alert('Please enter mass.');
  }else if(mass.value == ''){
    bValid = false;
    alert('Please enter mass.');
  }
  
  if(bValid){
    dojoRequestPost(p_sFormId, p_sAction, p_sResultDiv);
  }
}

function validateAtg(p_sFormId, p_sAction, p_sResultDiv){
  var bValid = true;
  
  var sAtgNum = document.getElementById('txtAtgNum');
  if(sAtgNum == null){
    bValid = false;
    alert('Please enter ATG number.');
  }else if(sAtgNum.value == ''){
    bValid = false;
    alert('Please enter ATG number.');
  }
  
  var sStart = document.getElementById('txtStart');
  if(sStart != null && sStart.value != ''){
    if(sStart.value <= 0){
      bValid = false;
      alert('Please enter start value greater than 0.');
    }
  }
  
  var sEnd = document.getElementById('txtEnd');
  if(sEnd != null && sEnd.value != ''){
    if(sEnd.value <= 0){
      bValid = false;
      alert('Please enter end value greater than 0.');
    }
  }
  
  var sFr = document.getElementById('txtFr');
  if(sFr != null){
    if(sFr.value.length > 10){
      bValid = false;
      alert('Fr should be less than 10 characters.');
    }
  }
  
  var sChromo = document.getElementById('txtChromo');
  if(sEnd != null && sEnd.value != ''){
    if(sEnd.value <= 0){
      bValid = false;
      alert('Please enter chromo value greater than 0.');
    }
  }
  
  var sPpn = document.getElementById('txtPPN');
  if(sPpn != null){
    if(sPpn.value.length > 60){
      bValid = false;
      alert('PPN should be less than 60 characters.');
    }
  }
  
  var sAka = document.getElementById('txtAKA');
  if(sAka != null){
    if(sAka.value.length > 120){
      bValid = false;
      alert('AKA should be less than 120 characters.');
    }
  }
  
  var sComment = document.getElementById('txtComment');
  if(sComment != null){
    if(sComment.value.length > 1024){
      bValid = false;
      alert('Comment should be less than 1024 characters.');
    }
  }
  
  var sDescription = document.getElementById('txtDescription');
  if(sDescription != null){
    if(sDescription.value.length > 2048){
      bValid = false;
      alert('Description should be less than 2048 characters.');
    }
  }
  
  if(bValid){
    dojoRequestPost(p_sFormId, p_sAction, p_sResultDiv);
  }
}

function validateCalibration(p_sFormId, p_sAction, p_sResultDiv){
  var bValid = true;
  
  var sName = document.getElementById('txtName');
  if(sName == null){
    bValid = false;
    alert('Please enter a unique name.');
  }else if(sName.value == ''){
    bValid = false;
    alert('Please enter a unique name.');
  }
  
  if(bValid){
    dojoRequestPost(p_sFormId, p_sAction, p_sResultDiv);
  }
}

function validateStandardMaterial(p_sFormId, p_sAction, p_sResultDiv){
  var bValid = true;
  
  var sName = document.getElementById('txtName');
  if(sName == null){
    bValid = false;
    alert('Please enter a name.');
  }else if(sName.value == ''){
    bValid = false;
    alert('Please enter a name.');
  }
  
  var sRecipe = document.getElementById('txtRecipe');
  if(sRecipe != null){
    if(sRecipe.value != ''){
      if(sRecipe.value.length() > 255){
        bValid = false;
        alert('Recipe should not exceed 255 characters.');
      }
    }
  }
  
  var sDescript = document.getElementById('txtDescription');
  if(sDescript != null){
    if(sDescript.value != ''){
      if(sDescript.value.length() > 255){
        bValid = false;
        alert('Description should not exceed 255 characters.');
      }
    }
  }
  
  if(bValid){
    dojoRequestPost(p_sFormId, p_sAction, p_sResultDiv);
  }
}

function validateIcpms(p_sFormId, p_sAction, p_sResultDiv){
  var bValid = true;
  
  var sName = document.getElementById('txtName');
  if(sName == null){
    bValid = false;
    alert('Please enter a name.');
  }else if(sName.value == ''){
    bValid = false;
    alert('Please enter a name.');
  }
  
  var sPurchaseDate = document.getElementById('txtPurDate');
  if(sPurchaseDate.value != ''){
    if(!isDate(sPurchaseDate.value)) {
      bValid = false;
    }
  }
  
  var sDetectorDate = document.getElementById('txtDetectorReplaceDate');
  if (sDetectorDate.value != "") {
	//check if input is a valid date
	if(!isDate(sDetectorDate.value)) {
	  validInput = false;
	}
  }
  
  var sCloneDate = document.getElementById('txtDetectorReplaceDate');
  if (sCloneDate.value != "") {
	//check if input is a valid date
	if(!isDate(sCloneDate.value)) {
	  validInput = false;
	}
  }
	
  var sDescript = document.getElementById('txtDescription');
  if (!maxLength(sDescript.value, 'descript', 'description', 'createIcpmseq', 255)) {
	validInput = false;
  }
  
  if(bValid){
    dojoRequestPost(p_sFormId, p_sAction, p_sResultDiv);
  }
}

function validateLine(p_sFormId, p_sAction, p_sResultDiv){
  var bValid = true;
  
  if(inputExists(document.getElementById('txtPPN').value, 'ppn', 'line name (piims preferred name)', 'createLine')) {
	if (maxLength(document.getElementById('txtPPN').value, 'ppn', 'line name (piims preferred name)', 'createLine', 60)) {
	  //input ok
	} else {
	  bValid = false;
	}
  } else {
	bValid = false;
  }
	
  //AKA (Less than 120 chars)
  if (maxLength(document.getElementById('txtAKA').value, 'aka', 'AKA (also known as)', 'createLine', 120)) {
	//input ok
  } else {
	bValid = false;
  }
  
  if(bValid){
    dojoRequestPost(p_sFormId, p_sAction, p_sResultDiv);
  }
}