﻿/**
 * @author wioksam
 */
 
/*
    Extend Array (add remove function)

*/
Array.prototype.remove = function(from, to) {
/*
    // Remove the second item from the array
    array.remove(1);
    // Remove the second-to-last item from the array
    array.remove(-2);
    // Remove the second and third items from the array
    array.remove(1,2);
    // Remove the last and second-to-last items from the array
    array.remove(-2,-1);    
*/
  var rest = this.slice((to || from) + 1 || this.length);
  this.length = from < 0 ? this.length + from : from;
  return this.push.apply(this, rest);
}; 
 

/* 
    Dialog 
*/ 
function showDialog(arg)  {
    // request: jquery.scrollTo-min.js
    //          jquery.bgiframe.min.js
    
    if (arg==undefined) return false;
    if (arg.loadurl==undefined) if(arg.message==undefined) return false;
    if (arg.title==undefined) arg.title=".";
    if (arg.width==undefined) arg.width = 700;
    if (arg.height==undefined) arg.height = 500;

	var dialogID = "Dialog"+(new Date()).getTime();

	$("body").after("<div id='" + dialogID + "'><div id='content' style='over-flow:auto' ></div></div>");
	
	$dialog = $("#" + dialogID);
	$cnt = $dialog.find("#content");
	
	if (arg.loadurl != undefined) {
	    
	    $cnt.prepend("<b id='dialog_loading' style='background-color:red;color:white'>Loading....</b>");
		var linktime = "t="+(+new Date());
		arg.loadurl += (arg.loadurl.indexOf("?")>0)?"&"+linktime:"?"+linktime;
		$cnt.load(arg.loadurl, function() {
		    if (arg.loadurlcallback != undefined) {
		        if (typeof (arg.loadurlcallback) == "function") {
		            arg.loadurlcallback();
		        }
		    }
		    //
/*
		    if (arg.height != undefined) {
		        $cnt.height(arg.height);
		    }
		    if (arg.buttons != undefined) {
		        $cnt.height(arg.height-60);
		    }
		    alert(arg.height)
		    alert($cnt.height())
		    */

		    $cnt.find("#dialog_loading").remove();
		});
	} else {
	    $cnt.html((arg.message!=undefined)?arg.message:"");
	    arg.height = $(dialogContentID).height();
	    arg.width = $(dialogContentID).width();
	    arg.width = (arg.height<500)?500:arg.width;
	    arg.height = (arg.height<300)?300:arg.height;
	    if (arg.buttons == undefined) {
            arg.buttons = {
                 "取消": function(){  
                    $(this).dialog("close");  
			        $(this).remove();
                 }
             }
         }
	}
	$dialog.bgiframe();

	$dialog.dialog({
	    modal: true,
	    overlay: {
		    "background-color": "#aaa",
		    "opacity": "0.75",
		    "-moz-opacity": "0.75"
		    },
	    resizable: false,
	    title: arg.title,
	    height: arg.height,
	    width: arg.width,
	    closeable: true,
	    close: function(){
		     $(this).remove();
	    },
        buttons: (arg.buttons==undefined)?{}:arg.buttons
    }).fadeIn();
    
    if (arg.buttons == undefined) {
        $dialog.find("#content").css({ overflow: "auto" }).height(arg.height).width(arg.width);
    } else {
        $dialog.find("#content").css({ overflow: "auto" }).height(arg.height - 100).width(arg.width);
    }

    return "#" + dialogID;
}

