function validator_keyword(input_id){
    var qurey_input = $(input_id);
    if (qurey_input.value == "" || qurey_input.value == "example.com"){
        alert("please input the key word");
        qurey_input.focus();
        return false;
    }
    return true;
}

function submit_email_friend_form(form){
    var first_email = $('email_friend_user_addr').value;
    var second_email = $('email_friend_addr2').value;
    var third_email = $('email_friend_addr3').value;
    var your_email = $('email_friend_user_addr').value;
    if(your_email == ''){
        alert("Please enter your email");
        return false;
    }

    if(first_email == '' && second_email == '' && third_email == ''){
        alert("Please enter at least one recipient email");
        return false;
    }

    if($('email_friend_title').value == '' && $('email_friend_content').value == ''){
        alert("Please enter title or content");
        return false;
    }

    if(!validte_email_value(your_email) || !validte_email_value(first_email) ||
        !validte_email_value(second_email) || !validte_email_value(third_email)){
        alert("Please enter a valid email");
        return false;
    }

    form.onsubmit();
    return true;
}

function validte_email_value(content){
    return content == '' || validate_email_fomat(content);
}

function validate_email_fomat(content){
    emial_exp = new RegExp('([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})', 'img');
    return emial_exp.exec(content);
}

function validator_submit_deal_form(){
    if($('submit_deal_deal_url').value == ''){
        alert('Please enter deal url');
        $('submit_deal_deal_url').focus();
        return false;
    }else if($('submit_deal_title').value == ''){
        alert('Please enter title');
        $('submit_deal_title').focus();
        return false;
    }
    return true;
}

function toggle_deal_share_form(deal_id){
    if($('full_deal_content')){
        document.getElementById('email_friend_content').value =  $('full_deal_content').innerHTML;
    }
    $('tellafriend'+deal_id).toggle();
}

//generated_form_deal_ids = [];
//function toggle_deal_share_form(deal_id){
//    Element.hide("problem"+deal_id)
//    if(generated_form_deal_ids.indexOf(deal_id) < 0){
//        var url = "/deals/"+deal_id+"/share";
//        new Ajax.Request(url,{method:'get'});
//        generated_form_deal_ids.push(deal_id);
//    }
//    Element.toggle("tellafriend"+deal_id);
//}

report_problem_form_deal_ids = [];
function toggle_report_probleme_form(deal_id){
    Element.hide("tellafriend"+deal_id);
    if(report_problem_form_deal_ids.indexOf(deal_id) < 0){
        Element.insert("problem"+deal_id, $('deal_report_problem_form').innerHTML);
        report_problem_form_deal_ids.push(deal_id);
        _form = $('problem' + deal_id).getElementsByTagName('form')[0];
        _form["problem_report[deal_id]"].value = deal_id;
        $("deal_id_for_report").value = deal_id;
    }
    Element.toggle("problem"+deal_id)
}


function vote_deal_comment(deal_id,if_good){
    if (if_good)
    {
        new Ajax.Request("/submit_valid_vote?deal_id=" +deal_id);
        var votes_span = $("votesgood");
        votes_span.innerHTML = (parseInt(votes_span.innerHTML) + 1)
    }
    else
    {
        new Ajax.Request("/submit_invalid_vote?deal_id=" +deal_id);
        var votes_span2 = $("votesbad");
        votes_span2.innerHTML = (parseInt(votes_span2.innerHTML) + 1)
    }
    var comment_div = $("deal_comment_div");
    _form = comment_div.getElementsByTagName('form')[0];
    _form["comment[comment_type]"].value = (if_good ? '1' : '2');
    comment_div.setAttribute('class','work_detail work_detail_gray')
    comment_div.setAttribute('className','work_detail work_detail_gray')
    $("valid_vote").onclick = 'return false;'
    $("invalid_vote").onclick = 'return false;'
}

