var errBgColor ="#C0E374";

/**
 * Structured dojo.byId(objectId)
 * @param object id & object
 * @exam $('inbox') = dojo.byId('inbox') = document.getElementById('inbox') =$(dojo.byId('inbox'))
 * @verson 20081014 By Amilim
 */
function $(){
	if(arguments.length <1) {
		//console.debug('Object does not exist');
		return false;
	}
	try {
		var elements = new Array();	
		for (var i = 0; i < arguments.length; i++) {		
			var element = arguments[i];		
			if (typeof element == 'string') {		
				element = dojo.byId(element);	
			}
				
			if (arguments.length == 1){		
				return element;		
			}
			
			elements.push(element);	
		}	
	}catch(e){
		return false;
	}
	return elements;
}

/**
 * Get elements by id igore the element exist or not.
 * @param objectid
 */
function byId( objectId ){
	var value = "";
	try{
		value = $( objectId ).value == "null" ? "" : $( objectId ).value;
	} catch(e){
		value = "";
	}
	return value;
}

function showError( msg,delayTime ) {
	if(delayTime=="" || delayTime ==null){
		delayTime =4000;
	}
	
	if( msg.indexOf("Error") != -1 ) {
		var re =/.*:/i;
		msg = msg.replace(re,"");
	}
	
	loading.errMsg(msg,delayTime);
}

function checkAll(){
	var isOk=true;
	
	/*
	var widgetSet = dijit.registry.filter(
		function(widget){
			return widget.declaredClass=="dijit.form.ValidationTextBox"?true:false;
		}
	);
	
	for(var widgetId in widgetSet._hash) {
		if(!widgetSet._hash[widgetId].isValid()){
			isOk = false;
			widgetSet._hash[widgetId].displayMessage(widgetSet._hash[widgetId].promptMessage );
			return;
		}
	}*/
	
	dijit.registry.filter(
		function(widget){
			return widget.declaredClass=="dijit.form.ValidationTextBox"?true:false;
		}).forEach(function(widget){
		if(!widget.isValid()){
			isOk = false;
			widget.displayMessage(widget.promptMessage );
			return;
		}
	});
	return isOk;
}

function minMaxString(textareaObj,maxObj,minObj,maxLength,minLength){
	textareaObj =dojo.byId(textareaObj);
	maxObj =dojo.byId(maxObj);
	minObj =dojo.byId(minObj);
	var min = minLength - parseInt(textareaObj.value.length);
	if ( min < 1){ min =0 };
	var max = maxLength - parseInt(textareaObj.value.length);
	if ( maxObj.value < 1) {max =0};
	maxObj.value = max;
	minObj.value = min;
	if(textareaObj.value <250){return false; }
	if(textareaObj.value >750){return false; }
}	
/**
 * Show Dialog
 * @param url, 异步页面URL地址
 * @param title, 显示dialog标题，可为空
 * @param width, 显示dialog width 可为空
 * @param height, 显示dialog height 可为空
 * @verson 20081021 By Amilim
 */
function messageShow(url,title,width,height){
	
	if(dijit.byId('teDialog')==undefined){ 
		var teDialog = new dijit.Dialog({
			name:'teDialog',
			refocus:'false',
			preventCache:'true',
			refreshOnShow:'true'
			},$('teDialog'));	
	}
	
	if(url ==null || url =="") { return false; }
	if(width==null || width =="") { width =700; }
	$('teDialog').style.display ="block";
	$('teDialog').style.width=width + "px"; 
	
	if(height==null || height==""){ 
		$('teDialog').style.height ="auto";
	}else{
		$('teDialog').style.height=height + "px";
	}
	dojo.byId('teDialog_title').innerHTML=title;
	dijit.byId('teDialog').setHref(url);
	dijit.byId('teDialog').show();
}	

/**
 * Confirm this message.
 * @param message which will be show on the confirm dialog
 * @verson 1.0.081127 David
 */
var confirmDlg;
function comfirmMsg(message,executeFunc){
	loading.hide();
	dojo.byId("comfirmMessageDiv").innerHTML = message;
	if(!confirmDlg){	
		var pane = dojo.byId('confirmDialog');
		confirmDlg = new dijit.Dialog({
			title: i18n.getMsg("candidate.confirmation.dialog"),
			execute:executeFunc
		},pane);
	}
	
	dojo.byId('confirmDialog').style.width = "400px"; 
	confirmDlg.show();
}	

function messageHide(){
	if(dijit.byId('teDialog')!=undefined){ 
		dijit.byId('teDialog').hide();
	}
}

function messageShowContent(content,title,width,height){
	console.debug(dijit.byId('teDialog'));
	
	if(dijit.byId('teDialog')==undefined){ 
		var teDialog = new dijit.Dialog({
			name:'teDialog',
			refocus:'false',
			preventCache:'true',
			refreshOnShow:'true'
			},$('teDialog'));	
	}
	
	if(content ==null || content =="") { return false; }
	if(title ==null ) { title =""; }
	if(width==null || width =="") { width =700; }
	$('teDialog').style.display ="block";
	$('teDialog').style.width=width + "px"; 
	if(height==null || height==""){ 
		$('teDialog').style.height ="auto";
	}else{
		$('teDialog').style.height=height + "px";
	}
	dojo.byId('teDialog_title').innerHTML=title;
	dijit.byId('teDialog').setContent(content);
	dijit.byId('teDialog').show();
}

