// JavaScript Document
var _debug_ = true;

Date.prototype.DatePart = function(week){
	var Year = this.getFullYear();
	var Mon = this.getMonth() + 1;
	Mon = Mon<10 ? "0" + Mon : Mon;
	var Day = this.getDate();
	vDay = Day<10 ? "0"+ Day : Day;
	var partStr = Year + "年" + Mon + "月" + vDay  + "日"
	if(week) {
		var Week = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']; 
		partStr += "&nbsp;" + Week[this.getDay()];
	}
	return partStr;
}

function checktags(tagobj, num, length){
	var $val = $(tagobj).val();
	var flag = 1;
	if($val.length > length){
		flag = -1;
	}else{
		var a = $val.split(" ");
		for(i=0;i<a.length;i++){
			if(a[i].length > num){
				flag = 0;
				break;
			}
		}
	}
	return flag;
}

/**
*
*  UTF-8 data encode / decode
*
**/ 
var Utf8 = {
 
	// public method for url encoding
	encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}

function getQuery(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null){
		return decodeURI(r[2]);
	}
	return null;
}

function addToBookMark(){
	var site_url = location.protocol + '//' + location.host;
	if(location.port) {
		site_url = site_url + ':' + location.port;
	}
	bookmarksite("唐山市驾驶号协会",  site_url);
}

function bookmarksite(title,url){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} 
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
}

function initTab(selector) {
	jQuery(selector + " .tabs li a").hover(function(){
		var obj = jQuery(this);
		var obj_id = "";
		while(obj.parent()) {
			if(obj.parent().hasClass('tabs_pannel')) {
				obj_id = obj.parent().attr("id");
				break;
			}
			else {
				obj = obj.parent();
			}
		}
		
		if(obj_id) {
			jQuery("#" + obj_id + " .tabs li a").removeClass("selected");
			jQuery(this).addClass("selected");
			
			jQuery("#" + obj_id + " div.tab_content").hide();
			jQuery("#content_" + jQuery(this).attr("href").substr(1) ).show();
		}
	})
	.click(function(){
		return false;				
	});	

}

function resizeImage(imgObj) {
	var argv=resizeImage.arguments;
	var argv_len=resizeImage.arguments.length;
	var maxWidth=(argv_len>1)?argv[1]:130;
	var maxHeight=(argv_len>2)?argv[2]:130;
	if(imgObj.width>maxWidth || imgObj.height>maxHeight){
		if(imgObj.width/maxWidth > imgObj.height/maxHeight) {
			imgObj.height=imgObj.height/(imgObj.width/maxWidth);
			imgObj.width = maxWidth;
		}
		else{
			imgObj.width=imgObj.width/(imgObj.height/maxHeight);
			imgObj.height=maxHeight;
		}
	}
}

function strip_tags(str, allowed_tags) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Luke Godfrey
    // +      input by: Pul
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // +      input by: Alex
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: strip_tags('<p>Kevin</p> <br /><b>van</b> <i>Zonneveld</i>', '<i><b>');
    // *     returns 1: 'Kevin <b>van</b> <i>Zonneveld</i>'
    // *     example 2: strip_tags('<p>Kevin <img src="someimage.png" onmouseover="someFunction()">van <i>Zonneveld</i></p>', '<p>');
    // *     returns 2: '<p>Kevin van Zonneveld</p>'
    // *     example 3: strip_tags("<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>", "<a>");
    // *     returns 3: '<a href='http://kevin.vanzonneveld.net'>Kevin van Zonneveld</a>'
 
    var key = '', tag = '', allowed = false;
    var matches = allowed_array = [];
    var allowed_keys = {};
 
    var replacer = function(search, replace, str) {
        return str.split(search).join(replace);
    };
 
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z]+)/gi);
    }
  
    str += '';
 
    // Match tags
    matches = str.match(/(<\/?[^>]+>)/gi);
 
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }
 
        // Save HTML tag
        html = matches[key].toString();
 
        // Is tag not in allowed list? Remove from str!
        allowed = false;
 
        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;
 
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
 
            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }
 
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
 
    return str;
}

/****************
Block消息及通用对话框
****************/
function showBlockMsg(obj, message) {
	if(showBlockMsg.arguments.length >= 3) {
		switch(showBlockMsg.arguments[2]) {
			case "error":
				icon = "error.gif";
				break;
			case "info":
				icon = "info.gif";
				break;
			case "ok":
				icon = "ok.gif";
				break;
			default:
				icon = "";
				break;
		}
	}
	else {
		icon = "ajax_loading.gif";
	}
	
	if(icon) {		
		message = "<h1><img src='images/" + icon+ "' align='absmiddle' />&nbsp;&nbsp;" + message + "</h1>";
	}
	else {
		message = "<h1>" + message + "</h1>";
	}
	
	
	var defaultCss = { 
		border: '3px solid #ccc', 
		padding:'20px', 
		width: '50%',
		'-webkit-border-radius': '10px',
		'-moz-border-radius': '10px'
	};
	
	if(showBlockMsg.arguments.length >= 4) {
		css = $.extend(defaultCss, showBlockMsg.arguments[3]);
	}
	else {
		css = defaultCss;
	}
	$(obj).block({ 
		message: message,  
		css: css
	});
}

function hideBlockMsg(obj) {
	$(obj).unblock();
}

function showConfirmDialog(options) {
	$(".content").block();
	var defaults = {
		title: "请确认",
		width: '300px',
		height: '150px',
		buttons: {
			"确定": function() {
				options.callback();
				$(this).dialog("destroy");
				$(".confirmDialog").remove();
				$(".content").unblock();
			},
			"取消": function() {
				$(this).dialog("destroy");
				$(".confirmDialog").remove();
				$(".content").unblock();
			}
		}
	};

	var opts = $.extend(defaults, options);
	var confirmDialog = $("<div class='confirmDialog' title='" + opts.title + "'><p>" + opts.message + "</p></div>");

	confirmDialog.dialog({
		modal: true,
		buttons: opts.buttons,
		resizable: false,
		width: opts.width,
		height: opts.height,
		overlay: {
			backgroundColor:'#fff', 
	        opacity:        '0.6' 
		}
	});
	return confirmDialog;
}
/*end*/

(function($) {

	var types = ['DOMMouseScroll', 'mousewheel'];
	
	$.event.special.mousewheel = {
		setup: function() {
			if ( this.addEventListener )
				for ( var i=types.length; i; )
					this.addEventListener( types[--i], handler, false );
			else
				this.onmousewheel = handler;
		},
		
		teardown: function() {
			if ( this.removeEventListener )
				for ( var i=types.length; i; )
					this.removeEventListener( types[--i], handler, false );
			else
				this.onmousewheel = null;
		}
	};
	
	$.fn.extend({
		mousewheel: function(fn) {
			return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
		},
		
		unmousewheel: function(fn) {
			return this.unbind("mousewheel", fn);
		}
	});
	
	
	function handler(event) {
		var args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true;
		
		event = $.event.fix(event || window.event);
		event.type = "mousewheel";
		
		if ( event.wheelDelta ) delta = event.wheelDelta/120;
		if ( event.detail     ) delta = -event.detail/3;
		
		// Add events and delta to the front of the arguments
		args.unshift(event, delta);
	
		return $.event.handle.apply(this, args);
	}

})(jQuery);

function isDate(value) {
	return /^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))$/.test(value)
}

