
var MIN_RED_AMT = 50;
// Functions for timer
function calcage(secs, num1, num2) {
	s = ((Math.floor(secs/num1))%num2).toString();
	if (LeadingZero && s.length < 2)
	s = "0" + s;
	return "" + s + "";
}

function CountBack(secs) {
	if (secs < 0) {
		document.getElementById("cntday").innerHTML = FinishMessage;
		return;
	}	
	//alert(datetime[0]);
	Displayday = calcage(secs,86400,100000);
	Displayhour =calcage(secs,3600,24);
	Displaymin =calcage(secs,60,60);
	Displaysec =calcage(secs,1,60);
	// DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));
	
	document.getElementById("cntday").innerHTML = Displayday > 1 ? Displayday + "&nbsp;&nbsp;&nbsp;Days" : Displayday + "&nbsp;&nbsp;&nbsp;Day";
	document.getElementById("cnthour").innerHTML = Displayhour > 1 ? (Displayhour > 9 ? Displayhour + " Hours" : Displayhour + "&nbsp;&nbsp;&nbsp;Hours") : Displayhour + "&nbsp;&nbsp;&nbsp;Hour";
	document.getElementById("cntmin").innerHTML = Displaymin > 1 ? (Displaymin > 9 ? Displaymin + " Minutes" : Displaymin + "&nbsp;&nbsp;&nbsp;Minutes") : Displaymin + "&nbsp;&nbsp;&nbsp;Minute";
	//document.getElementById("cntsec").innerHTML = Displaysec+" Secs";
	if (CountActive)
		setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}
function putspan(backcolor, forecolor) {
    document.getElementById('timer').innerHTML = "<div class='timeleft'><span id='cntday' style='background-color:" + backcolor + "; color:" +
    forecolor + "'></span><br><span id='cnthour' style='background-color:" + backcolor + "; color:" + forecolor + 
    "'></span><br><span id='cntmin' style='background-color:" + backcolor + "; color:" + forecolor + "'></span></div>";
}


// functions for comments
function showpost(commentdiv) {
	var comdiv ="";
	comdiv =   commentdiv;
	if(document.getElementById(comdiv).style.display!='block') {
		document.getElementById(comdiv).style.display = 'block';
		return false;
	}
}

function loginmsg(comdiv) {
	document.getElementById(comdiv).innerHTML = "<p>Please login to post a comment!</p>";
	if(document.getElementById(comdiv).style.display!='block') {
		document.getElementById(comdiv).style.display = 'block';
		return false;
	}
}

function checkLimit(limit, callback) {
	if(limit == 0) {
		alert("We are sorry. You cannot purchase anymore coupons for this deal now");
		return;
	} else {
		callback();
	}
}

// Validation for quantity in buy page
function updateTotal(limit) {
    if(limit == 0) {
            alert("We are sorry. You cannot purchase anymore coupons for this deal now");
            return;
    }

    if (limit < 0) {
            limit = 10;
    }
    var q = document.getElementById('quantity').value;

    var re = new RegExp('[1-9]([0-9]?)');
    if (!q.match(re)) {
            alert("We are sorry. You can purchase a maximum of " + limit + " coupons for this deal now");
            document.getElementById('quantity').value = '';
            document.getElementById('quantity').focus();
            document.getElementById('quantity').select();
            return;
    } else if(Number(q) > Number(limit)) {
            alert("We are sorry. You can purchase a maximum of " + limit + " coupons for this deal now");
            document.getElementById('quantity').value = '';
            document.getElementById('quantity').focus();
            document.getElementById('quantity').select();
            return;
    }
/*
	if(!q) {
		alert("Quantity Cannot be Empty");
	} else if(q != '[1-9]+') {
		alert("Invalid Quantity");
	}
*/
	
	var p = document.getElementById('coupon_price').innerHTML;
	var b = document.getElementById('ref_bal') ? document.getElementById('ref_bal').innerHTML : 0;
	var t = q * p;
	
	var r = b >= t ? t : b;
	
	if((t - r) >= MIN_RED_AMT || (t - r) == 0) {
		document.getElementById('redeem_balance').innerHTML = r;
		document.getElementById('hredeem_balance').value = r;
		document.getElementById('total').innerHTML = (t - r);
		document.getElementById('htotal').value = (t - r);
	} else {
		r = r - r%MIN_RED_AMT;
		document.getElementById('redeem_balance').innerHTML = r;
		document.getElementById('hredeem_balance').value = r;
		document.getElementById('total').innerHTML = (t - r);
		document.getElementById('htotal').value = (t - r);
	}
	
	if(t - r > 0) {
		document.getElementById('bcontinue').style.display = "block";
		document.getElementById('bbuy').style.display = "none";
	} else {
		document.getElementById('bcontinue').style.display = "none";
		document.getElementById('bbuy').style.display = "block";
	}
/*	document.getElementById('totalhidden').value = t; */
}

