jQuery.noConflict();
var bellmeId = null;
var dTag=null;

function callbackInit() {
  jQuery("#callbackPhone").focus(function() {
	var phoneNumber = (jQuery("#callbackPhone"))[0].value;
	if (phoneNumber == "Type your Ph here ...") {
	  (jQuery("#callbackPhone"))[0].value = "";
	}
  });
  jQuery("#callbackSubmit").click(function() {
	var name = (jQuery("#callbackName"))[0].value;
    var phoneNumber = (jQuery("#callbackPhone"))[0].value;
    var illegalCharsA = /\d$/;
    if (phoneNumber == "" || phoneNumber == null || phoneNumber == "Type your Ph here ..." || phoneNumber.search(illegalCharsA) == -1 || phoneNumber.length < 9 || phoneNumber.length > 11) {
      alert("Please Enter valid Phone Number including area code");
      return false;
    }
    
    // invoke bellme code
    if (bellmeId==null) { return; }
    (jQuery("#mynumber" + bellmeId))[0].value = phoneNumber;
    eval("addScript" + bellmeId + "()");
    
      /*
      var url = "http://bellmedia.com/bellme/but02/rf.php";
        url = url + "?a=" + phoneNumber;
        url = url + "&t=1303858720"; // bis act
        url = url + "&mb=";
        url = url + "&unID=" + bellmeId;
        url = url + "&noCIE=" + (new Date()).getTime();
      // this does trigger a HTTP request which triggers a call, but the response is 
      // always empty. Is probably a cross-domain issue. 
	  var jqhxr = jQuery.ajax({"url": url, 
	    "dataType" : "text", 
	    "success": function(data, textStatus, jqXHR) { 
	      //alert("success - data=" + data);
          data=data.replace(new RegExp("dynaBut" + bellmeId + "\\({"), "data2 = {");
          data=data.replace(new RegExp("}\\);"), "}");
	      if (data2.statusA=="204") {
	      	alert("Connection to the above number has been successful and your phone should be ringing in the next few seconds.");
	      } else {
	       	alert("There was an error calling the provided phone number: " + data2.subVar);
	      }
	    },
	    "error": function(jqXHR, textStatus, errorThrown) { 
	      alert("error (" + textStatus + ")" + data2); 
	    } });
	  */

      // alerts are now displayed via the bellme code 
	  var jqhxr2 = jQuery.ajax({"url": "phoneCallback.do",
	    "type" : "POST",
	    "data" : { 
	    	"name" : name, 
	    	"phone": phoneNumber, 
	    	// "pageref" : new String(window.location), just can't get this to work 
	    	"noCIE" : (new Date()).getTime() },
        "dataType" : "json", 
        "success": function(data, textStatus, jqXHR) { 
  	      if (data.statusA=="204") {
  	      	//alert("Connection to the above number has been successful and your phone should be ringing in the next few seconds.");
  	    	//alert("Thankyou for submitting your contact details. You will receive a phone call from one of our insurance specialists shortly.");
  	      } else {
  	       	//alert("There was an error calling the provided phone number: " + data.subVar);
  	      }
        },
        "error": function(jqXHR, textStatus, errorThrown) {
        	//alert("There was an error contacting the callback service: " + textStatus);
        } });
  });
}
function callbackTBInit() {
  jQuery("#callbackTBTrigger").click(function() {
	var el = jQuery("#callbackTBExpander");
	el.stop();
	var width = el.height(); 
	if (width==0) {
	  el.animate({ height: 120 }, 1000, function() { });
	} else {
	  el.animate({ height: 0 }, 1000, function() { });
	}
  });
  callbackInit();
}
function callbackLRInit() {
  jQuery("#callbackLRTrigger").click(function() {
	var el = jQuery("#callbackLRExpander");
	el.stop();
	var width = el.width(); 
	if (width==0) {
	  el.animate({ width: 380 }, 1000, function() { });
	} else {
	  el.animate({ width: 0 }, 1000, function() { });
	}
  });
  callbackInit();
}

function resetBellme() {
  // dTag.id = "bButUnDivId1570";
  if (dTag) { 
    bellmeId = new Number(dTag.id.substring(11));
    //alert("Resetting id " + bellmeId);
  } else {
    window.setTimeout(resetBellme, 100);
    return;
  }
  jQuery("#bellmeDiv").html("<div style=\"display:none;\">" +
    "<input id=\"ringring" + bellmeId + "\" name=\"ringring" + bellmeId + "\" type=\"text\" value=\"\">" +
    "<input id=\"mynumber" + bellmeId + "\" name=\"mynumber" + bellmeId + "\" type=\"text\" value=\"\">" +
    "<input id=\"bellme_remember" + bellmeId + "\" name=\"bellme_remember" + bellmeId + "\" type=\"checkbox\" value=\"\">" +
    "</div>");
}
