
var subFilled = new Array(false,false,false,false,false,false,false,false);

function getSubCats(str)
 { 
  var selectArray=new Array();
  selectArray = JOINdotPHPSelectDebuggerIE();
  if(str=="0")
    {
       if(selectArray[0]=='')
         {
           document.getElementById("selectSub1").innerHTML = '<option value="0">Subcategory 1</option>';
           document.getElementById("selectSub2").innerHTML = '<option value="0">Subcategory 2</option>';
           document.getElementById("selectSub3").innerHTML = '<option value="0">Subcategory 3</option>';
           document.getElementById("selectSub4").innerHTML = '<option value="0">Subcategory 4</option>';
         }
       else
         {
           document.getElementById("joinSubCats").innerHTML = selectArray[0]+'<option value="0">Subcategory 1</option>'+selectArray[4]+selectArray[1]+'<option value="0">Subcategory 2</option>'+selectArray[4]+selectArray[2]+'<option value="0">Subcategory 3</option>'+selectArray[4]+selectArray[3]+'<option value="0">Subcategory 4</option>'+selectArray[4];
         }
    }
   else
    {
      if(selectArray[0]=='')
         {
           document.getElementById("selectSub1").innerHTML = '<option value="0">One moment...</option>';
           document.getElementById("selectSub2").innerHTML = '<option value="0">One moment...</option>';
           document.getElementById("selectSub3").innerHTML = '<option value="0">One moment...</option>';
           document.getElementById("selectSub4").innerHTML = '<option value="0">One moment...</option>';
         }
       else
         {
           document.getElementById("joinSubCats").innerHTML = selectArray[0]+'<option value="0">One moment...</option>'+selectArray[4]+selectArray[1]+'<option value="0">One moment...</option>'+selectArray[4]+selectArray[2]+'<option value="0">One moment...</option>'+selectArray[4]+selectArray[3]+'<option value="0">One moment...</option>'+selectArray[4];
         }
     xmlHttp=GetXmlHttpObject()
      if (xmlHttp==null)
        {
          alert ("Browser does not support HTTP Request")
          return
        }
     var url="JOINgetSub.php"
     url=url+"?q="+str
     url=url+"&sid="+Math.random()
     xmlHttp.onreadystatechange=stateChangedSubJOIN
     xmlHttp.open("GET",url,true)
     xmlHttp.send(null)
    }
 }

function stateChangedSubJOIN()
 { 
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     { 
        var selectArray = new Array();
        selectArray = JOINdotPHPSelectDebuggerIE();
      if(selectArray[0]=='')
         {
           document.getElementById("selectSub1").innerHTML = '<option value="0">Subcategory 1</option>' + xmlHttp.responseText;
           document.getElementById("selectSub2").innerHTML = '<option value="0">Subcategory 2</option>' + xmlHttp.responseText;
           document.getElementById("selectSub3").innerHTML = '<option value="0">Subcategory 3</option>' + xmlHttp.responseText;
           document.getElementById("selectSub4").innerHTML = '<option value="0">Subcategory 4</option>' + xmlHttp.responseText;
         }
       else
         {
           document.getElementById("joinSubCats").innerHTML = selectArray[0]+'<option value="0">Subcategory 1</option>' + xmlHttp.responseText + selectArray[4] + selectArray[1] + '<option value="0">Subcategory 2</option>' + xmlHttp.responseText + selectArray[4] + selectArray[2] + '<option value="0">Subcategory 3</option>' + xmlHttp.responseText + selectArray[4] + selectArray[3] + '<option value="0">Subcategory 4</option>' + xmlHttp.responseText + selectArray[4];
         }
     }
 }

function GetXmlHttpObject()
  {
    var xmlhttp=null;
      try
        {
           // Firefox, Opera 8.0+, Safari
           xmlhttp=new XMLHttpRequest();
         }
       catch (e)
         {
           //Internet Explorer
           try
             {
               xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
             }
           catch (e)
             {
               xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
             }
         }
   return xmlhttp;
 }

function newSub(SubValue, val, old) //SubValue = value of input/drop-down; val = order; old = old Sub (vs create your own)
  {
    if(old) //check if the sub is input or drop-down 
      {
        var c = "0"; 
        var which = "createSub";
        var high = 7;
        var low = 4;
      } else
      {
        var c = "";
        var which = "selectSub";
        var high = 3;
        var low = 0;
      }
    if(SubValue!=c) //check if sub is not empty
     {
       if(!subFilled[val]) // check to see if the sub hasn't been filled yet;
         {                 // this keeps this function from running everytime a key is raised.
          disableSub(which, "0", high, low); // disables a sub on the other side
          subFilled[val]=true; // saves the sub as being filled
         }
     }
    else           // if sub is empty then:
     {
       if(subFilled[val]) // check to see if the sub has been filled;
         {                // this keeps 
           enableSub(which, low, high);
           subFilled[val]=false;
         }
     }
  }

function disableSub(which, val, start, stop)
  {
    var j=4;
    var s = which + j.toString();
    while((document.getElementById(s).disabled==true || subFilled[start]==true) && start>=stop)
      {
        start--;
        j--;
        s = which + j.toString();
      }
    document.getElementById(s).disabled=true;
  }

function enableSub(which, start, stop)
  {
    var j=1;
    var s = which + j.toString();
    while(document.getElementById(s).disabled==false && start<=stop)
      {
        start++;
        j++;
        s = which + j.toString();
      }
    document.getElementById(s).disabled=false;
  }

function JOINdotPHPSelectDebuggerIE()
  {
    var selectArray=new Array();
    if(navigator.appName == "Microsoft Internet Explorer")
      {
         selectArray[0]='<select name="selectSub1" id="selectSub1" onchange="newSub(this.value, 0, true)">';
         selectArray[1]='<select name="selectSub2" id="selectSub2" onchange="newSub(this.value, 1, true)">';
         selectArray[2]='<select name="selectSub3" id="selectSub3" onchange="newSub(this.value, 2, true)">';
         selectArray[3]='<select name="selectSub4" id="selectSub4" onchange="newSub(this.value, 3, true)">';
         selectArray[4]='</select><br>';
      }
    else
      {
        selectArray[0] = '';
      }
    return selectArray;
  }

function checkRadio(strID1, strID2)
 {
   if(document.getElementById(strID1).checked==true)
     {
       document.getElementById(strID2).disabled=false;
     }
   else
     {
       document.getElementById(strID2).disabled=true;
     }
 }

function originSubs()
 {
  var s='';
  var which = 'selectSub';
  for(i=0;i<4;i++)
  {
   s= which+(i+1).toString();
   if(document.getElementById(s).value!=0)
     {
        subFilled[i] = true;
     }
  }
  which = 'createSub';
  for(i=4;i<8;i++)
  {
   s=which+(i-3).toString();
   if(document.getElementById(s).value!=0)
     {
        subFilled[i] = true;
     }
  }   
 }

function mainChanged()
 {
  for(i=1;i<=4;i++)
   {
     document.getElementById("createSub"+i.toString()).disabled=false;
   }
 }