function xmlRequest(respfunc, dispfunc, taglist, attr) {

    var xmlhttp = init();
    xmlhttp.onreadystatechange = processRequest;

    if(taglist == null) {
	var taglist = new Array('title', 'link', 'pubDate', 'description', 'img', 'media:content', 'media:thumbnail', 'media:title', 'enclosure', 'itunes:summary');
    }
    function init() {

        if (window.XMLHttpRequest) { // code for all new browsers
	    return new XMLHttpRequest();
	}
	else if (window.ActiveXObject) { // code for IE5 and IE6
	    return new ActiveXObject("Microsoft.XMLHTTP");
	}
    }

    function processRequest() {

	if (xmlhttp.readyState==4) {// 4 = "loaded"

	    if (xmlhttp.status==200) {// 200 = OK
		respfunc(xmlhttp, dispfunc, taglist, attr);
	    }
	    else {
		alert('xmlhttp.status : ' + xmlhttp.status);
	    }
	}else {
	    //alert(xmlhttp.readyState);
	}
    }

    this.open = function (method, url, async) {
	var async = (async == null) ? true : async;
	xmlhttp.open(method, url, async);
    }

    this.header = function (key, value) {
	xmlhttp.setRequestHeader(key, value);
    }

    this.send = function (params) {
	xmlhttp.send(params);
    }
}


function rsshandle(xhr, dispfunc, tags, attr) {
    //alert(xhr.responseText);
    var rss = new parseRSS(xhr.responseXML, tags);
    dispfunc(rss);
}

function xmlhandle(xhr, dispfunc, tags, attr) { 
    //alert(xhr.responseText);
    var xml = new parseXML(xhr.responseXML, tags, attr);
    dispfunc(xml);
}

function google_weather_api_handle(xhr, dispfunc, tags, attr) {
    //alert(xhr.responseText);
    var xml = new parse_googleWeather(xhr.responseXML, tags, attr);
    dispfunc(xml);
}


function kma_weather_api_handle(xhr, dispfunc, tags, attr) {
    
    var xml = new parse_kmaWeather(xhr.responseXML, tags, attr);
    dispfunc(xml);
}

function flickr_api_handle(xhr, dispfunc, tags, attr) {

    dispfunc(xhr.responseXML);
}
 

function parse_kmaWeather(responseXML, tags, attr) {

    var data = new Array();
    var iconurl = 'http://www.kma.go.kr/images/icon/DY/';
    $(responseXML).find('data').each(function(idx) {
	    
	    data[idx] = new Object();
	    var day = $(this).find('day').text();
	    switch(day) {
	    case '0': data[idx].day = '오늘'; break;
	    case '1': data[idx].day = '내일'; break;
	    case '2': data[idx].day = '내일모레';
	    }

	    data[idx].hour = $(this).find('hour').text();
	    data[idx].sky = $(this).find('sky').text();
	    data[idx].pty = $(this).find('pty').text();
	    data[idx].wfKor = $(this).find('wfKor').text();
	    data[idx].pop = $(this).find('pop').text();
	    data[idx].temp = Math.round($(this).find('temp').text());
	    var icon = parseInt(data[idx]['sky'])+parseInt(data[idx]['pty']);
	    data[idx].icon = iconurl + 'DB0' + icon + '.png';
	} );

    this.data = data;
}

   
function parse_googleWeather(responseXML, tags, attr) {

    var data = new Object();

    var fi = $(responseXML).find('forecast_information');
    data.fi = new Object();
    data.fi.city = fi.find('city').attr('data');
    data.fi.forecast_date = fi.find('forecast_date').attr('data');
    data.fi.current_date_time = fi.find('current_date_time').attr('data');

    var cc = $(responseXML).find('current_conditions');
    data.cc = new Object();
    data.cc.condition = cc.find('condition').attr('data');
    data.cc.temp_f = cc.find('temp_f').attr('data');
    data.cc.humidity = cc.find('humidity').attr('data');
    data.cc.icon = cc.find('icon').attr('data');

    data.fc = new Array();
    $(responseXML).find('forecast_conditions').each(function(idx) {
	    data.fc[idx] = new Object();
	    data.fc[idx].day_of_week = $(this).find('day_of_week').attr('data');
	    data.fc[idx].low = $(this).find('low').attr('data');
	    data.fc[idx].high = $(this).find('high').attr('data');
	    data.fc[idx].icon = $(this).find('icon').attr('data');
	    data.fc[idx].condition = $(this).find('condition').attr('data');
	});

    this.data = data;
}

function parseXML(responseXML, tags, attr, chunk) {
  
    this.data = new Array();
    var ctgr = '';
    if(chunk == null) chunk = 'item';
    var item = responseXML.getElementsByTagName(chunk);    

    for(var i=0,ci=0; i<item.length; i++,ci++) {
      if(attr != null) {
	ctgr = item[i].getAttribute(attr);
	if(this.data[ctgr] == undefined) {
	  this.data[ctgr] = new Array();
	  ci = 0;
	}
	ci = this.data[ctgr].length
	this.data[ctgr][ci] = new Array();
      }
      else {
	this.data[i] = new Array();
      }

      for(var j=0; j<tags.length; j++) {
	var obj = item[i].getElementsByTagName(tags[j])[0];
	if(obj == undefined) {
	  if(attr == null) {
	    this.data[i][tags[j]] = '';
	  }else {
	    //this.data[ctgr][i][tags[j]] = '';
	  }
	}else {
	  if(tags[j] == 'description') {
	    if(obj.childNodes[1] != undefined) {
	      disc = obj.childNodes[1].nodeValue;
	    }
	    else {
		if(obj.childNodes[0] != undefined) {
		    disc = obj.childNodes[0].nodeValue;
		}else {
		    disc = '';
		}
	    }
	      disc = disc.replace(/<\/?[^>]+>/gi, '');
	      disc = disc.replace(/\s{2,}?/gi, '');
	      disc = disc.substring(0,250);
	    if(attr == null) {
		this.data[i][tags[j]] = disc ;
	    }
	  }else {
	      if(attr == null) {
		if(obj.childNodes[0] != undefined) {
		    this.data[i][tags[j]] = obj.childNodes[0].nodeValue;
		}else {
		    this.data[i][tags[j]] = '';
		}
	    }else {
		  if(obj.childNodes[0] != undefined) {
		      this.data[ctgr][ci][tags[j]] = obj.firstChild.nodeValue;
		  }else {
		      this.data[ctgr][ci][tags[j]] = '';
		  }
	    }
	  }
	}
      }
    }      
}

