
function createHttpRequest() {
	var xmlhttp;
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your browser does not support XMLHTTP!");
	}
	return xmlhttp;
}

function executeSyncronRequest(url) {
	var xmlhttp = createHttpRequest();
	xmlhttp.open("GET", url, false);
	xmlhttp.send(null);
	return xmlhttp.responseXML;
}


function postinterfaceSubmit(formid, requiredids, errordivids, listinput, checkboxids, customValidator) {
	var error = false;
	// handle mailing list checkboxes
	if (listinput && checkboxids && null != document.getElementById(listinput)) {
		var mailingListItem = document.getElementById(listinput);
		var checkList = checkboxids.split(',');
		handleCheckList(mailingListItem, checkList);
	}
	//validating
	if (requiredids && errordivids) {
		var reqIds = requiredids.split(',');
		var errorIds = errordivids.split(',');
		var errors = ';';
		for (var i=0; i<reqIds.length && i<errorIds.length; i++) {
			var lastError = checkError(reqIds[i]);
			error = error || lastError;
			if (null != document.getElementById(errorIds[i])) {
				if (lastError) {
					errors += errorIds[i] + ';';
					document.getElementById(errorIds[i]).style.display = '';
				} else if (errors.indexOf(';' + errorIds[i] + ';') < 0) {
					document.getElementById(errorIds[i]).style.display = 'none';
				}
			}
		}
	}
	// custom validation
	if (customValidator) {
		var customError = customValidator();
		error = error || customError;
	}
	if (error)
		return false;
	if (null != document.getElementById(formid))
		document.getElementById(formid).submit();
}

function checkError(itemId) {
	var item = document.getElementById(itemId);
	if (null == item)
		return true;
	if ('email' == item.name) {
		return validateEmail(item.value);
	} else if (item.tagName.toLowerCase() == 'input') {
		return item.value == '';
	} else if (item.tagName.toLowerCase() == 'select') {
		return item.selectedIndex == 0;
	}
	return true;
}

function validateEmail(email) {
	try {
		var xml = executeSyncronRequest('/ajax/Validator?type=email&email=' + URIEncode(email));
		var tags = xml.getElementsByTagName('param');
		if (tags.length > 0 && tags[0].getAttribute('result') == 'success')
			return false;
		else 
			return true;
	} catch (e){
		alert(e);
		return true;
	}
}

function handleCheckList(mailingListItem, checkList) {
	mailingListItem.value = '';
	
	var checkListSingle = new Array();
	var ind = 0;
	for (var i=0; i<checkList.length; ++i){
		var found = false;
		for (var j=0; j<checkListSingle.length; ++j){
			if (checkListSingle[j] == checkList[i]) {
				found = true;
				break;
			}
		}
		if (!found) {
			checkListSingle[ind] = checkList[i];
			++ind;
		}
	}
	
	for (var i=0; i<checkListSingle.length; i++) {
		var cb = document.getElementById(checkListSingle[i]);
		if (cb.value == '')
			continue;
		if (cb.checked) {
			mailingListItem.value += (mailingListItem.value == '' ? '' : ',') + cb.value;
		}
	}
}

function URIEncode (src) {
	var output = '';
	for (var i=0; i<src.length; i++) {
		var charCode = src.charCodeAt(i);
		if (charCode > 127) {
			output += ('&#' + charCode + ';'); 
		} else {
			output += src.charAt(i);
		}
	}
	return encodeURIComponent(output);
}
