
 
var open_composer;
var ajaxResult;
var total_acc;

//var loading;
//var div_notload = new Object();
//div_notload['content_index'] = 1;

var visible_el = new Object();
var eval_after_ajax = '';

 
var ab_sel_mob = new Array();
var ab_sel_email = new Array();

 
var all_mob_rcpt = new Array();
var all_email_rcpt = new Array();

 
var total_mob_rcp = 0;
var total_mail_rcp = 0;

 
var my_mob_added = 0;

 
var total_attach = 0;

 
var crlft;

 
var delrow = new Array();

 
var editcont = new Object();

 
var editcontid = 0;

  
var mobs_not_from_ab = '';

 
var upload_field_cache = '';

  
var nomsisdn = 0;

  
var msg_cost = 0;

var msg_folder = "inbox"; 
var sel_msgs = new Object();

 
var loaded_msgs = new Object();

  
var uploading = 0;

  
var tempOB = {};

 
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}


 
function setec(el){
	if(el.checked)
		editcont['c_'+el.value] = el.value;
	else
		editcont['c_'+el.value] = 0;
		
	
	 
	var checked_c = any_checked($('ab_form').ab_c);
	
	 
	dc_link(1);
	if(checked_c == 1){
		ec_link(1)
	}
	else if(checked_c > 1){
		ec_link(0)
	}
	else{
		dc_link(0);
		ec_link(0);
	}
}


 
function ec_link(state){
	if(state){
		$('ec_link').disabled = false;
		$('ec_link').onclick = function(){return true;}
		$('ec_link').className = '';	
	}
	else{
		$('ec_link').disabled = true;
		$('ec_link').onclick = function(){return false;}
		$('ec_link').className = 'dislink';		
	}
}


 
function dc_link(state){
	if(state){
		$('dc_link').disabled = false;
		$('dc_link').onclick = function(){return true;}
		$('dc_link').className = '';	
	}
	else{
		$('dc_link').disabled = true;
		$('dc_link').onclick = function(){return false;}
		$('dc_link').className = 'dislink';		
	}
}


 
function reset_composer(){
	
	editcontid = total_attach = total_mob_rcp = total_mail_rcp = 0;
	
	if(all_mob_rcpt.length)
		for (i = 0; i < all_mob_rcpt.length; i++) {
			if(document.getElementById('ab_m'+all_mob_rcpt[i]) != undefined)
				$('ab_m'+all_mob_rcpt[i]).checked = false;
		}
		
	if(all_email_rcpt.length)
		for (i = 0; i < all_email_rcpt.length; i++){
			if(document.getElementById('ab_e'+all_email_rcpt[i]) != undefined)
				$('ab_e'+all_email_rcpt[i]).checked = false;
		}
	
	ab_sel_mob = [];
	ab_sel_email = [];
	all_mob_rcpt = [];
	all_email_rcpt = [];
	Element.hide('AdressBook');
}


 
function main_menu(what,arg1){
	
	function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}
	
	//cookie = "RSID";
	cookie = getCookie("RSID");
	
	
	if(cookie != null && cookie != "")
	{
		session = "";
	} 
	else 
	{
		if(get_session() != "")
			session = ","+get_session();
		else
			session = "";
	}
	
		if(arg1 == 'nologin' || what == 'help_step1' || what == 'help_step2' || what == 'help_step3' || what == 'features' || what == 'mobile_email_alerts' || what == 'payment' || what == 'about' || what == 'privacy' || what == 'tos'){
		if(what == 'ex_fastpop')
			gotopage(what)
		else if(what == 'ex_forme')
			gotopage(what+session)
		else if(what == 'help_step1')
			gotopage("mobile-email-step1"+session)
		else if(what == 'help_step2')
			gotopage("mobile-email-step2"+session)
		else if(what == 'help_step3')
			gotopage("mobile-email-step3"+session)
		else if(what == 'send_pass_page')
			gotopage("send-pass-page")
		else if(what == 'features')
			gotopage(what+session)
		else if(what == 'mobile_email_alerts')
			gotopage(what+session)
		else if(what == 'payment')
			gotopage(what+session)
		else if(what == 'about')
			gotopage(what+session)
		else if(what == 'privacy')
			gotopage(what+session)
		else if(what == 'tos')
			gotopage(what+session)
	}
	else if(get_session()){
		 
		if(cancel_msg()){
			 
			if(nomsisdn == 1 && (what == 'p' ||  what == 'm' || what == 't' || what == 'e')){
				what = 'account';
				arg1 = 'open_reg_mobile';
				alert("Please register first your mobile phone with Renzoo. You will be redirected to 'My Account' page.");
			}
			
			if(what == 'p')
				gotopage("com_picture"+session)
			else if(what == 'm')
				gotopage("com_music"+session)
			else if(what == 't')
				gotopage("com_text"+session);
			else if(what == 'e'){
				var goto12 = "com_email"+session;
				if(arg1)
					goto12 += "email" + arg1+','+session;
				gotopage(goto12);
			}
			else if(what == 'home')
				gotopage("index"+session)
			else if(what == 'invite')
				gotopage("invite"+session)
			else if(what == 'account')
				gotopage("account," + arg1+session)
			else if(what == 'feedback')
				gotopage("feedback"+session)
			else if(what == 'help_push_desk')
				gotopage("help-push-desk"+session)
			else if(what == 'inbox' || what == 'inboxunread' || what == 'sent')
				gotopage("mailbox,"+what+session)
			else if(what == 'home_express')
				gotopage("settings"+session)
			else if(what == 'help_gmail')
				gotopage("help-gmail"+session)
		}
	}
	else
		alert("Please sign in first or register for free if you don't have your renzoopush account.");
}


 
function message_send(what){
	
	$('btn_send_msg').disabled = true;
	
	if(open_composer == 't' && valid_form('text')){
		ajax_req('send_text');
	}
	else if(open_composer == 'p' && valid_form('picture')){
		ajax_req('send_picture');
	}
	else if(open_composer == 'e' && valid_form('email')){
		ajax_req('send_email');
	}
	else if(open_composer == 'm' && valid_form('music')){
		ajax_req('send_music');
	}
	else if(what == 'invite' && valid_form('invite')){
		ajax_req('send_invite');
	}
	else if(what == 'feedback' && valid_form('feedback')){
		ajax_req('send_feedback');
	}
	else
		$('btn_send_msg').disabled = false;
}


 
function message_send_ok(arg1){
	
	if(arg1 != 'cancel' && !valid_form('add_to_ab_msg_sent'))
		return;
		
	if(open_composer == 't'){
		ajax_req('show_com_text',add_to_ab_msg_sent(arg1));
	}
	else if(open_composer == 'e'){
		ajax_req('show_com_email',add_to_ab_msg_sent(arg1));
	}
	else if(open_composer == 'p'){
		ajax_req('show_com_picture',add_to_ab_msg_sent(arg1));
	}
	else if(open_composer == 'i'){
		ajax_req('show_com_invite',add_to_ab_msg_sent(arg1));
	}
	else if(open_composer == 'm'){
		ajax_req('show_com_music',add_to_ab_msg_sent(arg1));
	}
}


 
function add_to_ab_msg_sent(arg1){

		var new_contacts = '';
	if(arg1 != 'cancel'){
		for(var i=1; i < 100 ; i++){
			if (document.getElementById("nic_mob_"+i) != undefined){
								if($('nic_name_'+i).value || $('nic_last_'+i).value){
					new_contacts += '#=#' + $('nic_mob_'+i).value + '|' + $('nic_email_'+i).value + '|' +  $('nic_name_'+i).value + '|' + $('nic_last_'+i).value;
				}
			}
			else
				break;
		}
	}
	return new_contacts;
}


 
function ajax_req(what,arg1,arg2){
	
	if(get_session() || what=='send_pass'){
		var url = get_ajax_url();

		if(what == 'send_email' || what == 'send_picture' || what == 'send_invite' || what == 'send_text' || what == 'send_music'){
			 
			temp_val = '';
			if(document.getElementById('file_index') != undefined && $('composer_form').file_index.length != undefined){
				for (i = 0; i < $('composer_form').file_index.length; i++) {
					if($('composer_form').file_index[i].value != 0 && $('attach_' + $('composer_form').file_index[i].value).value == 1)
						temp_val += '##' + $('file_data_' + $('composer_form').file_index[i].value).value;
				}
			}
			
			 
			var mob_rcpts = get_mob_rcpts();
			var email_rcpts = get_email_rcpts();
			
			eval_after_ajax = "reset_composer();";
			
			if(what == 'send_email')
				doAjax_update('com_main',url + '&page=' + what,{mobs_not_from_ab:mobs_not_from_ab,mob_rcpts:mob_rcpts,email_rcpts:email_rcpts,attachments:temp_val,to:$('to').value, subject:$('subject').value, message:$('message').value,copy2email:$('copy2email').value});
			else if(what == 'send_picture' || what == 'send_music')
				doAjax_update('com_main',url + '&page=' + what,{mobs_not_from_ab:mobs_not_from_ab,mob_rcpts:mob_rcpts,email_rcpts:email_rcpts,attachments:temp_val,to:$('to').value,message:$('message').value,copy2email:$('copy2email').value});
			else if(what == 'send_invite')
				doAjax_update('com_main',url + '&page=' + what,{mobs_not_from_ab:mobs_not_from_ab,mob_rcpts:mob_rcpts,email_rcpts:email_rcpts,message:$('message').value,copy2email:$('copy2email').value});
			else if(what == 'send_text')
				doAjax_update('com_main',url + '&page=' + what,{mobs_not_from_ab:mobs_not_from_ab,mob_rcpts:mob_rcpts,to:$('to').value,message:$('message').value,sms_enc:$('sms_enc').value});
		}

		else if(what == 'save_personal'){
			if(valid_form('personal')){
				if($('gender_male').checked)
					gender_value = 'M';
				else if($('gender_female').checked)
					gender_value = 'F';
					
				doAjax_update('account_personal_msg',url + '&page=save_personal',{first_name:$('first_name').value,last_name:$('last_name').value,middle_name:$('middle_name').value,address:$('address').value,zip:$('zip').value,city:$('city').value,gender:gender_value,occupation:$('occupation').value,company:$('company').value,day:$('day').value,month:$('month').value,year:$('year').value});
			}
		}

		else if(what == 'change_nick'){
			if(valid_form(what)){	
				doAjax_update('account_nick_msg',url + '&page=change_nick',{new_nick:$('new_nick').value});
			}
		}
		
		else if(what == 'send_pass'){
			if(valid_form(what,1)){
				if(!arg1)
					arg1 = 0;
				eval_after_ajax = 'send_pass('+ arg1 +');';
				doAjax(url + '&page=' + what,{fpass_email:$('fpass_email').value, recaptcha_challenge_field: $('recaptcha_challenge_field').value, recaptcha_response_field: $('recaptcha_response_field').value});
			}
		}

		else if(what == 'change_pass'){
			if(valid_form(what)){
				doAjax_update('account_pass_msg',url + '&page=change_pass',{pass_old:$('pass_old').value,pass_1:$('pass_1').value,pass_2:$('pass_2').value});
				$('pass_1').value = $('pass_2').value = $('pass_old').value = '';
			}
		}

		else if(what == 'save_news'){
			doAjax_update('account_news_msg',url + '&page=save_news',{news_email:$('news_email').checked,news_mob:$('news_mob').checked});
		}

		else if(what == 'send_feedback'){
			doAjax_update('content_feedback',url + '&page=' + what,{subject:$('subject').value,message:$('message').value});
		}

		else if(what == 'new_contact'){ 	
			if(valid_form('new_contact')){
				eval_after_ajax = 'ab_reload();ab_delform();dc_link(0);ec_link(0);';
				doAjax_update('AdressBookcontacts',url + '&page=new_contact,show_ab_contacts',{nc_name:$('nc_name').value,nc_mobile:$('nc_mobile').value,nc_mobile2:$('nc_mobile2').value,nc_email:$('nc_email').value,nc_email2:$('nc_email2').value,nc_lastname:$('nc_lastname').value,editcontid:editcontid});
			}
		}

		else if(what == 'del_contact'){
			if(yes_no('Do you want to delete selected contacts?')){
				temp_val = '';
				if($('ab_form').ab_c.length == undefined){
					if($('ab_form').ab_c.checked){
						temp_val += $('ab_form').ab_c.value;
						$('ab_form').ab_c.checked = false;
						editcont['c_'+$('ab_form').ab_c.value] = 0;
						delrow[delrow.length] = $('ab_form').ab_c.value;
					}
				}
				else{
					for (i = 0; i < $('ab_form').ab_c.length; i++) {
						if($('ab_form').ab_c[i].checked == true){
							temp_val += ',' + $('ab_form').ab_c[i].value;
							$('ab_form').ab_c[i].checked = false;
							editcont['c_'+$('ab_form').ab_c[i].value] = 0;
							delrow[delrow.length] = $('ab_form').ab_c[i].value;
						}
					}
				}
				
				eval_after_ajax = 'ab_delrow()'; //ab_reload()
				doAjax(url + '&page=' + what,{cid:temp_val});
			}
		}

		else if(what == 'show_com_music' || what == 'show_com_text' || what == 'show_com_email' || what == 'show_com_picture' || what == 'show_com_invite'){
			if(!arg1)
				arg1 = '';
			eval_after_ajax = "ab_recent_reload();Element.show('AdressBook');";
			if(what != 'show_com_invite'){
				eval_after_ajax += "ajax_req('credits_left');";
			}
			doAjax_update('com_main',url + '&page=' + what,{new_contacts:arg1});
		}

		else if (what == 'send_ver_code')
		{
			if(valid_form('change_mobile', 1))
				doAjax_update('account_confirm_code',url + '&page=' + what,{new_mobile:$('new_mobile').value});
		}
		
		else if (what == 'send_ver_code_email')
		{
			if(valid_form('change_email', 1))
				doAjax_update('account_confirm_code_email',url + '&page=' + what,{new_email:$('email2sms_email').value});
		}
		
		else if(what == 'verify_code')
		{
			if(valid_form('verify_code',1))
				doAjax_update('account_mobile_changed',url + '&page=' + what,{mobile_ver_code:$('mobile_ver_code').value,vercode_enc:$('vercode_enc').value,msisdn_enc:$('msisdn_enc').value});
		}

		else if(what == 'verify_code_email')
		{
			if(valid_form('verify_code_email',1))
				doAjax_update('account_email_changed', url + '&page=' + what, {email_ver_code: $('email_ver_code').value, vercode_enc: $('vercode_enc').value, email_enc: $('email_enc').value});
		}

		else if(what == 'change_push_type'){
			doAjax_update('push_type_pr',url + '&page=' + what,{cur_push_type:$('push_type_pr').innerHTML});
		}

		else if(what == 'submit_recharge'){
			doAjax_update('dtemp',url + '&page=' + what,{amount:$('amount').value,currency:$('currency').value});
		}

		else if(what == 'show_ab_contacts'){
			eval_after_ajax = 'ab_delform();dc_link(0);ec_link(0);';
			doAjax_update('AdressBookcontacts',url + '&page=' + what);
		}
		
		else if(what == 'show_ab_recent'){
			doAjax_update('AdressBookrecent',url + '&page=' + what);
			Element.show('AdressBookrecent_all');
		}
		
		else if(what == 'credits_left'){
			doAjax_update('credits_left_value',url + '&page=' + what);
		}
		
		if(what == 'inbox' || what == 'inboxunread'){
			var temp = '';
			if(arg1) temp = '&mailbox_page=' + arg1;

			if (what != msg_folder)
			{
				msg_folder = what;
				sel_msgs = new Object();
			}
			else
				eval_after_ajax = "mailbox_update();";

			loaded_msgs = new Object();
			doAjax_update('contentPageMainIntraLeftnoid',url + '&page=show_mailbox,'+ what + temp);
			if(what == 'inboxunread')
				$('mailbox_title').innerHTML = 'Inbox - unread messages';
			else
				$('mailbox_title').innerHTML = 'Inbox';
		}
		else if(what == 'sent'){
			var temp = '';
			if(arg1) temp = '&mailbox_page=' + arg1;

			if (what != msg_folder)
			{
				msg_folder = what;
				sel_msgs = new Object();
			}
			else
				eval_after_ajax = "mailbox_update();";
				
			loaded_msgs = new Object();
			doAjax_update('contentPageMainIntraLeftnoid',url + '&page=show_mailbox,sent' + temp);
			$('mailbox_title').innerHTML = 'Sent Items';
		}
		else if(what == 'delete_msgs' || what == 'read_msgs' || what == 'unread_msgs'){
			doAjax(url + '&page=' + what,{messages:arg1});
		}
		else if(what == 'msg_details'){
			doAjax_update('msg_prwd'+arg1,url + '&page=' + what,{msg_id:arg1});
		}
	}
	else
		alert("Please sign in first or register for free if you don't have your renzoopush account.");
}

 
function str_replace(search, replace, subject) {
	return subject.split(search).join(replace);
}

 
function get_mob_rcpts(){
	var rcpt_val = '';
	var mrcpts = '';
	
	if(total_mob_rcp){
		all_mob_rcpt = all_mob_rcpt.uniq();
		for (i = 0; i < all_mob_rcpt.length; i++) {
			if($('to_mobile_send' + all_mob_rcpt[i]).value == 1){
				rcpt_val = $('to_msisdn' + all_mob_rcpt[i]).value+ ';' + $('to_mobile_name' + all_mob_rcpt[i]).value;
				if($('to_mobile_user_entered' + all_mob_rcpt[i]).value)
					rcpt_val = str_replace(",","|",rcpt_val); 
				mrcpts += '|' + rcpt_val;
				if($('mfromab' + all_mob_rcpt[i]).value == '0')
					mobs_not_from_ab += '|' + $('to_msisdn' + all_mob_rcpt[i]).value;
			}
		}
	}
	return mrcpts;
}

 
function get_email_rcpts(){
	var rcpt_val = '';
	var ercpts = ''
	
	if(total_mail_rcp){
		all_email_rcpt = all_email_rcpt.uniq();
		for (i = 0; i < all_email_rcpt.length; i++) {
			if($('to_email_send' + all_email_rcpt[i]).value == 1){
				rcpt_val = $('to_email' + all_email_rcpt[i]).value + ';' + $('to_email_name' + all_email_rcpt[i]).value;
				if($('to_email_user_entered' + all_email_rcpt[i]).value)
					rcpt_val = str_replace(",","|",rcpt_val); 
				ercpts += '|' + rcpt_val;
			}
		}
	}
	return ercpts;
}

 
function ajaxComplete(Request){
	ajaxResult = Request.responseText;
	
	 
	if(eval_after_ajax){
		eval(eval_after_ajax);
		eval_after_ajax = '';
	}
	
	Element.hide('loading');
}

 
function get_session(){
	return $('sid').innerHTML;
}

 
function get_ajax_url(){
	var mainUrl = 'http://www.renzoo.co.uk/index.php?ajax=1';
	return mainUrl += '&sid=' + get_session() + '&rnd=' + rnd();
}

 
function rnd(){
	return Math.ceil(Math.random()*1000);
}

 
function doAjax_update(el,url,prms,sync){	
	
	var AjaxOB = {};
	Element.show('loading');
	url = url + '&rnd=' + rnd();
	if(!prms)
		prms = {a:'a'};
	
	async = true;
	if(sync) async = false;
	
	AjaxOB = new Ajax.Updater(el,url,{
		asynchronous: async,
		method: 'post',
		parameters: prms,
		onComplete: ajaxComplete,
		evalScripts: true
	});
	
	return AjaxOB;
}

 
function doAjax(url,prms,sync){
	
	var AjaxOB = {};
	Element.show('loading');
	url = url + '&rnd=' + rnd();
	if(!prms)
		prms = {a:'a'};
	
	async = true;
	if(sync) async = false;
		
	AjaxOB = new Ajax.Request(url,{
		asynchronous: async,
		method: 'post',
		parameters: prms,
		onComplete: ajaxComplete,
		evalScripts: true
	});
	
	return AjaxOB;
}

 
function valid_email_list(str){
	var arr = str.split(',');
	for (var i = 0; i < arr.length; i++){
		if(valid_email(arr[i]) == false)
			return arr[i];
	}
	return true;
}

 
function valid_mob_list(str){
	var arr = str.split(',');
	for (var i = 0; i < arr.length; i++){
		if(valid_mob(arr[i]) == false)
			return arr[i];
	}
	return true;
}

 
function valid_email(str){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(str.match(emailExp))
		return true;
	else
		return false;
}

 
function valid_mob(str){
	var emailExp =  /^[1-9+][0-9]+$/;
	if(str.match(emailExp))
		return true;
	else
		return false;
}

 
function valid_form(form,only_msg){
	var msg0 = 'Please check data you entered:' + '\n\n';
	var msg = '';
	var temp;
	
	if(form == 'text'){
		if(!$('message').value)
			msg += '\'Message\' field is empty' + '\n';
	
		if(!total_mob_rcp && !$('to').value)
			msg += 'One mobile or E-mail recipient must be entered' + '\n';
		else if($('to').value && !valid_mob($('to').value))
			msg += 'Please check your mobile phone number. Please use international format that includes country code and without leading zero (e.g. 448704900911)\n';
	}
	
	if(form == 'invite'){
		if(!total_mail_rcp && !$('copy2email').value)
			msg += 'Please enter email recipient first.' + '\n';
		else if($('copy2email').value && !valid_email($('copy2email').value)){
			msg += 'Invalid email address.\n';
		}
	}

	if(form == 'feedback'){
		if(!$('subject').value)
			msg += '\'Title\' field is empty' + '\n';
		
		if(!$('message').value)
			msg += '\'Description\' field is empty' + '\n';
	}

	if(form == 'change_nick'){
		if(!$('new_nick').value)
			msg += 'Error. Please enter new nick.' + '\n';
		$('account_pass_msg').innerHTML = '&nbsp;'
	}
	
	if(form == 'change_pass'){
		if(!$('pass_old').value)
			msg += 'Old password not entered' + '\n';
		
		if(!$('pass_1').value)
			msg += 'New password not entered' + '\n';
		
		if($('pass_1').value && !$('pass_2').value)
			msg += 'New password not confirmed' + '\n';
		
		if($('pass_1').value && $('pass_2').value && ($('pass_1').value != $('pass_2').value))
			msg += 'Entered new passwords do not match!' + '\n';
		
		$('account_pass_msg').innerHTML = '&nbsp;'
	}

	if(form == 'personal'){
		if(!$('day').value || !$('month').value || !$('year').value)
			msg += 'Birth date is invalid' + '\n';
		if(!$('day').value && !$('month').value && !$('year').value)
			msg = '';
		
		$('account_personal_msg').innerHTML = '&nbsp;'
	}

	if(form == 'upload'){
		if(!$('attachment_file').value)
			msg += 'Browse to file first.' + '\n';
	}

	if(form == 'upload_url'){
		if($('attachment_file_url').value == 'http://')
			msg += 'File url is not entered.' + '\n';
	}

	if(form == 'email'){
		
		if(!$('subject').value)
			msg += 'Message subject is empty' + '\n';
		if(!$('message').value)
			msg += '\'Message\' field is empty' + '\n';
		
		if((!total_mob_rcp && !$('to').value) && (!total_mail_rcp && !$('copy2email').value))
			msg += 'One mobile or E-mail recipient must be entered' + '\n';
		
		if($('copy2email').value && !valid_email($('copy2email').value)){
			msg += 'Invalid email address.\n';
		}
		if($('to').value && !valid_mob($('to').value)){
			msg += 'Please check your mobile phone number. Please use international format that includes country code and without leading zero (e.g. 448704900911)\n';
		}
	}

	if(form == 'picture' || form == 'music'){
		
		if((!total_mob_rcp && !$('to').value) && (!total_mail_rcp && !$('copy2email').value))
			msg += 'One mobile or E-mail recipient must be entered' + '\n';
		
		if($('copy2email').value && !valid_email($('copy2email').value)){
			msg += 'Invalid email address.\n';
		}
		if($('to').value && !valid_mob($('to').value)){
			msg += 'Please check your mobile phone number. Please use international format that includes country code and without leading zero (e.g. 448704900911)\n';
		}
		
		if(!total_attach){
			if(form == 'picture')
				msg += 'Please choose picture to send.\n';
			else if(form == 'music')
				msg += 'Please choose music to send.\n';
		}
	}

	if(form == 'login'){
		if(!$('login_email').value)
			msg += 'Email not entered.' + '\n';
		if(!$('login_pass').value)
			msg += 'Password not entered.' + '\n';
	}
	
	if (form == 'change_mobile')
	{
		if(!$('new_mobile').value)
			msg += 'Please enter mobile phone number.' + '\n';
		else if(!valid_mob($('new_mobile').value))
			msg += 'Please check your mobile phone number. Please use international format that includes country code and without leading zero (e.g. 448704900911)' + '\n';
	}
	
	if (form == 'change_email') 
	{
		if(!$('email2sms_email').value)
			msg += 'Please enter valid email alert service address.' + '\n';
		else if(!valid_email($('email2sms_email').value))
			msg += 'Please check your mobile phone number. Please use international format that includes country code and without leading zero (e.g. 448704900911)' + '\n';
	}

	if(form == 'verify_code'){
		if(!$('mobile_ver_code').value)
			msg += 'Please enter the verification code received in the text message.' + '\n';
	}

	if(form == 'verify_code_email'){
		if(!$('email_ver_code').value)
			msg += 'Please enter the verification code received in the text message.' + '\n';
	}

	if(form == 'new_contact'){
		if(!$('nc_name').value && !$('nc_lastname').value)
			msg += 'Name or last name not entered.' + '\n';
		if((!$('nc_mobile').value && !$('nc_mobile2').value) && (!$('nc_email').value && !$('nc_email2').value))
			msg += 'Please enter mobile number or email address.' + '\n';
		if(($('nc_mobile').value && !valid_mob($('nc_mobile').value)) || ($('nc_mobile2').value && !valid_mob($('nc_mobile2').value)))
			msg += 'Invalid mobile phone number. Please use international format that includes country code and without leading zero.' + '\n';
		if(($('nc_email').value && !valid_email($('nc_email').value)) || ($('nc_email2').value && !valid_email($('nc_email2').value)))
			msg += 'Invalid email address.' + '\n';
	}
	
	if(form == 'add_to_rcpm'){
		if(!$('to').value)
			msg += 'Please enter mobile recipient first.' + '\n';
		else if(!valid_mob($('to').value))
				msg += 'Please check your mobile phone number. Please use international format that includes country code and without leading zero (e.g. 448704900911)\n';
	}

	if(form == 'add_to_rcpe'){
		if(!$('copy2email').value)
			msg += 'Please enter email recipient first.' + '\n';
		else if(!valid_email($('copy2email').value))
				msg += 'Invalid email address.\n';
	}
	
	if(form == 'add_to_ab_msg_sent'){
		for(var i=1; i < 100 ; i++){
			if (document.getElementById("nic_mob_"+i) != undefined){
				
				if($('nic_email_'+i).value && $('nic_rcpt_'+i).value == 'mob' && (!$('nic_name_'+i).value && !$('nic_last_'+i).value))
					msg += 'Please enter name or last name for contacts you wish to add.\n';

				else if($('nic_mob_'+i).value && $('nic_rcpt_'+i).value == 'email' && (!$('nic_name_'+i).value && !$('nic_last_'+i).value))
					msg += 'Please enter name or last name for contacts you wish to add.\n';
					
				else if(!$('nic_name_'+i).value && !$('nic_last_'+i).value)
					msg += 'Please enter name or last name for contacts you wish to add.\n';
					
				if($('nic_email_'+i).value && !valid_email($('nic_email_'+i).value))
					msg += 'Invalid Email address: ' + $('nic_email_'+i).value + '\n';
				
				if($('nic_mob_'+i).value && !valid_mob($('nic_mob_'+i).value))
					msg += 'Invalid mobile number: ' + $('nic_mob_'+i).value + '\n';
			}
			else
				break;
		}
	}
	
	if(form == 'send_pass'){
		if(!$('fpass_email').value)
			msg += 'Invalid email address.\n';
	}
	
	if(msg){
		if(only_msg)
			alert(msg)
		else
			alert(msg0 + msg)
		return false;
	}
	else
		return true;
}

 
function showhide(el){
	if($(el).style.display != 'block')
		$(el).style.display = 'block';
	else
		$(el).style.display = 'none';
}

 
function showone_hide(elid){
	
	visible_el[elid] = 1;
	
	for (var i in visible_el){
		Element.hide(i);
	}
	$(elid).style.display = 'block';
}


 
function showEl(el){
	$(el).style.display = 'block';
}

 
function hideEl(el){
	$(el).style.display = 'none';
}

 
function char_count(el, count_el, maxlimit){
	var len = el.value.length;
	if (len > maxlimit)
		el.value = el.value.substring(0, maxlimit);
	else
		count_el.innerHTML = maxlimit - len;
}

 
function yes_no(ConfMsg){
	if (confirm(ConfMsg))
		return true;
	else
		return false;
}

 
function any_checked(field){
	var any_checked = 0;
	if(field.length == undefined){
		if(field.checked) any_checked = 1;
	}
	else{
		for (i = 0; i < field.length; i++) {
			if(field[i].checked) any_checked++;
		}
	}
	return any_checked;
}

 
function gotopage(url_query){
	var mainUrl = 'http://www.renzoo.co.uk/'; // deprecated: var mainUrl = 'http://www.renzoo.co.uk/index.php?sid=' + get_session() + '&';
	document.location.href = mainUrl + url_query;
}

 
function feedback_send_ok(){
	main_menu('home')
}

 
function invite_send_ok(){
	main_menu('home')
}

 
function ab_reload(){
	ajax_req('show_ab_contacts');
}

 
function ab_recent_reload(){
	$('AdressBookrecent').innerHTML = 'Loading...';
	ajax_req('show_ab_recent');
}

 
function ab_delrow(){
	for (i = 0; i < delrow.length; i++) {
		Element.hide('abrow1' + delrow[i]);
		Element.hide('abrow2' + delrow[i]);
	}
}

 
function ab_delform(){
	editcontid = 0;
	$('nc_name').value  = $('nc_mobile').value = $('nc_mobile2').value = $('nc_email').value = $('nc_email2').value = $('nc_lastname').value = '';
}

 
function recharge_account(){
	Element.hide('account_new_credits_s1');
	Element.hide('account_recharge_btn');
	ajax_req('submit_recharge');
}

 
function cancel_recharge_account(){
	Element.hide('account_new_credits');
	Element.show('account_new_credits_s1');
	Element.show('account_recharge_btn');
	Element.hide('worldpay');
	$('worldpay').src="";
}

 
function invite_preview(){
	var as;
	//if($('send_as_email').checked)
		as = 'email';
	//else if($('send_as_text').checked)
	//	as = 'sms';	
	windowprev = window.open('invite_preview.php?as=' + as,'invpreview','scrollbars=yes,resizable=yes,width=600,height=680');
	windowprev.focus();
}

 
function rcpt_list_all(id,what){
	if(what == 'm')
		all_mob_rcpt[all_mob_rcpt.length] = id;
	if(what == 'e')
		all_email_rcpt[all_email_rcpt.length] = id;
}

 
function ch_total_mob_rcp(value){
	total_mob_rcp += value;
	if(document.getElementById('sms_price') != undefined)
		sms_info();
}

 
function ch_total_mail_rcp(value){
	total_mail_rcp += value;
}

 
function ab_add_rcpe(id,remove,name_elid){
	
	if(document.getElementById('com_to_email') != undefined){
		if(!remove && $('ab_e' + id).checked){
			ch_total_mail_rcp(1);
			rcpt_list_all(id,'e');
			if(ab_sel_email[id] != 1){  
				ab_sel_email[id] = 1;
								if (!name_elid)
					name_elid = id;
				$('com_to_email').innerHTML += get_email_rcp_div(id,$('ab_email'+ id).innerHTML,$('ab_name'+ name_elid).innerHTML + ' ' + $('ab_lastname'+ name_elid).innerHTML,0,'ab_add_rcpe(\''+id+'\',1)');
			}
			else{  
				Element.show('to_e'+id);
				$('to_email_send'+id).value = 1;
			}
		}
		else{
			Element.hide('to_e'+id);
			$('to_email_send'+id).value = 0;
			ch_total_mail_rcp(-1);
			$('ab_e' + id).checked = false;
		}
	}
	else{
		alert("You cannot add email recipient.");
		$('ab_e' + id).checked = false;
	}
}

 
function ab_add_rcpm(id,remove,name_elid){
	
	if(document.getElementById('com_to_mobile') != undefined){
		if(!remove && $('ab_m' + id).checked){
			ch_total_mob_rcp(1);
			rcpt_list_all(id,'m');
			if(ab_sel_mob[id] != 1){ // if div not exists 
				ab_sel_mob[id] = 1;
								if (!name_elid)
					name_elid = id;
				$('com_to_mobile').innerHTML += get_mob_rcp_div(id,$('ab_mob'+ id).innerHTML,$('ab_name'+ name_elid).innerHTML + ' ' + $('ab_lastname'+ name_elid).innerHTML,0,'ab_add_rcpm(\''+id+'\',1)');
			}
			else{  
				Element.show('to_m'+id);
				$('to_mobile_send'+id).value = 1;
			}
		}
		else{
			Element.hide('to_m'+id);
			$('to_mobile_send'+id).value = 0;
			ch_total_mob_rcp(-1);
			$('ab_m' + id).checked = false;
		}
		upd_msg_cost();
	}
	else{
		alert("You cannot add mobile recipient.");
		$('ab_m' + id).checked = false;
	}
}

 
function to_add_rcpm(id,value,mymobile){
	
	if(id){
		Element.hide('to_m'+id);
		$('to_mobile_send'+id).value = 0;
		ch_total_mob_rcp(-1);
	}
	else if(ab_sel_mob[id] != 1){  
		if(mymobile || valid_form('add_to_rcpm',1)){
			ch_total_mob_rcp(1);
			var id = Math.ceil(Math.random()*100000000);
			rcpt_list_all(id,'m');
			ab_sel_mob[id] = 1;
			var name = '';
			if(mymobile)
				name = '<b>My mobile</b> ';
			$('com_to_mobile').innerHTML += get_mob_rcp_div(id,value,name,1,'$(\'btn_add_my_mobile\').disabled=false;to_add_rcpm('+id+')');
			$('to').value = '';
		}
	}
	upd_msg_cost();
}

 
function to_add_rcpe(id,value){
	
	if(id){
		Element.hide('to_e'+id);
		$('to_email_send'+id).value = 0;
		ch_total_mail_rcp(-1);
	}
	else if(ab_sel_email[id] != 1){  
		if(valid_form('add_to_rcpe',1)){
			ch_total_mail_rcp(1);
			var id = Math.ceil(Math.random()*100000000);
			rcpt_list_all(id,'e');
			ab_sel_email[id] = 1;
			$('com_to_email').innerHTML += get_email_rcp_div(id,value,'',1,'to_add_rcpe('+id+')');
			$('copy2email').value = '';
		}
	}
}

 
function get_email_rcp_div(id,email,name,user_enter,funct){
	var temp = '';
	if(!user_enter)
		temp = '[' + email + ']';
	else
		temp = email;
	return '<div id="to_e'+id+'">&bull; <b>' + name + '</b> ' + temp +
	'<input type="hidden" id="to_email'+id+'"      value="'+ email+'" />' + 
	'<input type="hidden" id="to_email_name'+id+'" value="'+name+'" />' + 
	'<input type="hidden" id="to_email_send'+id+'" value="1" />' + 
	'<input type="hidden" id="to_email_user_entered'+id+'" value="'+user_enter+'" />' + 
	' | <a href="javascript:'+funct+';">Remove</a>' +
	'</div>';
}

 
function get_mob_rcp_div(id,msisdn,name,user_enter,funct){
	var temp = '';
	var mfromab = 0;
	msisdn = String(msisdn);
	if(!user_enter)
		temp = '[+' + msisdn + ']';
	else{
		if(msisdn.charAt(0) == '+'){
			temp = msisdn;
			msisdn = msisdn.substr(1);
		}
		else
			temp = '+' + msisdn;
	}
	if(name)
		mfromab = 1;
	
	return '<div id="to_m'+id+'">&bull; <b>' + name + '</b> ' + temp +
	'<input type="hidden" id="mfromab'+id+'" value="'+mfromab+'" />' + 
	'<input type="hidden" id="to_mobile_name'+id+'" value="'+name+'" />' + 
	'<input type="hidden" id="to_msisdn'+id+'" value="'+ msisdn +'" />' + 
	'<input type="hidden" id="to_mobile_send'+id+'" value="1" />' + 
	'<input type="hidden" id="to_mobile_user_entered'+id+'" value="'+user_enter+'" />' + 
	' | <a href="javascript:'+funct+';">Remove</a>' +
	'</div>';
}

 
function upload_file(action){
	$('composer_form').action = 'upload.php?action=' + action + '&composer=' + open_composer;
	$('composer_form').submit();
	Element.hide('attachment_upl_form');
	Element.show('upload_wait');
	uploading = 1;
}

 
function cancel_upload(){
	$('hidframe_div').innerHTML = '';
	clear_upl_field();
	Element.show('attachment_upl_form');
	Element.hide('upload_wait');
	uploading = 0;
	$('hidframe_div').innerHTML = '<iframe name="hidframe" id="hidframe" width="300" height="300" src="upload.php" style="display:none;"></iframe>';
}

 
function add_attachment(file_id,file_name_htmlspec,file_size,temp_file,file_content_type,picture_url,encodedmp3){

	if(open_composer == 'e'){
		$('attachment_list').innerHTML += '<div id="f'+file_id+'">&bull; <b>'+file_name_htmlspec+'</b> '+file_size+' &nbsp;<a href="javascript:$(\'attach_'+file_id+'\').value=0;total_attach-=1;hideEl(\'f'+file_id+'\');">Remove</a> <input id="file_data_'+file_id+'" type="hidden" value="'+temp_file+'|'+file_name_htmlspec+'|'+file_content_type+'" /><input id="attach_'+file_id+'" type="hidden" value="1" /><input name="file_index['+file_id+']" id="file_index" type="hidden" value="'+file_id+'" /></div>';
		Element.show('attachment_upl_form');
	}
	else if(open_composer == 'p'){
		$('attachment_list').innerHTML += '<div id="f'+file_id+'"><p align="center"><img src="'+picture_url+'" class="pcomimage" /><br/><b>'+file_name_htmlspec+'</b> '+file_size+' &nbsp;<a href="javascript:$(\'attach_'+file_id+'\').value=0;Element.show(\'attachment_upl_form\');total_attach-=1;hideEl(\'f'+file_id+'\');">Remove</a> <input id="file_data_'+file_id+'" type="hidden" value="'+temp_file+'|'+file_name_htmlspec+'|'+file_content_type+'" /><input id="attach_'+file_id+'" type="hidden" value="1" /><input name="file_index['+file_id+']" id="file_index" type="hidden" value="'+file_id+'" /></p></div>';
		Element.hide('attachment_upl_form');
	}
	else if(open_composer == 'm'){
		Element.hide('attachment_upl_form');
		if(encodedmp3 != '0'){
			$('attachment_list').innerHTML += '<div id="f'+file_id+'"><div id="flplayer'+file_id+'"></div><p align="center"><b>'+file_name_htmlspec+'</b> '+file_size+' &nbsp;<a href="javascript:$(\'attach_'+file_id+'\').value=0;Element.show(\'attachment_upl_form\');total_attach-=1;$(\'flplayer'+file_id+'\').innerHTML=\'\';hideEl(\'f'+file_id+'\');">Remove</a> <input id="file_data_'+file_id+'" type="hidden" value="'+temp_file+'|'+file_name_htmlspec+'|'+file_content_type+'" /><input id="attach_'+file_id+'" type="hidden" value="1" /><input name="file_index['+file_id+']" id="file_index" type="hidden" value="'+file_id+'" /></p></div>';
			 
			show_flash_player('flplayer'+file_id,'/pull_music.php/'+get_session()+'/'+encodedmp3);
		}
		else  
			$('attachment_list').innerHTML += '<div id="f'+file_id+'"><p align="center"><img src="'+picture_url+'" /><br/>No preview available<br /><b>'+file_name_htmlspec+'</b> '+file_size+' &nbsp;<a href="javascript:$(\'attach_'+file_id+'\').value=0;Element.show(\'attachment_upl_form\');total_attach-=1;hideEl(\'f'+file_id+'\');">Remove</a> <input id="file_data_'+file_id+'" type="hidden" value="'+temp_file+'|'+file_name_htmlspec+'|'+file_content_type+'" /><input id="attach_'+file_id+'" type="hidden" value="1" /><input name="file_index['+file_id+']" id="file_index" type="hidden" value="'+file_id+'" /></p></div>';
	}

	total_attach += 1;
	clear_upl_field();
	$('attachment_file_url').value = 'http://';
	uploading = 0;
	Element.hide('upload_wait');
}


 
function show_flash_player(elid,mp3url){
	var so = new SWFObject('flash_player/mp3player.swf','mpl','100%','20','7');
	so.addParam('allowfullscreen','true');
	so.addVariable('file',mp3url);
	so.addVariable('height','20');
	so.addVariable('width','320');
	so.addVariable('frontcolor','0xFFFFFF');
	so.addVariable('backcolor','0x611a77');
	so.addVariable('lightcolor','0xfcb034');
	so.write(elid);
}


 
function clear_upl_field(){
	upload_field_cache = '';
	$('upl_browse_field').innerHTML = '';
	$('upl_browse_field').innerHTML = '<input name="attachment_file" type="file" class="w100" id="attachment_file" oncontextmenu="return false;" onkeydown="return false;" onfocus="handle_upload_field()" onclick="this.focus()" autocomplete="off" />';
}

 
function upd_msg_cost(){
	msg_cost = 0;
	if (document.getElementById("messsage_price") != undefined){
		var price = total_mob_rcp * 15;
		$("messsage_price").innerHTML = price;
		msg_cost = price;
	}
}

 
function can_add_rcptm(chkbx){
	if(crlft < 15){
		alert("You don't have enough credits to send a message. Please buy new credits.");
		if(chkbx)
			$(chkbx).checked = false;
		return false;
	}
	return true;
}

 
function edit_contact(){
	var id = 0;
	
	for (var i in editcont){
		if(editcont[i] && $('nc_name'))
			id = editcont[i]
	}
	
	if(id){
		ab_delform();
		$('nc_name').value     = $('ab_name' + id).innerHTML;
		$('nc_lastname').value = $('ab_lastname' + id).innerHTML;
		if (document.getElementById('ab_mob' + id) != undefined)
			$('nc_mobile').value   = $('ab_mob' + id).innerHTML;
		if (document.getElementById('ab_email' + id) != undefined)
			$('nc_email').value    = $('ab_email' + id).innerHTML;
		if (document.getElementById('ab_mob2' + id) != undefined)
			$('nc_mobile2').value   = $('ab_mob2' + id).innerHTML;
		if (document.getElementById('ab_email2' + id) != undefined)
			$('nc_email2').value    = $('ab_email2' + id).innerHTML;
		
		editcontid = id;
		Element.show('new_contact');
	}
}

 
function new_contact(){
	if(editcontid){
		ab_delform();
	}
	showhide('new_contact');
}

 
function sms_info(){
	
	var sms_price = 12;
	var total_recipients = total_mob_rcp;
	var msg_num = 1;
	var msg_header,msg_max_len,sms_type,chars_left,msg_len;
	var max_msgs = 3;  
	
		
		if(detect_ucs2_char()){
		msg_header = 3;
		msg_max_len = 70;
		sms_type = 'binary';
		$('sms_enc').value = 'utf8';
	}
	else{
		msg_header = 7;
		msg_max_len = 160;
		sms_type = 'plain';
		$('sms_enc').value = 'ascii';
	}

	msg_len = $('message').value.length;
	
	 
	if(msg_len > msg_max_len){
		msg_max_len = msg_max_len - msg_header;
	}

	 
	total_max_chars = max_msgs * msg_max_len;
	if (msg_len > total_max_chars){
		$('message').value = $('message').value.substring(0,total_max_chars);
		msg_len = total_max_chars;
	}
	
	 
	msg_num = Math.ceil(msg_len / msg_max_len);

	
	 
	if (msg_len > msg_max_len)
		chars_left = ((msg_num) * msg_max_len) - msg_len;
	else
		chars_left = msg_max_len - msg_len;
	
		$('sms_char_left').innerHTML = chars_left;
	$('sms_price').innerHTML = msg_num * sms_price * total_recipients;
	$('sms_type').innerHTML = sms_type;
	$('sms_mum').innerHTML = msg_num * total_recipients;
	
}

 
function detect_ucs2_char()
{
	var sms_body = $('message').value;
	var long_header = 7;

	var ucs2 = false;
	for (i=0; i < sms_body.length; i++){
		if(sms_body.charCodeAt(i) > 127){
			ucs2 = true;
			break;
		}
	}
	return ucs2;
}

 
function opwin(url,name,prms){
	var dim = 'height=630,width=600';
	if(url == 'up')
		window.open('use_policy.php','up',dim);
//	else if(url == 'pp')
//		window.open('privacy_policy.php','pp',dim);
//	else if(url == 'ab')
//		window.open('about.php','ab',dim);
//	else if(url == 'ts')
//		window.open('terms_of_service.php','ts',dim);
	else
		window.open(url,name,prms);	
}

 
function send_pass(arg){
	
	if(ajaxResult == '1'){
		alert('Message with your temporary password has been sent to your email.');
		if(arg == 0){  
			showEl('div_login_form');
			hideEl('ch_passwd');
		}
		else{  
			gotopage('');
		}
		
	}
	else if(ajaxResult == '2'){
		alert('The address you entered is not registered with renzoo.');
	}
	else if(ajaxResult == '3'){
		alert('The verification word is not correct!');
	}
	else{
		alert('Error sending email with temporary password. Please try later or contact support at support@renzoo.co.uk');
	}
}

 
function ie_flash()
{		
	if(navigator.appName=="Microsoft Internet Explorer")
	{
		theObjects = document.getElementsByTagName("object");		
		for (var i = 0; i < theObjects.length; i++)
		{
			theObjects[i].outerHTML = theObjects[i].outerHTML;
		}
	}
}

 
function handle_upload_field() {
	obj = $("attachment_file");
	if (obj.value.length > 0 ) {
		if (upload_field_cache != obj.value) {
			upload_field_cache = obj.value;
			if(valid_form('upload'))
				upload_file('upload');
		}
	}
}

 
function inv_login() {
	alert("Error: login failed. Invalid username or password.");
}

 
function unact_login() {
	alert("Error: login failed. Please activate your account. Click on activation link in email you received after registration.");
}

 
function cancel_msg() {
	
	var ask = 0;
	if(open_composer=='p' || open_composer=='t' || open_composer=='e' || open_composer=='m' || open_composer=='i' || open_composer=='f'){
		if(ask == 0 && document.getElementById('subject') != undefined)
			if($('subject').value) ask = 1;
		if(ask == 0 && document.getElementById('copy2email') != undefined)
			if($('copy2email').value) ask = 1;
		if(ask == 0 && document.getElementById('to') != undefined)
			if($('to').value) ask = 1;
		if(ask == 0 && document.getElementById('message') != undefined)
			if($('message').value) ask = 1;
		if(ask == 0 && total_attach != 0)
			ask = 1;
		if(ask == 0 && total_mob_rcp != 0)
			ask = 1;
		if(ask == 0 && total_mail_rcp != 0)
			ask = 1;
		if(uploading)
			ask = 1;
	}
	if(ask == 1)
		return yes_no("Your message has not been sent. Discard your message?");
	else
		return true;
}

 
function top10_nonick($uid){
	if(yes_no('Hey, you are one of the top 10 RenzooPushers but currently visible as ' + $uid + ' in the top 10 list. Please change this to something more recognizable by defining your Renzoo ID.'))
		main_menu('account','open_renzooID');
}

 
function msg_row_ov(row_el){
	row_el.className = 'msg_ov';
}

 
function msg_row_ou(row_el){
	row_el.className = 'msg_ou';
}

 
function sel_msg(checkid)
{
	if($(checkid).checked)
		sel_msgs[$(checkid).value] = true;
	else
		sel_msgs[$(checkid).value] = false;
}

 
function msgs_actions(folder,action,page){
	
	var msgs_comma = '';
	
	for (var i in sel_msgs){
		if(sel_msgs[i])
			 msgs_comma += ',' + i;
	}
	
	if(msgs_comma){
		eval_after_ajax = "ajax_req('"+folder+"',"+page+")";
		
		if(action == 'delete'){
			if(yes_no('Are you sure you want to delete selected messages?'))
			{
				ajax_req('delete_msgs',msgs_comma);
				sel_msgs = new Object();
			}
		}
		
		if(action == 'read'){
			ajax_req('read_msgs',msgs_comma);
			sel_msgs = new Object();
		}
		
		if(action == 'unread'){
			ajax_req('unread_msgs',msgs_comma);
			sel_msgs = new Object();
		}
	}
	else
		alert("Please select one or more messages.");
}

 
function msg_prew(msg_id,readed){
	
	if($('msg_prw'+msg_id).style.display == 'none'){
		Element.show('msg_prw'+msg_id);
		var temp1 = "Click to hide message content";
		$('msg_cl3'+msg_id).title = $('msg_cl4'+msg_id).title = $('msg_cl5'+msg_id).title = $('msg_cl6'+msg_id).title = temp1;
	}
	else{
		Element.hide('msg_prw'+msg_id);
		var temp3 = "Click to view message content";
		$('msg_cl3'+msg_id).title = $('msg_cl4'+msg_id).title = $('msg_cl5'+msg_id).title = $('msg_cl6'+msg_id).title = temp3;
	}
	
	if(loaded_msgs['l'+msg_id] != 1){
		$('msg_prwd'+msg_id).innerHTML = 'Loading...';
		ajax_req('msg_details',msg_id);
		loaded_msgs['l'+msg_id] = 1;
		 
		if(!readed){
			var unread = parseInt($('msg_unread_num').innerHTML);
			$('msg_unread_num_h').innerHTML = $('msg_unread_num').innerHTML = unread-1;
			var img_src = $('msg_img' + msg_id).src;
			$('msg_img' + msg_id).src = img_src.replace("_new", "_old");
			$('msg_cl3'+msg_id).className = $('msg_cl4'+msg_id).className = $('msg_cl5'+msg_id).className = $('msg_cl6'+msg_id).className = '';
		}
	}
}

 
var timeDiff  =  {
	setStartTime:function (){
		d = new Date();
		time  = d.getTime();
	},

	getDiff:function (){
		d = new Date();
		return (d.getTime()-time);
	}
}

 
function pressed_key(e){
	
	var characterCode
	if(e && e.which){
		e = e
		characterCode = e.which
	}
	else{
		e = event
		characterCode = e.keyCode
	}
	return characterCode
}





 
var acc_check_prog;
 
