// JavaScript Document
/**
 * @author srikrishna
 */
var cookieVisitID 	= 'my_LastVisit';	
	var cookieNumVisitID 	= 'my_NumVisits';
	var gLastVisit;
	var gNumVisits;

	SetLastVisit();	// Execute when loading page
	function GetLastVisit ()
	{
		if ( gLastVisit == "")
		{
			//return "Welcome to Cookie Central's Javascript and Cookie Example!";
		}
		else 
		{
			var oldVisitDate = new Date(gLastVisit);
			/*return 	"You have been here " 
					+ gNumVisits + " time" +(gNumVisits>1 ? "s" :"") 
					+" before.";*/
					if(gNumVisits >= 5)		
					DeleteCookie (cookieNumVisitID);
					
					document.getElementById('banner').innerHTML = '<img src="images/banner'+gNumVisits+'.jpg" alt="" border="0">';
					return gNumVisits;
		}
	}


	function SetLastVisit (name, value) 
	{
		var newVisitDate = new Date();
		var expDate = new Date (); 
		var numVisits = 0;

			// The expDate is the date when the cookie should
			// expire, we will keep it for a year
		expDate.setTime( expDate.getTime() + (365 * 24 * 60 * 60 * 1000) ); 

			// Info about last visit
		if (GetCookie (cookieVisitID) != null)
			gLastVisit = GetCookie (cookieVisitID);
		else
			gLastVisit = "";

		if (GetCookie (cookieNumVisitID) != null)
			{gNumVisits = GetCookie (cookieNumVisitID);			
			}
				
		else
			gNumVisits = 0;

			// Use eval() to convert a string to a number
		numVisits = eval(gNumVisits) +1;	

			// Store info about this visit
		SetCookie( cookieVisitID, 	newVisitDate, expDate); 
		SetCookie( cookieNumVisitID, numVisits, expDate); 
	}

    function getCookieVal (offset) {
      var endstr = document.cookie.indexOf (";", offset);
      if (endstr == -1)
        endstr = document.cookie.length;
      return unescape(document.cookie.substring(offset, endstr));
    }

    //
    //  Function to return the value of the cookie specified by "name".
    //    name - String object containing the cookie name.
    //    returns - String object containing the cookie value, or null if
    //      the cookie does not exist.
    //
    function GetCookie (name) {
      var arg = name + "=";
      var alen = arg.length;
      var clen = document.cookie.length;	  
      var i = 0;
      while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
          {return getCookieVal (j); }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
      }
	   return null;
    }

     function SetCookie (name, value) {
      var argv = SetCookie.arguments;
      var argc = SetCookie.arguments.length;
      var expires = (argc > 2) ? argv[2] : null;
      var path = (argc > 3) ? argv[3] : null;
      var domain = (argc > 4) ? argv[4] : null;
      var secure = (argc > 5) ? argv[5] : false;
      document.cookie = name + "=" + escape (value) +
        ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
        ((path == null) ? "" : ("; path=" + path)) +
        ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
    }

    //  Function to delete a cookie. (Sets expiration date to current date/time)
    //    name - String object containing the cookie name
    //
    function DeleteCookie (name) {
      var exp = new Date();
      exp.setTime (exp.getTime() - 1);  // This cookie is history
      var cval = GetCookie (name);
      document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
    }