
var timerID = null;
var timerRunning = false;
var datum;

function stopclock (){
        if(timerRunning)
                clearTimeout(timerID);
        timerRunning = false;
}

function startclock (input,namn,orgdate) {
        // Make sure the clock is stopped
        stopclock();
        showtime(input,namn,orgdate);
}

function showtime (input,namn,orgdate) {
        var now = new Date();
        var hours = now.getHours();
        var minutes = now.getMinutes();
        var seconds = now.getSeconds();

        var date = now.getDate();
        var month = now.getMonth()+1;
        var year = now.getYear();


        if (year < 1000)
         year+=1900

        var timeValue = "" + ((hours < 10 ) ? "0" : "") + hours
        // var timeValue = "" + ((hours >12) ? hours -12 :hours)
        timeValue += ((minutes < 10) ? ":0" : ":") + minutes
        timeValue += ((seconds < 10) ? ":0" : ":") + seconds
        // timeValue += (hours >= 12) ? " P.M." : " A.M."
        
        if (hours == 0 && minutes == 0 && seconds == 0) {
        window.status = "Midnatt!";
        document.clock.face.value = "Midnatt!";
         }
       else 
   	 {        
       	if (month<10) 
        		month = "0" + month;
        	if (date<10) 
        		date = "0" + date;          		 
        document.clock.face.value = timeValue + "  "  + year + "-" + month + "-" + date;    
        window.status = timeValue + " Den exakta tiden enligt din dators klocka!     " + year + "-" + month + "-" + date; 
     	}
     	
     		msPerDay = 24 * 60 * 60 * 1000  //millisekunder per dag
     	
     		if (input!="")
     		{
     			mytime = new Date(input)   
     			daysLeft = (mytime.getTime() - now.getTime()) / msPerDay;
     			daysLeft = Math.round(daysLeft);
 				document.Frame1.MyTime.value = daysLeft + " dagar kvar till "  + namn + " (" + orgdate + ")";
 			}
 			
        nextXmas = new Date("December 24, 2000")
        nextXmas.setYear(year)
        daysLeft = (nextXmas.getTime() - now.getTime()) / msPerDay;
        daysLeft = Math.round(daysLeft);
        document.Frame2.Xmas.value =  daysLeft + " dagar kvar till julafton!";
        
        nextXmas = new Date("December 31, 2000")
        nextXmas.setYear(year)
        daysLeft = (nextXmas.getTime() - now.getTime()) / msPerDay;
        daysLeft = Math.round(daysLeft);
        document.Frame3.NewYear.value = daysLeft + " dagar kvar till nyårsafton!";
        datum = input;
        timerID = setTimeout("showtime('" + input + "','" + namn +"','" + orgdate + "')",1000);

        timerRunning = true;
}

	function countdown(obj)
	{
		this.obj		= obj;
		this.TargetDate		= datum + " 0:00 AM";
		this.DisplayFormat	= "%%D%% Days, %%H%% Hours, %%M%% Minutes, %%S%% Seconds.";
		this.CountActive	= true;
		this.DisplayStr;

		this.Calcage		= cd_Calcage;
		this.CountBack		= cd_CountBack;
		this.Setup		= cd_Setup;
	}

	function cd_Calcage(secs, num1, num2)
	{
  		s = ((Math.floor(secs/num1))%num2).toString();
  		if (s.length < 2) s = "0" + s;
  		return (s);
	}
	function cd_CountBack(secs)
	{
  		this.DisplayStr = this.DisplayFormat.replace(/%%D%%/g,	this.Calcage(secs,86400,100000));
  		this.DisplayStr = this.DisplayStr.replace(/%%H%%/g,		this.Calcage(secs,3600,24));
  		this.DisplayStr = this.DisplayStr.replace(/%%M%%/g,		this.Calcage(secs,60,60));
  		this.DisplayStr = this.DisplayStr.replace(/%%S%%/g,		this.Calcage(secs,1,60));
  		
  		document.getElementById(this.Div).innerHTML= this.DisplayStr;
  		if (this.CountActive) setTimeout(this.obj +".CountBack(" + (secs-1) + ")", 990);
	}
	function cd_Setup()
	{
		var dthen	= new Date(this.TargetDate);
  		var dnow	= new Date();  
		ddiff		= new Date(dthen-dnow);
		gsecs		= Math.floor(ddiff.valueOf()/1000);
		this.CountBack	(gsecs);
	}


// Check for valid date :
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= " ";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this;
}

function isDate(dtStr){

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strYear=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(pos2+1)
	strYr=strYear
	
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Formatet på datum : ååååmmdd (år månad dag)");
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Månaden verkar inte stämma.");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Dagen verkar inte stämma.");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Året måste vara 4 siffror långt och mellan "+minYear+" och "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Datumet är måste vara i formatet : ååååmmdd (år månad dag)");
		return false;
	}
	// is date in future ?
	isnow = new Date();
	var ndate = isnow.getDate();
   var nmonth = isnow.getMonth()+1;
   var nyear = isnow.getYear();
   if (nyear < 1000)
   {
         nyear+=1900;			
    }    

   var nowDate = new Date(nyear,nmonth,ndate);		         
	var thenDateIs = new Date(strYear,strMonth,strDay);		

	if (nowDate>=thenDateIs)
	{
		alert('Datumet måste ligga framåt i tiden');
		return false;
	}
	else
	{	
		document.newDate.date.value = strMonth + "/" + strDay + "/" + strYear;
		return true;
	}
}

function checkDate(newDate){
	var dt=document.newDate.date
	var isOk = isDate(newDate);

	if (!isOk)
	{
		dt.focus();
		dt.value = "";
		return false;
	}
	else 
    	return true;
 }





