//Ext.BLANK_IMAGE_URL = './resources/images/default/s.gif';

//Ext.QuickTips.init();


//Ext.Ajax.beforerequest = new function(){
//	showFooter(false);
//};
//
//Ext.Ajax.requestcomplete = new function(){
//	window.setTimeout("showFooter(true)", 1000);
//};
//
//Ext.Ajax.requestexception = new function(){
//	window.setTimeout("showFooter(true)", 1000);
//};


/*
function showAlert(msg,processResult){
	//Ext.Msg.alert("警告", msg);
Ext.Msg.show({
   title:'警告',
   msg:msg,
   buttons: Ext.Msg.OK,
   fn: processResult
});
}

function showConfirm(msg,fnYes,fnNo){
	Ext.Msg.confirm("確認", msg, function(btn){
		if (btn == 'yes' && fnYes != null){
			alert(fnYes);
		}
		else if(fnNo != null)
			alert(fnNo);
	});
}
*/

function addFavorite(type, id){
	var url = "favorite.php";
	var param = "action=add&type="+ type +"&id="+id;
	Ext.Ajax.request({
		url: url,
		params: param,
		callback: evalAjaxResponse
	}	);
}

function cancelFavorite(type, id, secretary){
	var url = "favorite.php";
	var param = "action=cancel&type="+ type +"&id="+id+"&secretary="+secretary;
	Ext.Ajax.request({
		url: url,
		params: param,
		callback: evalAjaxResponse
	}	);
}

function evalAjaxResponse(options,success,response){
	//alert(response.responseText);
	eval(response.responseText);
}

function getAjaxPageNo(key){
	key = "ajax_history_page_"+ key;
	var page = Cookies.get(key);
	if(page == null)
		page = 1;
	Cookies.clear(key);
	return page;
}

function onload_body(){
	window.setTimeout("showFooter(true)", 500);
	var div = document.getElementById("ajax_div");
	if(div == null)
		return;
	var count = Cookies.get("ajax_history_count_"+ div.value);
	if(count == null)
		count = 0;
	ajax_history_init(div.value, count);
}

function onunload_body(){
	var doc = window.ajax_history_frame.document;
	var count = doc.getElementById("ajax_history_count")	;
	var div = doc.getElementById("ajax_history_div");
	var page = doc.getElementById("ajax_history_page");
	if(count == null || div == null || page == null)
		return;
	Cookies.set("ajax_history_count_"+ div.value, count.value);
	Cookies.set("ajax_history_page_"+ div.value, page.value);
}

function showFooter(isShow){
	var f = document.getElementById("footer");
	if(isShow)
		f.style.display = "";
	else
		f.style.display = "none";
}

//
function tk_addTaiwanCityOption(id,selectID){
	var city = null;
	var select = document.getElementById(id);
	select.length = 1;
	selectID = selectID == null ? -1 : selectID;
	for(var i=0; i<tk_taiwanCities.length; i++){
		city = tk_taiwanCities[i];
		select.options[i+1] = new Option(city["name"],city["id"]);
		if(city["id"] == selectID)
			select.options[i+1].selected = true;
	}
}

function tk_addMainlandProvinceOption(id,selectID){
	var prov = null;
	var select = document.getElementById(id);
	select.length = 1;
	selectID = selectID == null ? -1 : selectID;
	for(var i=0,j=0; i<tk_provinces.length; i++,j++){
		prov = tk_provinces[i];
		if(prov["id"] != 1){
			select.options[j+1] = new Option(prov["name"],prov["id"]);
			if(prov["id"] == selectID)
				select.options[j+1].selected = true;
		}
		else
			j--;
	}
}

function tk_addProvinceOption(id,selectID){
	var prov = null;
	var select = document.getElementById(id);
	select.length = 1;
	selectID = selectID == null ? -1 : selectID;
	for(var i=0; i<tk_provinces.length; i++){
		prov = tk_provinces[i];
		select.options[i+1] = new Option(prov["name"],prov["id"]);
		if(prov["id"] == selectID)
			select.options[i+1].selected = true;
	}
}

