// JavaScript Document
var dayarray=[];
var thetableclass = "scheduletable";
var theheaderclass = "scheduleheader";
var theheadertop = "scheduletabletop";
var theheadertop2 = "scheduletabletop2";
var thefreeclass = "freetime";
var thecloseclass = "closelink";
var thetakenclass = "taken";
var thetimeclass = "time";
var thetimehighlight = "timehighlight";
var listingaddress = "no address found";
var listingaddress2 = "in database";
var thelinkclass = "page";

//this function creates the whole table that you see
function createTimeTable(thearray,theid,daysback,daysahead,listingid,officeid,theboard,thereferrer,listing_board){
  var mybody = document.getElementById(theid);//find the right element to attach to
  //var mybody=document.getElementsByTagName("div").item(0);
  var mytable = document.createElement("TABLE"); //create the table tag
  //assign the different attributes to the table
  mytable.setAttribute("id","ShowingTable", 0);
  mytable.setAttribute("cellspacing","0",0);
  mytable.setAttribute("cellpadding","0",0);
  mytable.className=thetableclass;//assign the className
  var mytablebody = document.createElement("TBODY"); //create the body
  //Create the header indicating instructions and property information
  //this is the first cell [blank]
  var mycurrent_row=document.createElement("TR");
  var mycurrent_cell=document.createElement("TD");
  var currenttext=document.createTextNode(" ");
  mycurrent_cell.appendChild(currenttext);
  mycurrent_row.appendChild(mycurrent_cell);
  //this is the second cell [welcome. please select a time.]
  var mycurrent_cell=document.createElement("TD");
  var currenttext=document.createTextNode("Welcome! Please select a time.");
  mycurrent_cell.appendChild(currenttext);
  mycurrent_cell.setAttribute("align","left",0);
  mycurrent_cell.setAttribute("colspan","5",0);
  mycurrent_row.appendChild(mycurrent_cell);

  var mycurrent_cell=document.createElement("TD");
  var currenttext=document.createTextNode("Powered By");
  mycurrent_cell.appendChild(currenttext);
  mycurrent_cell.setAttribute("align","center",0);
  mycurrent_cell.setAttribute("colspan","3",0);
  mycurrent_row.appendChild(mycurrent_cell);

  mycurrent_row.className=theheadertop;//set the class for the header

  mytablebody.appendChild(mycurrent_row);
  //this is the second line where the property information is added to the header
  //first cell is [property:]
  var mycurrent_row=document.createElement("TR");
  var mycurrent_cell=document.createElement("TD");
  var currenttext=document.createTextNode("Property:");
//  var currenttext2=document.createTextNode("");
  mycurrent_cell.appendChild(currenttext);
  mycurrent_cell.className=theheadertop2;
//  mycurrent_cell.appendChild(document.createElement("br"));
//  mycurrent_cell.appendChild(document.createElement("&nbsp;"));
//  mycurrent_cell.appendChild(currenttext2);
  mycurrent_row.appendChild(mycurrent_cell);
  mycurrent_row.className=theheadertop;//set the class for the header
  //second cell is [123 sesame st]
  var mycurrent_cell=document.createElement("TD");
  var currenttext=document.createTextNode(listingaddress);
  var currenttext2=document.createTextNode(listingaddress2);
  mycurrent_cell.appendChild(currenttext);
  mycurrent_cell.appendChild(document.createElement("br"));
  mycurrent_cell.appendChild(currenttext2);
  mycurrent_cell.setAttribute("colspan","5",0);
  mycurrent_cell.setAttribute("align","left",0);
  mycurrent_row.appendChild(mycurrent_cell);
   //this is the third cell [AOS Logo]
  var mycurrent_cell=document.createElement("TD");
  //http://www.advancedofficesolution.com/images/AOSLogo.gif
	var image = document.createElement('img');
	image.setAttribute('src','images/AOSLogoNT_smallblue.gif');
  image.setAttribute('border','0');
  image.setAttribute('vspace','2');
  image.setAttribute('hspace','2');
  image.title="Powered By AOS";
  image.alt="Powered By AOS [AOS logo]";
  image.style.cursor="pointer";//sets the widths
  image.onclick = function(){
    window.open("http://www.reaos.com/","reaosDotCom","width=800px,height=600px,scrollbars=1,left=0,top=0");
  };
  //var currenttext=document.createTextNode("Powered By");
  //mycurrent_cell.appendChild(currenttext);
  mycurrent_cell.appendChild(image);
  mycurrent_cell.setAttribute("align","center",0);
  mycurrent_cell.setAttribute("colspan","3",0);
  mycurrent_cell.setAttribute("rowspan","2",0);
  mycurrent_row.appendChild(mycurrent_cell);
  mycurrent_row.className=theheadertop;//set the class for the header
  //add the row to the table


  //add the row to the table
  mytablebody.appendChild(mycurrent_row);
  //this is the third line where the blank line is
  var mycurrent_row=document.createElement("TR");
  var mycurrent_cell=document.createElement("TD");
  //var currenttext=document.createTextNode(" ");
  var currenttext = document.createElement('a');
  currenttext.innerHTML = "&laquo; Back To listings";
  currenttext.href = '#';
	currenttext.className = thecloseclass;
	currenttext.onclick = function(){
    window.close();
  };
  mycurrent_cell.appendChild(currenttext);
  mycurrent_cell.setAttribute("align","left",0);
  mycurrent_cell.setAttribute("vAlign","top");
  mycurrent_cell.setAttribute("colspan","6",0);
  mycurrent_cell.setAttribute("height","40px",0);
  mycurrent_row.appendChild(mycurrent_cell);
  mycurrent_row.className=theheadertop;//set the class for the header
  //add the row to the table
  mytablebody.appendChild(mycurrent_row);
  var mycurrent_row=document.createElement("TR");
  for(var j=0;j<thearray.length;j++) { //go through the array and output the header items
    var mycurrent_cell=document.createElement("TH");
    if(thearray[j][0] == "time1"){ //create a new link to go backwards
      var currenttext = document.createElement('a');
		  currenttext.innerHTML = "&laquo; Back";
		  currenttext.href = 'index.php?pg=timetable&listingid='+listingid+'&officeid='+officeid+'&board='+theboard+'&fromsite='+thereferrer+'&aheaddays='+daysback+'&listingboard='+listing_board;
		  currenttext.className = thelinkclass;
		  mycurrent_cell.className=thelinkclass;//set the class for the header
    }else if(thearray[j][0] == "time2"){ //create a link to go ahead
      var currenttext = document.createElement('a');
		  currenttext.innerHTML = "Ahead &raquo;";
		  currenttext.href = 'index.php?pg=timetable&listingid='+listingid+'&officeid='+officeid+'&board='+theboard+'&fromsite='+thereferrer+'&aheaddays='+daysahead+'&listingboard='+listing_board;
		  currenttext.className = thelinkclass;
		  mycurrent_cell.className=thelinkclass;//set the class for the header
    }else {
      var currenttext=document.createTextNode(thearray[j][1]);
      mycurrent_cell.className=theheaderclass;//set the class for the header
    }
    mycurrent_cell.setAttribute("id",thearray[j][0]);
    mycurrent_cell.appendChild(currenttext);

    mycurrent_row.appendChild(mycurrent_cell);
    mytablebody.appendChild(mycurrent_row);
  }
  //alert('thearray contains '+thearray.length+' subarrays. In each are '+thearray[1].length+' items');
  //figure out how tall/long the array is...
  //start j off at the 3rd item to skip both the ID and the title of the row
  /*the way this loop is done is backwards in thought
  think of it as a table with each column being the array instead of the usual row being the array
  then it is populated by row, left to right.
  NOTE: this is why, even though we start the loop for j(the second value) first, it is still the second value
  */
  var offset=2;
  for(j=offset;j<thearray[0].length;j++) {//create the whole body of the table now
/*0->0->0->0->0 [id's]
  1->1->1->1->1 [title/top rowl
  2->2->2->2->2 [data row 1]
  3->3->3->3->3 [data row 2]

  find the length of the array(0->1->2->3 = thearray[0].length)
  then find the width of the array(0->0->0->0->0 = thearray.length)*/
    var mycurrent_row=document.createElement("TR");
    mycurrent_row.setAttribute("id",'row_'+(j-offset));
    //and walk through the width/num of days wide the array is
    for(i=0;i<thearray.length;i++) {//walk through the array
      var mycurrent_cell=document.createElement("TD"); //create the cell
      if(thearray[i][1] == "Time"){//if the array is the time listing, then output that format
        mycurrent_cell.className=thetimeclass;
        mycurrent_cell.setAttribute("id",thearray[i][0]+'_'+(j-offset));
        var currenttext=document.createTextNode(thearray[i][j]);
      }else{ //otherwise check and see if the cell we're on is taken
        if(thearray[i][j] == "1"){//if the id is set to be taken
          var currenttext=document.createTextNode(" ");
          mycurrent_cell.setAttribute('title','Unavailable');
          mycurrent_cell.setAttribute("id",thearray[i][0]+'_'+(j-offset));
          mycurrent_cell.className=thetakenclass;
          mycurrent_cell.onclick = function(){
            alert("Time Unavaliable");
          };
          mycurrent_cell.onmouseover = function(){
            document.getElementById('time1_'+pullIndex(this.id)).className=thetimehighlight;
            document.getElementById('time2_'+pullIndex(this.id)).className=thetimehighlight;
          };
          mycurrent_cell.onmouseout = function(){
            document.getElementById('time1_'+pullIndex(this.id)).className=thetimeclass;
            document.getElementById('time2_'+pullIndex(this.id)).className=thetimeclass;
          };
        }else{ //else this time slot is open, and the corresponding functions can be created.
          var currenttext=document.createTextNode("_");
          mycurrent_cell.className=thefreeclass;
          //set the id to be the date in m-d-Y format and the spot in the array(minus the offset)
          mycurrent_cell.setAttribute("id",thearray[i][0]+'_'+(j-offset));
          mycurrent_cell.onclick = function(){
            window.open('index.php?pg=confirm&listingid='+listingid+'&officeid='+officeid+'&board='+theboard+'&fromsite='+thereferrer+'&date='+pullDate(this.id)+'&time='+indexToTime(this.id,8)+'&listingboard='+listing_board, 'confirmWindow','width=300px,height=200px,left=400,top=200');
          };
          //mycurrent_cell.ondblclick = function(){ alert(this.id);};
          mycurrent_cell.onmouseover = function(){
            //this.className = thefreeclassover;
            this.innerHTML = "Request";
            document.getElementById('time1_'+pullIndex(this.id)).className=thetimehighlight;
            document.getElementById('time2_'+pullIndex(this.id)).className=thetimehighlight;
          };
          mycurrent_cell.onmouseout = function(){
            //this.className = thefreeclass;
            this.innerHTML = "_";
            document.getElementById('time1_'+pullIndex(this.id)).className=thetimeclass;
            document.getElementById('time2_'+pullIndex(this.id)).className=thetimeclass;
          };
        }
      }
      mycurrent_cell.appendChild(currenttext);
      mycurrent_row.appendChild(mycurrent_cell);
    }
    mytablebody.appendChild(mycurrent_row);
  }
  mytable.appendChild(mytablebody);
  mybody.appendChild(mytable);
}