var tout_obj;

 
function ex_selected_acc(what,sel_all){
	var chkbox_id;
	var return_acc = new Array();
	return_acc.comma_string = '';
	return_acc.selected_num = 0;
	 
	
	if(what == 'filter')
		chkbox_id = 'ex_filter_sel';
	else
		chkbox_id = 'ex_mail_sel';	
	
	for(var i=1; i<100; i++){
		if(document.getElementById(chkbox_id + i) != undefined){
			if(sel_all || $(chkbox_id + i).checked){
				return_acc[return_acc.length] = $(chkbox_id + i).value;
				return_acc.comma_string += $(chkbox_id + i).value + ',';
				return_acc.selected_num += 1;
			}
		}
		else
			return return_acc;
	}	
}

 
function valid_ex_sel_acc(acc_selected,what){
	if(acc_selected == 0){
		if(what == 'filter')
			alert('Please select one or more message filters.');
		else
			alert('Please select one or more email accounts.');
		return 0;
	}
	else
		return 1;
}

 
function valid_ex(what,only_msg,acc_id){
	
	var msg;
	var msg0 = 'Please check data you entered:' + '\n\n';
	var msg = '';
	
	if(what == 'new_acc' || what == 'validate_acc' || what == 'save_acc'){
		if(what != 'validate_acc' && (!$('ex_email'+acc_id).value || !valid_email($('ex_email'+acc_id).value)))
			msg += 'Please enter valid email adress and password.\n';
		if(what != 'save_acc' && !$('ex_username'+acc_id).value)
			msg += 'Please enter your username.\n';
		if(!$('ex_pass'+acc_id).value)
			msg += 'Please enter your password.\n';
		if(what != 'save_acc' && !$('ex_port'+acc_id).value)
			msg += 'Please enter host port.\n';
		if(what != 'save_acc' && !$('ex_server'+acc_id).value && $('ex_protocol'+acc_id).value != 'webmail')
			msg += 'Please enter host name.\n';
	}
	
	else if(what == 'save_filter'){
		if(!$('ex_filter_name'+acc_id).value)
			msg += 'Please enter filter name.\n';
		if(!$('ex_filter_rule_text'+acc_id).value && $('rule_what'+acc_id).value != 'priority')
			msg += 'Please enter filter text.\n';
	}
	
	if(msg){
		if(only_msg)
			alert(msg)
		else
			alert(msg0 + msg)
		return false;
	}
	else
		return true;
	
}

 
function ex_show_details(what,objid){
	
	var selected_acc = ex_selected_acc(what);
	var div_id;
	
	if(what == 'filter')
		div_id = 'filter_det';
	else{
		div_id = 'acc_det';
		ex_ref_acc_stop();
	}
	
	if(objid){  
		Element.show(div_id + objid);
	}
	else{			if(valid_ex_sel_acc(selected_acc.selected_num)){
			for (i = 0; i < selected_acc.length; i++) {
				if($(div_id  + selected_acc[i]).style.display == 'none')
					Element.show(div_id + selected_acc[i]);	
			}
		}
	}
}

 
function ex_do_action(what,for_what,objid){
	
	var sel_all;
	var action;
	var url = get_ajax_url();
	var div_id;
	
	if(for_what == 'filter'){
		div_id = 'ex_filters_list';
		 
		if(what == 'ex_filter_dis_all' || what == 'ex_filter_en_all')
			sel_all = 1;
	}
	else
		div_id = 'ex_accounts_list';
	
	var selected_acc = ex_selected_acc(for_what,sel_all);
	
	if(objid){  
		doAjax_update(div_id,url + '&page=' + what,{acc:objid});
	}
	else if(valid_ex_sel_acc(selected_acc.selected_num,for_what)){
		if(what == 'ex_acc_del' && !yes_no('Are you sure you want to delete selected email accounts?'))
			return;
		else if(what == 'ex_filter_del' && !yes_no('Are you sure you want to delete selected filters?'))
			return;
		
		ex_ref_acc_stop();
		doAjax_update(div_id,url + '&page=' + what,{acc:selected_acc.comma_string});
	}
}

 
function ex_acc_save(acc_id,what){
	var url = get_ajax_url();
	
	if(what == 'filter'){
		if(valid_ex('save_filter',0,acc_id)){
			if(acc_id == 0){ //new
				Element.hide('ex_new_filter');
				$('ex_filters_list').innerHTML = 'Loading...';
				Element.show('ex_filters_list_all');	
			}
			doAjax_update('ex_filters_list',url + '&page=ex_save_filter',{filter_name:$('ex_filter_name'+acc_id).value,filter_id:acc_id,rule_what:$('rule_what'+acc_id).value, operator:$('ex_filter_oper'+acc_id).value, rule_text:$('ex_filter_rule_text'+acc_id).value,priority:$('ex_filter_prio'+acc_id).value, alert_type:radio_group_get_value('alert_action_radio')});
		}
	}
	else if(valid_ex('save_acc',0,acc_id)){
		if(acc_id == 0){ //new
			Element.hide('ex_new_account');
			$('ex_accounts_list').innerHTML = 'Loading...';
			Element.show('ex_accounts_list_all');
			
		}
		if ($('ex_protocol'+acc_id).value == "pop") {
			doAjax_update('ex_accounts_list',url + '&page=ex_save_acc',{enc_p:$('enc_p'+acc_id).value,acc_id:acc_id,filter_id:$('filter_id'+acc_id).value,folder_id:$('folder_id'+acc_id).value,email:$('ex_email'+acc_id).value,username:$('ex_username'+acc_id).value, pass:$('ex_pass'+acc_id).value, get_msg:$('ex_get_msg'+acc_id).value, port:$('ex_port'+acc_id).value,server:$('ex_server'+acc_id).value,protocol:$('ex_protocol'+acc_id).value});
		} else {
			// protocol is Webmail
			// send localhost as server name and 2001 as port number
			doAjax_update('ex_accounts_list',url + '&page=ex_save_acc',{enc_p:$('enc_p'+acc_id).value,acc_id:acc_id,filter_id:$('filter_id'+acc_id).value,folder_id:$('folder_id'+acc_id).value,email:$('ex_email'+acc_id).value,username:$('ex_username'+acc_id).value, pass:$('ex_pass'+acc_id).value, get_msg:$('ex_get_msg'+acc_id).value, port:"2001",server:"localhost",protocol:"pop"});
		}
	}
}

 
function ex_acc_verify(acc_id){
	var url = get_ajax_url();
	Element.hide('gmail_warning' + acc_id);
	if(valid_ex('validate_acc',0,acc_id)){
		Element.show('ex_new_mail_wait_msg' + acc_id);
		$('ex_acc_cancel_btt' + acc_id).disabled = true;
		eval_after_ajax = "check_mail_conect("+ acc_id + ",'" + $('ex_email'+acc_id).value + "');";

		 
		if ($('ex_protocol'+acc_id).value == "pop") {
			doAjax(url + '&page=pop3_verify',{enc_p:$('enc_p'+acc_id).value, acc_id:acc_id, email:$('ex_email'+acc_id).value, username:$('ex_username'+acc_id).value, pass:$('ex_pass'+acc_id).value, port:$('ex_port'+acc_id).value, server:$('ex_server'+acc_id).value, protocol:$('ex_protocol'+acc_id).value});
		} else {
			// protocol is Webmail
			// send localhost as server name and 2001 as port number
			doAjax(url + '&page=pop3_verify',{enc_p:$('enc_p'+acc_id).value, acc_id:acc_id, email:$('ex_email'+acc_id).value, username:$('ex_username'+acc_id).value, pass:$('ex_pass'+acc_id).value, port:2001, server:"localhost", protocol:"pop"});
		}
	}
}

 
function check_mail_conect(acc_id,email){
	
	Element.hide('ex_new_mail_wait_msg' + acc_id);
	$('ex_acc_cancel_btt' + acc_id).disabled = false;
	
	if(ajaxResult == 'ok'){
		alert('Verification successful. You can save account now.');
		Element.show('ex_acc_save_btt' + acc_id);
		$('acc_ver'+acc_id).value = 1;
		$('ex_acc_verify_btt' + acc_id).disabled = true;
		Element.hide('notfound_disclaimer');
	}
	else{
				if(email_explode(email).host == 'gmail.com'){
			Element.show('gmail_warning' + acc_id);
			if(yes_no('Error: Your email or password might have been entered incorrectly or your Gmail POP access has not been enabled. Do you want to learn how to enable Gmail POP access?'))
				main_menu('help_gmail');
		}
		else
			alert(ajaxResult);
	}
}

 
function ex_new()
{
	$('enc_p0').value = '';
	$('ex_email0').value = '';
	$('ex_username0').value = '';
	$('ex_pass0').value = '';

	$('ex_protocol0').value = 'pop';
	$('ex_port0').value = '110';
	$('ex_server0').value = '';
	mail_protocol_type_changed('0');
	
	if (total_acc == 0)
	{
		Element.hide('ex_accounts_list_all');
	}

	//Element.show('ex_email_accounts');
	//Element.hide('ex_no_acc_msg');
	Element.hide('ex_add_btt');
	Element.show('ex_new_account');
	
	Element.hide('det_user0');
	Element.show('det_pass0');
	Element.hide('det_host0');
	Element.hide('det_get_msg0');
	
	Element.show('ex_new_next_arr');
	Element.hide('ex_new_back_arr');
	$('ex_email0').readOnly = false;
	
	//$('ex_acc_save_btt0').disabled = true;
	//$('ex_acc_verify_btt0').disabled = true;
	
	Element.hide('ex_acc_save_btt0');
	Element.hide('ex_acc_verify_btt0');
	Element.show('ex_acc_cancel_btt0');
	
	Element.show('ex_email0');
	$('ex_new_email0').innerHTML = '';
	Element.hide('det_provider0');
	
	Element.show('sec_disclaimer');
	Element.hide('gmail_warning0');
	ex_ref_acc_stop();
	Element.hide('notfound_disclaimer');
}

 
function ex_filter_new(){
	
	$('ex_filter_rule_text0').value = '';
	$('ex_filter_name0').value = '';
	
	//Element.hide('ex_filters_list_all');
	Element.hide('ex_filter_add_btt');
	Element.show('ex_new_filter');
}

 
function ex_edit_cancel(acc_id,what){
	if(acc_id == 0){
		if(what == 'filter'){
			Element.hide('ex_new_filter');
			Element.show('ex_filter_add_btt');
			Element.show('ex_filters_list_all');
		}
		else{
			//if(total_acc == 0){
			//	Element.show('ex_no_acc_msg');
			//}
			//else{
				Element.show('ex_add_btt');
				Element.show('ex_accounts_list_all');
				Element.hide('ex_new_account');	
			//}
		}
	}
	else{
		if(what == 'filter')
			Element.hide('filter_det' + acc_id);
		else
			Element.hide('acc_det' + acc_id);
	}
}

 
function acc_guess_msg(){
	if(acc_check_prog){
		$('mail_domain').innerHTML = email_explode($('ex_email0').value).host;
		Element.show('ex_guess_msg');
	}
	Element.hide('ex_new_mail_wait_msg0');
}

 
function ex_new_next(ajax_result){
	
	var found;
	var acc_verified = 0;
	var email = $('ex_email0').value.trim();
	var i;
	
	 
	acc_check_prog = 0;
	clearTimeout(tout_obj);
	
	 
	if (!email || !valid_email(email) || !$('ex_pass0').value)
		alert('Please enter valid email adress and password.');
	 
	else
	{
		Element.hide('gmail_warning0');
		Element.hide('ex_guess_msg');
		
		 
		if (ex_used_emails != undefined)
		{
			for (i = 0; i < ex_used_emails.length; i++) 
			{
				if(ex_used_emails[i] == email)
				{
					alert('This email address is already registered');
					return;
				}	
			}
		}
		
		 
		if (!ajax_result && ajax_result != 'skip_verify')
		{
			var url = get_ajax_url();
			ajaxResult = '0';
			eval_after_ajax = "ex_new_next(ajaxResult);";
			tempOB = doAjax(url + '&page=mail_server_data',{email:email,pass:$('ex_pass0').value});
			
			 
			$('ex_new_next_arr').disabled = true;
			$('ex_acc_cancel_btt0').disabled = true;
			 
			Element.show('ex_new_mail_wait_msg0');
			Element.hide('ex_new_next_arr');
			Element.show('ex_new_next_skip');
			acc_check_prog = 1;
			if(email_explode($('ex_email0').value).host != 'gmail.com')
				tout_obj = setTimeout('acc_guess_msg()',10*1000+1000);
			return;
		}
		 
		else
		{
			 
			if (ajax_result == 'skip_verify')
			{
				eval_after_ajax = "";
				tempOB.transport.abort();
				ajax_result = '0';
			}
			
			ajaxResult = ajax_result;
			 
			$('ex_new_next_arr').disabled = false;
			$('ex_acc_cancel_btt0').disabled = false;
			Element.hide('ex_new_mail_wait_msg0');
			Element.hide('ex_new_next_skip');
			 
			Element.hide('sec_disclaimer');
		}

		 
		if (ajaxResult != '0')
		{
			 
			var server_data = ajaxResult.split(',');
			
			 
			if (server_data[0] == 'acc_verified')
			{
				Element.hide('det_user0');
				Element.hide('det_pass0');
				Element.hide('det_host0');
				Element.hide('det_pass0');
				Element.show('det_get_msg0');
				Element.hide('ex_acc_verify_btt0');
				Element.show('ex_acc_save_btt0');
				acc_verified = 1;
				$('ex_acc_provider0').innerHTML = server_data[1];
				$('ex_port0').value = server_data[3];
				$('ex_server0').value = server_data[2];
				if ($('ex_port0').value == '2001' && $('ex_server0').value == 'localhost')
					$('ex_protocol0').value = "webmail";
				else
					$('ex_protocol0').value = "pop";
				mail_protocol_type_changed('0');
				$('ex_username0').value = server_data[4];
			}
			 
			else
			{
				 
				if (server_data[4] == '1')
				{
					Element.show('ex_new_next_arr');
										if (email_explode($('ex_email0').value).host == 'gmail.com')
					{
						Element.show('gmail_warning0');
						if(yes_no('Error: Your email or password might have been entered incorrectly or your Gmail POP access has not been enabled. Do you want to learn how to enable Gmail POP access?'))
							main_menu('help_gmail');
					}
					else
						alert('Error: please check your email or password.');
					return;
				}
				 
				else
				{
					 
					if(server_data[4] == '0')
						Element.show('det_user0');
					else
						$('ex_username0').value = email;
							
					$('ex_port0').value = server_data[3];
					$('ex_server0').value = server_data[2];
					$('ex_acc_provider0').innerHTML = server_data[1];
				}
			}
		}
		 
		else
		{
			Element.show('det_user0');
			Element.show('det_host0');
			$('ex_acc_provider0').innerHTML = 'Unknown';
			Element.show('notfound_disclaimer');
		}
		
		 
		
		 
		$('ex_email0').readOnly = true;
		Element.hide('ex_new_next_arr');
		Element.show('ex_new_back_arr');
		Element.hide('ex_email0');
		$('ex_new_email0').innerHTML = $('ex_email0').value + '&nbsp;&nbsp;';
		Element.show('det_provider0');
		
		if (acc_verified != 1)
		{
			Element.show('det_pass0');
			Element.show('det_get_msg0');
			Element.show('ex_acc_verify_btt0');
		}
	}
}

 
function ex_new_back(){
	
	$('ex_email0').readOnly = false;
		Element.show('ex_new_next_arr');
	Element.hide('ex_new_back_arr');
	
	Element.hide('det_user0');
	Element.show('det_pass0');
	Element.hide('det_host0');
	Element.hide('det_get_msg0');	

	$('enc_p0').value = '';
	$('ex_username0').value = '';
	$('ex_pass0').value = '';
	$('ex_port0').value = '110';
	$('ex_server0').value = '';
	
	Element.hide('ex_acc_save_btt0');
	Element.hide('ex_acc_verify_btt0');
	
	Element.show('ex_email0');
	$('ex_new_email0').innerHTML = '';
	Element.hide('det_provider0');
	
	Element.show('sec_disclaimer');
	Element.hide('gmail_warning0');
	Element.hide('notfound_disclaimer');
}

 
function onchg_rule(value,filter_id){
	if(value == 'subject' || value == 'from' || value == 'to' || value == 'cc' || value == 'body'){
		Element.hide('ex_filter_prio_div' + filter_id);
		Element.show('ex_filter_oper_div' + filter_id);
	}
	else if(value == 'priority'){
		Element.hide('ex_filter_oper_div' + filter_id);
		Element.show('ex_filter_prio_div' + filter_id);
	}	
}

 
function email_explode(email){
	//new RegExp( "^.+@(.+)$" , "i" );
	//return email.match(emailExp);
	var objParts = {};	
	objParts.user = null;
	objParts.host = null;

	email.replace(
		new RegExp( "^(.+)@(.+)$" , "i" ),
		function( $0, $1, $2){
			objParts.user = $1;
			objParts.host = $2;
		}
	);

	return(objParts);
}

 
function ex_refrash_acc_list(){
	if($('ok_auto_refrash_list').value == 1){
		var url = get_ajax_url();
		doAjax_update('ex_accounts_list',url + '&page=refrash_mail_list');
	}
}

 
function ex_save_alert_control()
{
	var url = get_ajax_url();
	var alert_control = radio_group_get_value('alert_control_radio');
	$('alert_control_save').disabled = true;
	eval_after_ajax = "Element.show('alert_control_saved');";
	doAjax_update('alerts_text_status', url + '&page=save_alert_control', {new_alert_control: alert_control});
}

 
function alert_control_enable_save()
{
	$('alert_control_save').disabled = false;
	Element.hide('alert_control_saved');
}

 
function ex_save_alert_action()
{
	var url = get_ajax_url();
	var alert_action = radio_group_get_value('alert_action_radio');
	$('alert_action_save').disabled = true;
	eval_after_ajax = "Element.show('alert_action_saved');";
	doAjax(url + '&page=ch_filts_alert_type', {new_alert_action: alert_action});
}

 
function alert_action_enable_save()
{
	$('alert_action_save').disabled = false;
	Element.hide('alert_action_saved');
	Element.hide('account_mobile_changed');
	Element.hide('account_email_changed');
	alert_action_changed();
}

 
function ex_save_scheduler()
{
	if ($('time_start').value < $('time_end').value) 
	{
		var url = get_ajax_url();
	
		var days = '';
		($('she_1').checked) ? days+='1,' : days+='0,';
		($('she_2').checked) ? days+='1,' : days+='0,';
		($('she_3').checked) ? days+='1,' : days+='0,';
		($('she_4').checked) ? days+='1,' : days+='0,';
		($('she_5').checked) ? days+='1,' : days+='0,';
		($('she_6').checked) ? days+='1,' : days+='0,';
		($('she_0').checked) ? days+='1' : days+='0';
	
		$('scheduler_save').disabled = true;
		eval_after_ajax = "Element.show('scheduler_saved');";
		
		doAjax(url + '&page=save_scheduler',{user_time_zone:$('user_time_zone').value,days:days,time_start:$('time_start').value,time_end:$('time_end').value,max_daily_alerts:$('max_daily_alerts').value});
	} 
	else 
	{
		alert("Start time must be before end time.");
		$('time_start').focus();
	}
}

 
function scheduler_enable_save()
{
	$('scheduler_save').disabled = false;
	Element.hide('scheduler_saved');
}


 
function ex_ref_acc_stop(){
	$('ok_auto_refrash_list').value = 0;
}

 
function ex_save_acc_dis(acc_id){
	Element.hide('ex_acc_save_btt' + acc_id);
	$('ex_acc_verify_btt' + acc_id).disabled = false;
}

 
function ex_no_acc(no_acc)
{
	//Element.hide('ex_accounts_list_all');
	//ex_new();
	//Element.hide('ex_acc_cancel_btt0');
	//Element.show('ex_email_accounts');
	//Element.show('ex_alert_rules');

	//Element.hide('ex_accounts_list_all');
	//Element.show('ex_no_acc_msg');

	if (no_acc)
	{
		Element.hide('active_email_accounts_num');
		Element.show('no_active_email_accounts');
		Element.show('email_accounts_hint');
	}
	else
	{
		Element.show('active_email_accounts_num');
		Element.hide('no_active_email_accounts');
		Element.hide('email_accounts_hint');
	}
	
	update_alerts_text_status();
}

 
function ex_sett_menu(el)
{
	if ($(el).style.display != 'block')
	{
		$(el).style.display = 'block';
		$(el+'_bull').className = 'sec_bull_cl';
		$(el+'_info').style.display = 'none';
		$(el+'_tx').innerHTML = 'hide';
		writeCookie(el, "1", 240);
	}
	else 
	{
		$(el).style.display = 'none';
		$(el+'_bull').className = 'sec_bull_op';
		$(el+'_info').style.display = 'block';
		$(el+'_tx').innerHTML = 'configure';
		writeCookie(el, "0", 240);
	}
	
	if (el == 'ex_email_accounts')
	{
		if (total_acc == 0)
		{
			ex_new();
			$('ex_email0').focus();
		}
	}
	else if (el == 'ex_alert_sched')
	{
		Element.hide('alert_control_saved');
		Element.hide('alert_action_saved');
		Element.hide('account_mobile_changed');
		Element.hide('account_email_changed');
		Element.hide('scheduler_saved');
		
		var alert_action = radio_group_get_value('alert_action_radio');
		if (alert_action != 'email' && !mobile_number)
		{
			$('new_mobile').focus();
			show_alerts_tab('alert_action');
			showone_hide('account_change_mobile');
		}
		else if (alert_action == 'email' && !email_alert_service_address)
		{
			$('email2sms_email').focus();
			show_alerts_tab('alert_action');
			showone_hide('email2sms');
		}
	}
}

 
function ex_bull_mover(el,action){
	var img_src = el.src;
	if(action == 1)
		el.src = img_src.replace(".gif", "_h.gif");
	else
		el.src = img_src.replace("_h.gif", ".gif");
}

 
function sel_all(what,seldes){

		for(var i=1; i < 200 ; i++){
		if (document.getElementById(what+i) != undefined){
			if(document.getElementById(what+i).style.display != 'none'){
				if(seldes == 0)
					document.getElementById(what+i).checked = false;
				else
					document.getElementById(what+i).checked = true;
			}
		}
		else
			break;
	}
}

 
function writeCookie(name, value, hours){
	var expire = "";
	if(hours != null){
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + escape(value) + expire;
}


 
function readCookie(name){
	var cookieValue = "";
	var search = name + "=";
	if(document.cookie.length > 0){ 
		offset = document.cookie.indexOf(search);
		if (offset != -1){ 
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) 
				end = document.cookie.length;
			cookieValue = unescape(document.cookie.substring(offset, end))
		}
	}
	return cookieValue;
}

 
function reset_and_hide_account_change_mobile_form(){
	Element.hide('account_change_mobile');
	if ($('account_change_mobile_s1'))
	{
		Element.show('account_change_mobile_s1');
		Element.hide('account_confirm_code');
		Element.hide('account_mobile_changed');
		Element.hide('account_email_changed');
		$('new_mobile').value = '';
	}
}

 
function reset_and_hide_account_change_email_form(){
	Element.hide('email2sms');
	if ($('account_change_email_s1'))
	{
		Element.show('account_change_email_s1');
		Element.hide('account_confirm_code_email');
		Element.hide('account_email_changed');
		$('email2sms_email').value = '';
	}
}

 
function mail_protocol_type_changed(account_id) {
	if ($('ex_protocol' + account_id).value == 'webmail') {
		Element.hide('ex_server_field' + account_id);
		Element.hide('ex_port_field' + account_id);
	} else {
		Element.show('ex_server_field' + account_id);
		Element.show('ex_port_field' + account_id);
	}
}