function vote_coupon_comment(coupon_id,if_good){
    if (if_good)
    {
        new Ajax.Request("/submit_valid_vote?coupon_id=" +coupon_id);
        var goole_span = $("votesgood"+coupon_id);
        goole_span.innerHTML = (parseInt(goole_span.innerHTML) + 1)
    }
    else
    {
        new Ajax.Request("/submit_invalid_vote?coupon_id=" +coupon_id);
        var bad_span = $("votesbad"+coupon_id);
        bad_span.innerHTML = (parseInt(bad_span.innerHTML)+1)
    }
    var comment_div = $("coupon_comments_div_" +coupon_id);
    comment_div.setAttribute('class','bk16_N_r bk16_N_gray')
    comment_div.setAttribute('className','bk16_N_r bk16_N_gray')
    $("valid_vote_" + coupon_id).onclick = 'return false;'
    $("invalid_vote_" + coupon_id).onclick = 'return false;'
    _form = $('comments' + coupon_id).getElementsByTagName('form')[0];
    _form["comment[comment_type]"].value = (if_good ? '1' : '2');
    if(!Element.visible('comments'+coupon_id) ){
        Effect.BlindDown('comments'+coupon_id);
    }
}

function toggole_coupon_comments(coupon_id,comment_type){
    _form = $('comments' + coupon_id).getElementsByTagName('form')[0];
    _form["comment[comment_type]"].value = comment_type;
    Effect.toggle('comments'+coupon_id,'blind');
}


//request_comments_coupon_ids = []
//function request_coupon_comments(coupon_id){
//    if(request_comments_coupon_ids.indexOf(coupon_id) < 0){
//        new Ajax.Request("/coupon_comments/index?coupon_id=" +coupon_id,{
//            asynchronous:false,
//            evalScripts:true
//        });
//        request_comments_coupon_ids.push(coupon_id);
//        Element.show('comments'+coupon_id);
//    }else{
//        Element.toggle('comments'+coupon_id);
//    }
//    $("bad_comment" + coupon_id).hide();
//    $("goold_comment" + coupon_id).hide();
//    _form = $('comments' + coupon_id).getElementsByTagName('form')[0];
//    _form["coupon_id"].value = coupon_id;
//}

function submit_share_deal_form(_form){
    new Ajax.Request(_form.action,
    {
        asynchronous:true,
        evalScripts:true,
        method:'post',
        parameters:Form.serialize(_form)
    }
    );
}

function submit_remote_form(form){
    new Ajax.Request(form.action,
    {
        asynchronous:true,
        evalScripts:true,
        method:'post',
        parameters:Form.serialize(form)
    }
    );
    $A(form.elements).each(
        function(element){
            if(element.type == 'text' || element.tagName.toLowerCase() == 'textarea'){
                element.value = '';
            }
        }
        );
}


function generate_compare_price_htmls(type_id,product_id){
    var cobrand_id = '1423';
    var rating_star_type = 0;
    //var cb_limit = 6;
    var open_link_type2 = 1;
    var show_link_price2 = 1;
    var head_title_type = '1';
    document.write('<scr'+'ipt src="http://ah.pricegrabber.com/cb_table.php?'+
        type_id+'='+product_id+
        '&dw=1'+
        '&cobrand_id='+cobrand_id+
        '&rst='+rating_star_type+
        '&slp='+show_link_price2+
        '&olt='+open_link_type2+
        //'&l='+cb_limit+
        '&w=300'+
        '&htt='+head_title_type+
        '"></scr'+'ipt>');
}

function generate_width_compare_price_htmls(type_id,product_id){
    var cobrand_id = '1423';
    var rating_star_type = 1;
    //var cb_limit = 6;
    var open_link_type2 = 1;
    var show_link_price2 = 1;
    var head_title_type = '1';
    document.write('<scr'+'ipt src="http://ah.pricegrabber.com/cb_table.php?'+
        type_id+'='+product_id+
        '&dw=1'+
        '&cobrand_id='+cobrand_id+
        '&rst='+rating_star_type+
        '&slp='+show_link_price2+
        '&olt='+open_link_type2+
        //'&l='+cb_limit+
        '&w=550'+
        '&htt='+head_title_type+
        '"></scr'+'ipt>');
}

function cp_clipboard(text) {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
        var divholder = document.createElement('div');
        divholder.id = flashcopier;
        document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="http://livehotdeals.com/images/liveclipboard.swf" FlashVars="liveclipboard='+encodeURIComponent(text)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
}

function toggle_by_id(id1,id2)
{
    $(id1).toggle();
    $(id2).toggle();
}

function toggle_htgt(id)
{
    $('htgt_o'+id).toggle();
    $('htgt_c'+id).toggle();
}

function open_top_coupons()
{
    document.cookie = "topcoupons=true";
    $('top_coupon_o').style.display = "block";
    $('top_coupon_c').style.display = "none";
}

