/**
// Copyright (C) 2008 - 2010 Mike Feng Jinglong
//
// This program is a commercial software; you cannot redistribute it
// and/or modify it without the explicit consent from the author.
//
// All title and copyright in and to the Product is owned by Mike Feng
// and is protected by international copyright laws and treaty provisions.
// Mike Feng retains all ownership rights in the Product.
// The copyright notice must be reproduced and included on any copy
// that is expressly permitted under this Agreement.
**/

window.addEvent('domready', function() {
	if ($('piremaining')) {
		$('emails').disabled = true;
		setTimeout('countdown()', 1000);
	}
	if ($('nc_edit')) {
		$('nc_edit').addEvent('click', function(e) {
			e = new Event(e);
			if ($('nc_mymsg')) {
				$E('div[class=pitool-tip]').remove();
				var originalmsg = $('nc_originalmsg').value;
				$('nc_edit').innerHTML = '<textarea style="width:80%" rows="6" id="nc_custommsg">'+originalmsg+'</textarea><br /><div id="ajax-loading" style="margin-right:100px" class="ajax-loading">&nbsp;</div><input type="button" class="inputbox button" value="'+_PI_NC_SAVE_BTN+'" onclick="save_nc_custommsg(\'save\')" /> <input type="button" class="inputbox button" value="'+_PI_NC_CANCEL_BTN+'" onclick="save_nc_custommsg(\'cancel\')" />';
			}
		});
	}
	if ($('emailapp-help')) {
		var slide1 = new Fx.Slide('emailapp-help', {duration:0});
		var slide2 = new Fx.Slide('help-ol', {duration:0});
		var slide3 = new Fx.Slide('help-ole', {duration:0});
		var slide4 = new Fx.Slide('help-tb', {duration:0});
		var slide5 = new Fx.Slide('help-tbldif', {duration:0});
		slide1.hide();
		slide2.hide();
		slide3.hide();
		slide4.hide();
		slide5.hide();
		$('toggle-help').addEvent('click', function(e){
			e = new Event(e);
			slide1.toggle();
			slide2.hide();
			slide3.hide();
			slide4.hide();
			slide5.hide();
			e.stop();
		});
		$('toggle-ol').addEvent('click', function(e){
			e = new Event(e);
			slide3.hide();
			slide4.hide();
			slide5.hide();
			slide2.toggle();
			e.stop();
		});
		$('toggle-ole').addEvent('click', function(e){
			e = new Event(e);
			slide2.hide();
			slide4.hide();
			slide5.hide();
			slide3.toggle();
			e.stop();
		});
		$('toggle-tb').addEvent('click', function(e){
			e = new Event(e);
			slide2.hide();
			slide3.hide();
			slide5.hide();
			slide4.toggle();
			e.stop();
		});
		$('toggle-tbldif').addEvent('click', function(e){
			e = new Event(e);
			slide2.hide();
			slide3.hide();
			slide4.hide();
			slide5.toggle();
			e.stop();
		});
	}
});

function countdown() {
	var remaining = parseInt($('piremaining').innerHTML);
	if (remaining > 1) {
		remaining = remaining - 1;
		$('piremaining').innerHTML = remaining;
		setTimeout('countdown()', 1000);
	}
	else {
		$('emails').disabled = false;
		$('pidelaymessage').remove();
	}
}

function changeList(page) {
	var list = $('history-dropdown').value;
	var url = piurl2+"&no_html=1&task=changehistorylist&page="+page+"&list="+list;
	new Json.Remote(url, {
		onRequest: function() {
			$('ajax-loading').setStyle('display','block');
		},
		onComplete: function(responseJson) {
			$('history-summary').innerHTML = responseJson.history_summary;
			$('displaying-notice').innerHTML = responseJson.display_notice;
			$('history-listing').innerHTML = responseJson.history_listing;
			$('history-pagination').innerHTML = responseJson.history_pagination;
			$('ajax-loading').setStyle('display','none');
			$('hist_page').value = page;
			var piTips = new Tips($$(".piTips"), {
				className: "pitool"
			});
		},
		onFailure: function(){
			alert('There was an error with your request. Please try again later');
		}
	}).send();
}

