function do_search(id)
{
	var search = document.getElementById('search');
	if (search.style.backgroundColor == "rgb(255, 255, 255)" || search.style.backgroundColor == "#ffffff") {
		var str = search.value;
		str = str.replace(/[^0-9a-zA-Z-]/g, " ");
		if (str != '') {
			if (id == undefined) { 
				id = 'search-form'; 
			} else {
				id = 'search-form-' + id;
			}
			var form = document.getElementById(id);
			form.search.value = str;
			form.submit();
		}
	}
}

function contest_countdown_init(entity)
{
	/* all comments */
	var comment_arr = document.getElementsByName('comment-id-' + entity);
	if (comment_arr != null) {
		var i=0;
		for (i=0; i<comment_arr.length; i++) {
			comment = comment_arr[i];
			var id = comment.innerHTML;
			setTimeout("update_time(" + id + ")", 1000);
			/* all replies */
			var reply_arr = document.getElementsByName('reply-id-for-' + id);
			if (reply_arr != null) {
				var j=0;
				for (j=0; j<reply_arr.length; j++) {
					reply = reply_arr[j];
					var id = reply.innerHTML;
					setTimeout("update_time(" + id + ")", 1000);
				}
			}
		}
	}
}

function comment_countdown_init(entity)
{
	var obj_arr = document.getElementsByName('comment-id-' + entity);
	if (obj_arr != null && obj_arr.length > 0) {
		obj = obj_arr[obj_arr.length - 1];
		var id = obj.innerHTML;
		setTimeout("update_time(" + id + ")", 1000);
	}
}

function reply_countdown_init(comment)
{
	var obj_arr = document.getElementsByName('reply-id-for-' + comment);
	if (obj_arr != null && obj_arr.length > 0) {
		obj = obj_arr[obj_arr.length - 1];
		var id = obj.innerHTML;
		setTimeout("update_time(" + id + ")", 1000);
	}
}

function update_time(id)
{
	var obj = document.getElementById('countdown-timer-' + id);
	if (obj != null) {
		var seconds = obj.innerHTML;
		if (seconds > 1) {
			obj.innerHTML = (seconds - 1);
			setTimeout("update_time(" + id + ")", 1000);
		} else {
			var obj = document.getElementById('edit-control-' + id);
			obj.style.display='none';
		}
	}
}

function plusvote(id, type, entity_type, limit)
{
	if (type == 'deal') {
		var count = Number($('daily-vote-count').innerHTML);
		count = count + 1;
		$('daily-vote-count').innerHTML = count.toString();
		if (count > limit) {
			alert('You reached daily deal vote limit');
			return;
		}
	}
	new Ajax.Updater(type + '-vote-' + id, '/comment/submitvote', {asynchronous:true, evalScripts:false, parameters:'id=' + id + '&vote=plus&type=' + type + '&entity_type=' + entity_type});
}

function minusvote(id, type, entity_type, limit)
{
	if (type == 'deal') {
		var count = Number($('daily-vote-count').innerHTML);
		count = count + 1;
		$('daily-vote-count').innerHTML = count.toString();
		if (count > limit) {
			alert('You reached daily deal vote limit');
			return;
		}
	}
	new Ajax.Updater(type + '-vote-' + id, '/comment/submitvote', {asynchronous:true, evalScripts:false, parameters:'id=' + id + '&vote=minus&type=' + type + '&entity_type=' + entity_type});
}
function votepopup(id, type, entity_type, limit)
{
	if (type == 'deal') {
		var count = Number($('daily-vote-count').innerHTML);
		count = count + 1;
		$('daily-vote-count').innerHTML = count.toString();
		if (count > limit) {
			alert('You reached daily deal vote limit');
			return;
		}
	}
	new Ajax.Updater('help-popup', '/comment/votepopup', {asynchronous:true, evalScripts:false, parameters:'id=' + id + '&vote=minus&type=' + type + '&entity_type=' + entity_type, onComplete:function(request, json){displayfadeinbox('vote-box', true);}});
}
function uploadpopup(which, id, type, entity_type)
{
	new Ajax.Updater('help-popup', '/comment/uploadpopup', {asynchronous:true, evalScripts:false, parameters:'which=' + which + '&id=' + id + '&type=' + type + '&entity-type=' + entity_type, onComplete:function(request, json){displayfadeinbox('upload-box', true);}});
}
function videopopup(which, id, type, entity_type)
{
	new Ajax.Updater('help-popup', '/comment/videopopup', {asynchronous:true, evalScripts:false, parameters:'which=' + which + '&id=' + id + '&type=' + type + '&entity-type=' + entity_type, onComplete:function(request, json){displayfadeinbox('video-link', true);}});
}