function GridConfig($obj, opts) {

    if (opts == undefined) opts = {}

    if (opts.GridSelectControl != undefined) {

        // add checkbox in front of row
        $obj.find("tr").each(function() {
            $(this).prepend("<td><input type='checkbox'/></td>");
        });
        // set checkbox events
        $obj.find("tr:gt(0)").each(function() {
            $(this).find("input[type=checkbox]").click(function() {
                $(this).parent().parent().toggleClass("highlight");
            });
        });
        // set selectAll checkbox event
        $obj.find("tr:eq(0) input[type=checkbox]").click(function() {
            $obj.find("tr:gt(0)").each(function() {
                $(this).find("input[type=checkbox]").trigger("click");
            });
        });
    }
    if (opts.GridControl != undefined) {
        // Add control
        $obj.find("tr:gt(0)").each(function() {
            var btnDel = (opts.GridControl.btnDel) ? "<a href='#' id='btnDel'><img src='images/page_cross.gif' border='0'>Del</a>" : "";
            var btnEdit = (opts.GridControl.btnEdit) ? "<a href='#' id='btnEdit'><img src='images/page_edit.gif' border='0'>Edit</a>" : "";

            $(this).prepend("<td nowrap='nowrap'>" + btnEdit + " " + btnDel + "</td>");
        });
        $obj.find("tr:eq(0)").prepend("<th></th>");
    }

    if (opts.TopControlBar != undefined) {

        var dv
        if (opts.TopControlBar.Template == undefined) {
            var tmpid = "ControlBarTemplate" + (+new Date);
            var tmp = '';
            tmp += '<div id="' + tmpid + '" style="display:none; text-align:left; vertical-align:middle">'
            tmp += '<div style="float:left" id="controllist"><input id="btnNew" type="button" value="新增 + " /></div>';
            tmp += '<div style="float:right" id="searchbar"><label>Search</label><input id="txtSearch" type="text" /><input id="btnSearch" type="button" value="Go" /></div>';
            tmp += '<br /><br /></div>';

            $("body").after(tmp);
            dv = $("#" + tmpid).clone();

        } else {
            dv = opts.TopControlBar.Template.clone();
        }
        if (opts.TopControlBar.ControlList != undefined) {
            if (opts.TopControlBar.ControlList.btnNew) dv.find("input#btnNew").show();
        }
        if (opts.TopControlBar.SearchBar != undefined) {
            if (opts.TopControlBar.SearchBar.label != undefined) dv.find("label").html(opts.TopControlBar.SearchBar.label);
        }

        dv.attr("id", $obj.attr("id") + "ControlBar").show();

        $obj.prepend(dv);
    }

    // add rows mouseover and mouseout... events
    $obj.find("tr:gt(0)").each(function() {
        $(this).mouseover(function() {
            $(this).addClass("over");
        }).mouseout(function() {
            $(this).removeClass("over");
        })
    });
    if (opts.GridSelectControl == undefined) {
        $obj.find("tr:gt(0)").each(function() {
            $(this).click(function() {
                $obj.find("tr:gt(0)").each(function() { $(this).removeClass("highlight"); });
                $(this).toggleClass("highlight");
            });
        });
    }

    if (opts.GridStyle != undefined) {
        $obj.addClass(opts.GridStyle);
    } else {
        $obj.addClass("GrayGrid");
    }
    
}

function loading(arg) {
    if (arg != undefined) {
        var id = "____Loading";
        $("#"+id).remove();
        $("body").after("<div id='" + id + "'>Loading....</div>")
        $("#" + id).css({ left: "0px", top: "0px", color: "white", backgroundColor: "#ff0000" });

        if (arg == "hide") {
            $("#"+id).hide();
        } else if (arg == "show") {
            $("#"+id).show();
        }
    }
}
function alertDialog(arg) {
    var id = showDialog({
        title:arg.title, 
        message:"<table style='width:100%;height:200px;'><tr><td valign='middle' align='center'>"+arg.message+"</td></tr></table>",
        valign:arg.valign,
        align:arg.align
    });
}
function getScrollbarWidth() { 
    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>'); 
    // Append our div, do our calculation and then remove it 
    $('body').append(div); 
    var w1 = $('div', div).innerWidth(); 
    div.css('overflow-y', 'scroll'); 
    var w2 = $('div', div).innerWidth(); 
    $(div).remove(); 
    return (w1 - w2); 
}
function getScrollXY() {
    var x = 0, y = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    return [x, y];
}
            
