function wpml_changelist(list_id) {
	new Ajax.Updater('paiddiv', wpMailBase + '?cmd=list_paiddiv&id=' + list_id + '', {
		evalScripts: true,
		asynchronous: true,
		onLoading: function(request) {
			Element.show('paiddivloading');
		},
		onComplete: function(request) {
			Element.hide('paiddivloading');
			Effect.Appear('paiddiv', {from:0, to:1});
		}
	});
}

function offsite_form(list_id) {
	new Ajax.Updater('mailinglists', wpMailBase + '?cmd=offsite_form&id=' + list_id + '', {
		asynchronous: true,
		evalScripts: true,
		onLoading: function(request) {
			ajaxloading('show');
		},
		onComplete: function(request) {
			ajaxloading();
		}
	});
}

function div_appear(divid) {
	Effect.Appear(divid);
}

function div_switchoff(divid) {
	Effect.SwitchOff(divid);
}

function ajaxloading(action) {
	if (action == "show") {
		Effect.Appear('wpmailinglistloading', {duration:0.15, from:0, to:0.5});
		Effect.Appear('wpmailinglistloadingimage', {duration:0.15, from:0, to:1});
	} else {
		Effect.Fade('wpmailinglistloading', {duration:0.15, from:0.5, to:0});
		Effect.Fade('wpmailinglistloadingimage', {duration:0.15, from:1, to:0});
	}
}

function wpml_optin(wpoptinid, form) {
	var formValues = Form.serialize($('optinform' + wpoptinid));	
	var updatediv = 'wpml_optin' + wpoptinid + '';
	var mytime= new Date().getTime();

	new Ajax.Updater(updatediv, wpMailBase + '?cmd=optin&mytime=' + mytime + '&divid=' + wpoptinid + '&id=' + wpoptinid + '', {
		method: "POST",
		asynchronous: true,
		evalScripts: true,
		parameters: formValues,
		onLoading: function(request) {
			Element.show('widget_loading' + wpoptinid + '');
		},
		onComplete: function(request) {
			Element.hide('widget_loading' + wpoptinid + '');
			Effect.Appear('wpml_optin' + wpoptinid + '', {from:0, to:1});
		}
	});
}

function wpmloptinembed(update, form, listid) {
	var formValues = Form.serialize($(form));
	
	new Ajax.Updater('wpml_optin' + update + '', wpMailBase + '?cmd=optinembed&list_id=' + listid + '&divid=' + update + '&id=' + update, {
		method: "POST",
		asynchronous: true,
		evalScripts: true,
		parameters: formValues,
		onLoading: function(request) {
			Element.show('widget_loading' + update + '');
		},
		onComplete: function(request) {
			Element.hide('widget_loading' + update + '');
			Effect.Appear('wpml_optin' + update + '', {from:0, to:1});
		}
	});
}

function get_list_fields(list_id, divid, poststring) {
	new Ajax.Updater('listfieldsdiv' + divid + '', wpMailBase + '?cmd=get_list_fields&id=' + list_id + '', {
		asynchronous: true,
		evalScripts: true,
		parameters: poststring,
		onLoading: function(request) {
			Element.show('widget_loading' + divid + '');
			Element.hide('listfieldsdiv' + divid + '');
		},
		onComplete: function(request) {
			Element.hide('widget_loading' + divid + '');
			Effect.Appear('listfieldsdiv' + divid + '');
		}
	});
}

function fields(page, update) {
	new Ajax.Updater(update, wpMailBase + "?cmd=fields&wpmlpage=" + page + "&id=", {
		onLoading: function(request) {
			ajaxloading('show');
		},
		onComplete: function(request) {
			ajaxloading();
		}
	});
}

