﻿//=============================================================================
//
// Copyright (c) 2007 RuanYu
//
// FileName     :core.js
//
// Description  :global configuration file
//
// Author       :RuanYu
//
// Date         :2007-11-20
//
//=============================================================================

var RuanYu = {
	
	version : '1.0.0.0',
	
	publishDate : '2007-11-20',
	
	corporation : 'Yoshow',
	
	author : 'ruanyu1983@msn.com',
	
	msn : 'ruanyu1983@msn.com',
	
	debug : true,
	
	hostName : '',

  	required_prototype: '1.6.0',
  	
  	require: function(libraryName) {
    	// inserting via DOM fails in Safari 2.0, so brute force approach
        document.write('<script type="text/javascript" src="' + libraryName + '"></script>');
  	},
  
  	
  	load: function() {
	  	
		if(RuanYu.debug)
		{
		    RuanYu.hostName = "http://localhost/calculator/";         // test url
		}
		else
		{
		    RuanYu.hostName = "http://www.ruanyu.name/";     // publish url
		}
		
	    function convertVersionString(versionString)
	    {
	    	// convert version string to number eg. 100.1000.100.
	      var r = versionString.split('.');
	      
	      return parseInt(r[0])*100000 + parseInt(r[1])*1000 + parseInt(r[2]);
	    }
	 	
		/*
		if(typeof(Sys) == 'undefined')
		{    
		    throw("The system need microsoft's ajax 1.0.");
		}
		*/
	    if( (typeof(Prototype)=='undefined') || (typeof Element == 'undefined') || (typeof Element.Methods=='undefined') || (convertVersionString(Prototype.Version) < convertVersionString(RuanYu.required_prototype)))
	    {   
	   		throw("The javascript requires the Prototype JavaScript framework >= " + RuanYu.required_prototype);
	    }
	    
	    $A(document.getElementsByTagName("script")).findAll(function(s){
	      return (s.src && s.src.match(/core\.js(\?.*)?$/))
	    }).each( function(s) {
	      var path = s.src.replace(/core\.js(\?.*)?$/,'');
	      
	      //var r = /\?.*load=([a-z,]*)/;
	      //var r = '';
	      
	      //var includes = s.src.match(r);
	      
	      //(includes ? includes[1] : 'expression,page,net,utility').split(',').each(
	       //function(include) { RuanYu.require(path + include + '.js') 
	       //});
	    });
	}
}

RuanYu.load();

//===========================================================================
//
// Default Object | update: 2007.11.07
//
//===========================================================================

RuanYu.String = {

    trim : function(text)
    {
	    return text.replace(/(^\s*)|(\s*$)/g, '');
    },

    ltrim : function(text)
    {
		return text.replace(/(^\s*)/g, '');
    },
	
    rtrim : function(text)
    {
        return text.replace(/(\s*$)/g, '');
    }
}

RuanYu.Date = {

    create : function(dateValue)
    {
	    return new RuanYu.Date.Time(dateValue);
    },
    
    diff :function(begin,end,format)
    {
        var timeBegin = new RuanYu.Date.Time(begin);
        var timeEnd = new RuanYu.Date.Time(end);
        
        var result;
        
        switch(String(format).toLowerCase())
        {
            case "y":
            case "year":
                result = timeBegin.year-timeEnd.year;
                break;
                
            case "n":
            case "month":
                result = (timeBegin.year-timeEnd.year)*12 + (timeBegin.month-timeEnd.month);
                break;
            case "d":
            case "day":
                result = Math.round((Date.UTC(timeBegin.year,timeBegin.month-1,timeBegin.day)-Date.UTC(timeEnd.year,timeEnd.month-1,timeEnd.day))/(1000*60*60*24));
                break;
            case "h":
            case "hour":
                result = Math.round((Date.UTC(timeBegin.year,timeBegin.month-1,timeBegin.day,timeBegin.hour)-Date.UTC(timeEnd.year,timeEnd.month-1,timeEnd.day,timeEnd.hour))/(1000*60*60));
                break;
            case "m":
            case "minute":
                result = Math.round((Date.UTC(timeBegin.year,timeBegin.month-1,timeBegin.day,timeBegin.hour,timeBegin.minute)-Date.UTC(timeEnd.year,timeEnd.month-1,timeEnd.day,timeEnd.hour,timeEnd.minute))/(1000*60));
                break;
            case "s":
            case "second":
                result = Math.round((Date.UTC(timeBegin.year,timeBegin.month-1,timeBegin.day,timeBegin.hour,timeBegin.minute,timeBegin.second)-Date.UTC(timeEnd.year,timeEnd.month-1,timeEnd.day,timeEnd.hour,timeEnd.minute,timeEnd.second))/1000);
                break;
            case "ms":
            case "msecond":
                result = Date.UTC(timeBegin.year,timeBegin.month-1,timeBegin.day,timeBegin.hour,timeBegin.minute,timeBegin.second,timeBegin.msecond)-Date.UTC(timeEnd.year,timeEnd.month-1,timeEnd.day,timeEnd.hour,timeEnd.minute,timeEnd.second,timeBegin.msecond);
                break;
            case "w":
            case "week":
                result = Math.round((Date.UTC(timeBegin.year,timeBegin.month-1,timeBegin.day)-Date.UTC(timeEnd.year,timeEnd.month-1,timeEnd.day))/(1000*60*60*24)) % 7;
                break;
            default:
                result = Math.round((Date.UTC(timeBegin.year,timeBegin.month-1,timeBegin.day)-Date.UTC(timeEnd.year,timeEnd.month-1,timeEnd.day))/(1000*60*60*24));
        }
        
        return Math.abs(result);
    },
    
    add : function(dateValue,number,format)
    {
        var time = new RuanYu.Date.Time(dateValue);
        
        switch(String(format).toLowerCase())
        {
            case "y": 
            case "year": 
                time.year += number; break;
                
            case "n": 
            case "month": 
                time.month += number; break;
                
            case "d": 
            case "day": 
                time.day += number; break;
                
            case "h": 
            case "hour": 
                time.hour += number; break;
                
            case "m": 
            case "minute": 
                time.minute += number; break;
                
            case "s": 
            case "second": 
                time.second += number; break;
                
            case "ms": 
            case "msecond": 
                time.msecond += number; break;
                
            case "w": 
            case "week": 
                time.day += num*7; break;
            
            default: 
                time.day += number; break;
        }
        
        return(time.toDate());
    }
}

