function isIE(){ //ie?
	if (window.navigator.userAgent.toLowerCase().indexOf("msie") >= 1)
		return true;
	else
		return false;
}

if(!isIE()){ //firefox innerText define
  HTMLElement.prototype.__defineGetter__( "innerText",
  function(){
  var anyString = "";
  var childS = this.childNodes;
  for(var i=0; i <childS.length; i++) {
  if(childS[i].nodeType==1)
  anyString += childS[i].tagName=="BR" ? '\n' : childS[i].innerText;
  else if(childS[i].nodeType==3)
  anyString += childS[i].nodeValue;
  }
  return anyString;
  }
  );
  HTMLElement.prototype.__defineSetter__( "innerText",
  function(sText){
  this.textContent=sText;
  }
  );
}

Date.prototype.format = function(format)
{
    var o =
    {
        "M+" : this.getMonth()+1, //month
        "d+" : this.getDate(),    //day
        "h+" : this.getHours(),   //hour
        "m+" : this.getMinutes(), //minute
        "s+" : this.getSeconds(), //second
        "q+" : Math.floor((this.getMonth()+3)/3),  //quarter
        "S" : this.getMilliseconds(), //millisecond
        "N" : getMonthName(this.getMonth()), //month name
		"W" : this.getDay() //
    }

	function getMonthName(d){
		var s = "";
		switch(d){
			case 0:
				s = "January";
				break;
			case 1:
				s = "February";
				break;
			case 2:
				s = "March";
				break;
			case 3:
				s = "April";
				break;
			case 4:
				s = "May";
				break;
			case 5:
				s = "June";
				break;
			case 6:
				s = "July";
				break;
			case 7:
				s = "August";
				break;
			case 8:
				s = "September";
				break;
			case 9:
				s = "October";
				break;
			case 10:
				s = "November";
				break;
			case 11:
				s = "December";
				break;
			default:
				s = "";
				break;
		}
		return s;
	}

    if(/(y+)/.test(format))
    format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length));
    for(var k in o)
    if(new RegExp("("+ k +")").test(format))
    format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
    return format;
}
// var ddd = new Date();
// document.write (ddd.format('yy-MM-dd hh:mm:ss N'));

///////////////////////////////////////////////////////////////


function Cut(divid1, divid2){
	var div1 = document.getElementById(divid1);
	var div2 = document.getElementById(divid2);
	var s = "";
	
	/*if(isIE()){
		s = div1.innerText;
	}
	else{
		s = div1.textContent;
	}*/
	s = div1.innerText;
	var meetSpace = false;
	var count = 0;
	var i = 0;

	for(i = 0; i <= s.length - 1; i++){
		if(s.substr(i, 1) == " "){
			if(meetSpace == false){
				count++;
				meetSpace = true;
			}
			else{
			}
		
			if(count >= 25){
				break;
			}
		}
		else{
			meetSpace = false;
		}
	}

	div2.innerHTML = s.substr(0, i);

	if(div1.innerText.length > div2.innerText.length){
		div2.innerText = div2.innerText + "...";
	}
}


///////////////////////////////////////////////////////////////
				function CalTurn(addMonth){
					var obj = document.getElementById("divCurrMonth");
					var y = document.getElementById("txtSelYear");
					var m = document.getElementById("txtSelMonth");

					if(y.value == "" || m.value == ""){
						var d = new Date();
						y.value = d.getFullYear();
						m.value = d.getMonth() + 1;
					}

					var date = new Date(y.value, m.value - 1 + addMonth, 1);
					y.value = date.getFullYear();
					m.value = date.getMonth() + 1;

					obj.innerText = date.format('N yyyy');

					CalFill();
					//GetEventsUrl();
					CalPaintEvent();
				}



				function CalFill(){
					var tbl = document.getElementById("tblCalendar");
					var y = document.getElementById("txtSelYear");
					var m = document.getElementById("txtSelMonth");

					var d = new Date(y.value, m.value - 1, 1);
					//alert(d.format("W"));
					var space = 0;
					if(d.format("W") == "0"){
						space = 6;
					}
					else{
						space = d.format("W") * 1 - 1;
					}

					var daycount = 0;
					while(d.getMonth() == m.value - 1){
						d.setDate(d.getDate() + 1)
						daycount++;
					}
					
					//alert(daycount);
					//alert(d.format("yyyy-MM-dd W"));
					var today = new Date().format("yyyy-MM-dd");
					var aday;
					
					for(var i = 1, n = 1; i <= tbl.rows.length - 1; i++){
						for(var j = 0; j <= tbl.rows[i].cells.length - 1; j++){
							tbl.rows[i].cells[j].style.backgroundColor = "";
							tbl.rows[i].cells[j].className = "";
							tbl.rows[i].cells[j].style.cursor = "default";
							tbl.rows[i].cells[j].onclick = function(){};
							if(i == 1){
								if(j < space){
									tbl.rows[i].cells[j].innerText = "";
								}
								else{
									tbl.rows[i].cells[j].innerText = n;
									/*thisday = new Date(y.value, m.value - 1, n).format("yyyy-MM-dd");
									if(thisday == today)
									{
										tbl.rows[i].cells[j].style.backgroundColor = "#222222";
										tbl.rows[i].cells[j].className = "white13b";
									}
									tbl.rows[i].cells[j].style.cursor = "pointer";
									tbl.rows[i].cells[j].onclick = function(){CalClick(thisday)};
									*/
									n++;
								}
							}
							else{
								if(n <= daycount){
									tbl.rows[i].cells[j].innerText = n;
									/*thisday = new Date(y.value, m.value - 1, n).format("yyyy-MM-dd");
									if(thisday == today)
									{
										tbl.rows[i].cells[j].style.backgroundColor = "#222222";
										tbl.rows[i].cells[j].className = "white13b";
									}
									tbl.rows[i].cells[j].style.cursor = "pointer";
									tbl.rows[i].cells[j].onclick = function(){CalClick(thisday)};
									*/
									n++;
								}
								else{
									tbl.rows[i].cells[j].innerText = "";
								}
							}
						}
					}
				}

				/*function CalClick(d){
					frmPost.submit();
				}*/
