// JavaScript Document	
function setEmailText(){
	var default_text = "Email Address";
		$('#UserUsername').val(default_text);
		$('#UserUsername').click(function(){
			if($(this).val() == default_text){
				$(this).val("") ;
			}
		});
		
		$('#UserUsername').blur(function(){
			if($(this).val() == ""){
				$(this).val(default_text);
			}
		});	
}

function setDealsLoader(){
		var reapeter = $('#deals_loader').html();
		for(var i=0; i<9; i++){			
			$('#deals_loader').append(reapeter);	
		}
}

function formValidatio(obj){
	
	var default_text = "Email Address";		
	var email_adder = $('#UserUsername').val();
	if(email_adder == default_text || email_adder == ""){
		alert('Please enter your email address');	
		return false;
	}else{
		if(!checkEmail(email_adder))
			return false;
	}
	if($('#UserCity option:selected').val() == ""){
		alert('Please choose your city');	
		return false;
	}
}

function checkEmail(emailAddr) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailAddr)){
		return true;
	}
	alert("Invalid E-mail Address! Please re-enter.")
	return false;
}
function showCity()
{

			//$(".dropdown dt a span").html(def_city);
            
                $(".dropdown dd ul").toggle();
          
                        
            $(".dropdown dd ul li a").click(function() {
                var text = $(this).html();
				if(def_city != text)
                	$(".dropdown dt a span").html(text);
                $(".dropdown dd ul").hide();
				//alert(getSelectedValue("city"));  
				window.location = Croogo.basePath + 'deals/changeCity/'+getSelectedValue("city");             
            });
                        
            function getSelectedValue(id) {
                return $("#" + id).find("dt a span.value").html();
            }

            $(document).bind('click', function(e) {
                var $clicked = $(e.target);
                if (! $clicked.parents().hasClass("dropdown"))
                    $(".dropdown dd ul").hide();
            });
       
var basepath = Croogo.basePath;
}
function checkTimeZone() {
   var rightNow = new Date();
   var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
   var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
   var temp = date1.toGMTString();
   var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ")));
   var temp = date2.toGMTString();
   var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ")));
   
   var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
   var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);

   return hoursDiffStdTime;
   
} 

function assignDigit(num){	
	//var first = parseInt(num/10);
	//var second = num%10;
	a = String(num);
	var first = a.substring(0,1);
	var second = a.substring(1,2);
	if(second == '') first ='0'+first;	
	return '<div class="digit">' + first + '</div><div class="digit">' + second + '</div>';
}


function countdowntimer(year, month, day, hour, minute, format, divid)
{
   			timerdivid = '#'+divid ;
     	 Today = new Date();
         Todays_Year = Today.getFullYear() ;
         Todays_Month = Today.getMonth() + 1;   //why add 1?


         //Convert both today's date and the target date into miliseconds.
         Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(),
                                 Today.getHours(), Today.getMinutes(), Today.getSeconds() )).getTime();

	    var second = 0;

         Target_Date = (new Date(year, month, day, hour, minute, second)).getTime();  //00 is seconds



		
	   today_month = Todays_Month;
	   today_day = Today.getDate();
	   today_hour = Today.getHours();
	   today_min = Today.getMinutes();
	   today_sec = Today.getSeconds();		


	 

         //Find their difference, and convert that into seconds.
         Time_Left = Math.round((Target_Date - Todays_Date) / 1000);


	


	
	 
	  // if is -8 for PST, -6 for Memphis

     // Time_Left = Time_Left + (utc_offset+10) * 3600;   // jimmm added 5/4/2011  --  PST is (-8) + 8 = 0




	  var utc_offset = checkTimeZone();   // if is -8 for PST, -6 for Memphis

        Time_Left = Time_Left + ( utc_offset+8) * 3600;   // jimmm added 5/4/2011  --  PST is (-8) + 8 = 0




		
         if(Time_Left < 0)
            Time_Left = 0;
		
        
          

	//format is ALWAYS 1
 				//alert("case 1");

                    //More datailed.
                    days = Math.floor(Time_Left / (60 * 60 * 24));
					//days=Math.abs(days);
                    Time_Left %= (60 * 60 * 24);
                    hours = Math.floor(Time_Left / (60 * 60));
					//hours=Math.abs(hours);
                    Time_Left %= (60 * 60);
                    minutes = Math.floor(Time_Left / 60);
					//minutes=Math.abs(minutes);
                    Time_Left %= 60;
                    seconds = Time_Left;
					//seconds=Math.abs(seconds);

	

		
	
			$(timerdivid+' .deal_end_day').html(assignDigit(days));
			$(timerdivid+' .deal_end_hour').html(assignDigit(hours));
			$(timerdivid+' .deal_end_min').html(assignDigit(minutes));  
		 	$(timerdivid+' .deal_end_sec').html(assignDigit(seconds));  	
		
     		
         

       
	   
	   str = "countdowntimer(" + year + ',' + month +',' + day +',' + hour +',' + minute +',' + format +',"' + divid+ '")';

		//alert(str);
         setTimeout(str,1000);
         //setTimeout('countdown(2010, 3, 23, 1, 8, 1)', 1000);


}		
