function chtype(val)
{
   if(val==1)
   { 
    $('#idtitle').html("Share an idea");
    $('#idtit1').html("<b>Idea</b>");
    $('#idtit2').html("Question");
    $('#idtit3').html("Problem");
    $('#typopt').val("Idea");
    $('#idtit1').attr('style', "margin-left:20px;color:red;");
    $('#idtit2').attr('style', "margin-left:20px;color:blue;");
    $('#idtit3').attr('style', "margin-left:20px;color:blue;");
   }
   if(val==2)
   { 
    $('#idtitle').html("Ask a question");
    $('#idtit1').html("Idea");
    $('#idtit2').html("<b>Question</b>");
    $('#idtit3').html("Problem");
    $('#typopt').val("Question");
    $('#idtit1').attr('style', "margin-left:20px;color:blue;");
    $('#idtit2').attr('style', "margin-left:20px;color:red;");
    $('#idtit3').attr('style', "margin-left:20px;color:blue;");
   }
   if(val==3)
   {
    $('#idtitle').html("Report a problem");
    $('#idtit1').html("Idea");
    $('#idtit2').html("Question");
    $('#idtit3').html("<b>Problem</b>");
    $('#typopt').val("Problem");
    $('#idtit1').attr('style', "margin-left:20px;color:blue;");
    $('#idtit3').attr('style', "margin-left:20px;color:red;");
    $('#idtit2').attr('style', "margin-left:20px;color:blue;");
   }
}

function sendmail()
{
   if(formValidator())
   {
    $.ajax({type:"POST",
      dataType:"json",
      url:"fileaction.php",
      async:false,
      data: {opt:"sendmail",
             fromfile:fromfile,
             fbtype:$('#typopt').val(),
             firstname:$('#first_name').val(),
             lastname:$('#last_name').val(),
             email:$('#email').val(),
             comments:$('#comments').val()
            },
          success:function(data){
            $("#leftform").html("<br /><br />Thank you for your valuable feedback.<br /><br />Please ask your friends to check out Blessing Stream.<br /><br />-The Blessing Stream family");
          }
    });
   }
}       


function formValidator(){
  // Make quick references to our fields
  var firstname = document.getElementById('first_name');
  var email = document.getElementById('email');
  var comment = document.getElementById('comments');

  if(firstname.value=='Name')
  {
    alert("Please enter a valid Name");
    return false;
  }
  if(comment.value=='Comments')
  {
    alert("Please enter a valid Comments");
    return false;
  }

  // Check each input in the order that it appears in the form!
  if(isEmpty(firstname, "Please enter your Name.")){
    if(isAlphabet(firstname, "Please enter a valid Name.")){
      if(isEmpty(email, "Please enter your Email.")){
        if(emailValidator(email, "Please enter a valid Email.")){
          if(isEmpty(comment, "Please enter a Comment.")){
            if(isAlphanumeric(comment, "Please enter a valid Comment.")){
                return true;
            }
          }
        }
      }
    }
  }

  return false;

}


function isEmpty(elem, helperMsg){
  if(elem.value.length == 0){
    alert(helperMsg);
    elem.focus();
    return false;
  }else{
    return true;
  }
}

function isNumeric(elem, helperMsg){
  var numericExpression = /^[0-9]+$/;
  if(elem.value.match(numericExpression)){
    return true;
  }else{
    alert(helperMsg);
    elem.focus();
    return false;
  }
}

function isAlphabet(elem, helperMsg){
  var alphaExp = /^[a-zA-Z\s\d]+$/;
  if(elem.value.match(alphaExp)){
    return true;
  }else{
    alert(helperMsg);
    elem.focus();
    return false;
  }
}

function isAlphanumeric(elem, helperMsg){
  var alphaExp = /^[0-9a-zA-Z=;:?+-_@$%&()/"'!#<>,\s\d]+$/;
  if(elem.value.match(alphaExp)){
    return true;
  }else{
    alert(helperMsg);
    elem.focus();
    return false;
  }
}

function lengthRestriction(elem, min, max){
  var uInput = elem.value;
  if(uInput.length >= min && uInput.length <= max){
    return true;
  }else{
    alert("Please enter between " +min+ " and " +max+ " characters");
    elem.focus();
    return false;
  }
}

function qtyRestriction(elem, helperMsg){
  var uInput = elem.value;
  if(uInput <= 100){
    return true;
  }else{
    alert(helperMsg);
    elem.focus();
    return false;
  }
}

function madeSelection(elem, helperMsg){
  if(elem.value == "Please Choose"){
    alert(helperMsg);
    elem.focus();
    return false;
  }else{
    return true;
  }
}

function emailValidator(elem, helperMsg){
  var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
  if(elem.value.match(emailExp)){
    return true;
  }else{
    alert(helperMsg);
    elem.focus();
    return false;
  }
}