function close_top_coupons()
{
    var exptime = new Date();
    exptime.setDate(exptime.getDate()+70000);
    document.cookie = "topcoupons=false; expires=" + exptime.toGMTString();
    $('top_coupon_o').style.display = "none";
    $('top_coupon_c').style.display = "block";
}

var active_top_deal = ''
function toggle_weekly_deals(id)
{
    if(active_top_deal == id) return;
    if(active_top_deal && active_top_deal != ''){
        $('contr_'+id).className = 'current';
        $('contr_'+active_top_deal).className = '';
        Element.toggle(active_top_deal);
    }
    active_top_deal = id;
    Element.toggle(id);
}

function vaildate_search_form(searchForm){
    var check_box = searchForm['type'];
    var qurey_input = searchForm.keyword;
    if((check_box.options && check_box.options[check_box.options.selectedIndex].value == 'coupon')||
        (check_box.value && check_box.value=='coupon')){
        qurey_input.value = searchForm.suggest_keyword.value;
    }
    if (qurey_input.value == "" || qurey_input.value == 'example.com'){
        alert("please input the key word");
        qurey_input.focus();
        return false;
    }
    return true;
}

function changeSearchType(selected_value){
    if(selected_value == 'coupon'){
        Element.hide('keyword');
        Element.show('suggest_keyword');
        $('suggest_keyword').value = $('keyword').value
    }else{
        Element.hide('suggest_keyword');
        Element.show('keyword');
        $('keyword').value = $('suggest_keyword').value
    }
}

function fbs_click() {
    u=location.href;
    t=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}



/***********************************************
 * Cool DHTML tooltip script II- Dynamic Drive DHTML code library (www.dynamicdrive.com)
 * This notice MUST stay intact for legal use
 * Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
 ***********************************************/

var offsetfromcursorX=12 //Customize x offset of tooltip
var offsetfromcursorY=10 //Customize y offset of tooltip

var offsetdivfrompointerX=30 //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=6 //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

document.write('<div id="dhtmltooltip"></div>') //write out tooltip DIV
document.write('<img id="dhtmlpointer" src="http://livehotdeals.com/images/tooltip_arrow.gif">') //write out pointer image

var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
if (ie||ns6)
    var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : ""

var pointerobj=document.all? document.all["dhtmlpointer"] : document.getElementById? document.getElementById("dhtmlpointer") : ""

