// JavaScript Document
///////////////////////////////////////////////////////////////////////////////// START
$(document).ready(function(){
	
/////////////////////////////////////////// LAUNCH INTERFACE
var checkBackdrop = $("body").attr("backdrop");
$("body").css("background-image","url(bkgnds/"+checkBackdrop+")");

$(".btn_About").click(function() {
	$.ajax({
  		url: "function_about.php",
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("Midevenings with Jay Miller");
				});
				}
		});
});

//GOOGLE LOCATION
initialize();
setup_contactLink();

////// END DOC
});

////////////////////////////////////////// INDEX PAGE SETUP

////////////////////////////////////////// START CONTENT SETUP


////////////////////// INDEX PAGE MAILING LIST
var setup_contactLink = function() {
	$(".contactLink").click(function() {
	var checkListType =$(this).attr("data");
	launcher_contactSignup(checkListType);
});
}
var launcher_contactSignup = function(listType){
	getGoogleLocation = $('#location').attr('data');
		$.ajax({
  		url: "function_contactSignup.php?type="+listType+"&location="+getGoogleLocation,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			if (listType == "email") {
				setup_listEmail();
			} else if (listType == "txt") {
				setup_listTxt();
			}
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("Midevenings with Jay Miller");
				});
				}
		});
}

var setup_listTxt = function() {
	$(".listAlertText").css("display", "none");
	$(".listAlertText_no").css("display", "none");
	var txtList_location = $("#sigupSetup").attr("location");
	$("#listTxt_go").click(function() {
	var txtList_name = $("#txtList_name").val();
	var txtList_cellno = $("#txtList_cellno").val();
	var txtList_carrier = $("#txtList_carrier").val();
	if (txtList_name == '') {
		$("#txtList_name").css("border", "solid 2px #c6b84a");
	} else {
		$("#txtList_name").css("border", "none");
	}
	if (txtList_cellno == '') {
		$("#txtList_cellno").css("border", "solid 2px #c6b84a");
	} else {
		$("#txtList_cellno").css("border", "none");
	}
	if (txtList_carrier == 'null') {
		$("#txtList_carrier").css("border", "solid 2px #c6b84a");
	} else {
		 $("#txtList_carrier").css("border", "none");
	}
	if (txtList_name == '' || txtList_cellno == '' || txtList_carrier == 'null') {
	$(".listAlertText").css("display", "block");
	} else {
		setup_txtPhoneValidation(txtList_name, txtList_cellno, txtList_carrier, txtList_location);
	}
});
}
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

var setup_txtPhoneValidation = function (txtList_name, txtList_cellno, txtList_carrier, txtList_location) {

	if (checkInternationalPhone(txtList_cellno)==false){
		//alert("Please Enter a Valid Phone Number")
		//Phone=""
		//Phone.focus()
		$(".listAlertText_no").css("display", "block");
		$("#txtList_cellno").css("border", "solid 2px #c6b84a");
		$("#txtList_cellno").val('');
		//return false
	} else {
	$(".listAlertText").css("display", "none");
	$(".listAlertText_no").css("display", "none");
	$("#txtList_cellno").css("border", "none");
	launcher_listTxt_verify(txtList_name, txtList_cellno, txtList_carrier, txtList_location);
	}
	//return true
}
var checkInternationalPhone = function (strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
var isInteger = function(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
var trim = function (s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
var stripCharsInBag = function (s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

var setup_txtVerify = function() {
	$(".listAlertText_val").css("display", "none");
	var txtList_location = $("#signupData").attr("location");
	var txtList_name = $("#signupData").attr("name");
	var txtList_cellno = $("#signupData").attr("cellno");
	var txtList_carrier = $("#signupData").attr("carrier");
	var txtList_status = $("#signupData").attr("status");
	
	$("#listTxt_activate").click(function() {
	var txtList_key = $("#txtList_activation").val();
	if (txtList_key == '') {
		$(".listAlertText_val").css("display", "block");
		$("#txtList_activation").css("border", "solid 2px #c6b84a");
	} else {
		$(".listAlertText_val").css("display", "none");
		$("#txtList_activation").css("border", "none");
		launcher_listTxt_activate(txtList_location, txtList_name, txtList_cellno, txtList_carrier, txtList_key);
	}
});
}
var setup_txtActivate = function() {
	$(".listAlertText").css("display", "none");
	$(".listAlertText_no").css("display", "none");
	$(".listAlertText_val").css("display", "none");
	var txtList_location = $("#signupData").attr("location");
	var txtList_name = $("#signupData").attr("name");
	var txtList_cellno = $("#signupData").attr("cellno");
	var txtList_carrier = $("#signupData").attr("carrier");
	var txtList_status = $("#signupData").attr("status");
	if (txtList_status == "active") {
		$(".listAlertText").css("display", "block");
	} else {
		$(".listAlertText").css("display", "block");
		$("#listTxt_activate").click(function() {
		var txtList_key = $("#txtList_activation").val();
		if (txtList_key == '') {
			$(".listAlertText_val").css("display", "block");
			$("#txtList_activation").css("border", "solid 2px #c6b84a");
		} else {
			$("#txtList_activation").css("border", "none");
			$(".listAlertText").css("display", "none");
			$(".listAlertText_no").css("display", "none");
			$(".listAlertText_val").css("display", "none");
			launcher_listTxt_activate(txtList_location, txtList_name, txtList_cellno, txtList_carrier, txtList_key);
		}
		});
	}
}
var launcher_listTxt_verify = function (txtList_name, txtList_cellno, txtList_carrier, location) {
	$.ajax({
  		url: "function_contactSignup_txt_verify.php?name="+txtList_name+"&cellno="+txtList_cellno+"&carrier="+txtList_carrier+"&location="+location,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			setup_txtVerify();
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("Midevenings with Jay Miller");
				});
				}
	});
}
var launcher_listTxt_activate = function (location, txtList_name, txtList_cellno, txtList_carrier, key) {
	$.ajax({
  		url: "function_contactSignup_txt_activate.php?name="+txtList_name+"&cellno="+txtList_cellno+"&carrier="+txtList_carrier+"&location="+location+"&key="+key,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			setup_txtActivate();
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("Midevenings with Jay Miller");
				});
				}
	});
}

