String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
function checkSubmit(url,param,id,action,overall,pid,actionParam){
	if(document.getElementById('create_btnBox') != undefined){
		temp = document.getElementById('create_btnBox').innerHTML;
		document.getElementById('create_btnBox').innerHTML="<img src='../images/ajax-loader_small.gif'>";
		//document.getElementById('create_btnBox').innerHTML="";
	}
	
	
	if (overall == undefined || overall ==null || overall =='') overall =  'error_msg';
	var overall_error_div;
	document.getElementById(overall).innerHTML="";
	var temperrors=document.getElementById(id).getElementsByTagName("div");
	
	for(var j=0;j<temperrors.length;j++)
	{
		var temperror=temperrors[j];
		if (temperror.id==document.getElementById(overall).id) { 
		overall_error_div=temperror;
		}
	}
	// for swfuploader check thumbnail upload, check the ajax return hidden input.
	if(document.getElementById('thumbnails_con_error') != undefined){
	var thumbs = document.getElementById('thumbnails_con').getElementsByTagName("img");
		if(thumbs.length == 0 ){
			var div_error=document.getElementById('thumbnails_con_error');
			div_error.innerHTML = 'Please upload image';
			div_error.focus();
		}else{
			var div_error=document.getElementById('thumbnails_con_error');
			div_error.innerHTML = '';
		}
	}
	
	var selectss=document.getElementById(id).getElementsByTagName("select"); 
	for(var i=0;i<selectss.length;i++)
	{
		var selects=selectss[i];
		if (selects.title=='mandatory' && selects.value==-1  && !selects.disabled ) {
			var div_error=selects.parentNode.getElementsByTagName('div')[0];
			if (div_error.id==selects.id+'_error') {
			div_error.innerHTML = selects.name+':不能留空';
			selects.focus();
			}
		}
	} 
	
	var inputs=document.getElementById(id).getElementsByTagName("input"); 
	//alert(inputs.length);
	for(var i=0;i<inputs.length;i++)
	{
		var input=inputs[i];
		if (input.title=='mandatory' && input.value.trim()=="" && input.type=="text" && !input.disabled) {
			var div_error=input.parentNode.getElementsByTagName('div')[0];
				//alert(div_error.id+','+input.id+'_error');
			if (div_error.id==input.id+'_error') {
			div_error.innerHTML = input.name+':不能留空';
			input.focus();
			}
		}
		
		if (input.type=="password" && input.value.trim()=="") {
			var div_error=input.parentNode.getElementsByTagName('div')[0];
			if (div_error.id==input.id+'_error') {
			div_error.innerHTML = input.name+':不能留空';
			input.focus();
			}
		}
	} 
	
	var areas=document.getElementById(id).getElementsByTagName("textarea"); 
	for(var i=0;i<areas.length;i++)
	{
		
		var area=areas[i]; //alert(area.title+' area- '+area.name);
		if (area.title=='mandatory' && area.value.trim()=="") {
			var div_error=area.parentNode.getElementsByTagName('div')[0];
			if (div_error.id==area.id+'_error') {
			div_error.innerHTML = area.name+':不能留空';
			area.focus();
			}
		}
	}
	
	var errors=document.getElementById(id).getElementsByTagName("div");//alert(errors.length);
	for(var i=0;i<errors.length;i++)
	{
		var error=errors[i];//alert(error.innerHTML.trim());
		//alert(error.className+' - '+error.id+' - '+error.title);
		//if (error.title=='error' && error.innerHTML.trim()!="") {
		if (error.className=='error' && error.innerHTML.trim()!="" && overall_error_div.id!=error.id) {
		overall_error_div.innerHTML += error.innerHTML+"<br>";//alert(overall_error_div.innerHTML);
		}
	}
	
	if (overall_error_div.innerHTML=="")
		{
			submitForm(url,param,id,action,pid,actionParam);
		} else {
			if(document.getElementById('create_btnBox') != undefined)
				document.getElementById('create_btnBox').innerHTML = temp;
		}
}

