/** Aditera AB 2011-08-03
* - Samlar skript för hantering av tidtabell
* - displaytable hämtar information från tidtabell.php och byter css-mall
* - setdate anropas när kalender döljs i valCal-v2.1.js
* - printpage Skriv ut
* - tellfriend anropas från kod skapad i tidtabell.php
* - send mail anropas från det dolda formulär som ligger på sidan där denna implementeras och anropar i sin tur funktion i php för att skicka meddelandet.
* - CSS,Kalenderimplementation John Eriksson
* - PHP,JavaScript,AJAX Tommy Olofsson
* - Kalendern dev.base86
**/
var st=0;
var start="&start=0";
var charter="&charter=";
var date="&date=";
var todate="&todate=";
var seek="&seek=";
var useprint="";
var arr="";
var linje="";
var datum="";
var tid="";
var from="";
var bolag="";
var anmarkning="";
var name="";
var to="";
var info="";
var counter="&counter=10";
var tid="";
var till="";
var fran="";
var sortby="`date`,`time`";
var sorting="&sorting=asc";

var tempstart;
function getphp1(page,sendget,resultid,openin)
{
    var myurl=page+"?"+sendget+"&trand="+rand(1,1001);
    try{xmlHttp1=new XMLHttpRequest(); }
    catch (e)
    {try{xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");}
    catch (e)
    {try{xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");}
    catch (e)
    {alert("Ajax is not supported by your browser.");return false;}}}

    xmlHttp1.onreadystatechange=function()
    {
	    if(xmlHttp1.readyState==4)
	    {
		    if(openin=="div")
		    {
		    	document.getElementById(resultid).innerHTML="";
		    	document.getElementById(resultid).innerHTML=xmlHttp1.responseText;
		    }
		    else if(openin=="window")
		    {
		    	mywindow(xmlHttp1.responseText);
		    }
		    else
		    {
		    	return xmlHttp1.responseText;
		    }
	    }
    }
    xmlHttp1.open("GET", myurl, true);
    xmlHttp1.send(null);
}
function displaytable(startover)
{
    if(document.getElementById('inputDate1').value<document.getElementById('inputDate').value)
        document.getElementById('inputDate1').value=document.getElementById('inputDate').value
    document.getElementById('Printer').style.visibility="hidden";
    if(startover!=null)
	start="&start=0";
    if(document.getElementById('inputDate').value!="")
       date="&date="+document.getElementById('inputDate').value;
    if(document.getElementById('inputDate1').value!="")
       todate="&todate="+document.getElementById('inputDate1').value;
    var query=charter+date+seek+arr+start+todate+counter+"&sortby="+sortby;
                                       //&start='+start+'&counter=10
    getphp1('tidtabell.php','timetable='+query,'idarr','div');
    window.setTimeout("fillhidden();",1500);
    if(charter.indexOf("SK")!=-1)
        useprint="sk.css";
    else if(charter.indexOf("DC")!=-1)
        useprint="dc.css";
    else if(charter.indexOf("KF")!=-1)
        useprint="kf.css";
    else
        useprint="kaab.css";
    document.getElementById('changestyle').href=useprint;
}
function setdate()
{
    displaytable();
}
function fillhidden()
{
   //Visa alla rader
  if(document.getElementById('inputDate').value!="")
     date="&date="+document.getElementById('inputDate').value;
  if(document.getElementById('inputDate1').value!="")
     todate="&todate="+document.getElementById('inputDate1').value;
  var query=charter+date+seek+arr+"&start=0"+todate+"&counter=15500";
  getphp1('tidtabell.php','timetable='+query,'idarr2','div');
  //Slut visa alla rader
  document.getElementById('Printer').style.visibility="visible"; 
}
function printpage()
{
  document.getElementById('changestyle').href=useprint;
  window.setTimeout("reset('"+tempstart+"');",1500);
}
function reset(tempstart)
{
  window.print();
  start=tempstart;
  counter="&counter=10";
}
function tellfriend(datum1,tid1,till1,fran1,linje1,bolag1,anmarkning1)
{

    datum=datum1;
    tid=tid1;
    till=till1;
    fran=fran1;
    linje=linje1;
    bolag=bolag1;
    anmarkning=anmarkning1;

    //Visa div med formulär för utskick av mail
    document.getElementById('mailformSkicka').style.display="none";
    document.getElementById('mailformBg').style.display="block";

}
function validateEmail(adress)
{
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(adress);
}
function sendmail()
{
  var query="";
  name=document.tipsa.name.value;
  from=document.tipsa.from.value;
  to=document.tipsa.to.value;
  info=escape(document.tipsa.info.value);
  query+="&name="+name;
  query+="&from="+from;
  query+="&to="+to;
  query+="&info="+info;
  query+="&anmarkning="+anmarkning;
  query+="&linje="+linje;
  query+="&datum="+datum;
  query+="&tid="+tid;
  query+="&tellfriend=1";
  query+="&till="+till;
  query+="&fran="+fran;
  query+="&bolag="+bolag;

  if(validateEmail(from)&&validateEmail(to))
  {
    getphp('tidtabell.php','timetable=&start='+start+'&counter=10'+query,'idarr','div');
    document.getElementById('mailformSkicka').style.display="block";
    setTimeout("hideandreset()",3000);
  }
  else
  {
    alert("Ni måste fylla i E-postadresser.");
  }
}
function hideandreset()
{
    document.getElementById('mailformBg').style.display="none";
    document.getElementById('mailformSkicka').style.display="none";
    document.tipsa.name.value="";
    document.tipsa.from.value="";
    document.tipsa.to.value="";
    document.tipsa.info.value="";
}

$(window).load(function()
{

  window.addEvent('domready', function()
  {
      new vlaDatePicker('inputDate',{ separator: '-', style: 'apple_widget', startMonday: 'true', format: 'y/m/d'});
      new vlaDatePicker('inputDate1',{ separator: '-', style: 'apple_widget', startMonday: 'true', format: 'y/m/d'});
  });
   getphp1('mailform.html','','mailformBg','div');
   //getphp('tidtabellinc.html','','listcontrole','div');
   document.getElementById('mailformBg').style.display="none";
});
function preview()
{
  //Göm allt och visa bara tidtabellen
   var mywin=window.open("","","width=700,height:700,scrollbars=yes");
   var code=window.document.getElementById('idarr2').innerHTML;
   var addstr='<div id="rub"><h2>Tidtabell</h2></div><div id="BolagLogoKf"><img src="images/kf_logo.jpg"></div><div id="BolagLogoSk"><img src="images/sk_logo.gif"></div><div id="BolagLogoDc"><img src="images/dc_logo.jpg"></div><div id="BolagLogoKaab"><img src="images/kaab_logo.jpg"></div>';
   mywin.document.write('<html>\n<head>\n<LINK rel=stylesheet type=text/css href="style1.css">\n<LINK rel=stylesheet type=text/css href="kaab.css">\n</head>\n<body background="" style="background-color:#fff;" bgcolor="#ffffff">\n\n<a style="float:left;margin:10px;" onClick="setTimeout(function(){self.print();self.close();},5);">\n\t<img src="images/printer.jpg" alt="Printer" />\n</a>\n\n'+addstr+code+"</body>\n</html>");
   //<LINK rel=stylesheet type=text/css href="style.css">\n
   if(navigator.appName.indexOf("Microsoft")!=-1)
    mywin.location.reload();

   var useprint="kaab.css";
   //mywin.getElementById('changestyle').href=useprint;
   try{
   var obj=mywin.document.getElementById('changestyle');
   obj.setAttribute("media","screen");
   }catch(e){}
   /*
   var useprint="kaab.css";
   document.getElementById('changestyle').href=useprint;
   var obj=document.getElementById('changestyle');
   obj.setAttribute("media","screen");
  */


}




