//<![CDATA[
var myreqXML;
var editor_textfield_text = new Array();
var editor_htmlfield_html = new Array();
var editor_imagefield_src = new Array();
var thistextdiv = '';
var thishtmldiv = '';
var thisimagediv = '';
var thisimageparentdiv = '';
var thismapdiv = '';
var textfield_text = '';
var htmlfield_html = '';
var imagefield_src = '';
var mapfield_text = '';
var thisimagesrc = '';
var currentimagehtml = '';
var advertid = 0;
var revisions = new Array();
var thisrevertdiv = '';
var revision_count = 0;
var revision_orig_pos = 0;
var revision_pos = 0;

/* COMMON FUNCTIONS */

function urlencode(str) {
	return escape(str).replace(/\+/g,'%2B').replace(/%20/g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

function urldecode(str) {
	return unescape(str.replace('+', ' '));
}

/* LOGIN FUNCTIONS */

function password_reminder() {
	var email_address = document.getElementById('email_address').value;
	$.ajax({
		type: 'POST',
		url: '/cms/password_reminder.php',
		contentType: 'application/x-www-form-urlencoded',
		cache: false,
		data: { action: 'retrieve', email_address: email_address },
		dataType: 'text',
		success: function(response) {
			var password_reminder_status = response;
			if (password_reminder_status == 1)
				password_reminder_text = 'Thank you. Your login details have been sent to you via email.<br />Please click the \'Return to Login\' button to login.';
			else
				password_reminder_text = 'Your details have not been found. Please try again.';
			$('#status_text').html(password_reminder_text);
		}
	});
}

/*function password_reminder_returnbit() {
	var password_reminder_text = '';
	if (myreqXML.readyState == 4) {
		if (myreqXML.status == 200) {
			var password_reminder_status = myreqXML.responseText;
			if (password_reminder_status == 1)
				password_reminder_text = 'Thank you. Your login details have been sent to you via email.<br />Please click the \'Return to Login\' button to login.';
			else
				password_reminder_text = 'Your details have not been found. Please try again.';
			$('#status_text').html(password_reminder_text);
		}
	}
}*/

/* DATE FUNCTIONS */

function getMonthNum(abbMonth) {
	var arrMon = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	for(i=0;i<arrMon.length; i++) {
		if(abbMonth == arrMon[i])
			return i+1;
	}
}

function timeToHuman(unixtimestamp) {
	var theDate = new Date(unixtimestamp * 1000);
	dateString = theDate.toGMTString();
	arrDateStr = dateString.split(' ');
  humanDate = arrDateStr[1]+'/'+getMonthNum(arrDateStr[2])+'/'+arrDateStr[3]+' '+arrDateStr[4].substr(0,2)+':'+arrDateStr[4].substr(3,2)+':'+arrDateStr[4].substr(6,2);
  return humanDate;
}

/* POPUPS WINDOWS */

$(document).ready (
	function() {
		$('#thumbWindow').jqm({
  		modal: true,
  		trigger: 'a.thumbWindowTrigger',
  		toTop: true,
  		overlay: 75,
  		overlayClass: 'whiteOverlay'})
		$('#profileWindow').jqm({
  		modal: true,
  		trigger: 'a.profileWindowTrigger',
  		toTop: true,
  		overlay: 75,
  		overlayClass: 'whiteOverlay'})
		$('#statsWindow').jqm({
  		modal: true,
  		trigger: 'a.statsWindowTrigger',
  		toTop: true,
  		overlay: 75,
  		overlayClass: 'whiteOverlay'})
		$('#invoicesWindow').jqm({
  		modal: true,
  		trigger: 'a.invoicesWindowTrigger',
  		toTop: true,
  		overlay: 75,
  		overlayClass: 'whiteOverlay'})
		$('#editorWindow').jqm({
  		modal: true,
  		trigger: 'a.editorWindowTrigger',
  		toTop: true,
  		overlay: 75,
  		overlayClass: 'whiteOverlay'})
	}
);

/* AJAX POST FUNCTION */

function post(myurl,params,where,returnfunction) {
	ready = false;
	if (window.XMLHttpRequest) {
		myreqXML = new XMLHttpRequest();            // set the request
		if (where == 1) {
			var returnFn = eval(returnfunction);
			myreqXML.onreadystatechange = returnFn;    // function to call on each set
		}
		myreqXML.open('POST', myurl, true);          // set the page to request
		myreqXML.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
		myreqXML.setRequestHeader('Content-length', params.length);
		myreqXML.setRequestHeader('Connection', 'close');
		myreqXML.send(params);                     // initialize the request 
	}	else if (window.ActiveXObject) {
		myreqXML = new ActiveXObject('Microsoft.XMLHTTP');
		if (myreqXML) {
			if (where == 1) {
				var returnFn = eval(returnfunction);
				myreqXML.onreadystatechange = returnFn;  // function to call on each step
			}
			myreqXML.open('POST', myurl, true);       // set the page to request
			myreqXML.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			myreqXML.setRequestHeader('Content-length', params.length);
			myreqXML.setRequestHeader('Connection', 'close');
			myreqXML.send(params);                     // initialize the request
		}
	} else
		window.location.href = myurl;
}

/* CLIENT FUNCTIONS */

function email_user_email(invoiceid) {
	$.get('/cms/invoice_email.php', { invoiceid: invoiceid, type: 'user' }, function(response) {
		if (response == 'OK') {
			alert('Email sent.');
		}
	});
}

function save_payment_method(invoiceid, paymethod) {
	$.ajax({
		type: 'POST',
		url: '/cms/index.php',
		contentType: 'application/x-www-form-urlencoded',
		cache: false,
		data: { action: 'save_payment_method', invoiceid: invoiceid, paymethod: paymethod },
		success: function(response) {}
	});
}

function save_payment_method_sent(invoiceid, paymethod) {
	$.ajax({
		type: 'POST',
		url: '/cms/index.php',
		contentType: 'application/x-www-form-urlencoded',
		cache: false,
		data: { action: 'save_payment_method_sent', invoiceid: invoiceid, paymethod: paymethod },
		success: function(response) {}
	});
}

function count_caps(str) {
	var regexp = /[^\b][A-Z]{3,}/g;
	var matchList = str.match(regexp);
	if (matchList !== null) {
		var len = matchList.length;
	} else {
		var len = 0;
	}
	return len;
}

function remove_link(advertpageid) {
	$('#link_'+advertpageid).val('');
	$('#link_before_'+advertpageid).val('');
	$.ajax({
		type: 'POST',
		url: '/cms/index.php',
		contentType: 'application/x-www-form-urlencoded',
		cache: false,
		data: { action: 'remove_link', advertpageid: advertpageid },
		dataType: 'text',
		success: function(response) {
			advertpageid = response;
			// these no longer apply to the advert:
			$('#expired_'+advertpageid).hide();
			$('#live_msg_'+advertpageid).hide();
			// tell the user wos on
			$('#savemsg'+advertpageid).html('<span style="color: red; font-weight: bold">Removed successfully</span>');	
		}
	});
}

function save_link(advertpageid) {
	var link_text_value = $('#link_'+advertpageid).val();
	var link_before_text_value = $('#link_before_'+advertpageid).val();
	var link_text_value_length = link_text_value.length;
	if (link_text_value_length == 0) {
		alert('Please enter a description.');
		return false;
	} else if (count_caps(link_text_value) > 0) {
		alert('Please do not use uppercase words in your description. Please correct before saving.');
		return false;
	} else if (link_text_value_length > 420) {
		alert('Sorry your description is too long. We allow 3 lines with 420 characters.');
		return false;
	} else {
		$('#link_before_'+advertpageid).val(link_text_value);
		$.ajax({
			type: 'POST',
			url: '/cms/index.php',
			contentType: 'application/x-www-form-urlencoded',
			cache: false,
			data: { action: 'save_link', advertpageid: advertpageid, link: link_text_value, link_before: link_before_text_value },
			dataType: 'text',
			success: function(response) {
     		tmp = response.split('|');
				advertpageid = tmp[0];
				posted_on = tmp[1];
				rem = tmp[2];
				$('#expired_'+advertpageid).hide();
				$('#live_msg_'+advertpageid).show();
				$('#posted_on_'+advertpageid).html(posted_on);
				$('#remaining_'+advertpageid).html(rem);
				$('#savemsg'+advertpageid).html('<span style="color: red; font-weight: bold">Saved successfully</span>');				
			}
		});
	}
}

function save_link_main(advertid) {
	var link_text_value = $('#link_'+advertid).val();
	var link_text_value_length = link_text_value.length;
	var link_before_text_value = $('#link_before_'+advertid).val();
	if (link_text_value_length == 0) {
		alert('Please enter a description.');
		return false;
	} else if (count_caps(link_text_value) > 0) {
		alert('Please do not use uppercase words in your description. Please correct before saving.');
		return false;
	} else if (link_text_value_length > 420) {
		alert('Sorry your description is too long. We allow 3 lines with 420 characters.');
		return false;
	} else {
		$('#link_before_'+advertid).val(link_text_value);
		$.ajax({
			type: 'POST',
			url: '/cms/index.php',
			contentType: 'application/x-www-form-urlencoded',
			cache: false,
			data: { action: 'save_link_main', advertid: advertid, link: link_text_value, link_before: link_before_text_value },
			dataType: 'text',
			success: function(response) {
				advertid = response;
				$('#savemsg'+advertid).html('<span style="color: red; font-weight: bold">Saved successfully</span>');	
			}
		});
	}
}

function save_link_signup() {
	link_text_value = $('#link').val();
	advertid = $('#advertid').val();
	if (link_text_value.length == 0) {
		alert('Please enter a description.');
		return false;
	} else if (link_text_value.length > 420) {
		alert('Sorry your description is too long. We allow 3 lines with 420 characters.');
		return false;
	} else {
		params = 'action=save&advertid='+advertid+'&link='+link_text_value;
		post('/cms/signup_savelinktext.php', params, 1, 'linktextsignup_returnbit');
	}
}

/*function linktext_returnbit() {
	if (myreqXML.readyState == 4) {
		if (myreqXML.status == 200) {
			
			tmp = myreqXML.responseText.split('|');
			advertpageid = tmp[0];
			posted_on = tmp[1];
			rem = tmp[2];
			
			$('#expired_'+advertpageid).hide();
			
			$('#live_msg_'+advertpageid).show();
			$('#posted_on_'+advertpageid).html(posted_on);
			$('#remaining_'+advertpageid).html(rem);
			
			$('#savemsg'+advertpageid).html('<span style="color: red; font-weight: bold">Saved successfully</span>');
			
		}
	}
}*/

// remove adv
/*function linktext_returnbit3() {
	if (myreqXML.readyState == 4) {
		if (myreqXML.status == 200) {
			advertpageid = myreqXML.responseText;
		
			// these no longer apply to the advert:
			$('#expired_'+advertpageid).hide();
			$('#live_msg_'+advertpageid).hide();
			
			// tell the user wos on
			$('#savemsg'+advertpageid).html('<span style="color: red; font-weight: bold">Removed successfully</span>');
		}
	}
}*/

/*function linktext_returnbit2() {
	if (myreqXML.readyState == 4) {
		if (myreqXML.status == 200) {
			advertid = myreqXML.responseText;
			//$('#save_main_message_'+advertid).html('<span style="color: red; font-weight: bold">Saved Successfully</span>');
			$('#savemsg'+advertid).html('<span style="color: red; font-weight: bold">Saved successfully</span>');
		}
	}
}*/

function linktextsignup_returnbit() {
	if (myreqXML.readyState == 4) {
		if (myreqXML.status == 200) {
			result = myreqXML.responseText;
			if (result == 'OK') {
				$('#savemsg').html('<span style="color: red; font-weight: bold">Saved successfully</span>');
			}
		}
	}
}

function toggle_save_main(advertid) {
	$('#save_main_message_'+advertid).html('[<a onmouseover="this.style.cursor=\'pointer\'" class="standardlink" onclick="save_link_main('+advertid+')">Click To Save</a>]');
}

// JG: enable save button:
function enable_save(advertpageid) {
	$('#save_'+advertpageid).removeAttr("disabled");
}

function toggle_save(advertpageid) {
	$('#savemsg'+advertpageid).html('[<a onmouseover="this.style.cursor=\'pointer\'" class="standardlink" onclick="save_link('+advertpageid+')">Click To Save</a>]');
}

function toggle_save_signup() {
	$('#savemsg').html('<a onmouseover="this.style.cursor=\'pointer\'" onclick="save_link_signup()">[Click To Save]</a>');
	$('#savemsg').show();
}

function updateThumbWindow(advertid) {
	$('#advert_thumb').attr('src', '/advert_thumb_'+advertid+'.jpg?rand='+(Math.random()*10000000));
	$('#window_status').html('');
	document.form_change_thumb.advertid.value = advertid;
}

function closeThumbWindow() {
	var advertid = document.form_change_thumb.advertid.value;
	$('#thumb_image'+advertid).attr('src', '/advert_thumb_'+advertid+'.jpg?rand='+(Math.random()*10000000));
}

function updateEditorWindow(advertid) {
	$('#advertid').val(advertid);
	updateThemePicklist(advertid);
	$('#editorContent').load('/cms/editor.php div#content', { advertid: advertid }, function(){});
}

function updateThemePicklist(advertid) {
	$.get('/cms/advert_themes.php', { action: 'load', advertid: advertid }, function(response) {
		$('#themeSelect').html(response);
	});
}

function openStatsWindow(advertid) {
	$('#statsContent').load('/cms/stats.php div#content', { advertid: advertid }, function(){});
}

function openInvoicesWindow(advertid) {
	$('#invoicesContent').load('/cms/invoices.php div#content', { advertid: advertid }, function(){});
}

/* IMAGE UPLOAD FUNCTIONS */

function startUpload() {
	$('#thumb_upload_process').show();
	return true;
}

function startAdvertImageUpload() {
	$('#thumb_advert_image_upload_process').show();
	return true;
}

function stopAdvertImageUpload(success) {
	$('#thumb_advert_image_upload_process').hide();
	var advertid = $('#advertid').val();
	var current_image_src = $('#'+thisimagediv).attr('src');
	$('#'+thisimagediv).attr('src', current_image_src + '?rand='+(Math.random()*10000000));
	editor_cancel_imagefield();
}

function stopUpload(success) {
	if (success == 1) {
		$('#window_status').html('The thumbnail was uploaded successfully!<br /><br />');
		var advertid = document.form_change_thumb.advertid.value;
		$('#advert_thumb').attr('src', '/advert_thumb_'+advertid+'.jpg?rand='+(Math.random()*10000000));
	} else {
		$('#window_status').html('There was an error during thumbnail upload!<br /><br />');
	}
	$('#thumb_upload_process').hide();
}

/* EDITOR FUNCTIONS */
/* USES JQUERY JS FRAMEWORK WHERE POSSBILE TO DEAL WITH CROSS-BROWSER COMPATIBILITY ISSUES */

function close_editor_bar() {
	$('#okcancel').remove();
	$('#changeddate').remove();
}

function revision_position(dir, fieldtype) {
	$('#redoimage').attr('disabled', null);
	$('#undoimage').attr('disabled', null);
	$('#undoimage').attr('src', '/cms/images/toolbar_undo.png');
	$('#redoimage').attr('src', '/cms/images/toolbar_redo.png');
	if (dir == 'up') {
		revision_pos++;
		if (revision_pos == (revision_count - 1)) {
			$('#redoimage').attr('src', '/cms/images/toolbar_redo_disabled.png');
			$('#redoimage').attr('disabled', 'true');
		}
	} else {
		revision_pos--;
		if (revision_pos == 0) {
			$('#undoimage').attr('src', '/cms/images/toolbar_undo_disabled.png');
			$('#undoimage').attr('disabled', 'true');
		}
	}
	if (fieldtype == 'image') {
		var advertid = $('#advertid').val();
		var imagenum = thisimagediv.replace('image', '');
		html_data = revisions[revision_pos];
		changed_date_html = '<div id="changeddate">Changed On '+timeToHuman(html_data)+'</div>';
		$('#changeddate').remove();
		$('#'+thisimagediv).attr('src', '/advert_image_'+advertid+'_'+imagenum+'_'+html_data+'.jpg');
		$('#'+thisimageparentdiv).after(changed_date_html);
	} else {
		html_data = revisions[revision_pos].split('|');
		changed_date_html = '<div id="changeddate">Changed On '+timeToHuman(html_data[0])+'</div>';
		$('#changeddate').remove();	
		if (fieldtype == 'html') {
			$('#'+thishtmldiv).html(html_data[1]);
			var oFCKeditor = FCKeditorAPI.GetInstance(thishtmldiv);
			oFCKeditor.SetHTML(html_data[1]);
			$('#'+thishtmldiv).after(changed_date_html);
		} else {
			$('#textfield_value').attr('value', html_data[1]);
			$('#'+thistextdiv).text(html_data[1]);
			$('#'+thistextdiv).after(changed_date_html);
		}
	}
}

function field_changes(fieldtype) {
	var advertid = $('#advertid').val();
	if (fieldtype == 'image')
		thisdiv = thisimagediv;
	else if (fieldtype == 'html')
		thisdiv = thishtmldiv;
	else
		thisdiv = thistextdiv;
	$.get('/cms/token_changes.php', { token: thisdiv, advertid: advertid }, function(data) {
  	revisions = new Array();
  	revisions = data.split('[DIVIDER]');
		revision_count = revisions.length;
		revision_pos = revision_count - 1;
		revision_orig_pos = revision_pos;
		// DISABLE REDO INITIALLY
		$('#redoimage').attr('src', '/cms/images/toolbar_redo_disabled.png');
		$('#redoimage').attr('disabled', 'true');
		// DISABLE UNDO INITIALLY IF NO REVISIONS
		if (revision_count == 1) {
			$('#undoimage').attr('src', '/cms/images/toolbar_undo_disabled.png');
			$('#undoimage').attr('disabled', 'true');
		}
	});
}

/* TEXT FIELDS */

function editor_textfield_onmouseover(caller) {
	var divid = caller.id;
	$('#'+divid).css('background-color', '#FFFF00');
	$('#'+divid).css('cursor', 'pointer');
	$('#'+divid).css('overflow', 'hidden');
}

function editor_textfield_onmouseout(caller) {
	var divid = caller.id;
	$('#'+divid).css('background-color', '');
	$('#'+divid).css('cursor', 'default');
	$('#'+divid).css('overflow', 'hidden');
}

function editor_textfield_onclick(caller) {
	close_editor_bar();
	var divid = caller.id;
	thistextdiv = divid;
	$('#'+thistextdiv).css('background-color', '');
	$('#'+thistextdiv).css('cursor', 'default');
	textfield_text = $('#'+thistextdiv).text();
	editor_textfield_text[thistextdiv] = textfield_text;
	okcancel_html = '<div id="okcancel"><input id="textfield_value" type="text" value="'+textfield_text+'" class="textfield" />&nbsp;&nbsp;<input id="undoimage" type="image" src="/cms/images/toolbar_undo.png" class="toolbaricon" onclick="revision_position(\'down\', \'text\')">&nbsp;&nbsp;<input id="redoimage" type="image" src="/cms/images/toolbar_redo.png" class="toolbaricon" onclick="revision_position(\'up\', \'text\')">&nbsp;&nbsp;<input type="image" src="/cms/images/toolbar_save.png" style="width: 20px; height: 20px; border: none" onclick="editor_save_textfield()" />&nbsp;&nbsp;<input type="image" src="/cms/images/toolbar_close.png" class="toolbaricon" onclick="editor_cancel_textfield(true)" /></div>';
	$('#'+thistextdiv).after(okcancel_html);
	// GET CHANGES
	field_changes('text');
}

function editor_cancel_textfield(reset) {
	if (reset)
		$('#'+thistextdiv).text(editor_textfield_text[thistextdiv]);
	close_editor_bar();
}

function editor_save_textfield() {
	var advertid = $('#advertid').val();
	$('#'+thistextdiv).css('background-color', '');
	$('#'+thistextdiv).css('cursor', 'default');
	textfield_text = urlencode($('#textfield_value').val());
	if ((revision_pos != revision_orig_pos) && textfield_text != editor_textfield_text[thistextdiv]) {
		html_data = revisions[revision_pos].split('|');
		timestamp = html_data[0];
		$.get('/cms/token_changes.php', { action: 'save', timestamp: timestamp, token: thistextdiv }, function(data) {
			if (data == 'OK') {
				html_value = html_data[1];
				$('#'+thistextdiv).text(html_value);
				editor_cancel_textfield();
			} else {
				alert('There was a problem saving to this revision!');
			}
		});
	} else {
		params = 'action=save_textfield&advertid='+advertid+'&token='+thistextdiv+'&textfield='+textfield_text; // .replace(/\+/g, "%2b")
		$('#'+thistextdiv).html('<img src="/cms/images/loading.gif" style="border: none" />');
		post('/cms/index.php', params, 1, 'savetextfield_returnbit');
		editor_cancel_textfield();
	}
}

function savetextfield_returnbit() {
	if (myreqXML.readyState == 4) {
		if (myreqXML.status == 200) {
			$('#'+thistextdiv).text(myreqXML.responseText);
		}
	}
}

/* HTML FIELDS */

function editor_htmlfield_onmouseover(caller) {
	var divid = caller.id;
	$('#'+divid).css('background-color', '#FFFF00');
	$('#'+divid).css('cursor', 'pointer');
}

function editor_htmlfield_onmouseout(caller) {
	var divid = caller.id;
	$('#'+divid).css('background-color', '');
	$('#'+divid).css('cursor', 'default');
}

function editor_htmlfield_onclick(caller) {
	close_editor_bar();
	var divid = caller.id;
	thishtmldiv = divid;
	$('#'+thishtmldiv).css('background-color', '');
	$('#'+thishtmldiv).css('cursor', 'default');
	htmlfield_html = $('#'+thishtmldiv).html();
	editor_htmlfield_html[thishtmldiv] = htmlfield_html;
	var oFCKeditor = new FCKeditor(thishtmldiv);
	oFCKeditor.BasePath = '/cms/libs/fckeditor/';
	oFCKeditor.Height = '300';
	okcancel_html = '<div id="okcancel">'+oFCKeditor.CreateHtml()+'&nbsp;&nbsp;<input id="undoimage" type="image" src="/cms/images/toolbar_undo.png" class="toolbaricon" onclick="revision_position(\'down\', \'html\')">&nbsp;&nbsp;<input id="redoimage" type="image" src="/cms/images/toolbar_redo.png" class="toolbaricon" onclick="revision_position(\'up\', \'html\')">&nbsp;&nbsp;<input type="image" src="/cms/images/toolbar_save.png" class="toolbaricon" onclick="editor_save_htmlfield()" />&nbsp;&nbsp;<input type="image" src="/cms/images/toolbar_close.png" class="toolbaricon" onclick="editor_cancel_htmlfield(true)" /></div>';
	$('#'+thishtmldiv).after(okcancel_html);
	field_changes('html');
}

function editor_cancel_htmlfield(reset) {
	if (reset)
		$('#'+thishtmldiv).html(editor_htmlfield_html[thishtmldiv]);
	close_editor_bar();
}

function editor_save_htmlfield(caller) {
	var advertid = $('#advertid').val();
	$('#'+thishtmldiv).css('background-color', '');
	$('#'+thishtmldiv).css('cursor', 'default');
	var oFCKeditor = FCKeditorAPI.GetInstance(thishtmldiv);
	htmlfield_html = oFCKeditor.GetHTML();
	if ((revision_pos != revision_orig_pos) && textfield_text != editor_textfield_text[thistextdiv]) {
		html_data = revisions[revision_pos].split('|');
		timestamp = html_data[0];
		$.get('/cms/token_changes.php', { action: 'save', timestamp: timestamp, token: thishtmldiv }, function(data) {
			if (data == 'OK') {
				html_value = html_data[1];
				$('#'+thishtmldiv).html(html_value);
				editor_cancel_htmlfield();
			} else {
				alert('There was a problem saving to this revision!');
			}
		});
	} else {
		params = 'action=save_htmlfield&advertid='+advertid+'&token='+thishtmldiv+'&htmlfield='+urlencode(htmlfield_html); // .replace(/\+/g, "%2b")
		$('#'+thishtmldiv).html('<img src="/cms/images/loading.gif" style="border: none" />');
		post('index.php', params, 1, 'savehtmlfield_returnbit');
		editor_cancel_htmlfield();
	}
}

function savehtmlfield_returnbit() {
	if (myreqXML.readyState == 4) {
		if (myreqXML.status == 200) {
			$('#'+thishtmldiv).html(unescape(myreqXML.responseText));
		}
	}
}

/* IMAGE FIELDS */

function editor_imagefield_onmouseover(caller) {
	var divid = caller.id;
	var parentdivid = caller.parentNode.id;
	$('#'+parentdivid).css('background-image', 'url(/cms/images/click_to_edit.gif)');
	$('#'+divid).css('filter', 'alpha(opacity=20)');
	$('#'+divid).css('opacity', '0.4');
	$('#'+divid).css('cursor', 'pointer');
}

function editor_imagefield_onmouseout(caller) {
	var divid = caller.id;
	var parentdivid = caller.parentNode.id;
	$('#'+parentdivid).css('background-image', 'none');
	$('#'+divid).css('filter', '');
	$('#'+divid).css('opacity', '');
	$('#'+divid).css('cursor', 'default');
}

function editor_imagefield_onclick(caller) {
	var advertid = $('#advertid').val();
	close_editor_bar();
	var divid = caller.id;
	var parentdivid = caller.parentNode.id;
	thisimageparentdiv = parentdivid;
	thisimagediv = divid;
	$('#'+parentdivid).css('background-image', 'none');
	$('#'+thisimagediv).css('filter', '');
	$('#'+thisimagediv).css('opacity', '');
	$('#'+thisimagediv).css('cursor', 'default');
	imagefield_src = $('#'+thisimagediv).attr('src');
	editor_imagefield_src[thisimagediv] = imagefield_src;
	currentimagehtml = $('#'+parentdivid).html();
	okcancel_html = '<div id="okcancel"><form id="form_upload_advert_image" name="form_upload_advert_image" method="post" action="index.php" enctype="multipart/form-data" target="upload_advert_image_frame" onsubmit="startAdvertImageUpload()"><input id="advertimage" name="advertimage" type="file" />&nbsp;&nbsp;<input type="image" src="/cms/images/toolbar_save.png" style="width: 20px; height: 20px; border: none" onclick="document.form_upload_advert_image.submit()" />&nbsp;&nbsp;<input type="image" src="/cms/images/toolbar_close.png" class="toolbaricon" onclick="editor_cancel_imagefield(true)" /><input type="hidden" name="action" value="upload_advert_image" /><input type="hidden" id="advertid" name="advertid" value="'+advertid+'" /><input type="hidden" name="token" value="'+thisimagediv+'" /></form>&nbsp;&nbsp;<input id="undoimage" type="image" src="/cms/images/toolbar_undo.png" class="toolbaricon" onclick="revision_position(\'down\', \'image\')">&nbsp;&nbsp;<input id="redoimage" type="image" src="/cms/images/toolbar_redo.png" class="toolbaricon" onclick="revision_position(\'up\', \'image\')"><div id="thumb_advert_image_upload_process">Loading...<br /><img src="/cms/images/loader.gif" /></div><iframe id="upload_advert_image_frame" name="upload_advert_image_frame" src="#" style="width: 0px; height: 0px; border: none"></iframe></div>';
	$('#'+thisimagediv).after(okcancel_html);
	field_changes('image');
}

function editor_cancel_imagefield(reset) {
	if (reset) {
		$('#'+thisimageparentdiv).html(currentimagehtml);
		$('#'+thisimagediv).attr('src', editor_imagefield_src[thisimagediv]);
	}
	close_editor_bar();
}

/* MAP FIELDS */

function editor_mapfield_onmouseover(caller) {
	var divid = caller.id;
	var parentdivid = caller.parentNode.id;
	$('#'+parentdivid).css('background-image', 'url(/cms/images/click_to_edit.gif)');
	$('#'+divid).css('filter', 'alpha(opacity=20)');
	$('#'+divid).css('opacity', '0.4');
	$('#'+divid).css('cursor', 'pointer');
}

function editor_mapfield_onmouseout(caller) {
	var divid = caller.id;
	var parentdivid = caller.parentNode.id;
	$('#'+parentdivid).css('background-image', 'none');
	$('#'+divid).css('filter', '');
	$('#'+divid).css('opacity', '');
	$('#'+divid).css('cursor', 'default');
}

function editor_mapfield_onclick(caller) {
	var advertid = $('#advertid').val();
	close_editor_bar();
	var divid = caller.id;
	var parentdivid = caller.parentNode.id;
	thismapparentdiv = parentdivid;
	thismapdiv = divid;
	$('#'+parentdivid).css('background-image', 'none');
	$('#'+thismapdiv).css('filter', '');
	$('#'+thismapdiv).css('opacity', '');
	$('#'+thismapdiv).css('cursor', 'default');
	$.get('/cms/token_changes.php', { action: 'map_values', token: thismapdiv, advertid: advertid }, function(data) {
			result = data.split("|");
			if (result[0] == 'OK') {
				mapfield_text = result[1];
				okcancel_html = '<div id="okcancel"><span style="color: black">We use the Post Code of your holiday accommodation to create a Google Map showing your approximate location.<br />This will be created once you submit your new template and we have approved it.<br />Enter Post Code</span>&nbsp;<input id="mapfield_value" type="text" value="'+mapfield_text+'" class="textfield" style="width: 100px" />&nbsp;&nbsp;<input type="image" src="/cms/images/toolbar_save.png" style="width: 20px; height: 20px; border: none" onclick="editor_save_mapfield()" />&nbsp;&nbsp;<input type="image" src="/cms/images/toolbar_close.png" class="toolbaricon" onclick="editor_cancel_mapfield(true)" /></div>';
				$('#'+thismapdiv).after(okcancel_html);
			}
		});
}

function editor_cancel_mapfield(reset) {
	if (reset) {
		$('#'+thismapdiv).attr('src', '/grafx/google_map.jpg');
	}
	close_editor_bar();
}

function editor_save_mapfield() {
	var advertid = $('#advertid').val();
	$('#'+thismapdiv).css('background-color', '');
	$('#'+thismapdiv).css('cursor', 'default');
	mapfield_text = escape($('#mapfield_value').val());
	params = 'action=save_mapfield&advertid='+advertid+'&token='+thismapdiv+'&mapfield='+mapfield_text;
	// .replace(/\+/g, "%2b")
	post('/cms/index.php', params, 1, null);
	editor_cancel_mapfield();
}

/* JG: updated textCounter */
function strLen(page_id, len) {
	
	textarea = $('#link_'+page_id);
	remaing = $('#remLen_'+page_id);
	
	if (textarea.val().length > len)
		textarea.val(textarea.val().substring(0, len));
	
	remaing.val(len - textarea.val().length);
}

function textCounter(field, cntfield, maxlimit) {
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
	else
		cntfield.value = maxlimit - field.value.length;
}

function FCKeditor_OnComplete(oFCKeditor) {
	oFCKeditor.SetHTML(editor_htmlfield_html[oFCKeditor.Name]);
	/*oFCKeditor.LinkedField.form.onsubmit = function () {
		editor_save_htmlfield(oFCKeditor.Name);
		return false;
	}*/
}
//]]>
