function isValidPhone(txtPhone) {
 if(txtPhone!="") {
  var stripped = txtPhone.replace(/[\(\)\.\-\ ]/g, '');
  //strip out acceptable non-numeric characters
  if (isNaN(parseInt(stripped))) {
   return false;
  } else {
   return true;
  }
 } else {
  return false;
 }
}

function isValidEmail(txtEmail) {
 if(txtEmail!="") {
  return (txtEmail.indexOf(".") > 2) && (txtEmail.indexOf("@") > 0);
 } else {
  return false;
 }
}

$(document).ready(function(){
    $("#trynow").click(function(){
	  var div_mask = '<div id="TB_overlay" style="position: fixed;z-index:100;top: 0px;left: 0px;height:100%;width:100%;background-color:#888888 ; opacity:0.75;"><div id="message_div" style="position: fixed;top:50%;left: 35%;text-align:center" align="center"><font color="red" weight="bold" size="15px">Message Processing...</font><br /><img src="img/progress_bar.gif"/></div></div>';
      
	  if($("#txtNotificationType").val() == "phone"){
	    $("#cmd").val('dial');
	    if(isValidPhone($("#txtDataTryNow").val())){
	       if($("#txtFacilityName").val() == '' || $("#txtDate").val() == ''){
	          alert("Facility name and date are mandatory fields. Try again.");
              return false;
		    }else{
			  $(div_mask).appendTo(document.body);
			  $.post("handle_try_now.php",{
	                       cmd: $("#cmd").val(),
			selectId: $("#selectId").val(),
			txtDataTryNow: $("#txtDataTryNow").val(),
			txtDate: $("#txtDate").val(),
			txtFacilityName: $("#txtFacilityName").val(),
			txtNotificationType: $("#txtNotificationType").val(),
                         txtShift: $("#txtShift").val()
	           },function(data){
	                     //alert(data.facilityName+' '+data.dateStr+' '+ data.tryId+' '+data.phoneNumber+' '+data.url);
		             $.getJSON(data.url+"asterisk_redirect.php?dialnow=1&facilityName="+data.facilityName+"&assignmentDate="+data.dateStr+"&tryId="+data.tryId+"&dial="+data.phoneNumber+"&shiftStart="+data.shiftStart+"&shiftEnd="+data.shiftEnd+"&jsonpcallback=?", 
                                          function(response){
			                   if(response.status == "FAILED"){
			                      $('#TB_overlay').remove();
		                          $("#status").html('<font color="red" weight="bold">ERROR:</font> Phone message can not be sent.');
			                }else{
			                   setTimeout(function(){
					                 $.post("asterisk_confirm.php",{
				                                      cmd: 'getResponseState',
				                                      tnid: response.tryId
				                                 },function(response1){
				                                        $('#TB_overlay').remove();
		                                                $("#status").html('<font color="red">'+response1+'</font>');
				                                 });
					             },90000);
			                }
			               });
		             
	             },"json");
			}
	    }else{
	        alert("You have chosen to be notified by phone, but you did not enter a valid phone number. Try again.");
            return false;
	    }
	  }else if($("#txtNotificationType").val() == "email"){
	    $("#cmd").val('email');
		if(isValidEmail($("#txtDataTryNow").val())){
		  if($("#txtFacilityName").val() == '' || $("#txtDate").val() == ''){
	          alert("Facility name and date are mandatory fields. Try again.");
              return false;
		    }else{
			  $(div_mask).appendTo(document.body);
			  $.post("handle_try_now.php",{
	                    cmd: $("#cmd").val(),
			     selectId: $("#selectId").val(),
			txtDataTryNow: $("#txtDataTryNow").val(),
			txtDate: $("#txtDate").val(),
			txtFacilityName: $("#txtFacilityName").val(),
			txtNotificationType: $("#txtNotificationType").val(),
			txtShift: $("#txtShift").val()
	        },function(data){
	           if(data == "email_sent"){
		          $('#TB_overlay').remove();
		          $("#status").html('<font color="red">Thank you for your interest. You will soon get a confirmation email message.</font>');
		        }else if(data == "email_error"){
		          $('#TB_overlay').remove();
		          $("#status").html('<font color="red" weight="bold">ERROR:</font> Email can not be sent.');
		        }
	          });
			}
		}else{
		  alert("You have chosen to be notified by email, but you did not enter valid email address. Try again.");
          return false;
	    }
	  }else if($("#txtNotificationType").val() == "sms"){
	    $("#cmd").val('sms');
		if(isValidPhone($("#txtDataTryNow").val())){
	       if($("#txtFacilityName").val() == '' || $("#txtDate").val() == ''){
	          alert("Facility name and date are mandatory fields. Try again.");
              return false;
		    }else{
			  $(div_mask).appendTo(document.body);
			  $.post("handle_try_now.php",{
	        cmd: $("#cmd").val(),
			selectId: $("#selectId").val(),
			txtDataTryNow: $("#txtDataTryNow").val(),
			txtDate: $("#txtDate").val(),
			txtFacilityName: $("#txtFacilityName").val(),
			txtNotificationType: $("#txtNotificationType").val(),
                         txtShift: $("#txtShift").val()
	  },function(data){
		 if(data == "sms_sent"){
		    $('#TB_overlay').remove();
		    $("#status").html('<font color="red">Thank you for your interest. You will soon get a SMS message. After replying, you will get a confirmation SMS message.</font>');
		 }else if(data == "sms_error"){
		    $('#TB_overlay').remove();
		    $("#status").html('<font color="red" weight="bold">ERROR:</font> SMS can not be sent.');
		 }
		 });
			}
	    }else{
	        alert("You have chosen to be notified by text message, but you did not enter a valid phone number. Try again.");
            return false;
	    }
	  }
	});  
});