var hex_chr = "0123456789abcdef";
function rhex(num)
{
  str = "";
  for(j = 0; j <= 3; j++)
    str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) + hex_chr.charAt((num >> (j * 8)) & 0x0F);
  return str;
}
function str2blks_MD5(str)
{
  nblk = ((str.length + 8) >> 6) + 1;
  blks = new Array(nblk * 16);
  for(i = 0; i < nblk * 16; i++) blks[i] = 0;
  for(i = 0; i < str.length; i++) blks[i >> 2] |= str.charCodeAt(i) << ((i % 4) * 8);
  blks[i >> 2] |= 0x80 << ((i % 4) * 8);
  blks[nblk * 16 - 2] = str.length * 8;
  return blks;
}
function add(x, y)
{
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}
function rol(num, cnt)
{
  return (num << cnt) | (num >>> (32 - cnt));
}
function cmn(q, a, b, x, s, t)
{
  return add(rol(add(add(a, q), add(x, t)), s), b);
}
function ff(a, b, c, d, x, s, t)
{
  return cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function gg(a, b, c, d, x, s, t)
{
  return cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function hh(a, b, c, d, x, s, t)
{
  return cmn(b ^ c ^ d, a, b, x, s, t);
}
function ii(a, b, c, d, x, s, t)
{
  return cmn(c ^ (b | (~d)), a, b, x, s, t);
}
function calcMD5(str)
{
  x = str2blks_MD5(str);
  a =  1732584193;
  b = -271733879;
  c = -1732584194;
  d =  271733878;

  for(i = 0; i < x.length; i += 16)
  {
    olda = a; oldb = b;
    oldc = c; oldd = d;
    a = ff(a, b, c, d, x[i+ 0], 7 , -680876936);
    d = ff(d, a, b, c, x[i+ 1], 12, -389564586);
    c = ff(c, d, a, b, x[i+ 2], 17,  606105819);
    b = ff(b, c, d, a, x[i+ 3], 22, -1044525330);
    a = ff(a, b, c, d, x[i+ 4], 7 , -176418897);
    d = ff(d, a, b, c, x[i+ 5], 12,  1200080426);
    c = ff(c, d, a, b, x[i+ 6], 17, -1473231341);
    b = ff(b, c, d, a, x[i+ 7], 22, -45705983);
    a = ff(a, b, c, d, x[i+ 8], 7 ,  1770035416);
    d = ff(d, a, b, c, x[i+ 9], 12, -1958414417);
    c = ff(c, d, a, b, x[i+10], 17, -42063);
    b = ff(b, c, d, a, x[i+11], 22, -1990404162);
    a = ff(a, b, c, d, x[i+12], 7 ,  1804603682);
    d = ff(d, a, b, c, x[i+13], 12, -40341101);
    c = ff(c, d, a, b, x[i+14], 17, -1502002290);
    b = ff(b, c, d, a, x[i+15], 22,  1236535329);    

    a = gg(a, b, c, d, x[i+ 1], 5 , -165796510);
    d = gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
    c = gg(c, d, a, b, x[i+11], 14,  643717713);
    b = gg(b, c, d, a, x[i+ 0], 20, -373897302);
    a = gg(a, b, c, d, x[i+ 5], 5 , -701558691);
    d = gg(d, a, b, c, x[i+10], 9 ,  38016083);
    c = gg(c, d, a, b, x[i+15], 14, -660478335);
    b = gg(b, c, d, a, x[i+ 4], 20, -405537848);
    a = gg(a, b, c, d, x[i+ 9], 5 ,  568446438);
    d = gg(d, a, b, c, x[i+14], 9 , -1019803690);
    c = gg(c, d, a, b, x[i+ 3], 14, -187363961);
    b = gg(b, c, d, a, x[i+ 8], 20,  1163531501);
    a = gg(a, b, c, d, x[i+13], 5 , -1444681467);
    d = gg(d, a, b, c, x[i+ 2], 9 , -51403784);
    c = gg(c, d, a, b, x[i+ 7], 14,  1735328473);
    b = gg(b, c, d, a, x[i+12], 20, -1926607734);
    
    a = hh(a, b, c, d, x[i+ 5], 4 , -378558);
    d = hh(d, a, b, c, x[i+ 8], 11, -2022574463);
    c = hh(c, d, a, b, x[i+11], 16,  1839030562);
    b = hh(b, c, d, a, x[i+14], 23, -35309556);
    a = hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
    d = hh(d, a, b, c, x[i+ 4], 11,  1272893353);
    c = hh(c, d, a, b, x[i+ 7], 16, -155497632);
    b = hh(b, c, d, a, x[i+10], 23, -1094730640);
    a = hh(a, b, c, d, x[i+13], 4 ,  681279174);
    d = hh(d, a, b, c, x[i+ 0], 11, -358537222);
    c = hh(c, d, a, b, x[i+ 3], 16, -722521979);
    b = hh(b, c, d, a, x[i+ 6], 23,  76029189);
    a = hh(a, b, c, d, x[i+ 9], 4 , -640364487);
    d = hh(d, a, b, c, x[i+12], 11, -421815835);
    c = hh(c, d, a, b, x[i+15], 16,  530742520);
    b = hh(b, c, d, a, x[i+ 2], 23, -995338651);

    a = ii(a, b, c, d, x[i+ 0], 6 , -198630844);
    d = ii(d, a, b, c, x[i+ 7], 10,  1126891415);
    c = ii(c, d, a, b, x[i+14], 15, -1416354905);
    b = ii(b, c, d, a, x[i+ 5], 21, -57434055);
    a = ii(a, b, c, d, x[i+12], 6 ,  1700485571);
    d = ii(d, a, b, c, x[i+ 3], 10, -1894986606);
    c = ii(c, d, a, b, x[i+10], 15, -1051523);
    b = ii(b, c, d, a, x[i+ 1], 21, -2054922799);
    a = ii(a, b, c, d, x[i+ 8], 6 ,  1873313359);
    d = ii(d, a, b, c, x[i+15], 10, -30611744);
    c = ii(c, d, a, b, x[i+ 6], 15, -1560198380);
    b = ii(b, c, d, a, x[i+13], 21,  1309151649);
    a = ii(a, b, c, d, x[i+ 4], 6 , -145523070);
    d = ii(d, a, b, c, x[i+11], 10, -1120210379);
    c = ii(c, d, a, b, x[i+ 2], 15,  718787259);
    b = ii(b, c, d, a, x[i+ 9], 21, -343485551);

    a = add(a, olda);
    b = add(b, oldb);
    c = add(c, oldc);
    d = add(d, oldd);
  }
  return rhex(a) + rhex(b) + rhex(c) + rhex(d);
}

//delay imposed between showing the confirmation that an action was requested and actually sending the request (AJAX)
//this delay will prevent the 'please hold' message from appearing and disappearing so fast that it looks like flicker?>
var flicker_delay = 300;
function showPotdOverlay(show) {
	if(!show) {
		document.getElementById('potd_overlay').style.display = 'none';
	}
	else {
		if (typeof window.opera == "undefined") {
			var height = DomUtils.getElementHeight(document.body);
			document.getElementById('potd_overlay').style.height = height + 'px';
			document.getElementById('potd_overlay').style.display = 'block';
		}
	}
}

function showPotdBubble() {
}

function getCaptcha(){
    ajax = new AjaxRequest('POST', '/login.php/captcha', true, 3000);
    ajax.setCallback( potdCaptchaCallback );
    ajax.setParameter('type', 'captcha');
    ajax.send();
}

function potdCaptchaCallback(XmlResp){
        if(!XmlResp) {
           return false;
        }
        else {
           var msgnb = XmlResp.getElementsByTagName("nb")[0].firstChild.nodeValue;
           var msgdesc = XmlResp.getElementsByTagName("desc")[0].firstChild.nodeValue;
           var act = XmlResp.getElementsByTagName("action")[0].firstChild.nodeValue;
           if( act == 'captcha'){
                var captcha_encrypt = XmlResp.getElementsByTagName("encrypt")[0].firstChild.nodeValue;
                var captcha_iv = XmlResp.getElementsByTagName("iv")[0].firstChild.nodeValue;
                var captcha_raw_encrypt = XmlResp.getElementsByTagName("raw_encrypt")[0].firstChild.nodeValue;
                var captcha_raw_iv = XmlResp.getElementsByTagName("raw_iv")[0].firstChild.nodeValue;

                if(document.getElementById('captcha_image')){
                   document.getElementById('captcha_image').src = 'http://www.pricegrabber.com/cptch_img.php?s='+captcha_raw_encrypt+'&iv='+captcha_raw_iv;
                }
                if(document.getElementById('captcha_value')){
                   document.getElementById('captcha_value').value = captcha_encrypt;
                }
                if(document.getElementById('captcha_iv')){
                   document.getElementById('captcha_iv').value = captcha_iv;
                }
           }
        }
}


function showPotdOverlayBubble(show,type) {
	showPotdOverlay(show);
	if(!show && !potdBubbleIsClosed()) {
		document.getElementById('potdoverlaybubble').style.left = '-1000px';
                if(document.getElementById('captcha_image')) document.getElementById('captcha_image').src = 'http://i.pgcdn.com/images/spacer.gif';
	}
	else if(show){

                      getCaptcha();
           
		if(document.getElementById('potdbubblemsg')) {
			document.getElementById('potdbubblemsg').style.display = (type=='msg' ? '' : 'none'); 
        }
		if(document.getElementById('potdbubbleemail')) {
			document.getElementById('potdbubbleemail').style.display = (type=='email' ? '' : 'none'); 
        }
		if(document.getElementById('potdbubblezip')) {
			document.getElementById('potdbubblezip').style.display = (type=='enterzip' ? '' : 'none'); 
        }
		if(document.getElementById('price_history_bubble')) {
			document.getElementById('price_history_bubble').style.display = (type=='price_history_bubble' ? '' : 'none'); 
        }

		if(document.getElementById('potdbubblesubscribe')) {
            document.getElementById('potdbubblesubscribe').style.display = (type=='subscribe'? '' : 'none'); 
            zip_code = readCookie('zip_code');
            document.getElementById('ui_zipcode').value = (zip_code == null ? "" : zip_code);
        }
		var width = DomUtils.getWindowWidth();
		var height = DomUtils.getWindowHeight();
		var scrollX = DomUtils.getWindowScrollX();
		var scrollY = DomUtils.getWindowScrollY();
		if(width==0 || height==0 || typeof window.opera!="undefined") {
			if(typeof window.opera=="undefined" && document.documentElement.clientWidth) {
				width = document.documentElement.clientWidth;
				height = document.documentElement.clientHeight;
			}
			else {
				width = document.body.clientWidth;
				height = document.body.clientHeight;
			}
		}
		if(scrollX==0 || scrollY==0) {
			scrollX = document.documentElement.scrollLeft;
			scrollY = document.documentElement.scrollTop;
		}
		var bwidth = DomUtils.getElementWidth(document.getElementById('potdoverlaybubble'));
		var bheight = DomUtils.getElementHeight(document.getElementById('potdoverlaybubble'));
		
		if (type == "price_history_bubble") {
			bwidth = DomUtils.getElementWidth(document.getElementById('price_history_bubble'));
			bheight = DomUtils.getElementHeight(document.getElementById('price_history_bubble'));
		}
		document.getElementById('potdoverlaybubble').style.top = (scrollY + (height/2) - (bheight/2)) + 'px';
		document.getElementById('potdoverlaybubble').style.left = (scrollX + (width/2) - (bwidth/2)) + 'px';
	}
}

function potdBubbleClose() {
	showPotdOverlayBubble(0,'');
}
function potdBubbleIsClosed() {
	return parseInt(document.getElementById('potdoverlaybubble').style.left)>0 ? false : true;
}

function submitPotdOverlay(type) {
    if( type == 'subscribe') {
        potdBubbleClose();
        ShowPotdMessage('<img src="http://i.pgcdn.com/images/small_wait.gif" width=16 height=16>','Please hold while we process your request ...','#666666');
        var potd_alerts = new Array();
        for(var i=0; i<document.potd.elements.length; i++) {
            var cur_elem = document.potd.elements[i];
                            
            if(cur_elem.type == "checkbox" && cur_elem.name== "potd_alerts" && cur_elem.checked) {
                potd_alerts.push(cur_elem.value);
            }
        }
        ajax = new AjaxRequest('POST', '/deal_of_the_day.php/subscriptions', true, 3000);
        ajax.setParameter('potd_alerts', potd_alerts);
        ajax.setParameter('type', 'potd_alerts_submit');
        ajax.setCallback( callbackPotd );
        setTimeout('ajax.send();',flicker_delay);
    }
    else if( type == 'email' ) {
        var errors = 0;
        var error_msg = '';
        if( document.potd.sender_name.value=='' ) {
            errors = 1;
            error_msg = 'Please supply a name.';
        }
        else if( !isValidEmail(document.potd.friend_e.value) ) {
            errors = 1;
            error_msg = 'Invalid email address.';
        }
        
        if( errors == 1 ) {
            document.getElementById("potdbubble_email_error").innerHTML = error_msg;
        }
        
        else if( errors == 0 ) {
            potdBubbleClose();
            ShowPotdMessage('<img src="http://i.pgcdn.com/images/small_wait.gif" width=16 height=16>','Please hold while we process your request ...','#666666');
            ajax = new AjaxRequest('GET', '/deal_of_the_day.php/email', true, 3000);
            ajax.setParameter('sender_name', document.potd.sender_name.value);
            ajax.setParameter('friend_email', document.potd.friend_e.value);
            ajax.setParameter('captcha_value', document.potd.captcha_value.value);
            ajax.setParameter('captcha_iv', document.potd.captcha_iv.value);
            ajax.setParameter('captcha_response', document.potd.captcha_response.value);
            ajax.setParameter('email_msg', document.potd.email_msg.value);
            ajax.setParameter('type', 'potd_email');
            ajax.setCallback( callbackPotd );
            ajax.send();
        }
    }
    else if (type == 'enterzip') {
      ui_zipcode = document.potd.ui_zipcode.value;

      if ((zipcode = ui_zipcode.match(/^[0-9]{5}$/)) == null) {
        error_msg = 'Please enter a valid ZIP code.';
        document.getElementById("potdbubble_enterzip_error").innerHTML = error_msg;
      }
      else {
        var expires;
        var days = 1;
        var date = new Date();

        potdBubbleClose();

        date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
        expires = "; expires=" + date.toGMTString();

        document.cookie = 'zip_code=' + ui_zipcode + '; domain=.pricegrabber.com; path=/' + expires;
        document.location.reload();
      }
    }
}

function ShowPotdMessage(img,txt,color,bold) {
    if (bold != "undefined") document.getElementById('potdbubblemsgbody').style.fontWeight = 'bold';
    
	document.getElementById('potdbubblemsgbody').style.color = color;
	document.getElementById('potdbubblemsgbody').innerHTML = txt;
	if(img == '') {
		document.getElementById('potdbubblemsgimage').style.display = 'none';
	}
	else {
		document.getElementById('potdbubblemsgimage').innerHTML = img;
		document.getElementById('potdbubblemsgimage').style.display = '';
	}
	showPotdOverlayBubble(1,'msg');
}

function callbackPotd(XmlResp) {
	if(!XmlResp) {
		ShowLoginMessage('','Error while processing your request, please try again','#CC3232');
	}
	else {
		var error = XmlResp.getElementsByTagName('error');
		var msgnb = XmlResp.getElementsByTagName("nb")[0].firstChild.nodeValue;
		var msgdesc = XmlResp.getElementsByTagName("desc")[0].firstChild.nodeValue;
		var act = XmlResp.getElementsByTagName("action")[0].firstChild.nodeValue;
		if(error.length > 0) {
			ShowLoginMessage('',msgdesc,'#CC3232');
		}
		else {
			if( act=='subscribe_login' ) {
				var loggedin = XmlResp.getElementsByTagName("logged-in")[0].firstChild.nodeValue;
                var loginSuccess = XmlResp.getElementsByTagName("login_success")[0].firstChild.nodeValue;
				if(loggedin < 1) {
                    callbackfuncafterlogin = "getUserSubscriptions();showPotdOverlayBubble(1, 'subscribe');"; 
                    iframe_login('login_registration', callbackfuncafterlogin);
				}
				else {
                    getUserSubscriptions();
                    showPotdOverlayBubble(1, 'subscribe');
				}
			}
            else if( act == 'email_login' ) {
				var loggedin = XmlResp.getElementsByTagName("logged-in")[0].firstChild.nodeValue;
                if(loggedin > 0) {
                    var username = XmlResp.getElementsByTagName("username")[0].firstChild.nodeValue;
                    var email = XmlResp.getElementsByTagName("email")[0].firstChild.nodeValue;
                    if( username && email ) {
                        var input_elem = document.getElementById("sender_name");
                        if(input_elem) {
                            input_elem.value = username + " <"+email+">";
                        }
                    }
                }
                showPotdOverlayBubble(1, 'email');
            }
            else if( act == 'potd_email' ) {
				var msg_sent = XmlResp.getElementsByTagName("msg_sent")[0].firstChild.nodeValue;
                
                if( msg_sent > 0 ) {
			        ShowPotdMessage('',msgdesc,'#62A73B','bold');
                    setTimeout('potdBubbleClose();', 2000);
                }
                else {
                    var email_error = document.getElementById("potdbubble_email_error");
                    if(email_error) {
                        email_error.innerHTML = msgdesc;
                    }
                    showPotdOverlayBubble(1, 'email');
                }

            }
            else if( act == 'potd_alerts' ) {
				var loggedin = XmlResp.getElementsByTagName("logged-in")[0].firstChild.nodeValue;
                var loginSuccess = XmlResp.getElementsByTagName("login_success")[0].firstChild.nodeValue;
				if(loggedin < 1) {
                    callbackfuncafterlogin = "getUserSubscriptions();showPotdOverlayBubble(1, 'subscribe');"; 
                    iframe_login('login_registration', callbackfuncafterlogin);
				}
				else {
                    var alert_cnt = XmlResp.getElementsByTagName("potd_alert_count")[0].firstChild.nodeValue;
                    var i = 0;
                    var potd_alerts = new Array();
                    while(i < alert_cnt) {
                        var child_len = XmlResp.getElementsByTagName("topcat_"+i)[0].childNodes.length;

                        for(var j=0; j<child_len; j++) {
                            var node_name = XmlResp.getElementsByTagName("topcat_"+i)[0].childNodes[j].nodeName;
                            if( node_name == "some_id") {
                                var topcat_id = XmlResp.getElementsByTagName("topcat_"+i)[0].childNodes[j].firstChild.nodeValue;
                            }
                            else if( node_name == "cat_name") {
                                var topcat_name = XmlResp.getElementsByTagName("topcat_"+i)[0].childNodes[j].firstChild.nodeValue;
                            }
                            else if( node_name == "checked") {
                                var topcat_checked = XmlResp.getElementsByTagName("topcat_"+i)[0].childNodes[j].firstChild.nodeValue;
                            }
                        }
                        var topcat = new Array();
                        topcat[0] = topcat_id;
                        topcat[1] = topcat_name;
                        topcat[2] = topcat_checked;
                        
                        potd_alerts[topcat_id] = topcat;
                        i++;
                    }   //End while
                    
                    if(potd_alerts.length) {
                        for(var i=0; i<document.potd.elements.length; i++) {
                            var cur_elem = document.potd.elements[i];
                            
                            if(cur_elem.type == "checkbox" && cur_elem.name== "potd_alerts" ) {
                                var checkbox_id = cur_elem.value;
                                cur_elem.checked = false;
                                if( potd_alerts[checkbox_id][2]==1 ) {
                                    cur_elem.checked = true;
                                }
                            }
                        }
                    }
				}   //End else
            }
            else if (act == 'email_subscribe') {
                var msgsent = XmlResp.getElementsByTagName("msg_sent")[0].firstChild.nodeValue;
                ShowPotdMessage('',msgsent,'#62A73B','bold');
                setTimeout('potdBubbleClose();', 4000);
            }
            else if(act == 'potd_alerts_submit') {
			    ShowPotdMessage('',msgdesc,'#62A73B','bold');
                setTimeout('potdBubbleClose();', 3000);
            }
		}
	}
}

function cleanField(startvalue) {
    var field = document.getElementById("potd_e_subscr");
    if (field.value == 'Email Address') {
        field.value = '';
        field.style.color = '#000000';
    }
    return false;
}

function populateField() {
    var field = document.getElementById("potd_e_subscr");
	if (field){
	    field.value = 'Email Address';
	}
    return false;
}

function getUserSubscriptions() {
    ajax = new AjaxRequest('POST', '/deal_of_the_day.php/subscriptions', true, 3000);
    ajax.setCallback( callbackPotd );
    ajax.setParameter('type', 'potd_alerts');
    setTimeout('ajax.send();',flicker_delay);

}

function potdSubscribeAll() {
    for(var i=0; i<document.potd.elements.length; i++) {
        var cur_elem = document.potd.elements[i];
                            
        if(cur_elem.type == "checkbox" && cur_elem.name== "potd_alerts" ) {
            cur_elem.checked = true;
        }
    }
}

function potdUnsubscribeAll() {
    for(var i=0; i<document.potd.elements.length; i++) {
        var cur_elem = document.potd.elements[i];
                            
        if(cur_elem.type == "checkbox" && cur_elem.name== "potd_alerts" ) {
            cur_elem.checked = false;
        }
    }
}

function isValidEmail(emailStr) {
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    var quotedUser="(\"[^\"]*\")";
    var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom=validChars + '+';
    var word="(" + atom + "|" + quotedUser + ")";
    var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
    var matchArray=emailStr.match(emailPat);
    if (matchArray==null) {
        return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];
    if (user.match(userPat)==null) {
        return false;
    }
    var IPArray=domain.match(ipDomainPat);
    if (IPArray!=null) {
        for (var i=1;i<=4;i++) {
            if (IPArray[i]>255) {
                return false;
            }
        }
        return true;
    }
    var domainArray=domain.match(domainPat);
    if (domainArray==null) {
        return false;
    }
    var atomPat=new RegExp(atom,"g");
    var domArr=domain.match(atomPat);
    var len=domArr.length;
    if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>4) {
        return false;
    }
    if (len<2) {
        return false;
    }
    return true;
}

function potdTellAFriend() {
    var username = util.readCookie('bake_username');
    ajax = new AjaxRequest('GET', '/login.php/check', true, 3000);
    ajax.setCallback( callbackPotd );
    ajax.setParameter('type', 'email_login');
    if(username){
       var challenge = calcMD5(username) + '046366ef738d986a7d69e4c3d0f40fc9c3';
       ajax.setParameter('challenge', challenge);
    }
    ajax.send();
}

function potdSubscribe() {
    var cookieEnabled = navigator.cookieEnabled;

    if (!cookieEnabled) {
        var alertCookie = "Please enable cookies in the browser first. Thanks!";
        ShowPotdMessage('',alertCookie,'#FF0033','bold');
        setTimeout('potdBubbleClose();', 2000);
      return;
    }
    var username = util.readCookie('bake_username');
    ajax = new AjaxRequest('POST', '/login.php/check', true, 3000);
    ajax.setCallback( callbackPotd );
    ajax.setParameter('type', 'subscribe_login');
    if(username){
       var challenge = calcMD5(username) + '046366ef738d986a7d69e4c3d0f40fc9c3';
       ajax.setParameter('challenge', challenge);
    }
    ajax.send();
}

function potdEmailSubscribe() {
    var cookieEnabled = navigator.cookieEnabled;
    
    //We need cookies enabled
    if (!cookieEnabled) {
        var alertCookie = "Please enable cookies in the browser first. Thanks!";
        ShowPotdMessage('',alertCookie,'#FF0033','bold');
        setTimeout('potdBubbleClose();', 2000);
        return;
    }
    var email_addr = document.getElementById("potd_e_subscr");
    
    //Check email validity
    if( !isValidEmail(email_addr.value) ) {
        var error_msg = 'Invalid email address.';
        ShowPotdMessage('',error_msg,'#FF0033','bold');
        setTimeout('potdBubbleClose();', 2000);
        email_addr.focus();
        return;
    }
    
    //Valid Email address provided
    ShowPotdMessage('<img src="http://i.pgcdn.com/images/small_wait.gif" width=16 height=16>','Please hold while we process your request ...','#666666','');
    ajax = new AjaxRequest('POST', '/deal_of_the_day.php/emailsubscription', true, 3000);
    ajax.setCallback( callbackPotd );
    ajax.setParameter('type', 'email_subscribe');
    ajax.setParameter('email_addr', email_addr.value);
    ajax.send();
}

function potdPurchaseNow() {
    var cookieEnabled = navigator.cookieEnabled;

    if (!cookieEnabled) {
        var alertCookie = "Please enable cookies in the browser first. Thanks!";
        ShowPotdMessage('',alertCookie,'#FF0033','bold');
        setTimeout('potdBubbleClose();', 2000);
      return;
    }
    var username = util.readCookie('bake_username');
    ajax = new AjaxRequest('POST', '/login.php/check', true, 3000);
    ajax.setCallback( callbackPotd );
    ajax.setParameter('type', 'purchase_login');
    if(username){
       var challenge = calcMD5(username) + '046366ef738d986a7d69e4c3d0f40fc9c3';
       ajax.setParameter('challenge', challenge);
    }
    ajax.send();
}

function potdQueryPrice() {
    var username = util.readCookie('bake_username');
    ajax = new AjaxRequest('POST', '/login.php/check', true, 3000);
    ajax.setCallback( callbackPotd );
    ajax.setParameter('type', 'pricequery_login');
    if(username){
       var challenge = calcMD5(username) + '046366ef738d986a7d69e4c3d0f40fc9c3';
       ajax.setParameter('challenge', challenge);
    }
    ajax.send();
}

function potdSetZipCode() {
    var cookieEnabled = navigator.cookieEnabled;

    if (!cookieEnabled) {
        var alertCookie = "Please enable cookies in the browser first. Thanks!";
        ShowPotdMessage('',alertCookie,'#FF0033','bold');
        setTimeout('potdBubbleClose();', 2000);
      return;
    }
    showPotdOverlayBubble(1, 'enterzip');
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}


function open_price_history_chart()
{
	switch_price_history_chart('price_history_six_months');
	showPotdOverlayBubble(1, 'price_history_bubble');
}

function switch_price_history_chart(id)
{
	var html = document.getElementById(id).innerHTML;
	document.getElementById('price_history_bubble').innerHTML = html;
}

