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,430,385);
	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,430,385);
	return ret;
}; 

function showDialog(url,arg,dialogHeight,dialogWidth){
		var sFeatures="dialogHeight:"+(dialogHeight + 10) +"px;dialogWidth:"+dialogWidth+"px;scroll:0;status:0;"
		return window.showModalDialog(url,arg,sFeatures);
}

/// ??????
// input:
// obj.message
// obj.detail
function showError(obj){
	var url = root_url + "/error/report500client.jsp";
	return showDialog(url,obj,310,500);
}

/// ????
function processException(title, description){
	var obj = new Object();
	obj.message = title ? title : "????";
	obj.detail = description ? description : title;
	if(obj.detail.indexOf("User haven't logon yet!")!=-1){
		window.location.href="/B2C/modules/HQ/index.jsp";
		return;
	}
	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;
}