function checkEntry(src){
    switch(src){
        case 1:
            if (checkbottles(document.forms['mainform'].totbottles.value)){
                var params='totbottles='+document.forms['mainform'].totbottles.value;
                doajax('botslctr.php','botslctr',params);
            }
            break;
        case 2:
            checktasters(document.forms['mainform'].tottasters.value);        
            break;
        case 3:
            checkdate(document.forms['mainform'].tstdate.value);      
            break;
        case 4:
            checkemail(document.forms['mainform'].tstemail.value);    
            break;
    }
}

function checkemail(str){
    var foundat  = false;
    var founddot = false;
    for (var i = 0; i< str.length; i++){
        if (str.charAt(i) == '@')
            foundat = true;
        if (str.charAt(i) == '.')
            founddot = true;
    }
    if (!foundat || !founddot){
        alert('You must enter a valid email address');
        return false;
    }else
        return true;
}

function checkdate(str){
    var foundslash1 = false;
    var foundslash2 = false;
    for (var i = 0; i< str.length; i++){
        if (str.charAt(i) == '/' && !foundslash1){
            foundslash1 = true;
            i++;
        }
        if (str.charAt(i) == '/' && foundslash1)
            foundslash2 = true;
    }
    if (!foundslash1 || !foundslash2 || str.length < 6 || str.length > 10){
        alert('You must enter a valid date:mm/dd/yy');
        return false;
    }else
        return true;
}

function checktasters(num){
    if (!checkNumeric(num)) {
        alert ('You must enter a numeric number of tasters');
        return false;
    }
    num = num*1;
    if (num <= 0){
         alert('You must enter the total number of tasters');
         return false;
    }
    if (num > 30 ){
         alert('You must enter a reasonable number of tasters');
         return false;
    }
    return true;
}

//check that the Number of Bottles field entry is valid
function checkbottles(num){
  
    if (!checkNumeric(num)) {
        alert ('You must enter a numeric number of bottles');
        return false;
    }
    num = num*1;
    if (num <= 0){
         alert('You must enter the total number of bottles');
         return false;
    }
    if (num > 30 ){
         alert('You must enter a reasonable number of bottles');
         return false;
    }
    return true;
}

function checkVintage(num){

    if (!checkNumeric(num)) {
        alert ('You must enter a numeric Vintage number - example:2009');
        return false;
    }

    if (num < 1999 || num > 2015){
        alert ('Please enter a valid 4 digit Vintage (yyyy) example:2009');
        return false;
    }
    return true;
}

function checkVotes(num){
    if (num.length == 0 || num == 0)
        return true;
    if (!checkNumeric(num)) {
        alert ('You must enter a numeric number of votes');
        return false;
    }
    return true;
}

function bottleEntered(){
    if (checkWnry(document.forms['mainform'].tstwnry.value) && 
        checkWine(document.forms['mainform'].tstwine.value) && 
        checkVintage(document.forms['mainform'].tstvintage.value) &&
        checkVotes(document.forms['mainform'].firstvotes.value) &&
        checkVotes(document.forms['mainform'].secondvotes.value) &&
        checkVotes(document.forms['mainform'].thirdvotes.value))
        return true;
    else
        return false;
}

function checkWnry(str){
   if (str.length == 0){
       alert('This bottle\'s winery name has not been entered');
       return false;
   }else
       return true;
}

function checkWine(str){
   if (str.length == 0){
       alert('This bottle\'s wine name has not been entered');
       return false;
   }else
       return true;
}

function headEntered(){
    if (!document.forms['mainform'].tottasters.value){
        alert('Please enter the Number of Tasters above');
        return false;
    }

    if (!document.forms['mainform'].tstdate.value || document.forms['mainform'].tstdate.value == 'mm/dd/yy'){
        alert('Please enter the Tasting Session Date above');
        return false;
    }

    if (!document.forms['mainform'].tstemail.value){
        alert('Please enter the Contact Email above');
        return false;
    }
    if (checkemail(document.forms['mainform'].tstemail.value) &&
        checkdate(document.forms['mainform'].tstdate.value) &&
        checktasters(document.forms['mainform'].tottasters.value) &&
        checkbottles(document.forms['mainform'].totbottles.value)){
        return true;
    }else
        return false;
}

