$(document).ready(function(){
	$('#BodyFrame').show();
	$('input[@is_num]').bind('change', function(){
		if(isNaN(parseInt($(this).val(), 10))){
			alert('請輸入可辨識的數字');
			$(this).val('0');
			$(this).select();
			$(this).focus();
		}
		$(this).val(isNaN(parseInt($(this).val(), 10))?'':parseInt($(this).val(), 10));
	});
	$('input[@is_float]').bind('change', function(){
		if(isNaN(parseFloat($(this).val()))){
			alert('請輸入可辨識的數字');
			$(this).focus();
		}
		$(this).val(isNaN(parseFloat($(this).val()))?'':parseFloat($(this).val()));
	});
	$('input[@is_email]').bind('change', function(){
		$(this).val($.trim($(this).val()));
		if($(this).val() != ''){
			var emailReg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ ;
			if(!emailReg.test($(this).val())) {
				alert('E-mail 格式不正確，請填入正確的 E-mail');
				$(this).val('');
			}
		}
	});
	$('input[@is_datetime]').bind('change', function(){
		$(this).val($.trim($(this).val()));
		if($(this).val() != ''){
			datetime_check($(this).val(), $(this));
		}
	});
	$('input[@is_date]').bind('change', function(){
		$(this).val($.trim($(this).val()));
		if($(this).val() != ''){
			date_check($(this).val(), $(this));
		}
	});
	$('img[@swap]').each(function(){
		$(this).attr('src', $(this).attr('default_pic'));
		if($(this).attr('mover_pic') != ''){
			$(this).bind('mouseover', function(){
				$(this).attr('src', $(this).attr('mover_pic'));
			});
			$(this).bind('mouseout', function(){
				$(this).attr('src', $(this).attr('default_pic'));
			});
		}
	});
	$('input[@is_postcode]').bind('change', function(){
		$(this).val($.trim($(this).val()));
		if($(this).val() != ''){
			var postReg1 = /^[0-9]{3}$/ ;
			var postReg2 = /^[0-9]{5}$/ ;
			if(!postReg1.test($(this).val()) && !postReg2.test($(this).val())) {
				alert('請填入 3 或 5 位數字之郵遞區號');
				$(this).val('');
			}
		}
	});
	$('.listTbl TR:not(:first)').bind('mouseover', function(){
		$(this).addClass('listTblHighlightRow');
	});
	$('.listTbl TR:not(:first)').bind('mouseout', function(){
		$(this).removeClass('listTblHighlightRow');
	});
	$('.dataTbl TR TD:nth-child(1)').addClass('dataTblTh');
	$('.dataTbl TR').bind('mouseover', function(){
		$(this).addClass('listTblHighlightRow');
	});
	$('.dataTbl TR').bind('mouseout', function(){
		$(this).removeClass('listTblHighlightRow');
	});
});

function formCheck(form_id){
	var inputs = '#' + form_id + ' input[@type="text"],input[@type="password"],textarea';
	$(inputs).each(function(){
		$(this).val($.trim($(this).val()));
	});
	inputs = '#' + form_id + ' *[@not_null]';
	var no_null_input = true;
	$(inputs).each(function(){
		if($(this).val() == '' && no_null_input){
			no_null_input = false;
			alert('請輸入'+$(this).attr('label'));
			$(this).focus();
		}
	});
	return no_null_input;
}

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function AddtoFavorite(){
	if (navigator.appName == "Microsoft Internet Explorer"){
		window.external.AddFavorite(document.location.href , document.title);
	} 
	else {
		window.sidebar.addPanel(document.title, document.location.href,""); 
	}
	return false;
}

function FlashAddtoFavorite(){
	if (navigator.appName == "Microsoft Internet Explorer"){
		window.external.AddFavorite(document.location.href , document.title);
	} 
	else {
		window.sidebar.addPanel(document.title, document.location.href,""); 
	}
}

function datetime_check(datetime, el){
	datetime = $.trim(datetime);
	datetime = $.ajax({
		type: "POST",
		url: "ajax.php",
		dataType: "xml",
		data: "func=check&param=datetime&datetime=" + datetime,
		beforeSend: function(){
			$('body').css('cursor', 'wait');
		},
		success: function(xml) {
			$('body').css('cursor', 'default');
			if ($(xml).find('Error').text() == '1'){
				alert($(xml).find('ErrorMessage').text());
				el.val('');
			}
			else{
				el.val($(xml).find('datetime').text());
			}
		}
	});
}

function date_check(date, el){
	date = $.trim(date);
	date = $.ajax({
		type: "POST",
		url: "ajax.php",
		dataType: "xml",
		data: "func=check&param=date&date=" + date,
		beforeSend: function(){
			$('body').css('cursor', 'wait');
		},
		success: function(xml) {
			$('body').css('cursor', 'default');
			if ($(xml).find('Error').text() == '1'){
				alert($(xml).find('ErrorMessage').text());
				el.val('');
			}
			else{
				el.val($(xml).find('date').text());
			}
		}
	});
}
