var root_url = location.href;
root_url = root_url.replace(/\\/g, "");
root_url = root_url.split("//")[1];
root_url = root_url.split("/");
if(root_url.length >= 2)root_url ="/" + root_url[1];
else root_url = "";

/// 搜索部门
// input:
// name
// output:
// 		ret.id
// 		ret.name
//		ret.flag
//		ret.status
function searchOrgByName(name){
	var argv = new Object();
	argv.name = name;
	var url = root_url + "/common/jsp/UnitSelector.jsp";
	var ret = showDialog(url,argv,480,420);
	return ret;
}; 

function searchOrgByName(name,condition){
	var argv = new Object();
	argv.name = name;
	argv.condition = condition;
	var url = root_url + "/common/jsp/UnitSelector.jsp";
	var ret = showDialog(url,argv,480,420);
	return ret;
}; 

function showDialog(url,arg,dialogHeight,dialogWidth){
		var sFeatures="dialogHeight:"+(dialogHeight) +"px;dialogWidth:"+dialogWidth+"px;scroll:auto;status:1;resizable:yes"
		return window.showModalDialog(url,arg,sFeatures);
}

/// 显示错误信息
// input:
// obj.message
// obj.detail
function showError(obj){
	var url = root_url + "/error/report500client.jsp";
	var sFeatures="dialogHeight:200px;dialogWidth:500px;scroll:auto;status:0;resizable:yes"
	return window.showModalDialog(url,obj,sFeatures);
}

/// 异常处理
function processException(title, description){
	var obj = new Object();
	obj.message = title ? title : "未知错误";
	obj.detail = description ? description : title;
	return showError(obj);
}

/// 检查 xmlhttp 返回数据
function validateXMLHTTP(xmlhttp,title){
	var titleStr = (title=="") ? "获取数据错误" : title;
	
	var xmldom = xmlhttp.responseXML;
	if (xmldom==null || xmldom.xml=="") 
	   xmldom.loadXML(xmlhttp.responseText);
	if(!xmldom || !xmldom.xml || xmldom.xml==""){ 
		processException(titleStr, xmlhttp.responseText);
		return false;
	}
	var errs = xmldom.selectNodes("//xsql-error//message");
	if(errs && errs.length > 0){
		var sErr = "";
		for(var i=0; i<errs.length; i++){
			sErr += "#" + i + "> " + errs[i].text +"\n";
		}
		processException(titleStr, sErr);
		return false;
	}
	return true;
}

/// 检查 xmldom
function validateXMLDOM(xmldom,title){
	var titleStr = (title=="") ? "获取数据错误" : title;
	if(!xmldom || !xmldom.xml || xmldom.xml==""){ 
		processException(titleStr, titleStr);
		return false;
	}
	
	var errs = xmldom.selectNodes("//xsql-error//message");
	if(errs && errs.length > 0){
		var sErr = "";
		for(var i=0; i<errs.length; i++){
			sErr += "#" + i + "> " + errs[i].text +"\n";
		}
		processException(titleStr, sErr);
		return false;
	}
	
	return true;
}

function handleException( title , expDescription , fetal )
{
	// if fetal error , dispaly error message and close me!
	processException(title,expDescription);
	if(fetal)
		window.close();
	else 
		return;
}

// 票价级别
// 选择票价级别
function showSelectBasis(obj, isInter)
{
	try {  
		var ctrl= obj;
		var sFeatures = null;
		var url = null;
		if(!isInter) { // 国内
			sFeatures 	=	"dialogHeight:360px;dialogWidth:850px";
			url 		=	"/ECS/common/jsp/farebasis.htm";
		} else {
			sFeatures 	=	"dialogHeight:420px;dialogWidth:730px";
			url 		=	"/ECS/common/jsp/farebasisInter.html";
		}
		var ret = window.showModalDialog(url,null,sFeatures);
		if (!ret)return ;
		
		obj.value	=	ret;
	} catch(exp) {
		obj.readonly = false;
		processException("选择票价级别时发生错误，请手工输入票价级别。",exp.message);
	}
} 
