var Max_List = 20;
var crewcrewcode = '10090200';
function loadEvent(){
	loadeventDBMenu('crewcrew', encodeURL('event_db ED LEFT JOIN eventsupport_db ON (ED.f_eventnumber = eventsupport_db.f_eventnumber or ED.f_creator = eventsupport_db.f_creator)'),encodeURL('WHERE (ED.f_visibleflags != 8)  and (eventsupport_db.f_sscode = \''+ crewcrewcode + '\') and (ED.f_enddate >= CURRENT_DATE)'), '≪「<a href="http://www.crewcrew.net/" target="_blank">くるくる</a>」のオススメ≫');
	loadeventDBMenu('event',encodeURL('event_db ED'),encodeURL('WHERE (ED.f_visibleflags = 1) and ((ED.f_genre = \'弁当の日\') or (ED.f_keyword LIKE \'%弁当の日%\')) and (ED.f_enddate >= CURRENT_DATE)'),'≪弁当の日関連のイベント≫');
}

function loadeventDBMenu(id, tableName, Where, Title){
	var xmlhttp = createXMLHttp();
	if (xmlhttp){
		xmlhttp.onreadystatechange = function () {
			geteventDB(xmlhttp, id, Title);
		};
		xmlhttp.open('POST', 'selectwsdl.cgi', true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.send('table=' + tableName + '&field=ED.f_eventnumber,ED.f_title,ED.f_catch,ED.f_startdate,ED.f_enddate&order=' + encodeURL('ED.f_enddate ASC') + '&where=' + Where + '&quot="');
	}else{
		alert("サーバーにアクセスできませんでした");
	}
}
function geteventDB(xmlhttp, id, Title){
	var hMes = Title + '<br>';
	var i, listData, Line, Count = 0;
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
		listData = xmlhttp.responseText.substr(0, xmlhttp.responseText.lastIndexOf('"')).substr(1).split('"\n"');
		hMes += '<table width="100%" height="100%" align="center" border="0" cellpadding="0" cellspacing="4" class="waku">';
		hMes += '<tr><td valign="top">';
		hMes += '<dl>';
		for (i = 0; (i < Max_List) && (i < listData.length); i++){
			if (listData[i] != ''){
				Line = listData[i].split('","');
				if (Line[3] == Line[4]){
					hMes += '<dt>・<a href="yomo_calender.cgi?template=temp_calender_details.html&f_eventnumber=' + Line[0] + '" target="sub" class="a_dt">' + Line[3].substr(5) + ':' + Line[1] + '</a></dt>';
				} else {
					hMes += '<dt>・<a href="yomo_calender.cgi?template=temp_calender_details.html&f_eventnumber=' + Line[0] + '" target="sub" class="a_dt">' + Line[3].substr(5) + '-' + Line[4].substr(5) + ':' + Line[1] + '</a></dt>';
				}
				if (Line[2] != ''){
					hMes += '<dd><a href="yomo_calender.cgi?template=temp_calender_details.html&f_eventnumber=' + Line[0] + '" target="sub" class="a_dd">' + Line[2].replace(/\n/g, '<br>') + '</a></dd>';
				}
				Count++;
			}
		}
		hMes += '</dl>';
		hMes += '</td></tr>';
		hMes += '</table>';
		if (Count == 0){
			hMes = '';
		}
		DispHTML(id, hMes);
	}
}
function encodeURL(str){
    var s0, i, s, u;
    s0 = "";                // encoded str
    for (i = 0; i < str.length; i++){   // scan the source
        s = str.charAt(i);
        u = str.charCodeAt(i);          // get unicode of the char
        if (s == " "){s0 += "+";}       // SP should be converted to "+"
        else {
            if ( u == 0x2a || u == 0x2d || u == 0x2e || u == 0x5f || ((u >= 0x30) && (u <= 0x39)) || ((u >= 0x41) && (u <= 0x5a)) || ((u >= 0x61) && (u <= 0x7a))){       // check for escape
                s0 = s0 + s;            // don't escape
            }
            else {                  // escape
                if ((u >= 0x0) && (u <= 0x7f)){     // single byte format
                    s = "0"+u.toString(16);
                    s0 += "%"+ s.substr(s.length-2);
                }
                else if (u > 0x1fffff){     // quaternary byte format (extended)
                    s0 += "%" + (oxf0 + ((u & 0x1c0000) >> 18)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0x3f000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else if (u > 0x7ff){        // triple byte format
                    s0 += "%" + (0xe0 + ((u & 0xf000) >> 12)).toString(16);
                    s0 += "%" + (0x80 + ((u & 0xfc0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
                else {                      // double byte format
                    s0 += "%" + (0xc0 + ((u & 0x7c0) >> 6)).toString(16);
                    s0 += "%" + (0x80 + (u & 0x3f)).toString(16);
                }
            }
        }
    }
    return s0;
}

