var useAjax = 0;

var orrigform = "";

function checkAjax(responseAction) {


	//alert('checking');
	items = formData2QueryString(document.pageEdit);	
	items += '&AjaxForm=1';
	//alert(items);
	
	var i = document.getElementById('pendingContent');
	if (i) {
		i.style.display = "block";
		//var j = document.getElementById('formWrap');		
		//orrigform = j.innerHTML;
		//j.innerHTML = i.innerHTML;
	}
	xmlhttpPost("/index.html?action=ajax", items , "gotAjaxSubmit", responseAction );
	//alert('hi done');
}

function gotAjaxSubmit(inStr, ajax, responseAction) {
	
	var i = document.getElementById('pendingContent');
	if (i) {
		i.style.display = "none";
	}
	//alert(inStr);
	var errorList = inStr.split('~~');		
	//cs added second condition. needs better regex. don't know what's up with that.
	if (errorList[0] == "DONE" || errorList[0] == " DONE") {
		var j = document.getElementById('formWrap');

		j.innerHTML = errorList[1];
		eval(responseAction+"()");
		return;
	}
	else {
		var found = 0;
		for (x in errorList) {
			if (errorList[x]) {
				var err = errorList[x].split('|');
				if (err[1] != undefined) {
					//alert('error: ' + err[1] + ' on ' + err[0]);
					var i = document.getElementById('errorText_CEL_'+err[0]);
					i.style.display = 'block';
					i.innerHTML = err[1];
					found++;
				}
			}
		}
		if (found > 0) {
			alert('Please correct the errors in the form before continuing');
		}
	}
	
	//alert(inStr);
	//var j = document.getElementById('formWrap');
	//j.innerHTML = inStr;
}

function checkQuestion(qid, formid, value, parts) {

	if ( useAjax < 1 ) {
		return;
	}

	qstr = 'action=cform_ajax_check_field&formid='+formid+'&qid='+qid+'&Value='+value;
	xmlhttpPost("/index.html?action=ajax", qstr , "gotValidatorResponse" );
	
	
}

function gotValidatorResponse(returnStr) {

	sArray = returnStr.split('|');		
//	t = document.getElementById('testing');

	var qid = sArray[1].replace(/_part.*/, "");
//	t.innerHTML = "QID: " + qid;
	if (!returnStr || sArray[0] == 'ok') {
		var j = document.getElementById('errorText_'+qid);
		if (j){
			j.style.display = 'none';
			return;
		}
	}

	var i = document.getElementById(sArray[1]);
	//i.focus();
	var j = document.getElementById('errorText_'+qid);
	if (j) {
		j.style.display = 'block';
		j.innerHTML = sArray[2];
		//alert(sArray[2]);
	}
}

function ajaxWebFormSubmit() {
//	alert('here');
//	return false;
}