function uploadresult(which, id, type, entity_type, file_name)
{
	new Ajax.Updater('help-popup', '/comment/uploadpopup', {asynchronous:true, evalScripts:false, parameters:'which=' + which + '&id=' + id + '&type=' + type + '&entity-type=' + entity_type + '&file-name=' + file_name, onComplete:function(request, json){displayfadeinbox('upload-box', true);}});
}

function reporterror(error)
{
	alert(error);
}

function reportpopup(id, report, tag, search, type, merchant, brand)
{
	var params = 'id=' + id + '&report=' + report;
	if (tag != '') {
		params += '&tag=' + tag;
	}
	if (search != '') {
		params += '&search=' + search;
	}
	if (type != '') {
		params += '&type=' + type;
	}
	if (merchant != '') {
		params += '&merchant=' + merchant;
	}
	if (brand != '') {
		params += '&brand=' + brand;
	}
	new Ajax.Updater('help-popup', '/browser/reportpopup', {asynchronous:true, evalScripts:false, parameters:params, onComplete:function(request, json){displayfadeinbox('report-box', true);}});
}

function banpopup(id, active)
{
	var params = 'id=' + id + '&active=' + active;
	new Ajax.Updater('help-popup', '/admin/banpopup', {asynchronous:true, evalScripts:false, parameters:params, onComplete:function(request, json){displayfadeinbox('ban-box', true);}});
}

function original_image(image_id)
{
	new Ajax.Updater('image-popup', '/info/image', {asynchronous:true, evalScripts:false, parameters:'file=' + image_id, onComplete:function(request, json){displayfadeinbox('image-box', true, true);}});
}

function show_reply_form(comment_id, entity_type, entity_id)
{
	div = document.getElementById('comment-reply-' + comment_id);
	if (div.innerHTML.length == 0) {
		new Ajax.Updater('comment-reply-' + comment_id, '/comment/showreplyform', {
			asynchronous:true, evalScripts:false, 
			parameters:'comment_id=' + comment_id + '&entity_type=' + entity_type + '&entity_id=' + entity_id, 
			onComplete:function(request, json){
				insert_extToolbar('toolbar_placeholder', 'message', 'message', entity_id, entity_type);
				Element.hide('comment-reply-' + comment_id);
				Effect.BlindDown('comment-reply-' + comment_id);
			}
		});
	} else {
		if (div.style.display == 'none') {
			Effect.BlindDown('comment-reply-' + comment_id);
		} else {
			Effect.BlindUp('comment-reply-' + comment_id);
		}		
	}
}

function show_admin_reply_form(chat_id, reply_id)
{
	div = document.getElementById('chat-reply-' + chat_id);
	
	if (div.innerHTML.length == 0) {
		new Ajax.Updater('chat-reply-' + chat_id, '/admin/showchatreplyform', {
			asynchronous:true, evalScripts:false, 
			parameters:'chat_id=' + chat_id + '&reply_id=' + reply_id, 
			onComplete:function(request, json){
				Element.hide('chat-reply-' + chat_id);
				Effect.BlindDown('chat-reply-' + chat_id);
			}
		});
	} else {
		if (div.style.display == 'none') {
			Effect.BlindDown('chat-reply-' + chat_id);
		} else {
			Effect.BlindUp('chat-reply-' + chat_id);
		}		
	}
}

function show_plus_votes(comment_id)
{
	new Ajax.Updater('help-popup', '/comment/showvotes', {asynchronous:true, evalScripts:false, parameters:'comment_id=' + comment_id + '&vote=plus', onComplete:function(request, json){displayfadeinbox('vote-box', false);}});
}

function show_minus_votes(comment_id)
{
	new Ajax.Updater('help-popup', '/comment/showvotes', {asynchronous:true, evalScripts:false, parameters:'comment_id=' + comment_id + '&vote=minus', onComplete:function(request, json){displayfadeinbox('vote-box', false);}});
}