function radio_group_get_value(id)
{
	for (var i = 1; ; ++i)
	{
		var radio_elem = $(id+i);
		if (!radio_elem)
			break;
		if (radio_elem.checked)
			return radio_elem.value;
	}
	}

function radio_group_set_value(id, value)
{
	for (var i = 1; ; ++i)
	{
		var radio_elem = $(id+i);
		if (!radio_elem)
			break;
		if (radio_elem.value == value)
			radio_elem.checked = true;
	}
}

function radio_group_enable(id)
{
	for (var i = 1; ; ++i)
	{
		var radio_elem = $(id+i);
		if (!radio_elem)
			break;
		radio_elem.disabled = false;
	}
}

function radio_group_disable(id)
{
	for (var i = 1; ; ++i)
	{
		var radio_elem = $(id+i);
		if (!radio_elem)
			break;
		radio_elem.disabled = true;
	}
}

function alert_action_changed()
{
	var alert_action = radio_group_get_value('alert_action_radio');
	var i;
	if (alert_action == 'smspush')
	{
		Element.show('account_mobile_edit'); 
		Element.hide('email2sms_gw_edit');
		$('alert_action_hint_txt').innerHTML = "Email alerts come as text messages with a link to the new email(s).<p><strong>NOTE</strong>: If your phone does not have the option to open a link (e.g. http://...) please use <strong>Send service message (WAP push)</strong> option.</p>";
		$('alert_action_hint_img').src = "skins/skin2/images/alert_action_hint_smspush.PNG";
		i = 1;
	}
	else if (alert_action == 'wappush')
	{
		Element.show('account_mobile_edit'); 
		Element.hide('email2sms_gw_edit');
		$('alert_action_hint_txt').innerHTML = "Email alerts come as service messages (special text messages which include a link to the email). Normally this is quicker and easier way of accessing your emails. On same phones this type of message arrive to different places than text message inbox (e.g. WAP push inbox, bookmarks).<p><strong>IMPORTANT</strong>: Some network operators might block WAP service messages.</p>";
		$('alert_action_hint_img').src = "skins/skin2/images/alert_action_hint_wappush.PNG";
		i = 2;
	}
	else if (alert_action == 'sms')
	{
		Element.show('account_mobile_edit'); 
		Element.hide('email2sms_gw_edit');
		$('alert_action_hint_txt').innerHTML = "This is a text with the limited info (160 chars) about the email in the following format:<p><em>sender's email address##Subject#beggining of message body</em></p>Use this option when you do not require direct access to the full content of the email.";
		$('alert_action_hint_img').src = "skins/skin2/images/alert_action_hint_sms.PNG";
		i = 3;
	}
	else if (alert_action == 'smslong')
	{
		Element.show('account_mobile_edit'); 
		Element.hide('email2sms_gw_edit');
		$('alert_action_hint_txt').innerHTML = "This is same as <strong>Send Text (SMS)</strong> with the difference that text message info can be 480 chars max.";
		$('alert_action_hint_img').src = "skins/skin2/images/alert_action_hint_smslong.PNG";
		i = 4;
	}
	else if (alert_action == 'email')
	{
		Element.hide('account_mobile_edit'); 
		Element.show('email2sms_gw_edit');
		$('alert_action_hint_txt').innerHTML = "With this option you can make use of your operator's free email alert service (aka email2sms), if they provide one for your type of subscription. Many network operators provide free email alert services which allow their subscribers to receive text notifications for emails sent to their MSISDN@operator's domain email address.<p><strong>NOTE</strong>: Some operators might require you to activate the use of email alert service.</p>";
		$('alert_action_hint_img').src = "skins/skin2/images/alert_action_hint_email.PNG";
		i = 5;
	}
	
	$('alert_action_hint_arrow').style.top = (i*20 - 12) + 'px';
	
	update_alerts_text_status();
}

