var statusDataForm = false;
var statusAjax = false;
var required = new Array();
var required_show = new Array();
var required_preg = new Array();
var required_length = new Array();
var cur_lang = 'en';
function get_data_form (name_form)
{ 
	formXmlHTTP = new HttpRequest(window);
	formXmlHTTP.setMethodRequest('GET');
	formXmlHTTP.setEndPoint('hotelsrome.net/forms.php');
	formXmlHTTP.load('name_form='+name_form, false);
	formXmlHTTP.getResponse();
	//alert (formXmlHTTP.getText());
	xmlData = formXmlHTTP.getXML();
	
	insert_data_form (xmlData);
}

function insert_data_form (xml_data)
{
	xml_form = xml_data;
	/*if (!xml_form || !xml_form.documentElement)
	{
		throw ('Íåâåðíàÿ ñòðóêòòóðà XML:\n'+xmlHttpB.responseText);
	}	*/
	elArray = xml_form.getElementsByTagName('element');
	count_element = elArray.length;
	if (count_element > 0)
	{
		for (var el=0; el < count_element; el++)
		{
			current_el = elArray.item(el);
			nV = current_el.getAttribute ('nameVar');
			nF = current_el.getAttribute ('nameField');
			nP = current_el.getAttribute ('preg');
			nL = current_el.getAttribute ('length');
			required.push(nV);
			required_show.push(nF);
			required_preg.push(nP);
			required_length.push(nL);
		}
		statusDataForm = true;
	}
	else
	{
		statusDataForm = false;
	}
	statusAjax = true;
}
   

 function SendForm(form, lang)
 {
	if (lang == null || lang == 'undefined')
	{
		cur_lan = 'en';
	}
	else
	{
		cur_lan = lang;
	}
 	if (!statusAjax)
 	{
 		get_data_form(form.name);
 	}
 	if (statusDataForm == true)
 	{
		var i, j;
		for(j=0; j < required.length; j++) 
		{
		    for (i=0; i < form.length; i++) 
		    {
	          if (form.elements[i].name == required[j]) 
			  {
			  	fieldCheck = new checkField (form.elements[i], required_show[j], cur_lan)
			  	if (fieldCheck.ch_len(required_length[j]))
			  	{
			  		if (required_preg[j] == 'word')
			  		{
			  			if(!fieldCheck.ch_wor())
			  			{
			  				form.elements[i].focus();
			  				return false;
			  			}
			  		}			  		
			  		else if(required_preg[j] == 'wordAll')
			  		{
			  			if(!fieldCheck.ch_wor_a())
			  			{
			  				form.elements[i].focus();
			  				return false;
			  			}
			  		}			  		
			  		else if(required_preg[j] == 'email')
			  		{
			  			if(!fieldCheck.ch_mail())
			  			{
			  				form.elements[i].focus();
			  				return false;
			  			}
			  		}			  		
			  		else if(required_preg[j] == 'number')
			  		{
			  			if(!fieldCheck.ch_num())
			  			{
			  				form.elements[i].focus();
			  				return false;
			  			}
			  		}
			  		else
			  		{
			  			return false;
			  		}
			  	  }
			  	  else
			  	  {
			  	  	return false;
			  	  }
		       }
		    }
     	}
     	return true;
 	}
 	else
 	{
 		return true;
 	}
 } 

 function checkField (obj, name_field, lan)
 {
 	if (lan == null || lan == 'undefined')
	{
		this.curlan = 'en';
	}
	else
	{
		this.curlan = lan;
	}
 	if (obj.type == 'textarea')
 	{
 		this.obj = obj.value;
 	}	     	
 	if (obj.type == 'select-one')
 	{
 		this.obj = obj.selectedIndex;
 	}	     	
 	if (obj.type == 'text')
 	{
 		this.obj = obj.value;
 	}
 	this.curlen = 0;
 	this.field = name_field;
 	this.ch_len = check_length;
 	this.ch_wor = check_wor;
 	this.ch_wor_a = check_wor_all;
 	this.ch_num = check_num;
 	this.ch_mail = check_email;
 	this.er_form = false;
 	this.sms_lan = get_sms_lan;
 }
 
 function get_sms_lan(num_str)
 {
 	if(this.curlan == 'ru')
 	{
 		if(num_str == 1)
 		{
 			return 'Ð’ Ð¿Ð¾Ð»Ðµ '+this.field+'\n Ð”Ð¾Ð¿ÑƒÑÑ‚Ð¸Ð¼Ð¾ '+this.curlen+' ÑÐ¸Ð¼Ð²Ð¾Ð»Ð¾Ð², Ñƒ Ð’Ð°Ñ '+this.obj.length;
 		}
 		else if (num_str == 2)
 		{
 			return 'ÐÐµÐ²ÐµÑ€Ð½Ð¾Ðµ Ð¿Ð¾Ð»Ðµ '+this.field+'\n Ð”Ð¾Ð¿ÑƒÑÐºÐ°ÑŽÑ‚ÑÑ ÑÐ¸Ð¼Ð²Ð¾Ð»Ñ‹: a-z A-Z Ð-Ð¯ Ð°-Ñ 0-9 Ð¿Ñ€Ð¾Ð±ÐµÐ»';
 		} 		
 		else if (num_str == 3)
 		{
 			return 'ÐÐµÐ²ÐµÑ€Ð½Ð¾Ðµ Ð¿Ð¾Ð»Ðµ '+this.field+'\n Ð”Ð¾Ð¿ÑƒÑÐºÐ°ÑŽÑ‚ÑÑ ÑÐ¸Ð¼Ð²Ð¾Ð»Ñ‹: a-z A-Z Ð-Ð¯ Ð°-Ñ 0-9 Ð¿Ñ€Ð¾Ð±ÐµÐ» . , ! ? : ( ) ;';
 		} 		
 		else if (num_str == 4)
 		{
 			return 'ÐÐµÐ²ÐµÑ€Ð½Ð¾Ðµ Ð¿Ð¾Ð»Ðµ '+this.field;
 		} 		
 		else if (num_str == 5)
 		{
 			return 'ÐÐµÐ²ÐµÑ€Ð½Ð¾Ðµ Ð¿Ð¾Ð»Ðµ '+this.field+'\n Ð”Ð¾Ð¿ÑƒÑÐºÐ°ÑŽÑ‚ÑÑ ÑÐ¸Ð¼Ð²Ð¾Ð»Ñ‹: 0-9';
 		}
 		else
 		{
 			return 'Error: '+this.field;
 		}
 	}
 	else
 	{
 		if(num_str == 1)
 		{
 			return 'In field '+this.field+'\n Possible '+this.curlen+ 'symbols, beside You '+this.obj.length;
 		}
 		else if (num_str == 2)
 		{
 			return 'Invalid field '+this.field+'\n Are Allowed symbols: a-z A-Z But-I but-I 0-9 gaps';
 		} 		
 		else if (num_str == 3)
 		{
 			return 'The Invalid field '+this.field+'\n Are Allowed the symbols: a-z A-Z But-I but-I 0-9 gaps . , - _! ? : ( )';
 		} 		
 		else if (num_str == 4)
 		{
 			return 'Invalid field '+this.field;
 		} 		
 		else if (num_str == 5)
 		{
 			return 'Invalid field '+this.field+'\n Are Allowed symbols: 0-9';
 		}
 		else
 		{
 			return 'Error: '+this.field;
 		}
 	}
 }

 function check_length (count)
 {
 	this.curlen = count;
 	if (this.obj.length > count)
 	{
 		alert (this.sms_lan(1));
 		return false;
 	}
 	else
 	{
 		return true;
 	}
 }
 
 function check_wor ()
 {
 	valid_val = this.obj.match(/^[^\u0021-\u002f\u003a-\u0040\u005b-\u0060]+$/);
	if (valid_val)
	{
		return true;
	}
	else
	{
		alert (this.sms_lan(2));
		this.er_form = true;
		return false;
	}
 }	 
 
 function check_wor_all ()
 {
 	valid_val = this.obj.match(/^[^\u0022-\u0027\u002a-\u002b\u003c-\u003e\u005b-\u0060\u007c-\u007f\u002f\u002d]+$/);
	if (valid_val)
	{
		return true;
	}
	else
	{
		alert (this.sms_lan(3));
		this.er_form = true;
		return false;
	}
 } 
 
 function check_email ()
 {
 	valid_val = this.obj.match(/^[a-z0-9]+([-_\.]?[a-z0-9])+@[a-z0-9]+([-_\.]?[a-z0-9])+\.[a-z]{2,4}/);
	if (valid_val)
	{
		return true;
	}
	else
	{
		alert (this.sms_lan(4));
		this.er_form = true;
		return false;
	}
 }      
 
 function check_num()
 {
 	valid_val = this.obj.match(/^[0-9]+$/);
	if (valid_val)
	{
		return true;
	}
	else
	{
		alert (this.sms_lan(5));
		return false;
	}
 }