function loadDraudimasForm(form){
  $("#draudimo_forma").slideUp(200);
  //alert(form);
  try {
		xmlhttp = new XMLHttpRequest();
	}
	catch (e) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				xmlhttp = false;
				alert("Your Browser does not work with Web 2.0!\nPlease Update");
			}
		}
	}
  xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState==4){
      document.getElementById('draudimo_forma').innerHTML=xmlhttp.responseText;
      $("#draudimo_forma").slideDown(200);
      prepareInputsForHints();
    }
  }

  form_url="/cgi-bin/get_forma.php?form="+form;
  xmlhttp.open("GET", form_url, true);
  xmlhttp.send(null);
  
}

function checkFormData(){
  return checkFormTelephone(true);
}

function checkFormTelephone(submit){
  phone=document.getElementById('owner_phone').value;
  pattern1=new RegExp("^[+]{1}[0-9]{11}$");
  pattern2=new RegExp("^[0-9]{11}$");
  pattern3=new RegExp("^[0-9]{9}$");
  if (pattern1.test(phone) || pattern2.test(phone) || pattern3.test(phone)) {
    document.getElementById('owner_phone').style.background="";
    document.getElementById('owner_phone_error').style.display="";
    document.getElementById('owner_phone').style.borderColor="";
    return true;
  }
  else {
    document.getElementById('owner_phone').style.background="#FF4A4A";
    document.getElementById('owner_phone_error').style.display="block";
    document.getElementById('owner_phone').style.borderColor="red";
    if (submit)
      document.getElementById('owner_phone').focus();
    return false;
  }
}