function delete_field(id) {
	if (confirmMessage('field_delete') == true) {
		new Ajax.Request(wpMailBase + '?cmd=delete_field&id=' + id + '', {
			onLoading: function(request) {
				ajaxloading('show');
			},
			onComplete: function (request) {
				ajaxloading();
				$('Field.row' + id + '').style.backgroundColor = "#FF0000";
				Effect.Fade('Field.row' + id + '', {from:1, to:0});
			}
		});
	} else {
		return false;
	}
}

function orders(page, update) {
	new Ajax.Updater(update, wpMailBase + "?cmd=orders&wpmlpage=" + page + "&id=", {
		onLoading: function(request) {
			ajaxloading('show');
		},
		onComplete: function(request) {
			ajaxloading();
		}
	});
}

function order_delete(order_id) {
	if (confirmMessage('order_delete') == true) {
		new Ajax.Request(wpMailBase + '?cmd=order_delete&id=' + order_id + '', {
			evalScripts: true,
			asynchronous: true,
			onLoading: function(request) {
				ajaxloading('show');
			},
			onComplete: function(request) {
				ajaxloading();
				$('orderrow' + order_id + '').style.background = "#FF0000";
				Effect.Fade('orderrow' + order_id + '', {from:1,to:0});
			}
		});
	} else {
		return false;
	}
}

function templates(page, update) {
	new Ajax.Updater(update, wpMailBase + '?cmd=templates&wpmlpage=' + page + '&id=', {
		onLoading: function(request) {
			ajaxloading('show');
		},
		onComplete: function(request) {
			ajaxloading();
		}
	});
}

function view_template(id) {
	new Ajax.Updater('templates', wpMailBase + '?cmd=view_template&id=' + id + '', {
		onLoading: function(request) {
			ajaxloading('show');
		},
		onComplete: function(request) {
			ajaxloading();	
		}
	});
}

function delete_template(id) {
	var agree = confirmMessage('delete_template');

	if (agree == true) {
		new Ajax.Request(wpMailBase + '?cmd=delete_template&id=' + id + '', {
			onLoading: function(request) {
				ajaxloading('show');
			},
			onComplete: function(request) {
				ajaxloading();
				$('templaterow' + id + '').style.backgroundColor = "#FF0000";
				Effect.Fade('templaterow' + id + '', {from:1, to:0});
			}
		});
	} else {
		return false;
	}
}

function delete_templates() {
	var agree = confirmMessage('delete_templates');
	
	if (agree == true) {
		var formValues = Form.serialize($('templatesform'));
	
		new Ajax.Updater('templates', wpMailBase + '?cmd=delete_templates&id=', {
			parameters: formValues,
			onLoading: function(request) {
				ajaxloading('show');
			},
			onComplete: function(request) {
				ajaxloading();
			}
		});
	} else {
		return false;
	}
}

function queues(page, update) {
	new Ajax.Updater(update, wpMailBase + '?cmd=queues&wpmlpage=' + page + '&id=', {
		onLoading: function(request) {
			ajaxloading('show');
		},
		onComplete: function(request) {
			ajaxloading();
		}
	});
}

function view_queue(id) {
	new Ajax.Updater('queues', wpMailBase + '?cmd=view_queue&id=' + id + '', {
		onLoading: function(request) {
			ajaxloading('show');
		},
		onComplete: function(request) {
			ajaxloading();
		}
	});
}

function delete_queue(id) {
	var agree = confirmMessage('delete_queue');
	
	if (agree == true) {
		new Ajax.Request(wpMailBase + '?cmd=delete_queue&id=' + id + '', {
			onLoading: function(request) {
				ajaxloading('show');
			},
			onComplete: function(request) {
				ajaxloading();
				$('queuerow' + id + '').style.backgroundColor = "#FF0000";
				Effect.Fade('queuerow' + id + '', {from:1, to:0});
			}
		});
	} else {
		return false;
	}
}

function delete_queues() {
	var agree = confirmMessage('delete_queues');
	
	if (agree == true) {
		var formValues = $('queuesform').serialize(true);
	
		new Ajax.Updater('queues', wpMailBase + '?cmd=delete_queues&id=', {
			parameters: formValues,
			onLoading: function(request) {
				ajaxloading('show');
			},
			onComplete:function(request) {
				ajaxloading();
			}
		});
	} else {
		return false;
	}
}

