$(document).ready(function(){
	$("#cap").blur( function(e) {
		CheckSN('cap');
	});

	$(".only_int")
	.keypress( function(e) {
	    if($.browser.msie)
    	    return isNum(e.keyCode)
	    else
    	    return (e.keyCode) ? true : isNum(e.charCode)
	});
})



function CheckSN(Id) {
	SetAsNormal(Id);
	if ($('#cap').val() != '') {
		var url = '/feedback/ajax/check.php';
		var _sn = $('#cap').val();
		var pars = 'sn='+_sn;
	    var ajaxResult=false;

		var myAjax = $.ajax( {url:url, async: false, type:'POST', data:pars, 

		    error:function(a,b) {alert(a);},
			complete: 
            function(data, textStatus) {
				if (data.responseText != '')
				{
					ShowErrorPrompt(Id, "Контрольное число введено неверно. Пожалуйста, исправьте.");
					ajaxResult=false;
				}   	
				else if (ErrorInstance == Id)
				{
					HideErrorPrompt(Id);			
					ajaxResult=true;
				}
				else if (ShowHint == Id)
				{
					HideHint();
					ajaxResult=true;
				} else
				{
					ajaxResult=true;
				}
	
			}});
			return ajaxResult;
		}
		else if (ErrorInstance == Id) {
			HideErrorPrompt(Id);
			return true;
		}	
		return true;
	}


function showAggr()
	{
		var vpin = document.getElementById('client_que');
		if (vpin.options[vpin.selectedIndex].value != 'zero')
		{
			document.getElementById('aggr_number').style.display = 'block';	
		}	
	}
	
	function elemNotNull(id)
	{return (getElemById(id)!=null);}
	function getElemById(id)
	{
		if (document.all!=null)
		{return eval('document.all.' + id);}
		else{return document.getElementById(id);}
	}
	// Safe way to make an item visible or not
	function makeVisible(sElementID,bMakeVis)
	{
		if(elemNotNull(sElementID))
		{
			var oItem = getElemById(sElementID);
			if(bMakeVis==1){oItem.style.display='';}
			else{oItem.style.display='none';}
		}
		return false;
	}
	function existing_customer_yesnoyes() 
	{
	// If this is the last item selected in the 
			makeVisible("existing", 1);
			makeVisible("nonexisting", 0);
			document.getElementById('aggr_number').style.display = 'block';	
	}
	function existing_customer_yesnono() 
	{
	// If this is the last item selected in the 
			makeVisible("existing", 0);
			makeVisible("nonexisting", 1);
			document.getElementById('aggr_number').style.display = 'none';	

	}
		function question_general() 
	{
			makeVisible("a_question", 1);
			makeVisible("a_complaint", 0);
			makeVisible("a_question2", 0);
			makeVisible("a_compliment", 0);
	}
		function question_complaint() 
	{
			makeVisible("a_question", 0);
			makeVisible("a_question2", 0);
			
			var xx = document.getElementById("xoxo3").selectedIndex;
            if (xx != 5) makeVisible("a_complaint", 1);
            else makeVisible("a_complaint", 0);
			makeVisible("a_compliment", 0);
	}
		function question_compliment() 
	{
			makeVisible("a_question", 0);
			makeVisible("a_question2", 0);
			makeVisible("a_complaint", 0);
			var xx = document.getElementById("xoxo3").selectedIndex;
            if (xx != 5) makeVisible("a_compliment", 1);
            else makeVisible("a_compliment", 0);

	}
	
	function question_general2() 
	{
	        var xx = document.getElementById("xoxo3").selectedIndex;
            if (xx == 5)
            {
                document.getElementById('a_question2').style.display = "block";
                document.getElementById('a_question').style.display = "none";
            }
            else
            {
                makeVisible("a_question", 1);
                makeVisible("a_question2", 0);
            }
			
			makeVisible("a_complaint", 0);
			makeVisible("a_compliment", 0);
	}
	
	
	function obvng()
	{
	       if (document.getElementById('l1').checked == true)
	       {
	             question_general2(); 
	       }  
	       if (document.getElementById('l2').checked == true)
	       {
	             question_complaint(); 
	       }  
	}
	