function show_guest_reply_form(comment_id, entity_type, entity_id)
{
	div = document.getElementById('comment-reply-' + comment_id);
	if (div.innerHTML.length == 0) {
		new Ajax.Updater('comment-reply-' + comment_id, '/comment/showguestreplyform', {
			asynchronous:true, evalScripts:false, parameters:'comment_id=' + comment_id + '&entity_type=' + entity_type + '&entity_id=' + entity_id, 
			onComplete:function(request, json){
				insert_edToolbar('toolbar_placeholder', 'message', 'message');
				Element.hide('comment-reply-' + comment_id);
				Effect.BlindDown('comment-reply-' + comment_id);
			}
		});
	} else {
		if (div.style.display == 'none') {
			Effect.BlindDown('comment-reply-' + comment_id);
		} else {
			Effect.BlindUp('comment-reply-' + comment_id);
		}		
	}
}

function show_friends(customer_id, month, show)
{
	div = document.getElementById('customer-' + customer_id);
	link = document.getElementById('show-friends-' + customer_id);
	if (div.innerHTML.length == 0) {
		new Ajax.Updater('customer-' + customer_id, '/contest/showfriends', {
			asynchronous:true, evalScripts:false, parameters:'id=' + customer_id + '&m=' + month + '&show=' + show,
			onComplete:function(request, json){
				Element.hide('customer-' + customer_id);
				Effect.BlindDown('customer-' + customer_id);
			}
		});
		link.innerHTML = '[&#8722;]';
	} else {
		if (div.style.display == 'none') {
			Effect.BlindDown('customer-' + customer_id);
		} else {
			Effect.BlindUp('customer-' + customer_id);
		}		
		link.innerHTML = '[+]';
	}
}

function base64_decode( data ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Thunder.m
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // -    depends on: utf8_decode
    // *     example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
    // *     returns 1: 'Kevin van Zonneveld'
 
    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof window['btoa'] == 'function') {
    //    return btoa(data);
    //}
 
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, dec = "", tmp_arr = [];
 
    data += '';
 
    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));
 
        bits = h1<<18 | h2<<12 | h3<<6 | h4;
 
        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;
 
        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);
 
    dec = tmp_arr.join('');
    dec = utf8_decode(dec);
 
    return dec;
}

function utf8_decode ( str_data ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Norman "zEh" Fuchs
    // +   bugfixed by: hitwork
    // +   bugfixed by: Onno Marsman
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
 
    var tmp_arr = [], i = ac = c1 = c2 = c3 = 0;
 
    str_data += '';
 
    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
 
    return tmp_arr.join('');
}

function base64_encode( data ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Tyler Akins (http://rumkin.com)
    // +   improved by: Bayron Guevara
    // +   improved by: Thunder.m
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)        
    // -    depends on: utf8_encode
    // *     example 1: base64_encode('Kevin van Zonneveld');
    // *     returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='
 
    // mozilla has this native
    // - but breaks in 2.0.0.12!
    //if (typeof window['atob'] == 'function') {
    //    return atob(data);
    //}
        
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, enc="", tmp_arr = [];
    data = utf8_encode(data);
    
    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);
 
        bits = o1<<16 | o2<<8 | o3;
 
        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;
 
        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
    
    enc = tmp_arr.join('');
    
    switch( data.length % 3 ){
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }
 
    return enc;
}

function utf8_encode ( string ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
 
    string = (string+'').replace(/\r\n/g,"\n");
    var utftext = "";
    var start, end;
    var stringl = 0;
 
    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;
 
        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc != null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }
 
    if (end > start) {
        utftext += string.substring(start, string.length);
    }
 
    return utftext;
}

function set_cookies_comment(name, email, human, exp_time)
{
	var enc_name = base64_encode(name);
	var enc_email = base64_encode(email);
	var enc_human = base64_encode(human);
	document.cookie = 'buxr-name=' + enc_name + '; expires=' + exp_time + '; path=/'
	document.cookie = 'buxr-email=' + enc_email + '; expires=' + exp_time + '; path=/'
	document.cookie = 'buxr-human=' + enc_human + '; expires=' + exp_time + '; path=/'
}

