﻿
function OpenMenu(id){ 
		document.getElementById(id).style.display = 'block';
	 document.getElementById("mainNavi").style.zIndex = 4;
	 document.getElementById("mainNavi").style.height = '163px';
}

function CloseMenu(id){ 
		document.getElementById(id).style.display = 'none';
	 document.getElementById("mainNavi").style.zIndex = 2;
	 document.getElementById("mainNavi").style.height = '33px';
}


function validateMailForm(fm) {
	// msg panel hide
	$('result_pane').hide();
	
	// validate
	validator = new Validation('fmfriend', {onSubmit:false});
	// Message	
	Validation.addAllThese([
     		['required', 'Please enter information in this field.', function(v) {
       		return !Validation.get('IsEmpty').test(v);
     		}],
		['validate-email', 'Please enter a valid email address. For example, user@kawaii-tokyo.com', function (v) {
			return Validation.get('IsEmpty').test(v) || /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(v);
		}]
	]); 
	
	if (validator.validate()) {
		new Ajax.Request(
				"/confirmfriendmail.do",
				{
					method		: 'post',
					postBody	: Form.serialize(fm),
					onSuccess	: processRes,
					onFailure	: errorHandler
				}
		);
	}
	return false;

}

function sendMailForm(fm) {
	var ck_sending = false;
	
	if (!ck_sending) {
		ck_sending = true;
		new Ajax.Request("/sendafriend.do",
			{
				method		: 'post',
				postBody	: Form.serialize(fm),
				onSuccess	: processRes,
				onFailure	: errorHandler
			}
		);
		ck_sending = false;
	}

}

function processRes(xhr) {
	//alert(xhr.responseText);
	var rtn = xhr.responseText.evalJSON(true);

	if (rtn.result == "success") {
		mbox = '<div id="wrapperP">\n<div id="formBoxB">\n' 
			+ '<div style="padding:20px 0;"><img src="/tellafriend/images/tellafriend_logo2.gif" alt="" border="0"></div>\n'
			+ '<div style="padding:15px 0;"><p>Your message has been sent to the following friends:</p>'
			+ '<p id="friends_mail">' + rtn.message + '</p></div>'
			+ '<div style="padding:23px 0;"><a href="javascript:void(0);" onClick="Modalbox.hide({beforeHide: function() { alert(\'Before hide callback\'); } })">'
			+ '<img src="/tellafriend/images/btn_close.gif" name="cancel_btn" border="0" id="close_btn" value="Close" /></a>'
			+ '</div></div>';
			
		Modalbox.show(mbox, {title: 'Tell a Friend', width: 500, height: 470});
		
	} else if (rtn.result == "continue") {
		mbox = '<div id="wrapperP">\n<div id="formBox">\n' 
			+ '<div class="taC"><img src="/tellafriend/images/tellafriend_logo.gif" alt="" width="68" height="66" border="0"></div>\n'
			+ '<form action="/sendafriend.do" method="post" name="fmfriend" id="fmfriend" accept-charset="utf-8">'
			+ '<table width="450" border="0" cellpadding="0" cellspacing="0">'
			+ '<tr><td width="200" class="icon">Your Name</td><td width="360" align="left">' + rtn.your_name + '</td></tr>'
			+ '<tr><td class="icon">Your email</td><td align="left">' + rtn.your_email + '</td></tr>'
			+ '<tr><td valign="top" class="icon">Friend\'s email</td><td align="left" valign="top">' + rtn.friends_email + '</td></tr>'
			+ '<tr><td valign="top" class="icon">Message</td><td width="360" align="left">' + rtn.send_msg + '</td></tr>'
			+ '<tr><td colspan="2" align="center" style="padding-top:15px;">'
			+ '<div id="result_pane" class="result-advice" style="display:none;"></div>'
			+ '<input type="image" onClick="sendMailForm(document.fmfriend);return false;" value="continue" src="/tellafriend/images/btn_send.gif" alt="send" />\n'
			+ '<input type="image" src="/tellafriend/images/btn_cancel.gif" alt="cancel" onClick="Modalbox.hide(); return false;" class="sub" value=" Cancel "></td></tr>'
			+ '</table></form>'
			+ '</div></div>';
		Modalbox.show(mbox, {title: 'Tell a Friend', width: 500, height: 430});

	} else {
		$('result_pane').innerHTML = rtn.message;
		$('result_pane').show();
	}
}

function errorHandler() {
	$('result_pane').innerHTML = "Network protocol error.";
	$('result_pane').show();
}