//takes in the value of the spot and uses that to find the time and then offsets that with the houroffset
function indexToTime(thevalue,houroffset){
  var index=thevalue.indexOf("_"); //find the break in the ID
  var avalue=thevalue.substring(index+1,thevalue.length); //grab the substring from that break
  var thehour=(avalue >= 4)?Math.floor(avalue / 4):0;//inialize thehour to 0 or the spot divided by 4 if the spot is at least 4
  var themin=(avalue % 4 != 0)?15*(avalue-(thehour*4)):'00'; //initialize themin to 0 or to the number of minutes found by multipling the difference of the spotvalue and thehour found by 15
  thehour+=houroffset; //kick the hour up the number of starthours
  return thehour+':'+themin+":00";
}

//grabs the index from the id
function pullIndex(thevalue){
  var index=thevalue.indexOf("_"); //find the break in the ID
  return thevalue.substring(index+1,thevalue.length); //grab the substring from that break
}

function pullDate(thevalue){
  var index=thevalue.indexOf("_"); //find the break in the ID
  return thedate=thevalue.substring(0,index); //grab the substring from that break
}

//takes a date in YYYY-mm-dd format and returns information in the format of Mon dd
function convertDate(thevalue){//2005-05-18
  //thevalue = '-'+thevalue+'-'; //add the needed dashes for the better output
  var index = [];  //initialize the array
  var spot1 = thevalue.indexOf("-"); //locate the first dash(-)
  var spot2 = thevalue.indexOf("-",spot1+1); //locate the second dash(-)
  var years=thevalue.substring(0,spot1);
  var months=thevalue.substring(spot1+1,spot2);
  var days=thevalue.substring(spot2+1,thevalue.length);
   //find the corresponding month
  switch(months){
    case "01": var month = "Jan"; break;
    case "02": var month = "Feb"; break;
    case "03": var month = "Mar"; break;
    case "04": var month = "Apr"; break;
    case "05": var month = "May"; break;
    case "06": var month = "Jun"; break;
    case "07": var month = "Jul"; break;
    case "08": var month = "Aug"; break;
    case "09": var month = "Sep"; break;
    case "10": var month = "Oct"; break;
    case "11": var month = "Nov"; break;
    case "12": var month = "Dec"; break;
  }
  return month+' '+days+', '+years; //get the day
}