function parseRSS(responseXML, tags) {
    
    this.data = new Array();
    this.lastModified = null;
    this.title = null;
    this.link = null;
    this.description = null;
    
    var googlerss = false;
    if(responseXML.getElementsByTagName("generator")[0] && responseXML.getElementsByTagName("generator")[0].firstChild.nodeValue == 'NFE/1.0') googlerss = true;

    if(responseXML.getElementsByTagName('pubDate')[0] != undefined) {
	this.lastModified = responseXML.getElementsByTagName('pubDate')[0].firstChild.nodeValue;
    }else {
	this.lastModified = 'not known';
    }

    if(responseXML.getElementsByTagName('title')[0] != undefined) {
	this.title = responseXML.getElementsByTagName('title')[0].firstChild.nodeValue;
    }else {
	this.title = '';
    }

    if(responseXML.getElementsByTagName('link')[0] != undefined) {
	this.link = responseXML.getElementsByTagName('link')[0].firstChild.nodeValue;
    }else {
	this.link = '';
    }

    if(responseXML.getElementsByTagName('description')[0] != undefined) {
	if(responseXML.getElementsByTagName('description')[0].childNodes[0] != undefined) {
	    if(responseXML.getElementsByTagName('description')[0].childNodes[0].nodeValue != undefined) {
		this.description = responseXML.getElementsByTagName('description')[0].childNodes[0].nodeValue;
	    }
	}else {
	    this.description = '';
	}
    }else {
	this.link = '';
    }

    var item = responseXML.getElementsByTagName("item");
    
    for(i=0; i<item.length; i++) {
	this.data[i] = new Array();
	for(j=0; j<tags.length; j++) {
	    obj = item[i].getElementsByTagName(tags[j])[0];
	    if(obj == undefined) {
		this.data[i][tags[j]] = '';
	    }else {
		if(tags[j] == 'description') {
		    if(obj.childNodes[1] != undefined) {
			disc = obj.childNodes[1].nodeValue;
		    }
		    else {
			if(obj.childNodes[0] != undefined) {
			    disc = obj.childNodes[0].nodeValue;
			}else {
			    disc = '';
			}
		    }
		    
		    if(googlerss) {
			disc = strip_tags(disc, '<a><img>');
		    }else {
			//disc = strip_tags(disc, '<a>');
			disc = disc.replace(/<\/?[^>]+>/gi, '');
			disc = disc.replace(/\s{2,}?/gi, '');
			disc = disc.substring(0,300);
		    }
		    this.data[i][tags[j]] = disc;
		}

		else if(tags[j] == 'media:title') {
		    if(obj.firstChild != undefined) {
			this.data[i][tags[j]] = obj.firstChild.nodeValue;
		    }else {
			this.data[i][tags[j]] = '';
		    }
		}

		else if(tags[j] == 'media:content') {
		    this.data[i][tags[j]] = new Object();
		    this.data[i][tags[j]].url = obj.getAttribute('url');
		    this.data[i][tags[j]].type = obj.getAttribute('type');
		    this.data[i][tags[j]].height = obj.getAttribute('height');
		    this.data[i][tags[j]].width = obj.getAttribute('width');
		}

		else if(tags[j] == 'media:thumbnail') {
		    this.data[i][tags[j]] = new Object();
		    this.data[i][tags[j]].url = obj.getAttribute('url');
		    this.data[i][tags[j]].width = obj.getAttribute('width');
		    this.data[i][tags[j]].height = obj.getAttribute('height');
		}
		
		else if(tags[j] == 'enclosure') {
		    this.data[i][tags[j]] = new Object();
		    this.data[i][tags[j]].url = obj.getAttribute('url');
		    this.data[i][tags[j]].length = obj.getAttribute('length');
		    this.data[i][tags[j]].type = obj.getAttribute('type');
		}

		else if(tags[j] == 'itunes:summary') {
		    if(obj.firstChild != undefined) {
			this.data[i][tags[j]] = obj.firstChild.nodeValue;
		    }else {
			this.data[i][tags[j]] = '';
		    }
		}
		
		else {
		    if(obj.firstChild != undefined) {
			this.data[i][tags[j]] = obj.firstChild.nodeValue;
		    }
		    if(tags[j] == 'pubDate') {
			tmpdate = this.data[i][tags[j]];
			date = new Date(tmpdate);
			if(date == 'Invalid Date' || /N/.test(date)) {
			    re = /^(\d\d\d\d)\-(\d\d)\-(\d\d)T.*/;
			    this.data[i][tags[j]] = tmpdate.replace(re, "$1/$2/$3");
			}else {
			    this.data[i][tags[j]] = date.getFullYear() + '/' + (date.getMonth()+1) + '/' + date.getDate();
			}
		    }
		}
	    }
	}
    } 
}