function setScrollXY(x, y) {
    window.scrollTo(x, y);
}
function debug(s) {
	var dialogcont = '<div id="loadContent" style="overflow:auto; width:100%; height:100%">'+s+'</div>';
	var dialog= '<div id="debugDialog" class="flora" title="Debug...." style="display:none;width:550px;height:300px;">'+dialogcont+'</div>';
	$("#debugDialog").bgiframe();
	$("#debugDialog").dialog("close");
	$("body").after(dialog);
	setDialog("debugDialog",600,300,true);
	$("#debugDialog").dialog("open");
}
function loadJs(file){ 
     var head = $('head').remove('#loadScript');
    $("<script></script>").attr({src:file,type:'text/javascript',id:'load'}).appendTo(head);
}
function strpad(inputString, chars, padSting) {
	var pads = padSting;
	for (var k=0; k<chars; k++) {pads += padSting}
  	result = pads+inputString;
  	remFromLeft=result.length-chars;
  	return result.substr(remFromLeft);
}
function getDivCenter() {
	var css =  {
        top:  ($(window).height() - 450) /2 + 'px', 
        left: ($(window).width() - 500) /2 + 'px', 
        width: '500px'			
	}
	return css
}
function uncheckall(cheboxid) {
	$("input[id='"+cheboxid+"']").each(function(){
		this.checked=false;
	});		
}
function setDatePickerTo(id, yearRange) {
	$('#'+id).datepicker({ 
			dateFormat: "dd/mm/yy",
		    //yearRange: "-80:+0", 
			yearRange: yearRange, 
		    showOn: "button", 
		    buttonImage: "images/calendar.jpg", 
		    buttonImageOnly: true 
		});
}
function setDialog(id, width, height, isModal){
	$("#"+id).dialog({height: height,width: width,modal:isModal}).show();
	$("#"+id).dialog("close");
}
function hasErrorFocus(e, err) {
    
    
    $("#____ErrorFocusMsg").parent().find("input[type=text]").removeClass("alertbox");
    $("#____ErrorFocusMsg").remove();
    e.addClass("alertbox").focus();
    
	/*
	e.blur(function(){
		e.parent().find("b").remove();
		$(this).removeClass("alertbox");
	});
	*/
	
	msg = (err==null)? "Missing Value of "+e.attr("id")+".":err;
	e.after("<b id='____ErrorFocusMsg' style='color:red'><br/>"+msg+"</b>").fadeIn();
}
function setTextBoxFocusEvent() {
	$(":input").not(":button").focus(function(){
	    $(this).addClass("alertbox") 
	}).blur(function(){
		$(this).removeClass("alertbox");
	});
}
function disable_datepicker(id,isEnable) {
	var e = $("#"+id);
	if(isEnable) {
		e.css("background-color","");
	 	e.datepicker("enable");
		e.val("");
	} else {
		e.datepicker("disable");
		e.css("background-color","#EAEAEA");
	}
}
function htmlspecialchars(str) {
 if (typeof(str) == "string") {
  str = str.replace(/&/g, "&amp;"); /* must do &amp; first */
  str = str.replace(/"/g, "&quot;");
  str = str.replace(/'/g, "&#039;");
  str = str.replace(/</g, "&lt;");
  str = str.replace(/>/g, "&gt;");
  }
  alert(str);
 return str;
}	
function rhtmlspecialchars(str) {
 if (typeof(str) == "string") {
  str = str.replace(/&gt;/ig, ">");
  str = str.replace(/&lt;/ig, "<");
  str = str.replace(/&#039;/g, "'");
  str = str.replace(/&quot;/ig, '"');
  str = str.replace(/&amp;/ig, '&'); /* must do &amp; last */
  }
  alert(str);
 return str;
 }    
function fixHtmlSpecialChars(s) {
    s = s.replace(/\\/g,"\\");
    s = s.replace(/\n/g,"\\n");
    s = s.replace(/'/g,"&acute;");
    s = s.replace(/&/g, '&amp;');
    s = s.replace(/</g, '&lt;');
    s = s.replace(/>/g, '&gt;');
    s = s.replace(/}/g, '\}');
    s = s.replace(/{/g, '\{');
    
    return s
}


function printvo(_vo) {
    var s = "";
    $.each(_vo, function(key, val) {
        //alert( "Name: " + key + ", Value: " + val );
		var s1 = key + "=" + val+"\n";
		s=s+s1;
    });
    alert(s);
    return s;
}

function disableRightClick() {
function md(e) 
{ 
  try { if (event.button==2||event.button==3) return false; }  
  catch (e) { if (e.which == 3) return false; } 
}
document.oncontextmenu = function() { return false; }
document.ondragstart   = function() { return false; }
document.onmousedown   = md;
}