function sendDraudimasFormData(form, file){
  if (checkFormData()) {
    params=putToGet(form)+"send=send";
    //alert(params);
    try {
  		xmlhttp = new XMLHttpRequest();
  	}
  	catch (e) {
  		try {
  			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  		}
  		catch (e) {
  			try {
  				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  			}
  			catch (e) {
  				xmlhttp = false;
  				alert("Your Browser does not work with Web 2.0!\nPlease Update");
  			}
  		}
  	}
    
    xmlhttp.onreadystatechange=function(){
      if(xmlhttp.readyState==4){
        loadDraudimasForm("success");
      }
    }
  
    form_url="/cgi-bin/get_forma.php";
    xmlhttp.open("POST", form_url, true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", params.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(params);
  }
}

function putToGet(obj) {
  var getstr = "";
  inputs=obj.getElementsByTagName("input");
  for (i=0; i<inputs.length; i++) {
      if (inputs[i].type == "text") {
         getstr += inputs[i].name + "=" + inputs[i].value + "&";
      }
      if (inputs[i].type == "hidden") {
         getstr += inputs[i].name + "=" + inputs[i].value + "&";
      }
      if (inputs[i].type == "checkbox") {
         if (inputs[i].checked) {
            getstr += inputs[i].name + "=" + inputs[i].value + "&";
         } else {
            getstr += inputs[i].name + "=&";
         }
      }
      if (inputs[i].type == "radio") {
         if (inputs[i].checked) {
            getstr += inputs[i].name + "=" + inputs[i].value + "&";
         }
      } 
  }
  selects=obj.getElementsByTagName("select");
  for (i=0; i<selects.length; i++) {
      getstr += selects[i].name + "=" + selects[i].value + "&";
  }
  textareas=obj.getElementsByTagName("textarea");
  for (i=0; i<textareas.length; i++) {
      getstr += textareas[i].name + "=" + textareas[i].value + "&";
  }
  return getstr;
}


function changing_ins_gsd(type)
{   
    if (document.id(''+type+'_sex').value != "no")
    {
        //if (type != "other2")
            //document.id(''+type+'_tr_hide').removeClass('f_body_form_table_tr_hidden');

        document.id(''+type+'_hide').removeClass('f_body_form_table_tr_hidden');
    }
    else
    {
        //if (type != "other2")
            //document.id(''+type+'_tr_hide').addClass('f_body_form_table_tr_hidden');

        document.id(''+type+'_hide').addClass('f_body_form_table_tr_hidden');
    }
}

function changing_ins_gid(type)
{   
    if (document.id(''+type+'_sex').value != "no")
    {
        document.id(''+type+'_hide').removeClass('f_body_form_table_tr_hidden');
    }
    else
    {
        document.id(''+type+'_hide').addClass('f_body_form_table_tr_hidden');
    }
}

function changing_ins_gsd2(type)
{   
    if (document.id(''+type+'_sex').value != "no")
    {
        document.id(''+type+'_hide').removeClass('f_body_form_table_tr_hidden');
        document.id(''+type+'_age').removeClass('no_hidden_validation');
    }
    else
    {
        document.id(''+type+'_hide').addClass('f_body_form_table_tr_hidden');
        document.id(''+type+'_age').addClass('no_hidden_validation');
    }
}

function changing_ins_gvd(type)
{   
    if (document.id(''+type+'_sex').value != "no")
    {
        document.id(''+type+'_hide').removeClass('f_body_form_table_tr_hidden');
        document.id(''+type+'_age').removeClass('no_hidden_validation');
    }
    else
    {
        document.id(''+type+'_hide').addClass('f_body_form_table_tr_hidden');
        document.id(''+type+'_age').addClass('no_hidden_validation');
    }
}

var m_driving;

function changing_m_driving_kasko(type)
{      
    m_driving = type;
    
    if (type == 1)
    {
        document.id('f').getElements('.m_driving').each(function(Obj){
            Obj.removeClass('f_body_form_table_tr_hidden');
        });
        
        document.id('age_youngest').removeClass('no_hidden_validation');
    }
    else
    {
        document.id('f').getElements('.m_driving').each(function(Obj){
            Obj.addClass('f_body_form_table_tr_hidden');
        });
        
        document.id('age_youngest').addClass('no_hidden_validation').style.border = "1px solid #c8c7c6";       
    }
}

function changing_owner_type_kasko(type)
{
    if (type == 0)
    {
        document.id('f').getElements('.owner').each(function(Obj){
            Obj.removeClass('f_body_form_table_tr_hidden');
        });
        
        if (m_driving == 1)
        {
            document.id('f').getElements('.m_driving').each(function(Obj){
                Obj.removeClass('f_body_form_table_tr_hidden');
            });
            
            document.id('age_youngest').removeClass('no_hidden_validation');
        }
    }
    else
    {
        if (m_driving == 1)
        {
            document.id('f').getElements('.m_driving').each(function(Obj){
                Obj.addClass('f_body_form_table_tr_hidden');
            });
            
            document.id('age_youngest').addClass('no_hidden_validation').style.border = "1px solid #c8c7c6"; 
        }
        
        document.id('f').getElements('.owner').each(function(Obj){
            Obj.addClass('f_body_form_table_tr_hidden');
        });
        
    }
}

function changing_m_driving(type)
{      
    m_driving = type;
    
    if (type == 1)
    {
        document.id('f').getElements('.m_driving').each(function(Obj){
            Obj.removeClass('f_body_form_table_tr_hidden');
        });
        
        document.id('age_youngest').removeClass('no_hidden_validation');
    }
    else
    {
        document.id('f').getElements('.m_driving').each(function(Obj){
            Obj.addClass('f_body_form_table_tr_hidden');
        });
        
        document.id('age_youngest').addClass('no_hidden_validation').style.border = "1px solid #c8c7c6";       
    }
}

function changing_owner_type(type)
{
    if (type == 0)
    {
        document.id('f').getElements('.owner').each(function(Obj){
            Obj.removeClass('f_body_form_table_tr_hidden');
        });
        
        if (m_driving == 1)
        {
            document.id('f').getElements('.m_driving').each(function(Obj){
                Obj.removeClass('f_body_form_table_tr_hidden');
            });
            
            document.id('age_youngest').removeClass('no_hidden_validation');
        }
    }
    else
    {
        if (m_driving == 1)
        {
            document.id('f').getElements('.m_driving').each(function(Obj){
                Obj.addClass('f_body_form_table_tr_hidden');
            });
            
            document.id('age_youngest').addClass('no_hidden_validation').style.border = "1px solid #c8c7c6"; 
        }
        
        document.id('f').getElements('.owner').each(function(Obj){
            Obj.addClass('f_body_form_table_tr_hidden');
        });
        
    }
}

function changing_driving_abroad(type)
{
    if (type == 0)
    {
        document.id('driving_abroad').addClass('f_body_form_table_tr_hidden');
        
        if (driving_abroad_time == 1)
        {
            document.id('driving_abroad_time').addClass('f_body_form_table_tr_hidden');
            document.id('driving_abroad_time_enter').addClass('no_hidden_validation');
        }
    }
    else
    {
        document.id('driving_abroad').removeClass('f_body_form_table_tr_hidden');

        if (driving_abroad_time == 1)
        {
            document.id('driving_abroad_time').removeClass('f_body_form_table_tr_hidden');
            document.id('driving_abroad_time_enter').removeClass('no_hidden_validation');
        }
    }
}

var driving_abroad_time;

function changing_driving_abroad_time(type)
{
    driving_abroad_time = type;

    if (type == 0)
    {
        document.id('driving_abroad_time').addClass('f_body_form_table_tr_hidden');
        document.id('driving_abroad_time_enter').addClass('no_hidden_validation');
    }
    else
    {
        document.id('driving_abroad_time').removeClass('f_body_form_table_tr_hidden');
        document.id('driving_abroad_time_enter').removeClass('no_hidden_validation');
    }
}

function prepareInputsForHints() {
  var inputs = document.id("draudimo_forma").getElementsByTagName("input");
  var selects = document.id("draudimo_forma").getElementsByTagName("select");
  var textareas = document.id("draudimo_forma").getElementsByTagName("textarea");
  putInputsForHints(inputs);
  putInputsForHints(selects);
  putInputsForHints(textareas);
}

function putInputsForHints(inputs){
  for (var i=0; i<inputs.length; i++){
    inputs[i].onfocus = function () {
        var k=this.parentNode.getElementsByTagName("span");
        for (var j=0; j<k.length; j++){
          if (this.parentNode.getElementsByTagName("span")[j].innerHTML!="" && this.parentNode.getElementsByTagName("span")[j].className=="hint") {
            this.parentNode.getElementsByTagName("span")[j].style.display = "inline";
            break;
          }
        }
    }
    inputs[i].onblur = function () {
      var k=this.parentNode.getElementsByTagName("span");
        for (var j=0; j<k.length; j++){
          if (this.parentNode.getElementsByTagName("span")[j].innerHTML!="" && this.parentNode.getElementsByTagName("span")[j].className=="hint") {
            this.parentNode.getElementsByTagName("span")[j].style.display = "none";
            break;
          }
        }
    }
  }
}
//addLoadEvent(prepareInputsForHints);