function ietruebody(){
    return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function ddrivetip(thetext, thewidth, thecolor, thetextcolor){
    if (ns6||ie){
        if (typeof thewidth!="undefined") tipobj.style.width=thewidth+"px"
        if (typeof thecolor!="undefined" && thecolor!="") tipobj.style.backgroundColor=thecolor
        if (typeof thetextcolor!="undefined" && thetextcolor!="") tipobj.style.color=thetextcolor
        tipobj.innerHTML=thetext
        enabletip=true
        return false;
    }
    return true;
}

function positiontip(e){
    if (enabletip){
        var nondefaultpos=false
        var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
        var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
        //Find out how close the mouse is to the corner of the window
        var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-20
        var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-20

        var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX
        var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY

        var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000

        //if the horizontal distance isn't enough to accomodate the width of the context menu
        if (rightedge<tipobj.offsetWidth){
            //move the horizontal position of the menu to the left by it's width
            tipobj.style.left=curX-tipobj.offsetWidth+"px"
            nondefaultpos=true
        }
        else if (curX<leftedge)
            tipobj.style.left="5px"
        else{
            //position the horizontal position of the menu where the mouse is positioned
            tipobj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px"
            pointerobj.style.left=curX+offsetfromcursorX+"px"
        }

        //same concept with the vertical position
        if (bottomedge<tipobj.offsetHeight){
            tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px"
            nondefaultpos=true
        }
        else{
            tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px"
            pointerobj.style.top=curY+offsetfromcursorY+"px"
        }
        tipobj.style.visibility="visible"
        if (!nondefaultpos)
            pointerobj.style.visibility="visible"
        else
            pointerobj.style.visibility="hidden"
    }
}

function hideddrivetip(){
    if (ns6||ie){
        enabletip=false
        tipobj.style.visibility="hidden"
        pointerobj.style.visibility="hidden"
        tipobj.style.left="-1000px"
        tipobj.style.backgroundColor=''
        tipobj.style.width=''
    }
}

document.onmousemove=positiontip

var clip = null;
// setup single ZeroClipboard object for all our elements
clip = new ZeroClipboard.Client();
ZeroClipboard.setMoviePath( 'http://livehotdeals.com/images/ZeroClipboard.swf' );
clip.setHandCursor( true );
clip.addEventListener('mouseOver', function(client) {
    ddrivetip('Click to copy coupon code and open site...', 240, 'fff','85562d');
});
clip.addEventListener('mouseOut', function(client) {
    hideddrivetip('');
});

function openlink(link) {
    window.open(link);
}

function flashChecker()
{
    var hasFlash=0;         //
    var flashVersion=0;     //
    if(document.all){       //if ie(not lower than ie4 )
        var swf = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
        if(swf) {
            hasFlash=1;
            VSwf = swf.GetVariable("$version");
            flashVersion = parseInt(VSwf.split(" ")[1].split(",")[0]);
        }
    }
    else{
        if (navigator.plugins && navigator.plugins.length > 0){
            var theswf = navigator.plugins["Shockwave Flash"];
            if (theswf){
                hasFlash=1;
                var words = theswf.description.split(" ");
                for (var i = 0; i < words.length; ++i)
                {
                    if (isNaN(parseInt(words[i]))) continue;
                    flashVersion = parseInt(words[i]);
                }
            }
        }
    }
    return {
        f:hasFlash,
        v:flashVersion
    };
}

function copy_to_clipboard(text, link, object) {
    var fls = flashChecker();
    if(fls.f==1 && fls.v>8){
        // set the clip text to our innerHTML
        clip.setText(text);
        //if this is the first time , create it ,if not , reposition the movie over our element
        if (clip.div) {
            //clip.receiveEvent('mouseout', null);
            clip.reposition(object);
        }
        else clip.glue(object);
        //clip.receiveEvent('mouseover', null);
        var our_link = link;
        clip.setEventListener('mouseup', function(client) {
            openlink(our_link);
            hideddrivetip('');
        });
    }
}

function intval(v)
{
    v = parseInt(v);
    return isNaN(v) ? 0 : v;
}

function getPos(e)
{
    var l = 0;

    var t = 0;

    var w = intval(e.style.width);

    var h = intval(e.style.height);

    var wb = e.offsetWidth;

    var hb = e.offsetHeight;

    while (e.offsetParent){

        l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0);

        t += e.offsetTop + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0);

        e = e.offsetParent;

    }

    l += e.offsetLeft + (e.currentStyle?intval(e.currentStyle.borderLeftWidth):0);

    t += e.offsetTop + (e.currentStyle?intval(e.currentStyle.borderTopWidth):0);

    return {
        x:l,
        y:t,
        w:w,
        h:h,
        wb:wb,
        hb:hb
    };

}


function getScroll()
{
    var t, l, w, h;

    if (document.documentElement && document.documentElement.scrollTop) {

        t = document.documentElement.scrollTop;

        l = document.documentElement.scrollLeft;

        w = document.documentElement.scrollWidth;

        h = document.documentElement.scrollHeight;

    } else if (document.body) {

        t = document.body.scrollTop;

        l = document.body.scrollLeft;

        w = document.body.scrollWidth;

        h = document.body.scrollHeight;

    }

    return {
        t: t,
        l: l,
        w: w,
        h: h
    };
}

function scroller(el, duration)
{
    if(typeof el != 'object') {
        el = document.getElementById(el);
    }
    if(!el) return;
    var z = this;
    z.el = el;
    z.p = getPos(el);
    z.s = getScroll();
    z.clear = function(){
        window.clearInterval(z.timer);z.timer=null
    };

    z.t=(new Date).getTime();
    z.step = function(){
        var t = (new Date).getTime();
        var p = (t - z.t) / duration;
        if (t >= duration + z.t) {
            z.clear();
            window.setTimeout(function(){
                z.scroll(z.p.y, z.p.x)
            },13);

        } else {
            st = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.y-z.s.t) + z.s.t;
            sl = ((-Math.cos(p*Math.PI)/2) + 0.5) * (z.p.x-z.s.l) + z.s.l;
            z.scroll(st, sl);
        }
    };
    z.scroll = function (t, l){
        window.scrollTo(l, t)
    };

    z.timer = window.setInterval(function(){
        z.step();
    },13);

}

