function trackIT(action, label) {
	if(isLiveServer){
		//Track this event through GA, when we are in the live env
		_gaq.push(['_trackEvent', 'Mobile', action, label]);
	}else{
		//Alert a message when not in the live env
		alert('trackIT ' + 'Category: Mobile Action: ' + action + ' Label: ' + label);
	}
}

function externalLink(a) {
	if (!a.href) return false;
	
	//Track External Link
	trackIT('Outbound Link', a.href);
	
	var newWindow = window.open(a.href);

	return !newWindow;                               
}

function autoFormatPhone(keyUpEvent,phoneField){
	var uagent = navigator.userAgent.toLowerCase();
	var keynum;
	var keychar;
	var phone = phoneField.value;	
	if(keyUpEvent.which) // Netscape/Firefox/Opera/Chrome/Safari
	{
		keynum = keyUpEvent.which;
	}
	else if(window.event) // IE
	{
		keynum = keyUpEvent.keyCode;
	}
	keychar = String.fromCharCode(keynum)
	
	//If not backspace and not on android, then format
	if(keynum != 8  &&  uagent.search("android") == -1){
		// Strip out any extra characters that we do not need only keep numbers
		phone = phone.replace(/[^0-9]/gi, "");
		
		if(phone.length > 0 && phone.length < 3 ){
			phoneField.value = '(' + phone;
		}else if(phone.length >= 3 && phone.length < 6 ){
			phoneField.value = '(' + phone.substr(0,3) + ') ' + phone.substr(3,phone.length-3);
		}else if(phone.length >= 6 && phone.length < 10){
			phoneField.value = '(' + phone.substr(0,3) + ') ' + phone.substr(3,3) + '-' + phone.substr(6,phone.length-4);
		}else if(phone.length >= 10){
			phoneField.value = '(' + phone.substr(0,3) + ') ' + phone.substr(3,3) + '-' + phone.substr(6,4);
		}

	}
}

function setJobMessageBody(referenceNumber){
	var msgBody;
	msgBody = '<p>Below is the Sharp HealthCare job opportunity that you requested. To apply, select the Title and Job Details link.</p>';
	msgBody += document.getElementById('job-title-' + referenceNumber).innerHTML + '<br />';
	msgBody += document.getElementById('job-facility-' + referenceNumber).innerHTML + '<br />';
	msgBody += document.getElementById('job-city-' + referenceNumber).innerHTML + '<br />';
	msgBody += document.getElementById('job-reference-number-' + referenceNumber).innerHTML + '<br />';
	//Set msgBody
	document.getElementById('msg-reference-number').innerHTML = referenceNumber;
	document.getElementById('msg-body').innerHTML = msgBody;
}

function showTxtFormDialog(whatToTxt,apiKey){
	//Collect Phone
	$("#txtFormDialog").dialog({ bgiframe: true, zIndex: 10, modal: true, resizable: false, buttons: {"Send to My Phone": function() {txt(whatToTxt,document.getElementById('phone').value,apiKey);} } });
	// Remove focus on all buttons within the div with class ui-dialog
	$('.ui-dialog :button').blur();
}

function showEmailFormDialog(whatToEmail,apiKey){	
	//Collect Email
	$("#emailFormDialog").dialog({ bgiframe: true, zIndex: 10, modal: true, resizable: false, buttons: {"Send": function() {email(whatToEmail,document.getElementById('email').value,apiKey); } } });
	
	// Remove focus on all buttons within the div with class ui-dialog
	$('.ui-dialog :button').blur();
}

function showInsuranceDialog(){
	//Show Insurances
	$("#insuranceDialog").dialog({ bgiframe: true, zIndex: 10, modal: true, resizable: false });
	// Remove focus on all buttons within the div with class ui-dialog
	$('.ui-dialog :button').blur();
}

function email(whatToEmail, email, apiKey){
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		
	if(!emailPattern.test(email)){
		//Write Error
		document.getElementById('emailError').innerHTML='Please enter a valid email.';
	}else{
		//Try and send eamil
		var to = email;
		var message = document.getElementById('msg-body').innerHTML;
		
		switch(whatToEmail.toLowerCase()){
			case "job":{
				var subject = 'Sharp Job Information';
				//trackIT Label
				var label = 'Email: Job-' + document.getElementById('msg-reference-number').innerHTML;
				var from = sharpJobsContactEmail;
				break;
			}
			case "location":{
				var subject = 'Sharp Location Information';
				//trackIT Label
				var label = 'Email: Location-' + document.getElementById('location-name').innerHTML;
				var from = sharpMobileContactEmail;
				break;
			}
		}
		
		//trackIT
		trackIT('Email',label);
		
		//Send Email
		sendEmail(to,from,subject,message,apiKey);
		
		//Clear Email
		document.getElementById('email').value='';
		//Close Dialog
		$("#emailFormDialog").dialog("close");
	}
}

function txt(whatToTxt,phone,apiKey){
	if(phone.length != 14){
		//Write Error
		document.getElementById('phoneError').innerHTML='Please enter a valid phone number.';
	}else{
		//Try and send txt
		// Strip out any extra characters that we do not need only keep numbers
		var phone = phone.replace(/[^0-9]/gi, "");
		var to = phone;
		var message = document.getElementById('msg-body').innerHTML;
		
		switch (whatToTxt.toLowerCase()) {
			case "location":{
				//trackIT Label
				var label = 'Text: ' + document.getElementById('location-name').innerHTML;
				var from = sharpMobileContactEmail;
				break;
			}
		}
		
		//trackIT
		trackIT('Text', label);
		
		//Send TXT
		sendTxt(to,message,apiKey);
		
		//Clear Phone
		document.getElementById('phone').value='';
		
		//Close Dialog
		$("#txtFormDialog").dialog("close");
	}

}

function sendTxt(to, message, apiKey){	
	//Use JQuery to make an Ajax Call to send txt
	$.ajax({url:"/webservices/ajax.cfc?method=txtMe&returnformat=json", data:({"to":to, "message":message, "apiKey":apiKey }), dataType:"json", success: txtSent});
}

function txtSent(data, textStatus, XMLHttpRequest){
	if(data.SUCCESSFUL){
		$("#confirmationDialog").dialog({ bgiframe: true, zIndex: 10, modal: true, resizable: false });
	}
}

function sendEmail(to, from, subject, message, apiKey){	
	//Use JQuery to make an Ajax Call to send email
	$.ajax({url:"/webservices/ajax.cfc?method=emailMe&returnformat=json", data:({"to":to, "from":from, "subject":subject, "message":message, "apiKey":apiKey }), dataType:"json", success: emailSent});
}

function emailSent(data, textStatus, XMLHttpRequest){
	if(data.SUCCESSFUL){
		$("#confirmationDialog").dialog({ bgiframe: true, zIndex: 10, modal: true, resizable: false });
	}
}