/////////////////////////////////////////////////////////////////////////////////////


function GetEventsUrl(){
	var url = "EventsUrl.asp?date=";
	var y = document.getElementById("txtSelYear");
	var m = document.getElementById("txtSelMonth");

	var tbl = document.getElementById("tblCalendar");
	for(var i = 1, n = 1; i <= tbl.rows.length - 1; i++){
		for(var j = 0; j <= tbl.rows[i].cells.length - 1; j++){
			if(tbl.rows[i].cells[j].innerText != ""){
				var d = new Date(y.value, m.value - 1, tbl.rows[i].cells[j].innerText);
				var s = _GetEventsUrl(url + d.format("yyyy-MM-dd"));

				if(s != ""){
					tbl.rows[i].cells[j].style.backgroundColor = "#222222";
					tbl.rows[i].cells[j].className = "white13b";
					tbl.rows[i].cells[j].style.cursor = "pointer";
					eval('tbl.rows[i].cells[j].onclick = function (){CalClick2(\'' + s + '\')}')
				}
			}
			else{
				//alert("empty");
			}
		}
	}
	return;
}

function CalClick2(url){
	//alert(url);
	document.location = url;
}

function _GetEventsUrl(url){
	var xmlHttp=null;
	var s = "";

	if (window.XMLHttpRequest){// code for IE7, Firefox, Opera, etc.
		xmlHttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject){// code for IE6, IE5
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (xmlHttp!=null){
		xmlHttp.open("GET", url, false);
		xmlHttp.send(null);
		s = xmlHttp.responseText;
	}
	else{
		//alert("Your browser does not support XMLHTTP.");
	}
	xmlHttp = null;
	return s;
}



var xmlAllEvents = null;

function GetAllEventsID(){
	var xmlHttp = null;
	var s = "";

	if (window.XMLHttpRequest){// code for IE7, Firefox, Opera, etc.
		xmlHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject){// code for IE6, IE5
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	if (xmlHttp != null){
		xmlHttp.open("GET", "EventsUrl.asp", false);
		xmlHttp.send(null);
		s = xmlHttp.responseText;
		LoadEvents(s);
	}
	else{
		//alert("Your browser does not support XMLHTTP.");
	}
	xmlHttp = null;
	return s;
}

var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');

function LoadEvents(xml){
	if (moz) {
		xmlAllEvents = document.implementation.createDocument("", "", null);
		xmlAllEvents.async = false; 
		var oParser = new DOMParser();
		xmlAllEvents = oParser.parseFromString(xml, "text/xml");

		if ((xmlAllEvents.documentElement) && (xmlAllEvents.documentElement.tagName == 'parsererror')) {
			xmlAllEvents = null;
		}
	} 
	else if (ie) {   
		xmlAllEvents = new ActiveXObject("Microsoft.XMLDOM");//("MSXML2.DOMDocument.3.0");   
		xmlAllEvents.async = false;   
		xmlAllEvents.loadXML(xml);

		if(xmlAllEvents.parseError != 0){
			xmlAllEvents = null;
		}
	}

	//alert(xmlAllEvents);
}

function CalPaintEvent(){
	var y = document.getElementById("txtSelYear");
	var m = document.getElementById("txtSelMonth");

	var tbl = document.getElementById("tblCalendar");
	for(var i = 1, n = 1; i <= tbl.rows.length - 1; i++){
		for(var j = 0; j <= tbl.rows[i].cells.length - 1; j++){
			if(tbl.rows[i].cells[j].innerText != ""){
				//var d = new Date(y.value, m.value - 1, tbl.rows[i].cells[j].innerText);
				//var s = _GetEventsUrl(url + d.format("yyyy-MM-dd"));
				var xpath = "//ALLEVENTS/D[@y='" + y.value + "'][@m='" + m.value + "'][@d='" + tbl.rows[i].cells[j].innerText + "']";
				//alert(xpath);
				var id = GetEventId(xmlAllEvents, xpath)
				if(id != "-1"){
					tbl.rows[i].cells[j].style.backgroundColor = "#222222";
					tbl.rows[i].cells[j].className = "white13b";
					tbl.rows[i].cells[j].style.cursor = "pointer";
					tbl.rows[i].cells[j].eventid = id;
					eval('tbl.rows[i].cells[j].onclick = function (){CalClick3(this);}')
				}
			}
			else{
				//alert("empty");
			}
		}
	}
	return;
}

function CalClick3(cell){
	//alert(url);
	document.location = "EventsDetail.asp?aid=" + cell.eventid;
}


function GetEventId(dom, xpath){//No Event, Return -1
	var id = "-1";

	if(ie){
		var nodes = dom.selectNodes(xpath);
		if(nodes.length > 0){
			id = nodes[0].getAttribute("id");
		}
	}
	else if(moz){
		var xpe = new XPathEvaluator();   
		var nsResolver = xpe.createNSResolver(dom.ownerDocument == null ? dom.documentElement: dom.ownerDocument.documentElement);
		var result = xpe.evaluate(xpath, dom , nsResolver, 0, null);
		var found = [];
		var res = result.iterateNext();
		while (res){
			found.push(res);
			res = result.iterateNext();
		}
		if(found.length > 0){
			id = found[0].getAttribute("id");
		}

	}
	return id;
}