var setup_listEmail = function() {
	var echeckVerify = true;
	$(".listAlertText").css("display", "none");
	$(".listAlertText_email").css("display", "none");
	var emailList_location = $("#sigupSetup").attr("location");
	$("#listEmail_go").click(function() {
	var emailList_name = $("#emailList_name").val();
	var emailList_email = $("#emailList_emailAddr").val();
	
	if (emailList_name == '') {
		$("#emailList_name").css("border", "solid 2px #c6b84a");
	} else {
		$("#emailList_name").css("border", "none");
	}
	if (emailList_email == '') {
		$("#emailList_emailAddr").css("border", "solid 2px #c6b84a");
	} else {
		$("#emailList_emailAddr").css("border", "none");
	}
	if (echeck(emailList_email) == false) {
		var echeckVerify = false;
		$(".listAlertText_email").css("display", "block");
		$("#emailList_emailAddr").css("border", "solid 2px #c6b84a");
	} else {
		$(".listAlertText_email").css("display", "none");
	}
	if (emailList_name == '' || emailList_email == '' || echeckVerify == false) {
		$(".listAlertText").css("display", "block");
		if (echeckVerify == false) {
			$(".listAlertText_email").css("display", "block");
		}
	} else {
		$(".listAlertText").css("display", "none");
		$(".listAlertText_email").css("display", "none");
		launcher_listEmail_join(emailList_name, emailList_email, emailList_location);
	}
});
}
function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){return false}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false}
		if (str.indexOf(at,(lat+1))!=-1){return false}
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false}
		if (str.indexOf(dot,(lat+2))==-1){return false}
		if (str.indexOf(" ")!=-1){return false}
 		return true					
}
var launcher_listEmail_join = function (emailList_name, emailList_email, location) {
	$.ajax({
  		url: "function_contactSignup_email.php?name="+emailList_name+"&email="+emailList_email+"&location="+location,
  		cache: false,
  		success: function(incomingContent){
			$("#modalBox").empty();
    		$("#modalBox").append(incomingContent);
			$("#modalWrapper").show();
			$("#modalBoxExit").click(function() {
				$("#modalBox").empty();
				$("#modalWrapper").hide();
				$("title").text("Midevenings with Jay Miller");
				});
				}
	});
}

var initialize = function() {
	//alert('google');
    // Initialize default values
    var googleLocation = "null";

    // If ClientLocation was filled in by the loader, use that info instead
    if (google.loader.ClientLocation) {
      googleLocation = getFormattedLocation();
	  //alert(googleLocation);
    }
    //document.getElementById("location").innerHTML = location;
	$('#location').attr('data', googleLocation);
	//alert("location is "+$('#location').attr('data'));
  }
var getFormattedLocation = function() {
    if (google.loader.ClientLocation.address.country_code == "US" &&
      google.loader.ClientLocation.address.region) {
      return google.loader.ClientLocation.address.city + ", " 
          + google.loader.ClientLocation.address.region.toUpperCase();
    } else {
      return  google.loader.ClientLocation.address.city + ", "
          + google.loader.ClientLocation.address.country_code;
    }
  }
