// -----------------------------------------------------
// TalkNow template javascript for customer integration. 
// This is intended as an example for customers to follow to add TalkNow to their website.
// Copyright Lekane 2010
// -----------------------------------------------------

var personalNew = false;

// Create the validator once we have loaded the page fragment (dependant on if the TalkNow server responds)
var sidebarValidator;
Validation.add('sidebarPhone', 'Please enter a valid phone number (digits only)',  function (v) { return /^[+]?[0-9\s]{6,13}$/.test(v)});

// -----------------------------------------------------

/**
 * This is where you define what you want to do when you receive a response to the callback request.
 * @param caller is who will be calling the web user back, or null if the request timed out.
 */
var mySidebarCallRequestCallback = function callRequestCallback( caller, callerId, callerPic ) {
	$('sb_talkNowHeader').innerHTML = '';

	var messageText = $('sb_talkNowMessage');
	
	// If the caller hasn't been set it comes through to javascript as null
	if( caller != null && caller != '' ) {
	 	// Someone from the requested group has accepted the call request and will call the customer back
	 	messageText.innerHTML = '<table><tr><td><img src="' + callerPic +'"></td><td>Thank you for contacting us.<br><br>' + caller + ' will call you back now.</td></tr></table>'; 	
	} else {
	 	// No one has accepted the call request within the time period, so we display that someone will contact them
	 	messageText.innerHTML = '<p>Unfortunately we are busy, but we will call you back as soon as possible.</p><br><br><br><br>';
	}
};
/**
 * This is where you define what you want to do when you receive a response to the personal offer details request.
 */
var mySidebarPersonalOfferDetailsCallback = function personalOfferDetailsCallback( callerPic, callerName, callerRole ) {
	if( null != callerName ) {
		$('personalImg').setAttribute('src', callerPic);
		$('personalName').innerHTML = callerName;

		if( null != callerRole && '' != callerRole) {
			$('personalRole').innerHTML = '(' + callerRole + ')';
		} else {
			$('personalRole').innerHTML = '';
		}

		if( personalNew ) {
			Effect.SlideDown('sb_tn');
		} else {
			$('sb_tn').show();
		}	
	}
};

/**
 * An example function that shows the format of the data to send to TalkNow server.
 * This example takes some data that was generated on the server side of the form key and value's
 * stored in an Array. It adds in the phone number entered on the web page.
 * This technique can be used to add any data input by the user that are required to be broadcast to the mobile phones.
 */
function sendSidebarRequest() {
	// Manually run the validator to see if we should submit
	var isValid = sidebarValidator.validate();

	if (isValid == false || $F('sb_phoneNumber').length == 0) {
		if (isValid) {
			// IE 5.5 is not properly supported by prototype and the validator just tell the user to type a phone number 
			alert('Please enter a valid phone number (digits only)');
		}
		return;
	}

	// Get the phone number from the form and append to the end of other data.
	var phoneNumberTextField = $('sb_phoneNumber');
	sidebarData[0][sidebarData[0].length] = 'phoneNumber';
	sidebarData[1][sidebarData[1].length] = phoneNumberTextField.value;
	
	$('sb_talkNowMessage').innerHTML = '';
	$('sb_talkNowHeader').innerHTML = '<p><img src="' + localTalkNowURL + 'images/ajax-loader.gif"/>&nbsp;&nbsp;Contacting Lekane...</p><br><p>Please wait, this should only take a few moments.</p><br><br>';

    // Remove form to minimize double submits.
    var talkNowForm = $('sb_talkNowForm');
    talkNowForm.parentNode.removeChild( talkNowForm );
	
	// Call TalkNow send request
	sendCallRequest( "", sidebarData, sidebarMetadata, mySidebarCallRequestCallback );
}

function initSidebarTalkNow() {	
	$('sb_tn').hide();
	// Replace the talknow div with content
	new Ajax.Updater('sb_talknow', localTalkNowURL+'/talknow-sidebar.frag', {
		 onComplete: function( done ) {
			// Wire up the validator
			sidebarValidator = new Validation('sb_talkNowForm', {immediate : true}); 	
		  }	
	});
}

function entsub( event, srcForm ) {
  if ( event && event.which == 13 && sidebarValidator.validate() )
    sendSidebarRequest();
  else
    return true;
}

function action2() {
  if( enableForPage() ) {
    Effect.SlideDown('sb_tn');
  }
}

function action3() {
  if( enableForPage() ) {
    $('sb_tn').show();
  }
}

function action4() {
	if( enableForPage() ) {
		personalNew = true;
		loadPersonalSidebar();
	}
}

function action5() {
	if( enableForPage() ) {
		personalNew = false;
		loadPersonalSidebar();
	}
}

function loadPersonalSidebar() {
  	// Replace the talknow div with content
	new Ajax.Updater('sb_talknow', localTalkNowURL+'/talknow-sidebar-personal.frag', {
		 onComplete: function( done ) {
			// Wire up the validator
			sidebarValidator = new Validation('sb_talkNowForm', {immediate : true}); 
		  }	
	});
	getPersonalOfferDetails( mySidebarPersonalOfferDetailsCallback );
}

function enableForPage() {
	var url = location.href;
	var paramsIndex = url.indexOf('?');
	if( -1 != paramsIndex ) {
		url = url.substring(0, paramsIndex);
	}
	return url != 'http://www.lekane.com/';
}