// For loading user details in the buy page 
function loaduserdetail() {
	if(document.getElementById('userdetail').style.display = "none") {
		document.getElementById('userdetail').style.display = "block";
	}
}

function buy() {
	var yes = confirm("Your Referral account balance will cover your purchase. Click OK to book your coupon");
	if(yes){
		document.getElementById('purchased').value = "true";
		document.forms.buyform.submit();
	} else {
		return false;
	}
	
}

// For loading different coupon listing tabs 
// TODO: This approach to be changed
function loadAv() {
	if( document.getElementById('tab1').style.display = "none") {
		document.getElementById('tab1').style.display = "block";
		document.getElementById('tab2').style.display = "none";
		document.getElementById('tab3').style.display = "none";
	}
}

function loadPe() {
	if( document.getElementById('tab2').style.display = "none") {
		document.getElementById('tab2').style.display = "block";
		document.getElementById('tab1').style.display = "none";
		document.getElementById('tab3').style.display = "none";
	}
}

function loadCa() {
	if( document.getElementById('tab3').style.display = "none") {
		document.getElementById('tab3').style.display = "block";
		document.getElementById('tab1').style.display = "none";
		document.getElementById('tab2').style.display = "none";
	}
}

Popup = {

		init : function () {
			$('a.action_print').bind('click', Popup.printIt);
		},

		printIt : function () {
		    // Open popup with print content
			var win = window.open( $(this).attr('href'), "printGrabbon", "status = 1, height = 360, width = 630, resizable = 0" );

			// Wait for 3 seconds before calling print, waiting for the page to load
	        jQuery(function($){
	            setTimeout(function(){
	            win.print();
	            }, 3000 );
	        });

			return false;
		}
	}


bitlyResponse = function(data) {
    var s = '';
    var first_result;
    var shorturl;
    try {
	    for (var r in data.results) {
	            first_result = data.results[r]; break;
	    }
	    
	    shorturl = first_result['shortUrl'];
    } catch (e) {
    	shorturl = "http://www.grabbon.com";
    }
    if(!shorturl) {
    	shorturl = "http://www.grabbon.com";
    }
    
    if(document.getElementById('twitlink')) {
    	document.getElementById('twitlink').href = document.getElementById('twitlink').href  + shorturl;
    }
    if(document.getElementById('twitlink1')) {
    	document.getElementById('twitlink1').href = document.getElementById('twitlink1').href  + shorturl;
    }
    if(document.getElementById('twitlink2')) {
    	document.getElementById('twitlink2').href = document.getElementById('twitlink2').href  + shorturl;
    }

    //alert(document.getElementById('twitlink').href);
}


function validate(email) {
	   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,6})$/;
	   var address = document.getElementById(email).value;
	   if(reg.test(address) == false) {
	      alert('Enter a valid Email Address');
	      document.getElementById(email).focus();
	      return false;
	   }
	}