function submit_comment(entity_id, ser_form, button_id)
{
	new Ajax.Updater('comment-list-' + entity_id, '/comment/addcomment', 
	{
		asynchronous:true, 
		evalScripts:false, 
		onLoaded:function(request, json)
		{
			Element.hide('refresh-comment-progress-' + entity_id); 
			$(button_id).disabled=false;
		}, 
		onLoading:function(request, json)
		{
			Element.show('refresh-comment-progress-' + entity_id);
			$(button_id).disabled=true;
		}, 
		parameters:ser_form
	});
}

function submit_reply(comment_id, ser_form)
{
	new Ajax.Updater('single-comment-' + comment_id, '/comment/submitreply', 
	{
		asynchronous:true, 
		evalScripts:false, 
		complete:reply_countdown_init(comment_id),
		onLoaded:function(request, json)
		{
			Element.hide('refresh-reply-progress-' + comment_id); 
		}, 
		onLoading:function(request, json)
		{
			Element.show('refresh-reply-progress-' + comment_id);
		}, 
		parameters:ser_form
	});
}

/* anonymous comment */
function submit_comment_func(entity_id, form)
{
	var name = $('name-' + entity_id).value;
	if (name.length == 0) {
		alert('Name is required');
		return false;
	}
	if (name.length > 20) {
		alert('Name is too long');
		return false;
	}
	var email = $('email-' + entity_id).value;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		alert('Invalid email');
		return false;
	}
	var message = $('message-' + entity_id).value;
	if (message.length == 0) {
		alert('Message cannot be empty');
		return false;
	}
	if (message.length > 512) {
		alert('Message is too long');
		return false;
	}
	var human = $('human-' + entity_id).value;
	if (human != 6) {
		alert('Human check failed');
		return false;
	}
	
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+14);
	
	set_cookies_comment(name, email, human, exdate.toGMTString());
	submit_comment(entity_id, Form.serialize(form), 'submit-button');
	$('message-' + entity_id).value='';
	new Ajax.Updater('help-popup', '/help/show', 
		{asynchronous:true, evalScripts:false, parameters:'id=moderated_comment', onComplete:function(request, json){displayfadeinbox('help-box', true);}});
}

/* anonymous comment reply */
function submit_reply_func(comment_id, form)
{
	var name = $('name').value;
	if (name.length == 0) {
		alert('Name is required');
		return false;
	}
	if (name.length > 20) {
		alert('Name is too long');
		return false;
	}
	var email = $('email').value;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		alert('Invalid email');
		return false;
	}
	var message = $('message').value;
	if (message.length == 0) {
		alert('Message cannot be empty');
		return false;
	}
	if (message.length > 512) {
		alert('Message is too long');
		return false;
	}
	var human = $('human').value;
	if (human != 6) {
		alert('Human check failed');
		return false;
	}
	
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+14);
	
	set_cookies_comment(name, email, human, exdate.toGMTString());
	submit_reply(comment_id, Form.serialize(form));
	new Ajax.Updater('help-popup', '/help/show', {asynchronous:true, evalScripts:false, parameters:'id=moderated_comment', onComplete:function(request, json){displayfadeinbox('help-box', true);}});
}

function textCounter(field_id, cntfield_id, maxlimit) 
{
	var field = document.getElementById(field_id);
	var cntfield = document.getElementById(cntfield_id);
	
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	} else {
		cntfield.value = maxlimit - field.value.length;
	}
}

function newSearchAutocomplete()
{
	new Ajax.Autocompleter('search', 'search-tips', '/search/autocomplete', {
		disableReturnKey: 'yes',
		disableUpKey: 'yes',
		disableDownKey: 'yes'
	});
}

function sortOptionsList(lb)
{
	arrTexts = new Array();
	arrValues = new Array();
	arrOldTexts = new Array();

	for(i=0; i<lb.length; i++)
	{
		arrTexts[i] = lb.options[i].text;
		arrValues[i] = lb.options[i].value;

		arrOldTexts[i] = lb.options[i].text;
	}

	arrTexts.sort();

	for(i=0; i<lb.length; i++)
	{
		lb.options[i].text = arrTexts[i];
		for(j=0; j<lb.length; j++)
		{
			if (arrTexts[i] == arrOldTexts[j])
			{
				lb.options[i].value = arrValues[j];
				j = lb.length;
			}
		}
	}
}