//takes in the value of the time in HH:MM format and outputs the time in 12hr AM PM style
function convert24hrTo12hr(thevalue){
  var index=thevalue.indexOf(":"); //find the break in the time
  var thehour=parseInt(thevalue.substring(0,index)); //grab the substring of hours from that break
  var themin=thevalue.substring(index+1,thevalue.length); //grab the substring of minutes from that break
  var ampm=(thehour>11)?"PM":"AM"; //if the hour is noon or later, it's PM, otherwise, AM
  thehour-=(thehour>12)?12:0; //if the hour is past noon, drop it down
  return thehour+":"+themin+" "+ampm;//return the "HH:MM AM" format
}


/*
array goes by day
day has ((24x4)+2) = 98 items in the array
day has ((13x4)+2) = 54 items in the array

/-----------------------------------------------------\
| Welcome. Please select a time.                       |
| Property: [compileAddress(mls#)]                     |
| [<< Return to listings]

[_<<__][arry1][arry2][arry3][arry4][arry5][arry6][arry7][__>>_]
[times][day11][day12][day13][day14][day15][day16][day17][times]
[time1][_____][_____][_____][_____][_____][_____][_____][time1]
[time2][_____][_____][_____][_____][_____][_____][_____][time2]
[time3][_____][_____][_____][_____][_____][_____][_____][time3]
[time4][_____][_____][_____][_____][_____][_____][_____][time4]
[time5][_____][_____][_____][_____][_____][_____][_____][time5]
[time6][_____][_____][_____][_____][_____][_____][_____][time6]
[time7][_____][_____][_____][_____][_____][_____][_____][time7]
[time8][_____][_____][_____][_____][_____][_____][_____][time8]
[time9][_____][_____][_____][_____][_____][_____][_____][time9]

every cell has an id

*/

