formInfo = "";

function onClickCancel() {
   var p_pup = document.getElementById('pup');
   while(p_pup.childNodes.length){
       	p_pup.removeChild(p_pup.firstChild);
   }
   location.reload(true);
}

function onClickSave() {
    submitButton = document.getElementById('btnSave');
    submitButton.disabled=true; 
    url = document.getElementById('url').value;
    what = document.getElementById('what').value;
    where = document.getElementById('where').value;
    when = document.getElementById('when').value;
    appDate = document.getElementById('appDate').value;
    v_type = document.getElementById('v_type').value; 
    offset = document.getElementById('offset').value;
    Ourl = url+'?what='+what+'&where='+where+'&when='+when+'&appDate='+appDate+'&v_type='+v_type+'&offset='+offset;
    document.getElementById('QuickAddForm').action=Ourl;
    document.getElementById('QuickAddForm').submit();    
}

function showPopup(when,d_date,offset) {
 if(d_date.split('-')[2]=='00' || d_date.split('/')[2]=='00'){
  return false;
 }
 else {  
  url1 = document.getElementById('url1').value;
  passurl = url1+'?dateval='+d_date+'&timeval='+when
  v_type = document.getElementById('v_type').value;


  if(when=='-1') {
        msg = "Quick Add ( "+d_date+" )"; 
  }
  else {
        time = convert(when);  
	msg = "Quick Add ( "+d_date+" "+time+" )";
  }

  var p_pup = document.getElementById('pup');
  while(p_pup.childNodes.length){
       	p_pup.removeChild(p_pup.firstChild);
  }
 
  var theNewParagraph = document.createElement('div');
  
  var theNewDiv = document.createElement('div');
  theNewDiv.setAttribute('id','formDiv');
  
  var browser=navigator.appName;
  
  if(browser!='Microsoft Internet Explorer'){
     theNewDiv.setAttribute('style','background-color: #C1E0C5; border: 1px solid #A1BBA4 padding: 6px 6px 2px 6px');
  }
  else {
     theNewDiv.style.setAttribute('cssText','background-color: #C1E0C5; border: 1px solid #A1BBA4 padding: 6px 6px 2px 6px'); 
  }

  var theForm = document.createElement('form'); 
  theForm.setAttribute('id','QuickAddForm');
  theForm.setAttribute('name','QuickAddForm');
  theForm.setAttribute('method','post');
  
  theTextTitle=document.createTextNode(msg);
  theTextWhat=document.createTextNode("What");
  theTextWhere=document.createTextNode("Where");

  theWhat=document.createElement("input");
  theWhat.setAttribute('type','text');
  theWhat.setAttribute('id','what');
  theWhat.setAttribute('name','what');

  theWhere=document.createElement("input");
  theWhere.setAttribute('type','text');
  theWhere.setAttribute('id','where');
  theWhere.setAttribute('name','where');

  theWhen=document.createElement("input");
  theWhen.setAttribute('type','hidden');
  theWhen.setAttribute('id','when');
  theWhen.setAttribute('name','when');
  theWhen.value=when

  theDate=document.createElement("input");
  theDate.setAttribute('type','hidden');
  theDate.setAttribute('id','appDate');
  theDate.setAttribute('name','appDate');
  theDate.value=d_date;

  theType=document.createElement("input");
  theType.setAttribute('type','hidden');
  theType.setAttribute('id','v_type');
  theType.setAttribute('name','v_type');
  theType.value=v_type;

  theOffset=document.createElement("input");
  theOffset.setAttribute('type','hidden');
  theOffset.setAttribute('id','offset');
  theOffset.setAttribute('name','offset');
  theOffset.value=offset; 

  theButtonSubmit=document.createElement("button");
  theButtonSubmit.setAttribute('id','btnSave');
  theButtonSubmit.setAttribute('name','btnSave');
  theButtonSubmit.style.height=20;
  theButtonSubmit.style.width=75;
  theTextSave=document.createTextNode("Create");
  theButtonSubmit.onclick=onClickSave;
  theButtonSubmit.appendChild(theTextSave);

  theButtonCancel=document.createElement("button");
  theButtonCancel.setAttribute('id','btnCancel');
  theButtonCancel.setAttribute('name','btnCancel');
  theButtonCancel.style.height=20;
  theButtonCancel.style.width=75;
  theTextCancel=document.createTextNode("Cancel");
  theButtonCancel.onclick=onClickCancel;
  theButtonCancel.appendChild(theTextCancel);

  newlink=document.createElement('a');
  linktext=document.createTextNode("edit appointment");
  newlink.appendChild(linktext);
  newlink.setAttribute('href', 'javascript:void(0);');
  newlink.setAttribute('onclick', 'javascript:makeurl(passurl);');
   
  tbl=document.createElement("table");
  tblbody=document.createElement("tbody");
  row1=document.createElement("tr");
  cell11=document.createElement("th");
  cell11.setAttribute('colspan',4);
  cell11.setAttribute('align','left');
  cell11.appendChild(theTextTitle);
  row1.appendChild(cell11);
  row2=document.createElement("tr");
  cell21=document.createElement("td");
  cell21.appendChild(theTextWhat);
  cell22=document.createElement("td");
  cell22.appendChild(theWhat);
  cell23=document.createElement("td");
  cell23.appendChild(theTextWhere);
  cell24=document.createElement("td");
  cell24.appendChild(theWhere);
  row2.appendChild(cell21);
  row2.appendChild(cell22);
  row2.appendChild(cell23);
  row2.appendChild(cell24);
  row3=document.createElement("tr");
  cell31=document.createElement("td");
  cell31.appendChild(theButtonSubmit);
  cell32=document.createElement("td");
  cell32.appendChild(theButtonCancel);
  cell33=document.createElement("td");
  cell33.appendChild(newlink);
  row3.appendChild(cell31);
  row3.appendChild(cell32);
  row3.appendChild(cell33);
  tblbody.appendChild(row1);
  tblbody.appendChild(row2);
  tblbody.appendChild(row3); 
  tbl.appendChild(tblbody);
  theForm.appendChild(tbl);  
  theForm.appendChild(theWhen);
  theForm.appendChild(theDate);
  theForm.appendChild(theType); 
  theForm.appendChild(theOffset);

  theNewDiv.appendChild(theForm);

  document.getElementById('pup').appendChild(theNewDiv);
 } 
}


function makeurl(crearte_url) {
   what=document.getElementById('what').value;
   where=document.getElementById('where').value;
   redirect_to=passurl+'&what='+what+'&where='+where;
   window.location=redirect_to;
}

function convert(when) {
   if(when=='00') {
      return "12 am";
   }
   else if(when=='12') {
      return "12 pm";
   }
   else if(parseInt(when)>12) {
      return (parseInt(when)-12)+" pm";
   }
   else {
      return when+" am";
   }     
}