function tk_addCityOption(id,provinceID,selectID){
	var prov = null;
	var select = document.getElementById(id);
	select.length = 1;
	selectID = selectID == null ? -1 : selectID;
	for(var i=0; i<tk_provinces.length; i++){
		prov = tk_provinces[i];
		if(provinceID == prov["id"]){
			var cities = prov["cities"];
			if(cities.length == 0)
				select.style.visibility = "hidden";
			else
				select.style.visibility = "visible";
			for(var j=0; j<cities.length; j++){
				select.options[j+1] = new Option(cities[j]["name"],cities[j]["id"]);
				if(cities[j]["id"] == selectID)
					select.options[j+1].selected = true;
			}
			return;
		}
	}
}

function tk_addAreaOption(id,cityID,selectID){
	var prov = null;
	var city = null;
	var select = document.getElementById(id);
	select.length = 1;
	selectID = selectID == null ? -1 : selectID;
	for(var i=0; i<tk_provinces.length; i++){
		prov = tk_provinces[i];
		for(var j=0; j<prov["cities"].length; j++){
			city = prov["cities"][j];
			if(cityID == city["id"]){
				var areas = city["areas"];
				if(areas.length == 0)
					select.style.visibility = "hidden";
				else
					select.style.visibility = "visible";
				for(var k=0; k<areas.length; k++){
					select.options[k+1] = new Option(areas[k]["name"],areas[k]["id"]);
					if(areas[k]["id"] == selectID)
						select.options[k+1].selected = true;
				}
				return;
			}
		}
	}
}


function tk_addJobOption(id,selectID){
	var job = null;
	var select = document.getElementById(id);
	select.length = 1;
	selectID = selectID == null ? -1 : selectID;
	for(var i=0; i<tk_jobs.length; i++){
		job = tk_jobs[i];
		select.options[i+1] = new Option(job["name"],job["id"]);
		if(job["id"] == selectID)
			select.options[i+1].selected = true;
	}
}

function tk_addJobDetailOption(id,jobID,selectID){
	var job = null;
	var select = document.getElementById(id);
	select.length = 1;
	selectID = selectID == null ? -1 : selectID;
	for(var i=0; i<tk_jobs.length; i++){
		job = tk_jobs[i];
		if(jobID == job["id"]){
			var details = job["details"];
			if(details.length == 0)
				select.style.visibility = "hidden";
			else
				select.style.visibility = "visible";
			for(var j=0; j<details.length; j++){
				select.options[j+1] = new Option(details[j]["name"],details[j]["id"]);
				if(details[j]["id"] == selectID)
					select.options[j+1].selected = true;
			}
			return;
		}
	}
}

function tk_addPositionOption(id,selectID){
	var pos = null;
	var select = document.getElementById(id);
	select.length = 1;
	selectID = selectID == null ? -1 : selectID;
	for(var i=0; i<tk_positions.length; i++){
		pos = tk_positions[i];
		select.options[i+1] = new Option(pos["name"],pos["id"]);
		if(pos["id"] == selectID)
			select.options[i+1].selected = true;		
	}
}

function tk_addGradeOption(id,selectID){
	var grade = null;
	var select = document.getElementById(id);
	select.length = 1;
	selectID = selectID == null ? -1 : selectID;
	for(var i=0; i<tk_grades.length; i++){
		grade = tk_grades[i];
		select.options[i+1] = new Option(grade["name"],grade["id"]);
		if(grade["id"] == selectID)
			select.options[i+1].selected = true;		
	}
}

function tk_addMarriageOption(id,selectID){
	var marr = null;
	var select = document.getElementById(id);
	select.length = 1;
	selectID = selectID == null ? -1 : selectID;
	for(var i=0; i<tk_marriage.length; i++){
		marr = tk_marriage[i];
		select.options[i+1] = new Option(marr["name"],marr["id"]);
		if(marr["id"] == selectID)
			select.options[i+1].selected = true;				
	}
}

function tk_addInterestOption(id,selectID){
	var interest = null;
	var select = document.getElementById(id);
	select.length = 1;
	selectID = selectID == null ? -1 : selectID;
	for(var i=0; i<tk_interest.length; i++){
		interest = tk_interest[i];
		select.options[i+1] = new Option(interest["name"],interest["id"]);
		if(interest["id"] == selectID)
			select.options[i+1].selected = true;		
	}
}

function tk_showLocationByCity(cityID,showID){
	var prov = null;
	for(var i=0; i<tk_provinces.length; i++){
		prov = tk_provinces[i];
		if(!prov["cities"])
			continue;
		var cities = prov["cities"];
		for(var j=0; j<cities.length; j++){
			if(cities[j]["id"] == cityID){
				tk_showInfo(prov["name"]+"-"+cities[j]["name"],showID);
				return;
			}
		}
	}
	tk_showInfo("未知",showID);
}