RuanYu.Date.Time = Class.create();
	
RuanYu.Date.Time.prototype = {

    initialize : function(dateValue)
	{
		 var date = new Date(dateValue);
		 
        this.year = date.getFullYear();
        this.month = date.getMonth()+1;
        this.day = date.getDate();
        this.hour = date.getHours();
        this.minute = date.getMinutes();
        this.second = date.getSeconds();
        this.msecond = date.getMilliseconds();
        this.week = date.getDay();
	},
    
    toDate : function()
    {
        return new Date(this.toString());
    },
    
	toString : function(format)
	{
	    var outString = '';
	    
	    switch(format)
	    {
	        case 'yyyy-MM-dd':
	        
	        default:
                outString = this.year + "/" + this.month + "/" + this.day + " " + this.hour + ":" + this.minute + ":" + this.second;
                break;
        }
        
        return outString;
	}
}

//===========================================================================
//
// RuanYu.Cookies | update: 2007.10.12
//
//===========================================================================

RuanYu.Cookies = {
	
	addCookie : function(name,value,expire,path)
	{
	    document.cookie = name+"="+escape(value)+((!expire)?"":("; expires="+expire.toGMTString()))+"; path="+((!path)?"/":path);
	},
	
	add : function() {this.addCookie()},
	
	removeCookie : function(name,path)
	{
	    if(RuanYu.Cookies.findCookie(name))
	    {
	        document.cookie=name+"="+"; path="+((!path)?"/":path)+"; expires=" + new Date(0).toGMTString();
	    }
	},

	remove : function(name){ return this.removeCookie(name); },
	
	findCookie : function(name)
	{
	    var value='', search=name+'=';
	    
	    if(document.cookie.length>0)
	    {
	        var offset=document.cookie.indexOf(search);
	        
	        if(offset!=-1){
	            
	            offset+=search.length;
	            
	            var end = document.cookie.indexOf(";",offset);
	            
	            if( end == -1 ) 
	                end = document.cookie.length;
	            
	            value = unescape(document.cookie.substring(offset,end));
	       }
	    }
	    
	    return value;
	},
	
	find : function(name){ return this.findCookie(name); },
	
	findAllCookies : function()
	{
		var outString = '';
	    
	    var list = document.cookie.split(';');
	    
	    var temp;
	    
	    outString = '{';
	    
	    for(var i=0; i<list.length; i++)
	    {
	        temp = list[i].split('=');
	            
	        outString += '"' + String(temp[0]).trim() + '":"' + temp[1] + '"';
	        
	        if(i < list.length-1) 
	            outString+=',';
	    }
	    
	    outString += '}';
	    
	    return outString.evalJSON();
	},
	
	findAll : function(){ return this.findAllCookies(); }
}

//===========================================================================
//
// RuanYu.MasterPage | update: 2007.07.10
//
//===========================================================================

RuanYu.MasterPage = {
	
	getElementById : function(value)
	{
	    return $('ctl00_contentPlaceHolder_' + value);
	}
}

//===========================================================================
//
// RuanYu.MasterPage | update: 2007.07.10
//
//===========================================================================

RuanYu.Exception = {
	
	show : function(value)
	{
		
		this.create();
		
		$('ajaxException').innerHTML = value;
		
		this.open(); 
	},
	
	create : function()
	{
		if($('ajaxExceptionContainer') == null)
		{	
			var outString = '';
			
			var div = document.createElement('div');
	  
	 	   	div.id = 'ajaxExceptionContainer';
	    	div.name = 'ajaxExceptionContainer';
	    	div.className = 'ajax-exception-container';
	    	
			div.innerHTML = '<div class="ajax-exception-wrapper">'
				+ '<div id="ajaxException"></div>'
				+ '<div class="ajax-exception-toolbar"><a href="javascript:RuanYu.Exception.close()">Close</a></div>'
				+ '</div>';
				
		    document.body.appendChild(div);
		}
	},
	
	open : function()
	{
	    $('ajaxExceptionContainer').className = 'ajax-exception-container';
	},
	
	close : function()
	{
		$('ajaxExceptionContainer').className = 'hidden';
	}
}