function test1 () {
	//publickey = $('recaptcha_div').readAttribute('publickey');
	publickey = '6LeWqwQAAAAAAOJJHBb-8OJpIP-KO4zBxSu6cO7I';	// 192.168.0.3
	//Recaptcha.create(publickey, 'recaptcha_div', { theme: 'custom' });
	Recaptcha.create(publickey, 'testdiv', { theme: 'red' });
}

function notifyChanges(customerid) {
	// hide the list of entries, show the mod entry fields, load the requested customer details
	// load the captcha fields
	e1 = $('entrylisttable');
	e1.hide();
	$('customeridfield').value = customerid;
	if (approvedAgents()) {	$('approvedagentstable').show(); }
	else {	$('approvedagentstable').hide();}
	e1 = $('modifyentry');
	e1.show();
	getExistingDetails(customerid, 'existingdetails');
	publickey = $('recaptcha_div').readAttribute('publickey');
	Recaptcha.create(publickey, 'recaptcha_div', {
		theme: 'custom',
		callback: Recaptcha.focus_response_field
	});
	return false;
}
function getExistingDetails(customerid, existingdetails) {
	e1 = $(existingdetails);
	e1.innerHTML = 'Loading ... <img src="/jpg/ajaxloader.gif" />';

	var url = '/samex/index.php';
	var pars = 'option=com_directory&view=entry2&customerid=' + customerid + '&format=raw';
   	//var myAjax = new Ajax.Request(url, { method: 'get', parameters: pars,
   	new Ajax.Request(url, { method: 'get', parameters: pars,
   		onSuccess: function(request) {
   			var json = request.responseText.evalJSON();
			jsondata = json['data'];
			e1 = $(existingdetails);
			e1.innerHTML = jsondata['clientdata'];
 	    },
    	onFailure: function(request) {
			e1 = $(existingdetails);
			e1.innerHTML = 'Unable to get information for customer ' + customerid;
		}
   	    });
}

function approvedAgents() {
	return $('modifyentry').getAttribute('approvedagents') == '1';
}

function processModifyEntryForm() {
	if (!validateForm()) { return false; }
    $('submitbtn').disabled = true;
    $('submitbtn').value = "Please Wait...";
    $('modifyentryform').request({
      onSuccess: function(transport) {
   		var json = transport.responseText.evalJSON();
		jsondata = json['data'];
		if (jsondata['result'] == '1') {
          	alert('Your message has been sent!');
          	if (jsondata['sponsoredlink'] != '1') {
          		window.location = '/samex/index.php/component/directory/purchase/' + jsondata['customerid'];
        	}
        	else {
	       		cancelModify();
       		}
        } else {
        	alert(jsondata['result']);
        }
        $('submitbtn').value = 'Submit';
        $('submitbtn').disabled = false;
      },
      onFailure: function(transport) {
      	alert('Request failed ' + transport.statusText);
        $('submitbtn').value = 'Submit';
        $('submitbtn').disabled = false;
      	}
    });
	return false;
}

function validateForm() {
	// must enter either corrections or a licence number
	var e1 = $('corrections')
	var s1 = trim(e1.value) + trim($('hajjlicenceno').value);
	if (s1 == '') {
		alert('Please enter Corrections or Cancel');
		e1.focus();
		return false;
	}
	if (e1.value.length > 500) {
		alert('Please shorten your corrections.');
		e1.focus();
		return false;
	}

	if (approvedAgents()) {
		var e1 = $('confirmapproved')
		if (!e1.checked) {
			alert('Please confirm your status as an approved agency');
			e1.focus();
			return false;
		}
	}

	if (Recaptcha.get_response() == '') {
		alert('Please enter the Security Code');
		Recaptcha.focus_response_field();
		return false;
	}
	return true;
}

function cancelModify() {
	e1 = $('modifyentry');
	e1.hide();
	Recaptcha.destroy();
	e1 = $('existingdetails');
	e1.innerHTML = 'Loading ... <img src="/jpg/ajaxloader.gif" />';
	e1 = $('entrylisttable');
	e1.show();
	return false;
}

function purchaseEntry(customerid) {
	// if an approved agent, show the confirm licence fields then move on to the purchase prompts
	// if not an approved agent, just show the purchase prompts
	$('customeridfield').value = customerid;
	if (approvedAgents()) {
		e1 = $('entrylisttable');
		e1.hide();
		getExistingDetails(customerid, 'confirmstatusexistingdetails');
		$('confirmstatushajjlicenceno').value = '';
		$('confirmstatusapproved').checked = false;
		e1 = $('confirmstatus');
		e1.show();
	}
	else {
		purchaseEntry2();
	}
	return false;
}


function purchaseEntry2() {
	var customerid = $('customeridfield').value;
	window.location = '/samex/index.php?option=com_directory&view=purchase&customerid=' + customerid;
}

function cancelConfirmStatus() {
	e1 = $('confirmstatus');
	e1.hide();
	e1 = $('entrylisttable');
	e1.show();
	//return false;
}
function processConfirmStatusForm() {
	// make sure the licence is entered and confirmed
	// send email to samirad notifying confirmation then switch to purchase form
	var e1 = $('confirmstatushajjlicenceno');
	var sLicence = trim(e1.value);
	if (sLicence == '') {
		alert('Please enter Licence Number or Cancel');
		e1.focus();
		return false;
	}
	var e1 = $('confirmstatusapproved')
	if (!e1.checked) {
		alert('Please confirm your status as an approved agency or Cancel');
		e1.focus();
		return false;
	}
	// ajax call to post confirmation to table
    $('submitbtn').disabled = true;
    $('submitbtn').value = "Please Wait...";

	var customerid = $('customeridfield').value;
	var url = '/samex/index.php';
	var pars = 	'option=com_directory&view=confirmstatus&customerid=' + customerid + '&format=raw' +
                '&hajjlicenceno=' + sLicence + '&confirmapproved=1';
    new Ajax.Request(url, { method: 'get', parameters: pars,
   		onSuccess: function(request) {
	   		var json = request.responseText.evalJSON();
			jsondata = json['data'];
			if (jsondata['result'] == '1') {
				// contnue to the buy screen
				purchaseEntry2();
       		}
        	else {
        		alert(jsondata['result']);
        	}
	        $('submitbtn').value = 'Submit';
    	    $('submitbtn').disabled = false;
 	    },
    	onFailure: function(request) {
    		alert('Unable to confirm status ' + request.statusText);
	        $('submitbtn').value = 'Submit';
    	    $('submitbtn').disabled = false;
		}
  	});
	return false;
}
function notifyChanges2() {
	// notify changes clicked from confirmstatus screen, retrieve customer id from hidden field etc
	cancelConfirmStatus();
	customerid = $('customeridfield').value;
	notifyChanges(customerid);
}