function history(page, update) {
	new Ajax.Updater(update, wpMailBase + '?cmd=history&wpmlpage=' + page + '&id=', {
		asynchronous:true,
		evalScripts:true,
		onLoading:function(request) {
			ajaxloading('show');
		},
		onComplete:function(request) {
			ajaxloading();
		}
	});
}

function view_history(id) {
	new Ajax.Updater('history', wpMailBase + '?cmd=view_history&id=' + id + '', {
		onLoading: function(request) {
			ajaxloading('show');
		},
		onComplete: function(request) {
			ajaxloading();
		}
	});
}

function delete_history(id) {

	var agree = confirmMessage('delete_history');

	if (agree == true) {
		new Ajax.Request(wpMailBase + '?cmd=delete_history&id=' + id + '', {
			asynchronous:true,
			evalScripts:true,
			onLoading:function(request) {
				ajaxloading('show');
			},
			onComplete:function(request) {
				ajaxloading();
				$('historyrow' + id + '').style.backgroundColor = "#FF0000";
				Effect.Fade('historyrow' + id + '', {from:1, to:0});
			}
		});
	}
}

function delete_histories() {
	var agree = confirmMessage('delete_histories');

	if (agree == true) {
		var formValues = $('historyform').serialize(true);
	
		new Ajax.Updater('history', wpMailBase + '?cmd=delete_histories&id=', {
			asynchronous:true,
			evalScripts:true,
			parameters: formValues,
			onLoading:function(request) {
				ajaxloading('show');
			},
			onComplete:function(request) {
				ajaxloading();
			}
		});
	} else {
		return false;
	}
}

function mailinglists(page, update) {
	if (update == "") {
		var update = 'mailinglists';
	}

	new Ajax.Updater(update, wpMailBase + '?cmd=mailinglists&wpmlpage=' + page + '&id=', {
		asynchronous:true,
		onLoading:function(request) {
			ajaxloading('show');
		},
		onComplete:function(request) {
			ajaxloading();
		}
	})
}

function save_list(id, form) {
	if (form == false) {
		var formValues = false;
	} else {
		var formValues = Form.serialize(form);
	}

	new Ajax.Updater('mailinglists', wpMailBase + '?cmd=save_list&id=' + id + '', {
		asynchronous:true,
		evalScripts:true,
		parameters:formValues,
		onLoading: function(request) {
			ajaxloading('show');
		},
		onComplete: function(request) {
			ajaxloading();
		}
	});
}

function view_list(id, update) {
	new Ajax.Updater(update, wpMailBase + '?cmd=view_list&id=' + id + '', {
		asynchronous:true,
		onLoading:function(request) {
			ajaxloading('show');
		},
		onComplete:function(request) {
			ajaxloading();
		}
	});
}

function delete_list(id, update) {
	var agree = confirmMessage('delete_list');
	
	if (agree == true) {
		if (update == "") {
			new Ajax.Request(wpMailBase + '?cmd=delete_list&id=' + id + '', {
				asynchronous:true,
				onLoading:function(request) {
					ajaxloading('show');
				},
				onComplete:function(request) {
					ajaxloading();
					$('listrow' + id).style.backgroundColor = '#ff0000';
					Effect.Fade('listrow' + id + '', {from:1, to:0});
				}
			});
		} else {
			new Ajax.Updater(update, wpMailBase + '?cmd=delete_list&id=' + id + '', {
				onLoading: function(request) {
					ajaxloading('show');
				},
				onComplete: function(request) {
					ajaxloading();
				}
			});
		}
	} else {
		return false;
	}
}

function subscribers(page, update) {
	new Ajax.Updater(update, wpMailBase + '?cmd=subscribers&wpmlpage=' + page + '&id=', {
		asynchronous:true,
		evalScripts:true,
		onLoading:function(request) {
			ajaxloading('show');
		},
		onComplete:function(request) {
			ajaxloading();
		}
	});
}

