SCAYTTextArea = function(nTextArea,width,height){
	this._reCaretReturnElements = new RegExp("^(address|blockquote|center|div|dl|fieldset|form|h1|h2|h3|h4|h5|h6|hr|ol|p|pre|table|ul|dd|dt|li|td|th|tr)$","img")
	this._reListElement = new RegExp("^li$","img")
	this._reNewLineElement = new RegExp("^br$","img")
	this._sIFrameText = '';
	this.width = 0;
	this.height = 0;
	this.id = "iFrame";
	this.initalTextValue = '';
	this.text = null;
	this.html = null;
	this.htmlDoc = null;
	this.htmlBody = null;
	this.htmlWnd = null;
	this.SCAYT = null;
	this.initialize = function(nTextArea,width,height){
		if(nTextArea && nTextArea.nodeName.match(/textarea/img)){
			this.text = nTextArea;
			this.width = width || this.text.offsetWidth;
			this.height = height || this.text.offsetHeight;
			//alert(this.text.innerHTML);
			//this.initalTextValue = this.text.value || '';
			this.initalTextValue = this.text.innerHTML.replace(/&lt;/mg,"<").replace(/&gt;/mg,">");
			this.text.style.display = "none";
			this.id = this.id + (new Date()*1);
			this.html = this._createIFrame(this.id,this.width,this.height);
			
			this._setHTMLContent(this.html.contentWindow.document, this.initalTextValue,this.width);

			this.htmlWnd = this.html.contentWindow;
			this.htmlDoc = this.html.contentWindow.document;
			this.htmlBody = this.html.contentWindow.document.body;
			this.htmlDoc.designMode = "On";
			
			this.html.contentWindow.scaytId = (this.id+'SCAYT');
			this.html.contentWindow.html = this.html;

			var self = this;
			delay = function (){
				try{
					if(SCAYT){
						self.SCAYT = new SCAYT({editor:self.html, name:('SCAYT' + self.id), lang:'en' });
					}
				}catch(e){
//					alert(e.message);
				}

			}
			setTimeout(delay,100);
			setInterval(function(){if(self.SCAYT && self.SCAYT.processingMsg)self.SCAYT.processingMsg.close();},2000);
		}else{
			alert("Please specify valid textarea element node");
			return;
		}
	}
	
	this.restoreTextToControl = function(){
		this._sIFrameText = '';
		this._HTMLProcessing(this.html.contentWindow.document.body);
		this.text.value = this.normalizeText(this._sIFrameText);

	}
	this.restoreTextToIframe = function(){
		this.html.contentWindow.document.body.innerHTML = this.text.value;
	}	
	
	this.normalizeText = function(sText){
		return sText.replace(/^\s*/,'').replace(/\s*$/,'');
	}
	this.highlight = function(color){
		this.html.style.borderColor = color;
	}
	this.getId = function(){
		return this.text.id;
	}
	this._setHTMLContent = function(htmlDoc,htmlText,maxWidth){
		htmlDoc.open();
		//htmlDoc.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');		
		htmlDoc.writeln('<html xmlns="http://www.w3.org/1999/xhtml">');		
		htmlDoc.writeln('<head>');		
		htmlDoc.writeln('<title>contenteditable document</title>');				
		htmlDoc.writeln('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');				
		htmlDoc.writeln('<style type="text/css">');
		htmlDoc.writeln('html,body{margin:0px; padding:0px; font-family:"Courier New", Courier, monospace; font-size:13px;}');				
		htmlDoc.writeln('body{padding:0px; width:' + (maxWidth-25) + 'px;}');				
		htmlDoc.writeln('p{margin:0px;}');
		htmlDoc.writeln('ul,ol{margin:0px 32px; padding:0px;}');				
		htmlDoc.writeln('</style>');				
		htmlDoc.writeln('</head>');
		htmlDoc.writeln('<body>');				
		htmlDoc.writeln(htmlText);				
		htmlDoc.writeln('</body>');
		htmlDoc.writeln('</html>');				
		htmlDoc.close();
	}
	
	this._createIFrame = function(id,width,height){
		var iframeSkeletonHTML = '<iframe src="javascript:void(0);" frameborder="0" id="' + id + '" scrolling="auto" style="width:'+width+'px; height:'+height+'px;"></iframe>';
		
		//create insertion point
		var nDummy = document.createElement("div");
		nDummy.style.display = "inline";
		if(this.text.nextSibling){
			this.text.parentNode.insertBefore(nDummy,this.text.nextSibling);
		}else{
			this.text.parentNode.appendChild(nDummy);
		}
		if(nDummy.outerHTML){
			nDummy.outerHTML = iframeSkeletonHTML;
		}else if(document.createRange){
			var range = document.createRange();
			range.setStartBefore(nDummy);
			range.setEndAfter(nDummy);
			range.insertNode(range.createContextualFragment(iframeSkeletonHTML));
		}else{
			return false;
		}
		return this.__$(id);
	}
	
	this._HTMLProcessing = function (node){
		if(node.nodeType == 3){
			this._sIFrameText += node.nodeValue;
		}else if(node.nodeType == 1){
			for(var i=0;i<node.childNodes.length;i++){
				var childNode = node.childNodes[i];
				if(this._reCaretReturnElements.test(childNode.nodeName)){
					this._sIFrameText += "\n";
				}
				if(this._reListElement.test(childNode.nodeName)){
					this._sIFrameText += "\t* ";
				}

				this._HTMLProcessing(childNode);
				if(this._reCaretReturnElements.test(childNode.nodeName) || this._reNewLineElement.test(childNode.nodeName)){
					this._sIFrameText += "\n";
				}
			}
		}
	}
	this.__$ = function(id){
		return document.getElementById(id);
	}
	
	this.initialize(nTextArea,width,height);
}

