/* $Id: scripts.js 449 2010-01-18 22:16:25Z dviner $ */
function getCheckedValue(radioObj) 
{
    if(!radioObj)
        return "";
    var radioLength = radioObj.length;
    if(radioLength == undefined)
        if(radioObj.checked)
            return radioObj.value;
        else
            return "";
    for(var i = 0; i < radioLength; i++) 
    {
        if(radioObj[i].checked) 
        {
            return radioObj[i].value;
        }
    }
    return "";
}

function SubmitForm()
{
    var mynum, URL, holenumber, pg, cname, mymess, bitly_url, load;
    
    mynum = getCheckedValue(document.forms['myform'].elements['choice']);

    mynum=mynum-1;
    holenumber = document.myform.holenumber.value * 1;
    pg = document.myform.pg.value * 1;
    cname = document.myform.cname.value;
    URL = document.myform.ref_url.value;
    
    mymess=new Array();
    
    if (holenumber > 0)
    {
        mymess[0] = encodeURIComponent("I birdied this hole! ... See it..")+"%20";
        mymess[1] = encodeURIComponent("This is my favorite hole in this course... Check it out..")+"%20";
        mymess[2] = encodeURIComponent("This was such a fun hole ... Look at it..")+"%20";
        mymess[3]="";
    }
    else
    {
        mymess[0] = encodeURIComponent("This is the golf course I'm playing this week... Take a tour..")+"%20";
        mymess[1] = encodeURIComponent("This is my favorite golf course... Check it out..")+"%20";
        mymess[2] = encodeURIComponent("I just shot a __ at this golf course!!! Look at it..")+"%20";
        mymess[3]="";
    }

    load =
        window.open('http://twitter.com/home?status=' + mymess[mynum] + URL, '', 'scrollbars=yes,menubar=yes,height=600,width=800,resizable=yes,toolbar=yes,location=yes,status=yes');

    return false;
}


function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}



function resiz()
{
    document.getElementById('modal').style.height = '500px';
    document.getElementById('modal').style.width = '800px';
}


jQuery('document').ready(function() {
    $(".submit").click(function() {
    
        jQuery('.votebox').fadeOut(200).hide();
        jQuery('.loading').fadeIn(200).show();
    
        var c_value = "";
        var cna="";
        var hole_id = jQuery("#hole_id").val();
    
    
        for (var i=0; i < document.vform.vote.length; i++)
        {
            if (document.vform.vote[i].checked)
            {
                cna= document.vform.vote[i].value;
                c_value = c_value + "&type[]="+ cna;
                set_cookie ( cna+hole_id, "1" );
            }
        }
    
    
    
        var dataString = 'hole_id='+ hole_id + c_value;
        console.log('sending data string: ' + dataString);
        //http://www.fairways360.com/svc/vote.php/v1?hole_id=123&type[]=scenic&type[]=favorite
        //http://www.fairways360.com/svc/vote.php/v1
        jQuery.ajax({
            'type': "GET",
            'url': "/svc/vote.php/v1",
            'data': dataString,
            'success': function(){
                jQuery('.loading').fadeOut(200).hide();
                jQuery('.success').fadeIn(200).show();
            }
        });
        return false;
    });
});