function submitForm(url,param,id,action,pid,actionParam){
	var purl=url;
	var params=param;
	
	
	
	var inputs=document.getElementById(id).getElementsByTagName("input"); 
	
	var multipleNames = Array(); // multiple field use only
	var multipleValueLists = Array(); // multiple field use only
	var multipleCounter = 0;
	
	for(var i=0;i<inputs.length;i++)
	{
		var input=inputs[i];
		//if (input.id=='FCKeditor1')input.value=FCKeditorAPI.GetInstance('FCKeditor1').GetXHTML();
		if (( input.type=="text")  || (input.type == "hidden" ) || (input.type == "file") || (input.type=="radio" && input.checked) ) { // || (input.checked && input.type=="radio")(input.value!="" && input.type=="text")
		params+='&p'+input.id+'='+encodeURIComponent(input.value); 
		}  
		/**/
		if(  input.type=="checkbox" && input.checked ){
					var checkedvale = (input.value!='' && input.value!=null && input.value!=undefined) ? input.value : 1;
				//alert(input.id+','+input.checked+','+checkedvale);
				params+='&p'+input.id+'='+encodeURIComponent(checkedvale);
		}
		
		 //alert(input.alt+'0:'+input.name);
		//if(input.alt=="multiple" && input.title=="multiple"){ alert('1:'+input.name);
		if(input.alt=="multiple" || input.className=="multiple"){
			
			if( elementExistinArray( multipleNames , input.name ) ){//alert('2.1:'+input.name);
				var indexNum = getElementIndexInAry(multipleNames , input.name);
				switch(input.type){
					case "checkbox":
					case "radio":
								//alert('a '+input.name+' '+input.checked);
								var radioValue = (input.checked) ? 1 : 0;
								multipleValueLists[indexNum].push(encodeURIComponent(radioValue));
								break;
					default:	
								multipleValueLists[indexNum].push(encodeURIComponent( input.value));
								break;
				}
			}else{		//alert('2.2:'+input.name);	
				
				multipleNames.push(input.name);	
				multipleValueLists[multipleCounter] = Array();
				switch(input.type){
					case "checkbox":
					case "radio":
						//alert('a2 '+input.name+' '+input.checked);
						var radioValue = (input.checked) ? 1 : 0;
						multipleValueLists[multipleCounter].push(encodeURIComponent(radioValue));
						break;
					default:
						multipleValueLists[multipleCounter].push(encodeURIComponent( input.value));
						break;
				}
				multipleCounter++;
			}		
		}
	}
	
	var multiparams= '';
	//alert(multipleNames);
	for(var a=0; a<multipleNames.length; a++ ){
			multiparams+='&p'+multipleNames[a]+'=';
			for(var b=0; b<multipleValueLists[a].length; b++){
				multiparams+=multipleValueLists[a][b];
				if( (b+1)<multipleValueLists[a].length ){
						multiparams+= '|';
				}
			}
	}
	params+=multiparams;
	

	var selectss=document.getElementById(id).getElementsByTagName("select"); 
	for(var i=0;i<selectss.length;i++)
	{
		var selects=selectss[i];
		if (selects.value!=-1 && !selects.multiple) {
		params+='&p'+selects.id+'='+selects.value; 
		} else if (selects.id==(selects.title+'right') ) { //&& selects.length>0
			var multiParam='';
			for (j=0;j<selects.length;j++)
   			 {
    			if( j == (selects.length-1)){
					multiParam+=selects.options[j].value;
				}else{
					multiParam+=selects.options[j].value+'|';	
				}
    		 }
			 params+='&p'+selects.title+'='+multiParam; 
		}
	}
	
	var areas=document.getElementById(id).getElementsByTagName("textarea"); 
	for(var i=0;i<areas.length;i++)
	{
		var area=areas[i]; 
		if (area!="") {
		params+='&p'+area.id+'='+encodeURIComponent(area.value);
		} 
	}
	
	
	var password=document.getElementById("password");
	if (password!=null && password!=undefined && password.type=="password") params+='&p'+password.id+'='+password.value;
	
	var editor=document.getElementById( 'editorcontents'+pid );
	if(editor != null && editor!=undefined ){
		//destory = (action == createResultChangedRemain) ? false : true;
		destory = true;
		removeEditor(pid,destory);
		if (editor.value!=""){
			tempContent=document.getElementById( 'editorcontents'+pid ).value;		
			//params+='&peditor1='+encodeURIComponent(tempContent);
			params+='&peditor1='+encodeURIComponent(tempContent);
			//removeEditor();
		}
	}
	if (action == undefined || action ==null || action =='') action =  createResultChanged;
	startPostRequest(purl, action, params,actionParam);
}

function changeFromStatus(id){ 
 	if(httpRequest.readyState == 4) {
        if(httpRequest.status == 200) {
			var myJSONObject = httpRequest.responseText.evalJSON();
			if(myJSONObject[0] == true){
				document.getElementById(id).innerHTML = myJSONObject[1];
			}else{
				
				
			}
        }
    }
}

function createXMLHttpRequest() {
var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
            } 
            catch (e) {
                try {
                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                } 
                catch (e) {}
            }
        }

return httpRequest;
}

function startPostRequest(purl, pfun, parameters, param) {
   //var xmlHttp=null;
   httpRequest=createXMLHttpRequest();
   
   if (httpRequest==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
 
	parameters+="&sid="+Math.random();
	
	httpRequest.onreadystatechange=function() {
												pfun(param);
	}
	
	httpRequest.open("POST", purl, true)
	httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	httpRequest.send(parameters)
}