function prepareResend(iid) {
	var frm = document.historyform;
	var checked = 0;
	var len = frm.elements.length;
	var email = '';
	var selectedemails = '';
	var lastemail = '';
	if (iid != 0) {
		//singular select - uncheck other selections then check the current one
		for(i=0; i<len; i++) {
			if (frm.elements[i].name == 'invites[]' || frm.elements[i].name == 'checkall')
				frm.elements[i].checked = false;
		}
		$('invitecheck-'+iid).checked = true;
	}
	for(i=0; i<len; i++) {
		if (frm.elements[i].name == 'invites[]' && frm.elements[i].checked == true) {
			email = $('invitecheck-'+frm.elements[i].value).getProperty('rel');
			if (email != '') {
				checked++;
				if (checked == 1)
					selectedemails = email;
				else if (checked <= 3)
					selectedemails = selectedemails + ', ' + email;
				lastemail = email;
			}
			else
				frm.elements[i].checked = false;
		}
	}
	confirmResend(checked, selectedemails, lastemail);
}

function deleteSelected() {
	var frm = document.historyform;
	var checked = 0;
	var len = frm.elements.length;
	var iids = '';
	for(i=0; i<len; i++) {
		if (frm.elements[i].name == 'invites[]' && frm.elements[i].checked == true)
			iids = iids + '|' + frm.elements[i].value;
	}
	if (iids == '') {
		alert (_PI_HIST_SELECT_AT_LEAST_ONE)
		return false;
	}
	var list = $('history-dropdown').value;
	var page = $('hist_page').value;
	var url = piurl2+"&no_html=1&task=deletehistory&page="+page+"&list="+list+"&iids="+iids;
	new Json.Remote(url, {
		onRequest: function() {
			$('ajax-loading').setStyle('display','block');
		},
		onComplete: function(responseJson) {
			$('history-summary').innerHTML = responseJson.history_summary;
			$('displaying-notice').innerHTML = responseJson.display_notice;
			$('history-listing').innerHTML = responseJson.history_listing;
			$('history-pagination').innerHTML = responseJson.history_pagination;
			$('ajax-loading').setStyle('display','none');
			var piTips = new Tips($$(".piTips"), {
				className: "pitool"
			});
		},
		onFailure: function(){
			alert('There was an error with your request. Please try again later');
		}
	}).send();
}

function resendInvites(popupform) {
	var frm = document.historyform;
	var emails = '';
	var len = frm.elements.length;
	for(i=0; i<len; i++) {
		if (frm.elements[i].name == 'invites[]' && frm.elements[i].checked == true)
			emails = emails + ',' + $('invitecheck-'+frm.elements[i].value).getProperty('rel');
	}
	if (emails == '')
		return false;
	$('selectedemails').value = emails;
	$('custommsg').value = popupform.popupcustommsg.value;
	var url = piurl+"&task=resendinvites";
	$('historyform').setProperty('action', url);
	$('historyform').submit();
}


function save_nc_custommsg(func) {
	if (func == "save")
		var nc_custommsg = $('nc_custommsg').value;
	else
		var nc_custommsg = '';
	var url = piurl2+"&no_html=1&task=save_nc_custommsg&func="+func;
	new Ajax(url, {
		data: {'custommsg': nc_custommsg},
		method: 'get',
		onRequest: function() {
			$('ajax-loading').setStyle('display','block');
		},
		onComplete: function(responseText) {
			$('ajax-loading').setStyle('display','none');
			$('nc_edit').innerHTML = responseText;
			var piTips = new Tips($$(".piTips"), {
				className: "pitool"
			});
		},
		onFailure: function(){
			alert('There was an error with your request. Please try again later');
		}
	}).request();
}

function toggleAll(cb, frm) {
	var val = cb.checked;
	var len = frm.elements.length;
	var i = 0;
	for(i=0; i<len; i++) {
		if ((frm.elements[i].name == 'emails[]' || frm.elements[i].name == 'invites[]') && frm.elements[i].disabled == false) {
			frm.elements[i].checked=val;
		}
	}
}