function tk_showLocationByArea(cityID,areaID,showID){
	var prov = null;
	var city = null;
	for(var i=0; i<tk_provinces.length; i++){
		prov = tk_provinces[i];
		if(!prov["cities"])
			continue;
		var cities = prov["cities"];
		for(var j=0; j<cities.length; j++){
			city = cities[j];
			if(city["id"] != cityID)
				continue;
			if(areaID<=0 || !city["areas"]){
				tk_showInfo(prov["name"]+"-"+city["name"],showID);
				return;
			}
			var areas = city["areas"];
			for(var k=0; k<areas.length; k++){
				if(areas[k]["id"] == areaID){
					tk_showInfo(prov["name"]+"-"+city["name"]+"-"+areas[k]["name"],showID);
					return;
				}
			}
		}
	}
	tk_showInfo("未知",showID);
}

function tk_showTaiwanCity(cityID,showID){
	var city = null;
	for(var i=0; i<tk_taiwanCities.length; i++){
		city = tk_taiwanCities[i];
		if(city["id"] == cityID){
			tk_showInfo(city["name"],showID);
			return;
		}
	}
	tk_showInfo("未知",showID);
}

function tk_showTaiwanArea(cityID,areaID,showID){
	var city = null;
	for(var i=0; i<tk_taiwanCities.length; i++){
		city = tk_taiwanCities[i];
		if(city["id"] != cityID)
			continue;
		if(areaID<=0 || !city["areas"]){
			tk_showInfo(city["name"],showID);
			return;
		}
		var areas = city["areas"];
		for(var j=0; j<areas.length; j++){
			if(areas[j]["id"] == areaID){
				tk_showInfo(city["name"]+"-"+areas[j]["name"],showID);
				return;
			}
		}
	}
	tk_showInfo("未知",showID);
}

function tk_showSex(id,showID){
	var sex = null;
	for(var i=0; i<tk_sex.length; i++){
		sex = tk_sex[i];
		if(sex["id"] == id){
			tk_showInfo(sex["name"],showID);
			return;
		}
	}
	tk_showInfo("未知",showID);
}

function tk_showJob(id,showID){
	var job = null;
	for(var i=0; i<tk_jobs.length; i++){
		job = tk_jobs[i];
		if(job["id"] == id){
			tk_showInfo(job["name"],showID);
			return;
		}
	}
	tk_showInfo("未知",showID);
}

function tk_showJobDetail(id,detailID,showID){
	var job = null;
	for(var i=0; i<tk_jobs.length; i++){
		job = tk_jobs[i];
		if(job["id"] != id)
			continue;
		if(detailID<=0 || !job["details"]){
			tk_showInfo(job["name"],showID);
			return;
		}
		var details = job["details"];
		for(var j=0; j<details.length; j++){
			if(details[j]["id"] == detailID){
				tk_showInfo(job["name"]+"-"+details[j]["name"],showID);
				return;
			}
		}
	}
	tk_showInfo("未知",showID);
}

function tk_showInfo(info,showID){
	if(showID == null)
		document.write(info);
	else
		document.getElementById(showID).innerHTML = info;

}

// old school cookie functions grabbed off the web
var Cookies = {};
Cookies.set = function(name, value){
     var argv = arguments;
     var argc = arguments.length;
     var expires = (argc > 2) ? argv[2] : null;
     var path = (argc > 3) ? argv[3] : '/';
     var domain = (argc > 4) ? argv[4] : null;
     var secure = (argc > 5) ? argv[5] : false;
     document.cookie = name + "=" + escape (value) +
       ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
       ((path == null) ? "" : ("; path=" + path)) +
       ((domain == null) ? "" : ("; domain=" + domain)) +
       ((secure == true) ? "; secure" : "");
};

Cookies.get = function(name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	var j = 0;
	while(i < clen){
		j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return Cookies.getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if(i == 0)
			break;
	}
	return null;
};

Cookies.clear = function(name) {
  if(Cookies.get(name)){
    document.cookie = name + "=" +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
};

Cookies.getCookieVal = function(offset){
   var endstr = document.cookie.indexOf(";", offset);
   if(endstr == -1){
       endstr = document.cookie.length;
   }
   return unescape(document.cookie.substring(offset, endstr));
};