var editor = null;

function initEditor() {
	var config = new HTMLArea.Config();
	config.toolbar = [
	[ "fontname", "space",
	"fontsize", "space",
	"bold", "italic", "underline", "separator",
	"strikethrough", "subscript", "superscript", "separator",
	"copy", "cut", "paste", "space", "undo", "redo" ],

	[ "justifyright", "justifycenter", "justifyleft", "justifyfull", "righttoleft", "lefttoright","separator",
	"indent","outdent", "separator","orderedlist", "unorderedlist", "separator",
	"forecolor", "hilitecolor", "separator",
	"inserthorizontalrule", "createlink", "insertimage", "inserttable", "htmlmode" ]
	];

	for (i=1; i <= 12; i++) {
	cb = eval('document.form.textarea'  + '' + i );
	if (cb) {
		editor = new HTMLArea(cb,config);
		editor.generate();
	}

	}
	cd = eval('document.form.title' );
	if (cd) { cd.focus(); }
}



var xmlHttp;
var cache = new Array();

// AJAX creator
function createXMLHttpRequestObject() {
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
		"MSXML2.XMLHTTP.5.0",
		"MSXML2.XMLHTTP.4.0",
		"MSXML2.XMLHTTP.3.0",
		"MSXML2.XMLHTTP",
		"Microsoft.XMLHTTP");

		for (var i = 0; i<xmlHttpVersions.length && !xmlHttp; i++) {
			try {
				xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
			} catch (e) { }
		}
	}

	if (!xmlHttp) {
		displayError("Error creating XMLHttpRequest Object");
	} else {
		return xmlHttp;
	}

}

// Display Error to Browser
function displayError(message) {
	alert("AJAX ERROR: " + message.toString());
}



// Members - Vaildate form input
function validate(inputValue, fieldId) {

	if (!xmlHttp) {
		xmlHttp = createXMLHttpRequestObject();
	}

	// Make sure xml object exists
	if (xmlHttp) {
		// add field to check array
		if (fieldId) {
			// encode values
			inputValue = encodeURIComponent(inputValue);
			fieldId = encodeURIComponent(fieldId);

			// add to cache array
			cache.push("value=" + inputValue + "&id=" + fieldId);
		}
		try {
			if ((xmlHttp.readyState == 4 || xmlHttp.readyState==0) && cache.length>0) {
				var cacheEntry = cache.shift();
				xmlHttp.open("POST", "http://www.shipozim.co.il/index.php", true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlHttp.onreadystatechange = handleValidateStateChange;
				xmlHttp.send("design=xml&module=members&task=check&" + cacheEntry);
			}
		} catch (e) {
			displayError(e.toString());
		}
	}
}

// function will enter the real function, when ajax object is done
function handleValidateStateChange() {
	if (xmlHttp.readyState==4) {
		if (xmlHttp.status==200) {
			try {
				readValidateResponse();
			} catch (e) {
				alert(e.toString());
			}
		}
	}
}

function readValidateResponse() {
	var response = xmlHttp.responseText;

	// get response in XML format (assume the response is valid XML)
	var responseXml = xmlHttp.responseXML;

	message = responseXml.getElementsByTagName("message")[0].firstChild.data;
	fieldId = responseXml.getElementsByTagName("field")[0].firstChild.data;
	
	var errorDiv = document.getElementById(fieldId + "Error");
	// these is a error
	if (message.length>1) {
		errorDiv.innerHTML = message + "<br />";
		errorDiv.style.display = "";
	} else {
		// no error or error was corrected
		errorDiv.style.display = "none";
	}
	
	
	
	setTimeout("validate();", 100);
}


// White Spaches
function is_ws(nod) {
	return !(/[^\t\n\r ]/.test(nod.data));
}

function findWhiteSpace(node, nodeNo) {
	for (i=0; i<node.childNodes.length; i++) {
		if (node.childNodes[i].nodeType == 3 && is_ws(node.childNodes[i])) {
			nodesToDelete[nodesToDelete.length] = node.childNodes[i]
		}
		if (node.childNodes[i].hasChildNodes()) {
			findWhiteSpace(node.childNodes[i], i);
		}
	}
	node = node.parentNode;
	i = nodeNo;
}

function stripWhiteSpace(node) {
	nodesToDelete = Array();
	findWhiteSpace(node, 0);
	for(i=nodesToDelete.length-1;i>=0;i--) {
		nodeRef = nodesToDelete[i];
		nodeRef.parentNode.removeChild(nodeRef)
	}
}