
/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var inputClass = '.textToPhoneNmbr';

//loading popup with jQuery magic!
function loadPopup(map){
	
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
		$(".UTypeID, .selectTime").hide();
		if(map != 'cellItMap'){
			$("#mapStuff").hide();
			$("#noMap").show();
		} else {
			$("#mapStuff").show();
			$("#noMap").hide();
		}
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
		
	}
}

//centering popup
function centerPopup(page){
	
	var windowWidth = document.body.clientWidth;
	var windowHeight = document.body.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	if(page != 'cellItMap'){
		$("#popupContact").css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});
	} else {
		$("#popupContact").css({
		"position": "absolute",
		"top": 200,
		"left": 420
		});
	}
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight,
		"width": windowWidth
	});
}

function placeHolder(){
	// Text to Phone
	//var textNmbr = $('#textToPhoneForm label').remove().text();
	var textNmbr = 1234567890
	$(inputClass).addClass('placeholder').val(textNmbr)
		.focus(function() {
				if (this.value == textNmbr) {
					$(this).removeClass('placeholder').val('');
				};
		})
		.blur(function() {
				if (this.value == '') {
				$(this).addClass('placeholder').val(textNmbr);	
				}
	})
}

$(document).ready(function(){
	//LOADING POPUP
	//Click the button event!
	$(".textToPhone").click(function(event){
		//if we are on the Map page
		event.preventDefault();
		/*
		if($(this).attr('id')== 'cellItMap'){
			inputClass = '.textToPhoneNmbrMap';
		}else{
			inputClass= '.textToPhoneNmbr';
		}
		*/
		
		inputClass= '.textToPhoneNmbr';
		placeHolder();
		
		//centering with css
		centerPopup($(this).attr('id'));
		
		//Clear input fields
		$('FORM#textToPhoneForm #mapToPhoneNmbr, FORM#textToPhoneForm #startingAddress').val("");
			
		//load popup
		loadPopup($(this).attr('id'));		
	});
	
	//Hover over button
	$('#closeBtn').hover(function()
		{
			$(this).css({'border-style':'inset','background-color':'#84261a', 'color':'#ffffff'});
		},function()
		{
		$(this).css({'border-style':'outset','background-color':'#d6cda4', 'color':'#5c3d17'});					
	});	
					
	//CLOSING POPUP
	//Click the x event!
	$('#closeBtn').click(function(){
		disablePopup();
	});
	

	// Format the phone number from 1234567890 to (123) 456-7890
	if($('.textToPhoneNmbrMap').val() != ''){
		classes = '.textToPhoneNmbrMap, .textToPhoneNmbr';
	}else{
		classes = '.textToPhoneNmbr';
	}
	
	// Check to make sure that the keypress is a number or backspace.  This includes the keypad.
	$('.textToPhoneNmbr').keydown(function(event) {
		if ( event.keyCode == 46 || event.keyCode == 8 ) {
		} else {
			if (event.keyCode < 95) {
				if (event.keyCode < 48 || event.keyCode > 57 ) {
				event.preventDefault();
				}
			} else {
				if (event.keyCode < 96 || event.keyCode > 105 ) {
				event.preventDefault();
				}
			}
		}
	}); 
	
	$('.textToPhoneNmbr').keyup(function(event){
		var sLength = $(inputClass).val().length;
	
		if ((sLength <=14 && (event.keyCode >= 48 && event.keyCode <= 57)) || 
			(sLength <=14 && (event.keyCode >= 96 && event.keyCode <= 105))) {
			
			// Format the area code once the number of digits equals 3
			if (sLength == 3) {
				$(inputClass).val('('+$(inputClass).val()+') ');
			};
			
			// Once the area code and prefix has been added add the dash ( - )
			if (sLength == 9) {
				$(inputClass).val($(inputClass).val()+'-');
			};
			
			$('#counter').empty().append($(inputClass).val().length);
		} 
		else {
				sLength--;
				$(inputClass).val($(inputClass).val().substr(0,14));
		};
	
	});
	
    
   onError = function() {
			// Create an error response
			alert ("We're sorry, we hit an error while trying to send your message.  Please try again.");
		}
        
    onSuccess = function() {
			// Create an error response
			alert ("The information has been sent to your cell phone.");
		}        
    
    
		
	$('#submitBtn, #driveSubmitBtn').click(function() {	
            $.ajax({
                error: onError,
                success: onSuccess,
                url: 'cellit/cellitCommunicator.php?cid=' + $('.commID').val() + '&textToPhoneNumbr=' + $(inputClass).val() + (($('.startingAddress').val())?'&startingAddress=' +$('.startingAddress').val():'')
              });
			// ($('.commID'));
           disablePopup();
	});
	
	$('.inputSpan').click(function() {
			var field = this.id;
			if(field == 'areaCode') { $(inputClass).focus(); }
			if(field == 'starting') { $('#startingAddress').focus(); }
	});

    
});