function update_alerts_text_status()
{
	var alert_action = radio_group_get_value('alert_action_radio');

	var tmp_flag = false;

	if (alert_action != 'email' && !mobile_number)
	{
		Element.show('mobile_not_defined');
		$('alerts_hint_text').innerHTML = "You need to enter your mobile number to start getting email alerts on your mobile."
		tmp_flag = true;
	}
	else
		Element.hide('mobile_not_defined');

	if (alert_action == 'email' && !email_alert_service_address)
	{
		Element.show('email_alert_service_address_not_defined');
		$('alerts_hint_text').innerHTML = "You need to enter the email address of your email alert service to start getting email alerts on your mobile."
		tmp_flag = true;
	}
	else
		Element.hide('email_alert_service_address_not_defined');
		
	if (tmp_flag && total_acc > 0)
		Element.show('alerts_hint');
	else
		Element.hide('alerts_hint');
	

	if (!(alert_action != 'email' && !mobile_number) && !(alert_action == 'email' && !email_alert_service_address))
		Element.show('alerts_text_status');
	else
		Element.hide('alerts_text_status');
}

function show_alerts_tab(tab)
{
	if (tab == 'alert_control')
	{
		Element.show('alert_control');
		Element.removeClassName('alert_control_tab', 'unselected');
		Element.addClassName('alert_control_tab', 'selected');
	}
	else
	{
		Element.hide('alert_control');
		Element.removeClassName('alert_control_tab', 'selected');
		Element.addClassName('alert_control_tab', 'unselected');
	}

	if (tab == 'alert_action')
	{
		Element.show('alert_action');
		Element.removeClassName('alert_action_tab', 'unselected');
		Element.addClassName('alert_action_tab', 'selected');
	}
	else
	{
		Element.hide('alert_action');
		Element.removeClassName('alert_action_tab', 'selected');
		Element.addClassName('alert_action_tab', 'unselected');
	}

	if (tab == 'scheduler')
	{
		Element.show('scheduler');
		Element.removeClassName('scheduler_tab', 'unselected');
		Element.addClassName('scheduler_tab', 'selected');
	}
	else
	{
		Element.hide('scheduler');
		Element.removeClassName('scheduler_tab', 'selected');
		Element.addClassName('scheduler_tab', 'unselected');
	}
	
	Element.hide('alert_control_saved');
	Element.hide('alert_action_saved');
	Element.hide('account_mobile_changed');
	Element.hide('account_email_changed');
	Element.hide('scheduler_saved');
}

function mailbox_check_all(flag)
{
	for (var i = 1; ; ++i)
	{
		var check_elem = $('msg_check'+i);
		if (!check_elem)
			break;
		check_elem.checked = flag;
		sel_msg($('msg_check'+i));
	}
}

function mailbox_update()
{
	for (var i in sel_msgs)
	{
		for (var j = 1; ; ++j)
		{
			var check_elem = $('msg_check'+j);
			if (!check_elem)
				break;
			if (check_elem.value == i)
				check_elem.checked = sel_msgs[i];
		}
	}
}

function mailbox_select(operation)
{
	if (operation == 'all')
	{
		mailbox_check_all(true);
	}
	else if (operation == 'none')
	{
		sel_msgs = new Object();
		mailbox_check_all(false);
	}
}

