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('http://akopylov.www.hotels.su.test/newDes/forms.php');
	formXmlHTTP.setEndPoint('http://hotels.su/newDes/forms.php');
	formXmlHTTP.load('name_form='+name_form, false);
	formXmlHTTP.getResponse();
	//xmlData = formXmlHTTP.getText();
	xmlData = formXmlHTTP.getXML();
	insert_data_form (xmlData);
	//alert (xmlData);
}

function insert_data_form (xml_data)
{
	xml_form = xml_data;
	/*if (!xml_form || !xml_form.documentElement)
	{
		throw ('Bad structure 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-\u005e\u007c-\u007f\u002f\u0060]+$/);
	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;
	}
 }
 
function whoForm (elem, numb)
{
	form1 = document.getElementById ('affil_1');
	form2 = document.getElementById ('affil_2');
	statusAjax = false;
	if (numb == 1)
	{
		
		form1.style.visibility = 'visible';
		form1.style.display = 'block';
		form2.style.visibility = 'hodden';
		form2.style.display = 'none';
	}
	if (numb == 2)
	{
		form2.style.visibility = 'visible';
		form2.style.display = 'block';
		form1.style.visibility = 'hidden';
		form1.style.display = 'none';
	}
}