function level1List(mode)
{
    var url = 'sfcp.php';
    var pars = 'level1=all&mode='+mode;
   
	$.ajax({
		type: "GET",
		url: url,
		dataType: "xml",
		data: pars,
		success: function(xml) {
            showLevel1(xml);
		}
	});
}

function showLevel1(xml)
{
    $('#level1').html('');
    $(xml).find('item').each(function(){
        $('#level1').append('<option value="' + $(this).attr('item_id') + '">'   + $(this).attr('name') + '</option>');
    });
    var mode = $(xml).find('mode').text();
    level2List(mode);
}

function level2List()
{
    var level1 = $('#level1').val();
    var url = 'sfcp.php';
    
    if (level1 != '')
    {
        var pars = 'level1='+level1+'&mode=';
        
        	$.ajax({
    		type: "GET",
    		url: url,
    		dataType: "xml",
    		data: pars,
    		success: function(xml) {
                showLevel2(xml);
    		}
    	});
    }
    else
    {
        $('#level2').html('');
    }
}

function showLevel2(xml)
{
    $('#level2').html('');
    $(xml).find('item').each(function(){
        $('#level2').append('<option value="' + $(this).attr('item_id') + '">'   + $(this).attr('name') + '</option>');
    });
  //  $('#level2').append('<option value="0">Другое</option>');
}
	
	
	
function SendFeedback()
{
	SumErrors = '';
	
	SetAsNormal('email');
	SetAsNormal('name');
	SetAsNormal('surname');
	SetAsNormal('middle_name');
	SetAsNormal('region');

	SetAsNormal('yesnoyes');
	SetAsNormal('yesnono');
	SetAsNormal('l1');
	SetAsNormal('l2');
	
	
	SetAsNormal('level1');
	SetAsNormal('level2');
	
	if ((document.getElementById('telephone_area').value == '' ||  
		document.getElementById('telephone_number').value == '')  && 
		CheckPhoneNumber('telephone'))
		{
			SetAsWrong('telephone');
			SumErrors++;
		}
		
		if (document.getElementById('name').value == '')
	{
		SumErrors++;
		SetAsWrong('name');
	}
		if (document.getElementById('email').value == '')
	{
		SumErrors++;
		SetAsWrong('email');
	}
						
	if (document.getElementById('surname').value == '')
	{
		SumErrors++;
		SetAsWrong('surname');
	}
			
		
	if (document.getElementById('middle_name').value == '')
	{
		SumErrors++;
		SetAsWrong('middle_name');
	}
	
	if ($('#region').val() == '')
	{
	   SumErrors++;
	   SetAsWrong('region');
	}

	if (!document.getElementById('yesnoyes').checked && !document.getElementById('yesnono').checked)
	{
		SetAsWrong('yesnoyes');
		SetAsWrong('yesnono');
		SumErrors++;
	}

	if (!document.getElementById('l1').checked && !document.getElementById('l2').checked)
	{
		SetAsWrong('l1');
		SetAsWrong('l2');
		SumErrors++;
	}

	if ($('#level1').val() == '')
	{
	   SumErrors++;
	   SetAsWrong('level1');
	}
	
	if ($('#level2').val() == '')
	{
	   SumErrors++;
	   SetAsWrong('level2');
	}
			
	if ($('#cap').val() == '' || 
		!CheckSN('cap'))
	{
	   SumErrors++;
	   SetAsWrong('cap');
	}

			
	if (SumErrors > 0)
	{
		document.getElementById('FormSubmitError').style.display = 'block'; //alert(ErrorString); ErrorString = '';
		return false;
	}
	else
	{	
		document.getElementById('FormSubmitError').style.display = 'none';
		
		if (($('#level1').val() == '') || ($('#level2').val() == ''))
      	{
      	     document.getElementById('FormSubmitError').style.display = 'block'; //alert(ErrorString); ErrorString = '';
		      return false;
      	}
      	else return true;
	}
	
}
function isNum(cCode){  
    return /[0-9]/.test(String.fromCharCode(cCode))
}
