$(document).ready(function() {


  $("#ddummy").val($("#ClientIntendedUse").val());
  $('#residential').hide();  
  $('#legend-commercial').click(function() {
      if ($("#ddummy").val()=="Retail") $("#Retail-Choices").show();
      if ($("#ddummy").val()=="Office") $("#Office-Choices").show();
      var dummy=$("#ddummy").val();
      if (dummy=="Residential") dummy="Office";
      $("#ClientIntendedUse").val(dummy);
      $("#commercial").show();
      $('#residential').hide();
   });
  
  $('#ClientIntendedUse').change(function() {
    var str = "";
    $("select option:selected").each(function () {
      if ($(this).text()=="Residential") {
        $("#commercial").hide();
        $('#residential').show();
      }
    });
  });

  function show_residential() {
    $("#commercial").hide();
    $('#residential').show();
  }

  function seeChecked() {
    var wt=$(this).attr("wt");
    if (wt==1) { var tam=$(this).attr("wt_tam"); var str_arr="west_side"; }
    var et=$(this).attr("et");
    if (et==1) { var tam=$(this).attr("et_tam"); var str_arr="east_side"; }
    var mt=$(this).attr("mt");
    if (mt==1) { var tam=$(this).attr("mt_tam"); var str_arr="midtown"; }
    var up=$(this).attr("up");
    if (up==1) { var tam=$(this).attr("up_tam"); var str_arr="upper_manhattan"; }
    var dt=$(this).attr("dt");
    if (dt==1) { var tam=$(this).attr("dt_tam"); var str_arr="downtown"; }


    var ckbox_selected=$(this).val();
    if (ckbox_selected==1){
      if ($(this).attr("checked")){
        for (k=2; k<=tam; k++) {
          $("#"+str_arr+"_"+k).attr('checked', this.checked);
        }
      }
      else {
        for (k=2; k<=tam; k++) {
          $("#"+str_arr+"_"+k).attr('checked', false);
        }
      }
    }

  }


 if ($('#ClientIntendedUse :selected').text()=="Residential") show_residential();

 $(":checkbox").click(seeChecked);


$('#talk_to_us').click(function() {

   $("#talk_to_us_block").slideToggle();

   
});

function hide(t, id) {
  var tags = document.getElementsByTagName(t);
  for (var i = 5; i < tags.length; i++) {
    if (tags[i].style.display == 'none')
      tags[i].style.display = 'block';
    else tags[i].style.display = 'none';
  }
}


});

function carga() {
   document.LookingForSpaceContactInfoForm.other.style.visibility = 'hidden';
 }
  
  function toggleOther(chosen, texto) {
    var idx = chosen.selectedIndex;
    var Appr = chosen.options[idx].value;
  if (Appr == 'Other') {
    document.getElementById('Other-Choices').style.display = 'block';
    document.getElementById('Retail-Choices-T').style.display = 'none';
    document.getElementById('Office-Choices').style.display = 'none';
    document.getElementById('Retail-Choices').style.display = 'none';
    document.LookingForSpaceContactInfoForm.other.style.visibility = 'visible';
    document.LookingForSpaceContactInfoForm.other.value=texto;

  } else     
  if (Appr == 'Office') {
    document.getElementById('Office-Choices').style.display = 'block';
    document.getElementById('Retail-Choices-T').style.display = 'none';
    document.getElementById('Retail-Choices').style.display = 'none';
    document.getElementById('Other-Choices').style.display = 'none';
    document.LookingForSpaceContactInfoForm.other.style.visibility = 'hidden';
  } else     
  if (Appr == 'Retail') {
    document.getElementById('Retail-Choices-T').style.display = 'block';
    document.getElementById('Retail-Choices').style.display = 'block';    
    document.getElementById('Office-Choices').style.display = 'none';
    document.getElementById('Other-Choices').style.display = 'none';    
    document.LookingForSpaceContactInfoForm.other.style.visibility = 'hidden';
  } else {
    document.LookingForSpaceContactInfoForm.other.style.visibility = 'hidden';
    document.LookingForSpaceContactInfoForm.other.value = '';
    document.getElementById('Office-Choices').style.display = 'none';
    document.getElementById('Retail-Choices-T').style.display = 'none';
    document.getElementById('Retail-Choices').style.display = 'none';
  }
 }

function echeck(str) {

  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1){
   document.getElementById('email-issue').innerHTML = "Invalid E-mail or use spaces@optimalspaces.com"
     return false
  }

  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
   document.getElementById('email-issue').innerHTML = "Invalid E-mail or use spaces@optimalspaces.com"
     return false
  }

  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
    document.getElementById('email-issue').innerHTML = "Invalid E-mail or use spaces@optimalspaces.com"
   return false
  }

   if (str.indexOf(at,(lat+1))!=-1){
    document.getElementById('email-issue').innerHTML = "Invalid E-mail or use spaces@optimalspaces.com"
   return false
   }

   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
    document.getElementById('email-issue').innerHTML = "Invalid E-mail or use spaces@optimalspaces.com"
   return false
   }

   if (str.indexOf(dot,(lat+2))==-1){
    document.getElementById('email-issue').innerHTML = "Invalid E-mail or use spaces@optimalspaces.com"
   return false
   }

   if (str.indexOf(" ")!=-1){
    document.getElementById('email-issue').innerHTML = "Invalid E-mail or use spaces@optimalspaces.com"
   return false
   }

    return true
 }

function ValidateForm(){
 var emailID=document.LookingForSpaceContactInfoForm.ClientEmail

 if ((emailID.value==null)||(emailID.value=="")){
  document.getElementById('email-issue').innerHTML = "Invalid E-mail or use spaces@optimalspaces.com"
  emailID.focus()
  return false
 }
 if (echeck(emailID.value)==false){
  emailID.value=""
  emailID.focus()
  return false
 }
 return true
 }


check = []; //this is an array that stores all the true/false values for each checkbox

function checkBox(id)
 {
 if(check[id] != true) 
  {
  document.getElementById('imgCheck' + id).src = "imagez/true.png"; //change the image
  document.getElementById('inputCheck' + id).value = "true"; //change the field value
  check[id] = true; //change the value for this checkbox in the array
  }
 else
  {
  document.getElementById('imgCheck' + id).src = "imagez/false.png";
  document.getElementById('inputCheck' + id).value = "false";
  check[id] = false;
  }
 }

function handleEnter (field, event) {
  var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  if (keyCode == 13) {
   var i;
   for (i = 0; i < field.form.elements.length; i++)
    if (field == field.form.elements[i])
     break;
   i = (i + 1) % field.form.elements.length;
   field.form.elements[i].focus();
   return false;
  } 
  else
  return true;
 }   

  function toggleDiv(divid){
 if(document.getElementById(divid).style.display == 'none'){
   document.getElementById(divid).style.display = 'block';
 }else{
   document.getElementById(divid).style.display = 'none';
 }
  }