function subtmit_vote_comment(form){
    submit_remote_form(form);
    Element.hide('deal_review_form');
    scroller('deal_comments_link',800);
//    window.location.hash="comments";
}

function display_deal_comment_form(a_link){
    var deal_comment_from = $('add_deal_comment');
    if(Element.visible(deal_comment_from)){
        a_link.setAttribute('class','add_comment');
        a_link.setAttribute('className','add_comment');
        Element.hide('comments_notice_wrong')
        Effect.toggle(deal_comment_from,'slide')
    }else{
        a_link.setAttribute('class','add_comment add_comment_2');
        a_link.setAttribute('className','add_comment add_comment_2');
        Element.show(deal_comment_from)
        scroller('add_deal_comment',800);
    }
}

var current_htgt_div_for_landing_page = '';
function display_htgt_div(htgt_div_id){
    if(current_htgt_div_for_landing_page != ''){
        Element.hide(current_htgt_div_for_landing_page);
    }
    Element.show(htgt_div_id);
    current_htgt_div_for_landing_page = htgt_div_id;
}


function show_full_deal_description(_id){
    $(_id).style.overflow="visible";
    $(_id).style.height="auto";
    $("full_deal_button").hide();
    $("section_deal_button").show();
}

function show_partial_deal_description(_id){
    $(_id).style.overflow="hidden";
    $(_id).style.height="80px";
    $("full_deal_button").show();
    $("section_deal_button").hide();
}

function if_show_deal_description_button(_id){
    if($(_id).offsetHeight > 112){
        show_partial_deal_description(_id);
    }
    else{
        show_full_deal_description(_id);
        $("section_deal_button").hide();
    }
}

function add_src_type_for_link(obj,type){
    var src_sym = obj.href.indexOf('?') >= 0 ? '&' : '?';
    obj.href = obj.href + src_sym + 'utm_source=' + type;
    obj.onmousedown = function(){
        return true;
    }
    return true;
}

//function openDealOut(url,sName,sFeatures,bReplac){
//    var adPopup = window.open('about:blank',sName,sFeatures,bReplac);
//    adPopup.blur();
//    adPopup.opener.focus();
//    adPopup.location = url;
//}



//usefull_comments_coupon_ids = []
//nouse_comments_coupon_ids  = []
//function vote_coupon_comment(coupon_id,if_good){
//    $("comments" + coupon_id).hide();
//    if (if_good)
//    {
//        $("bad_comment" + coupon_id).hide();
//        if(usefull_comments_coupon_ids.indexOf(coupon_id) < 0){
//            $('goold_comment' + coupon_id).update($('coupon_comments_form').innerHTML);
//            _form = $('goold_comment' + coupon_id).getElementsByTagName('form')[0];
//            _form["coupon_id"].value = coupon_id;
//            _form["comment[comment_type]"].value = '1';
//            usefull_comments_coupon_ids.push(coupon_id);
//            new Ajax.Request("/submit_valid_vote?coupon_id=" +coupon_id);
//        }
//        var goole_span = $("votesgood"+coupon_id);
//        goole_span.innerHTML = (parseInt(goole_span.innerHTML) + 1)
//        $("goold_comment" + coupon_id).toggle();
//    }
//    else
//    {
//        $("goold_comment" + coupon_id).hide();
//        if(nouse_comments_coupon_ids.indexOf(coupon_id) < 0){
//            $('bad_comment' + coupon_id).update($('coupon_comments_form').innerHTML);
//            _form = $('bad_comment' + coupon_id).getElementsByTagName('form')[0];
//            _form["coupon_id"].value = coupon_id;
//            _form["comment[comment_type]"].value = '2';
//            nouse_comments_coupon_ids.push(coupon_id);
//            new Ajax.Request("/submit_invalid_vote?coupon_id=" +coupon_id);
//        }
//        var bad_span = $("votesbad"+coupon_id);
//        bad_span.innerHTML = (parseInt(bad_span.innerHTML)+1)
//        $("bad_comment" + coupon_id).toggle();
//    }
//    var comment_div = $("coupon_comments_div_" +coupon_id);
//    comment_div.setAttribute('class','bk16_N_r bk16_N_gray')
//    comment_div.setAttribute('className','bk16_N_r bk16_N_gray')
//    $("valid_vote_" + coupon_id).onclick = 'return false;'
//    $("invalid_vote_" + coupon_id).onclick = 'return false;'
//}