SCAYTText = {
	MODE_ALL_TEXTAREAS:0x1,
	mode:0,
	controls: new Array(),
	initialize: function(params){
		SCAYTText.mode = (params.mode == SCAYTText.ALL_TEXTAREAS) ? params.mode : SCAYTText.ALL_TEXTAREAS ;
		SCAYTText.width = params.width || 300;
		SCAYTText.height = params.height || 300;
 		SCAYTText.onLoad(function(e){
			switch(SCAYTText.mode){
				case SCAYTText.ALL_TEXTAREAS:
					var textareas = document.getElementsByTagName("textarea");
					for(var i=0;i<textareas.length;i++){
						if(SCAYTText.isSupported() == true){
							SCAYTText.controls.push( new SCAYTTextArea(textareas[i],SCAYTText.width,SCAYTText.height));
						}else{
							textareas[i].style.display = "inline";
						}
					}
				break;
			}
		});
	},
	onLoad: function(handler){
		if (window.attachEvent){
			window.attachEvent('onload', handler);
		}else{
			window.addEventListener('load', handler, false);
		}
	},
	beforeSubmit: function(){
		for(var i=0;i<SCAYTText.controls.length;i++){
			SCAYTText.controls[i].restoreTextToControl();
		}
	},
	copyFromControlToIframe: function(){
		for(var i=0;i<SCAYTText.controls.length;i++){
			SCAYTText.controls[i].restoreTextToIframe();
		}
	},	
	highlight: function(id,color){
		var item = SCAYTText.getItemById(id);
		if(item){
			item.highlight(color);
		}
	},
	getItemById: function(id){
		for(var i=0;i<SCAYTText.controls.length;i++){
			if(SCAYTText.controls[i].getId() == id ){
				return SCAYTText.controls[i];
			}
		}
		return false;
	},
	$: function(id){
		return document.getElementById(id);
	},
	isSupported: function(){
		var bIsIE = (( navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (navigator.userAgent.toLowerCase().indexOf("opera") == -1) && navigator.appVersion.match(/MSIE (\d)/)[1] >= 6)?(true):(false);
		var bIsGecko = (navigator.product == "Gecko" && window.XMLHttpRequest && (navigator.userAgent.toLowerCase().indexOf("safari") == -1))?(true):(false);
		return bIsIE || bIsGecko;
	}
}


/*window.onload = function(){
	if(SCAYTText.$('mailform')){
		SCAYTText.$('mailform').reset();
	}
}*/

function validateMailForm(form) {
	//Name is not empty
	if (form.name.value == '')
	{
		form.name.style.borderColor = "#FF0000";
		alert('Please type your name');
		return false;
	}
	form.name.style.borderColor = "#7F9DB9";

	//address retype
    try {
	    var v1 = form.address.value;
	    var v2 = form.addressretype.value;
	
	    if (v1.toLowerCase() != v2.toLowerCase()) {
	       alert('Please verify that you have correctly typed in your e-mail address');
	       return false;
	    }
    }
    catch (er) {
    } 
	
	//address
	if (!form.address.value.match(".+\@.+[.].+")) {
		form.address.style.borderColor = "#FF0000";
		alert("Please enter valid e-mail address");
	  	form.address.focus();
	  	return false;
	}
	form.address.style.borderColor = "#7F9DB9";

	//message
	if (SCAYTText.controls.length > 0){
		SCAYTText.beforeSubmit();
		if (!form.message.value.match(".+")) {
			SCAYTText.highlight("message", "#FF0000");
			alert("Please enter a message text");
			//form.message.focus();
			return false;
		}
		SCAYTText.highlight("message","#7F9DB9");
	}
	
	//captcha
	if(form.capCode){
		if ((/^\s*$/).test(form.capCode.value)) {
			form.capCode.style.borderColor = "#FF0000";
			alert("Enter characters shown on the image.");
		  	form.address.focus();
		  	return false;
		}
		form.capCode.style.borderColor = "#7F9DB9";
	}
		
  	return true;
}


