﻿/*Author: Andon*/

/*用发Email推荐给好友*/
var he;
function SendToFriend(url) {
	// he=heightt
	$.get("/BaseAppCode/SendToFriend.ashx?tourl=" + escape(url), function(data) {
		// create a modal dialog with the data
		$(data).modal({
			close: false,
			overlayId: 'contact-overlay',
			containerId: 'contact-container',
			onOpen: contact1.open1,
			onShow: contact1.show1,
			onClose: contact1.close1
		});
	});
}

var contact1 = {
	message1: null,
	open1: function(dialog) {
		// add padding to the buttons in firefox/mozilla
		if ($.browser.mozilla) {
			$('#contact-container .contact-button').css({
				'padding-bottom': '2px'
			});
		}
		// input field font size
		if ($.browser.safari) {
			$('#contact-container .contact-input').css({
				'font-size': '.9em'
			});
		}

		var title = $('#contact-container .contact-title').html();
		$('#contact-container .contact-title').html('Loading...');
		dialog.overlay.fadeIn(200, function() {
			dialog.container.fadeIn(200, function() {
				dialog.data.fadeIn(200, function() {
					$('#contact-container .contact-content').animate({
						height: 250
					}, function() {
						$('#contact-container .contact-title').html(title);
						$('#contact-container form').fadeIn(200, function() {
							$('#contact-container #contact-name').focus();

							// fix png's for IE 6
							if ($.browser.msie && $.browser.version < 7) {
								$('#contact-container .contact-button').each(function() {
									if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
										var src = RegExp.$1;
										$(this).css({
											backgroundImage: 'none',
											filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + src + '", sizingMethod="crop")'
										});
									}
								});
							}
						});
					});
				});
			});
		});
	},
	show1: function(dialog) {
		$('#contact-container .contact-send').click(function(e) {
			e.preventDefault();
			// validate form
			if (contact1.validate()) {
				$('#contact-container .contact-message').fadeOut(function() {
					$('#contact-container .contact-message').removeClass('contact-error').empty();
				});
				$('#contact-container .contact-title').html('正在发送...');
				$('#contact-container form').fadeOut(200);
				$('#contact-container .contact-content').animate({
					height: '80px'
				}, function() {
					$('#contact-container .contact-loading').fadeIn(200, function() {
						$.ajax({
							url: '/BaseAppCode/SendToFriend.ashx',
							data: $('#contact-container form').serialize() + '&action=send',
							type: 'post',
							cache: false,
							dataType: 'html',
							complete: function(xhr) {
								$('#contact-container .contact-loading').fadeOut(200, function() {
									$('#contact-container .contact-title').html('Email已发出&nbsp;<a href=\"#\" onclick=\"$.modal.close();\">返回</a>');
									$('#contact-container .contact-message').html(xhr.responseText).fadeIn(200);
								});
							},
							error: contact1.error
						});
					});
				});
			}
			else {
				if ($('#contact-container .contact-message:visible').length > 0) {
					var msg = $('#contact-container .contact-message div');
					msg.fadeOut(200, function() {
						msg.empty();
						contact1.showError();
						msg.fadeIn(200);
					});
				}
				else {
					$('#contact-container .contact-message').animate({
						height: '30px'
					}, contact1.showError);
				}

			}
		});
	},
	close1: function(dialog) {

		$('#contact-container .contact-message').fadeOut();
		$('#contact-container .contact-title').html('请稍候...');
		$('#contact-container form').fadeOut(200);
		$('#contact-container .contact-content').animate({
			height: 40
		}, function() {
			dialog.data.fadeOut(200, function() {
				dialog.container.fadeOut(200, function() {
					dialog.overlay.fadeOut(200, function() {
						$.modal.close();
					});
				});
			});
		});
	},
	error: function(xhr) {
		alert(xhr.statusText);
	},
	validate: function() {
		contact1.message1 = '';
		if (!$('#contact-container #contact-tit').val()) {
			contact1.message1 += '好友名称必填';
		}
		/*验证Email*/
		var email = $('#contact-container #contact-email').val();
		if (!email) {
			contact1.message1 += 'Email地址必填';
		}
		else {
			if (!contact1.validateEmail1(email)) {
				contact1.message1 += 'Email地址无效';
			}
		}

		if (!$('#contact-container #contact-message').val()) {
			contact1.message1 += '邮件内容必须填写';
		}

		if ($('#contact-container #contact-message').val().length > 150) {
			contact1.message1 += '邮件内容字符不能超过150个';
		}

		if (contact1.message1.length > 0) {
			return false;
		}
		else {
			return true;
		}
	},
	validateEmail1: function(email) {
		var at = email.lastIndexOf("@");

		// Make sure the at (@) sybmol exists and  
		// it is not the first or last character
		if (at < 1 || (at + 1) === email.length)
			return false;

		// Make sure there aren't multiple periods together
		if (/(\.{2,})/.test(email))
			return false;

		// Break up the local and domain portions
		var local = email.substring(0, at);
		var domain = email.substring(at + 1);

		// Check lengths
		if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
			return false;

		// Make sure local and domain don't start with or end with a period
		if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
			return false;

		// Check for quoted-string addresses
		// Since almost anything is allowed in a quoted-string address,
		// we're just going to let them go through
		if (!/^"(.+)"$/.test(local)) {
			// It's a dot-string address...check for valid characters
			if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
				return false;
		}

		// Make sure domain contains only valid characters and at least one period
		if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
			return false;

		return true;
	},
	showError: function() {
		$('#contact-container .contact-message')
			.html($('<div class="contact-error">').append(contact1.message1))
			.fadeIn(200);
	}
};


/* 复制到剪贴板可以在Firefox等浏览器用，需要一个flash文件，该文件放在flv/_clipboard.swf */
function copyit(textit) {
	if (window.clipboardData) {
		window.clipboardData.setData("Text", textit);
	}
	else {
		FirefoxInitCopy();
		//flash安全机制变更，下面代码已经失效了
		//        var flashcopier = 'flashcopier';
		//        if (!document.getElementById(flashcopier)) {
		//            var divholder = document.createElement('div');
		//            divholder.id = flashcopier;
		//            document.body.appendChild(divholder);
		//        }
		//        document.getElementById(flashcopier).innerHTML = '';
		//        var divinfo = '<embed src="/flv/_clipboard.swf" FlashVars="clipboard=' + textit + '" width="0" height="0" type="application/x-shockwave-flash"></embed>';
		//        document.getElementById(flashcopier).innerHTML = divinfo;
	}
	alert("复制成功，请粘贴到你的QQ/MSN上推荐给你的好友!");
}

/*读取URL地址*/
function ret() {
	var url = document.getElementById("url").value;
	window.location.href = '/Login.aspx?returnurl=' + escape(url);
}

//var clip;
function FirefoxInitCopy() {
	//clip = new ZeroClipboard.Client();
	//clip.setHandCursor(true);
	clip.glue('contact-tit');
	clip.setText(f$("contact-tit").value);
}