function search_subscribers() {
	var formValues = $('subscribersearchform').serialize(true);
	
	new Ajax.Updater('subscribers', wpMailBase + "?cmd=search_subscribers&id=", {
		parameters: formValues,
		onLoading: function(request) {
			ajaxloading('show');
		},
		onComplete: function(request) {
			ajaxloading();
		}
	});
}

function save_subscriber(id, form, update) {
	if (form == false) {
		var formValues = false;
	} else {
		var formValues = $(form).serialize(true);
	}
	
	if (update == "") {
		var update = "subscribers";
	}
	
	new Ajax.Updater(update, wpMailBase + '?cmd=save_subscriber&id=' + id + '', {
		asynchronous:true,
		evalScripts:true,
		parameters:formValues,
		onLoading:function(request) {
			ajaxloading('show');
		},
		onComplete:function(request) {
			ajaxloading();
		}
	});
}

function view_subscriber(id) {
	new Ajax.Updater('subscribers', wpMailBase + '?cmd=view_subscriber&id=' + id + '', {
		asynchronous:true,
		onLoading:function(request) {
			ajaxloading('show');
		},
		onComplete:function(request) {
			ajaxloading();
		}
	});
}

function delete_subscriber(id, update) {
	var agree = confirmMessage('delete_subscriber');
	
	if (agree == true) {
		if (update == "") {
			new Ajax.Request(wpMailBase + '?cmd=delete_subscriber&id=' + id + '', {
				onLoading:function(request) {
					ajaxloading('show');
				},
				onComplete:function(request) {
					$('subscriberrow' + id + '').style.backgroundColor = "#FF0000";
					Effect.Fade('subscriberrow' + id + '', {from:1, to:0});
					ajaxloading();
				}
			});
		} else {
			new Ajax.Updater(update, wpMailBase + '?cmd=delete_subscriber&update=subscribers&id=' + id + '', {
				onLoading: function(request) {
					ajaxloading('show');
				},
				onComplete: function(request) {
					ajaxloading();
				}
			});
		}
	} else {
		return false;
	}
}

function delete_subscribers() {
	var agree = confirmMessage('delete_subscribers');
	
	if (agree == true) {
		new Ajax.Updater('subscribers', wpMailBase + '?cmd=delete_subscribers&id=', {
			asynchronous:true,
			parameters:$('subscribersform').serialize(true),
			onLoading:function(request) {
				ajaxloading('show');
			},
			onComplete:function(request) {
				ajaxloading();
			}
		});
	} else {
		return false;
	}
}

function checkAllBoxes(obj, formname, fieldname) {		
	if(document.forms[formname].elements[fieldname+"[]"].length) {
		for (i = 0; i < document.forms[formname].elements[fieldname+"[]"].length; i++) {
			document.forms[formname].elements[fieldname+"[]"][i].checked = true;
		}
	} else {
		$(formname).elements[fieldname+"[]"].checked = true;
	}
	
	$('mainCheckLink').innerHTML = '<a href="javascript:unCheckAllBoxes(this,\'' + formname + '\',\'' + fieldname + '\');" title="Uncheck all checkboxes">Uncheck</a>';
}

function unCheckAllBoxes(obj, formname, fieldname) {
	if(document.forms[formname].elements[fieldname+"[]"].length) {
		for (i = 0; i < document.forms[formname].elements[fieldname+"[]"].length; i++)
		{
			document.forms[formname].elements[fieldname+"[]"][i].checked = false;
		}
	} else {
		document.forms[formname].elements[fieldname+"[]"].checked = false;
	}
	
	$('mainCheckLink').innerHTML = '<a href="javascript:checkAllBoxes(this,\'' + formname + '\',\'' + fieldname + '\');" title="Check all checkboxes">Check</a>';
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}