function msgShow(content,title,width,height){
	
	if(dijit.byId('teMsgDialog')==undefined){ 
		var teMsgDialog = new dijit.Dialog({
			name:'teMsgDialog',
			refocus:'false',
			preventCache:'true',
			refreshOnShow:'true'
			},$('teMsgDialog'));	
	}
	
	if(content ==null || content =="") { return false; }
	if(title ==null ) { title =""; }
	if(width==null || width =="") { width =700; }
	$('teMsgDialog').style.display ="block";
	$('teMsgDialog').style.width=width + "px"; 
	if(height==null || height==""){ 
		$('teMsgDialog').style.height ="auto";
	}else{
		$('teMsgDialog').style.height=height + "px";
	}
	dojo.byId('teMsgDialog_title').innerHTML=title;
	dijit.byId('teMsgDialog').setContent(content);
	dijit.byId('teMsgDialog').show();
}

//Check form valid, if true 直接执行valid func if false 执行invalidMsgFuc
function checkForm( formId, validFunc , invalidMsgFunc ) {
	alert("checkForm:" + dijit.byId(formId).validate());
	if(dijit.byId(formId).validate()){
		validFunc
	}else{
		invalidMsgFunc
	}
}
//static Html menu
function outMenu(){
	if(arguments.length <1) {
		//console.debug('Object does not exist');
		return false;
	}
	if(arguments[0]==null||arguments[0].length<1){return false;}
	var obj =$(arguments[0]);
	obj.className ="selectNav";
}

// get dojo.editor value
// add Amilim By 2008-10-31
function getEditorValue(obj){
	var getValue =dijit.byId(obj).getValue().Trim();
	getValue =formatHTML(getValue);
	return getValue;
}
// get input of hidden value for select
function setSelectValue(id, val){
	$(id).value=val;
}
// Sort By
// add Amilim By 2008-11-14
// Modified input parameters by David 2008-11-5
// Modified hide description by Amilim 2008-11-27  
function sortBy(description,fieldName){
	var previousFieldName =byId('sortByValue');
	var sortByValue;
	if(fieldName.length < previousFieldName.length){
		sortByValue =fieldName;
		//description = description + " ▲";
	}else{
		if(fieldName==previousFieldName){
			  sortByValue = fieldName + " desc";
			  //description = description + " ▼";
		}else{
			sortByValue = fieldName;
			//description = description + " ▲";
		}
	}
	
	$('sortByValue').value =sortByValue;
	$('_sortByValue').textContent =description;
	$('_sortByValue').innerHTML =description;
}

/**
 * column sort by
 * @param obj  onclick object object || onclick object id
 * @param fieldName  sort by conditions
 */
function columnSortBy(obj,description,fieldName){
	var previousFieldName =byId('sortByValue');
	var sortByValue;
	var sign="";
	
	if(fieldName.length < previousFieldName.length){
		sortByValue =fieldName;
		sign =" ▲";
	}else{
		if(fieldName==previousFieldName){
			sortByValue = fieldName + " desc";
			sign =" ▼";
		}else{
			sortByValue = fieldName;
			sign =" ▲";
		}
	}
	
	obj.style.cursor ="pointer";
	description = description + sign;
	$('sortByValue').value =sortByValue;
	obj.innerHTML =description;
}


// get today date
// add Amilim By 2008-11-14
function getToday(){
	var d =new Date();
	var today =d.getFullYear() +"-" + d.getMonth() +"-" +d.getDate();
	return today;
}

function sendEmail(email, subject, message) {
	loading.show(i18n.getMsg("loading"));
	var uri = byId("uri");
	dojo.xhrPost( {
       	url: uri+"/mail/sending.do",
       	form: dojo.byId("emailForm"),
        handleAs: "text",
        preventCache: true,
        load: function(response, ioArgs) {
        	loading.hide();
			messageShowContent(response,'Message has been sent',400);
			$('emailForm').reset();
			$('message').value ="";
        },
		content: {
			email : email,
			subject : subject,
			message:message
		}
    });
}

/*
    * This function will not return until (at least)
    * the specified number of milliseconds have passed.
    * It does a busy-wait loop.
    */
function pause() {
	//Set numberMillis = 5000 million seconds
	var numberMillis = 5000;
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
            return;
    }
}

/**
 * delayGoUrl
 * 延迟页面跳转
 * @verson 20081021 By Amilim
 **/
function delayGoUrl(url,delayTime) {
	if(delayTime=="" ||delayTime==null){
		delayTime =2000;
	}
	setTimeout(function(){location.href = url},delayTime);
}
/**
 * linkBgChange
 * 改变link的样式
 * @verson 20081021 By Amilim
 **/
 function linkBgChange(linkDivId,cssname) {
 	var linkDiv =$(linkDivId);
 	linkDiv.className =cssname;
 }
 
 /**
  * return the true or false , if this object get value.
  * */
 function isEmpty( objectId ){
	 return byId(objectId)=="";
 }