function checkNumeric(str){
   var strValidChars = "0123456789.-";
   var strChar;
   
   if (str.length == 0)
      return false;
   
   for (i = 0; i < str.length; i++){
      strChar = str.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         return false;
   }
   return true;
}

function setChecked(chkstr,tot){
   var chkary = chkstr.split(',');
   var eloffset = 7;	//bot1 = elements[8]
   var idx = 0;
   for (x=1;x<=tot;x++){
       idx = eloffset + x;
       if (chkary[x-1] == "checked")
           document.forms['mainform'].elements[idx].checked = true;
       else 
           document.forms['mainform'].elements[idx].checked = false;
   }
}

/*
function setBottle(bottlenum){
    var eloffset = 8;	//bot1 = elements[8]
    var step = 7;
    //set the current one to checked
    idx = eloffset + ((bottlenum-1)*step);
    document.forms['mainform'].elements[idx].checked = true; //bot
}
*/

function saveBottle(bottlenum){
    var eloffset = 8;	//bot1 = elements[8]
    var step = 7;
    //set the current one to checked
    
    idx = eloffset + ((bottlenum-1)*step);
    document.forms['mainform'].elements[idx+1].value = document.forms['mainform'].tstwnry.value;
    document.forms['mainform'].elements[idx+2].value = document.forms['mainform'].tstwine.value;
    document.forms['mainform'].elements[idx+3].value = document.forms['mainform'].tstvintage.value;
    document.forms['mainform'].elements[idx+4].value = document.forms['mainform'].firstvotes.value;
    document.forms['mainform'].elements[idx+5].value = document.forms['mainform'].secondvotes.value;
    document.forms['mainform'].elements[idx+6].value = document.forms['mainform'].thirdvotes.value;

    //check if valid
    if (document.forms['mainform'].elements[idx+1].value.length > 0 &&
        document.forms['mainform'].elements[idx+2].value.length > 0 &&
        document.forms['mainform'].elements[idx+3].value.length > 0 )
        document.forms['mainform'].elements[idx].checked = true;
    else
        document.forms['mainform'].elements[idx].checked = false;
}

function checkAllBottles(totbottles){
    var botstr = '';
    var eloffset = 8;	//bot1 = elements[8]
    var step = 7;
    var allbotsok = true;

    for (x=1;x<=totbottles;x++){
        idx = eloffset + ((x-1)*step);
        if (document.forms['mainform'].elements[idx+1].value.length == 0 ||
            document.forms['mainform'].elements[idx+2].value.length == 0 ||
            document.forms['mainform'].elements[idx+3].value.length == 0 ){
            botstr = botstr+x+' ';
            allbotsok = false;
        }
    }

    if (!allbotsok){
        if (confirm('Bottles:'+botstr+'are not fully entered.  Do you really want to submit? (OK=yes:Cancel=no)'))
            return true;
        else
            return false;
    }else
        return true;
}


function setBottleChks(curbot,totbottles){
    var botstr = '';
    var eloffset = 8;	//bot1 = elements[8]
    var step = 7;
    var allbotsok = true;

    for (x=1;x<=totbottles;x++){
        idx = eloffset + ((x-1)*step);
        //check if valid
        if ((document.forms['mainform'].elements[idx+1].value.length > 0 &&
            document.forms['mainform'].elements[idx+2].value.length > 0 &&
            document.forms['mainform'].elements[idx+3].value.length > 0 ) || x == curbot)
            document.forms['mainform'].elements[idx].checked = true;
        else
            document.forms['mainform'].elements[idx].checked = false;
    }
}

