function swap_platform_changed( formid, pagename )
{
  form = document.getElementById( formid );
  form.action = pagename;
  form.submit();
}

function wish_choosegame( formname, gameid )
{
  form = document.forms[ formname ];
  form.wishid.value = gameid;
  form.action = "userprofile.php";
  form.submit();
}

function swap_choosegame( formname, gameid )
{
  form = document.forms[ formname ];
  form.gameid.value = gameid;
  form.action = "userprofile.php";
  form.submit();
}

function deleteWish( formname, wishid )
{
  form = document.forms[ formname ];
  form.deletewishid.value = wishid;
  form.submit();
}

function deleteSwap( formname, swapid )
{
  form = document.forms[ formname ];
  form.deletegameid.value = swapid;
  form.submit();
}

function feedbackstar( attrib, num )
{
  $hidd = document.getElementById( attrib );
  $hidd.value = num;
  for( i=1;i<=num;i++)
  {
    $in = document.getElementById( attrib + "_" + i );
    $in.src = "images/icons/star_24.png";
  }
  for( j=num+1;j<=5;j++)
  {
    $in = document.getElementById( attrib + "_" + j );
    $in.src = "images/icons/ghost_star_24.png";
  }
}


function submit_form( form )
{
  document.forms[ form ].submit();
}

function customizePublisher( formname, idname , indic ){
  document.getElementById( idname ).innerHTML = "<input onblur=\"javascript:sendNewpublisher('" + formname + "','" + idname + "','" + indic + "');\" class=\"searchtxtbox\" size=8 type=text name=custompublisher value=\"\">";
}

function flipCashvalue($selecter,idname,idname1)
{
  $in = document.getElementById( idname );
  $in1 = document.getElementById( idname1 );
  if( $selecter.value === "yes" )
  {
    $in.style.display = 'block';
    $in1.style.display = 'block';
  } else {
    $in.style.display = 'none';
    $in1.style.display = 'none';
  }
}
function switchGameblocks(allblocks,block,type)
{
  var bsplit = allblocks.split("-");
  for(i=0;i<=bsplit.length;i++)
  {
    $in = document.getElementById( "block_" + type + "_" + bsplit[ i ] );
    if( bsplit[ i ] === block )
    {
      $in.style.display = 'block';
    } else {
      $in.style.display = 'none';
    }
  }
}
function switchAdtypes(block, selectname )
{
  if( block == "" ){
    $select = document.getElementById( selectname );
    block = $select.value + "type";
  }
  var allblocks = "swaptype-auctiontype-fixedsaletype-nearestoffertype-verynearestoffertype-nearestofferwgtype-verynearestofferwgtype";
  var bsplit = allblocks.split("-");
  for(i=0;i<bsplit.length;i++)
  {
    $in = document.getElementById( "swap_" + bsplit[ i ] );
    $in2 = document.getElementById( "pay_" + bsplit[ i ] );
//alert(  bsplit[ i ] + "==" + block + "==" + selectname );
    if( bsplit[ i ] == block )
    {
      $in.style.display = 'block';
      if( $in2 != null )
      {
        $in2.style.display = 'none';
      }
    } else {
      $in.style.display = 'none';
      if( $in2 != null )
      {
        $in2.style.display = 'block';
      }
    }
  }
}

function customizeDeveloper( formname, idname , indic ){
  document.getElementById( idname ).innerHTML = "<input onblur=\"javascript:sendNewdeveloper('" + formname + "','" + idname + "','" + indic + "');\" class=\"searchtxtbox\" size=8 type=text name=customdeveloper value=\"\">";
}

function sendNewpublisher( formname, spanid , indic ){
   form = document.forms[ formname ];
   value = form.custompublisher.value;
   sendNewupdate( spanid , indic, 'publisher', value );
}

function sendNewdeveloper( formname, spanid , indic ){
   form = document.forms[ formname ];
   value = form.customdeveloper.value;
   sendNewupdate( spanid , indic, 'developer', value );
}

function sendNewupdate( spanid , indic, page, value )
{
  var ajaxRequest;  // The variable that makes Ajax possible!
  try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
  } catch (e){
      // Internet Explorer Browsers
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
  }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
          document.getElementById( spanid ).innerHTML = ajaxRequest.responseText;
        }
    }
//    gametitle = document.getElementById( "gametitle" ).value;
//    platform = document.getElementById( "platform" ).value;
// TODO you need to html encode the following parameter
    ajaxRequest.open("GET", "update" + page + ".php?indic=" + indic + "&value=" + value, true);
    ajaxRequest.send(null); 
}   
function submitComment( spanid )
{
  var ajaxRequest;  // The variable that makes Ajax possible!
  try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
  } catch (e){
      // Internet Explorer Browsers
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
  }
    comment = document.getElementById( "acomment" ).value;
    name = document.getElementById( "aname" ).value;
    email = document.getElementById( "aemail" ).value;
    ajaxRequest.open("POST", "submitcomment.php?comment=" + comment + "&name=" + name + "&email=" + email , true);
    ajaxRequest.send(null);
    document.getElementById( spanid ).innerHTML = "Thank you for your comment!";
}

function sendGameswapQuery( formid, game_input_name, pagename , spanid )
{
  gametitle_input = document.getElementById( game_input_name );
  gametitle = gametitle_input.value;

  if( gametitle.length == 0 ) return;
  else if( gametitle.length == 1 ){
     gametitle_input.value = ""; 
     return;
  }

  form = document.getElementById( formid );
  var ajaxRequest;  // The variable that makes Ajax possible!
  try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
  } catch (e){
      // Internet Explorer Browsers
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
  }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
          document.getElementById( spanid ).innerHTML = ajaxRequest.responseText;
        }
    }
    platform = document.getElementById( "platform" ).value;
// TODO you need to html encode the following parameter
    ajaxRequest.open("GET", "swap_search.php?gametitle=" + gametitle + "&platform=" + platform, true);
    ajaxRequest.send(null); 
}   


function sendGamewishQuery( formid, game_input_name, pagename , spanid )
{
  gametitle_input = document.getElementById( game_input_name );
  gametitle = gametitle_input.value;

  if( gametitle.length == 0 ) return;
  else if( gametitle.length == 1 ){
     gametitle_input.value = ""; 
     return;
  }

  form = document.getElementById( formid );
  var ajaxRequest;  // The variable that makes Ajax possible!
  try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
  } catch (e){
      // Internet Explorer Browsers
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
  }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
          document.getElementById( spanid ).innerHTML = ajaxRequest.responseText;
        }
    }
    platform = document.getElementById( "wishplatform" ).value;
// TODO you need to html encode the following parameter
    ajaxRequest.open("GET", "wish_search.php?gametitle=" + gametitle + "&platform=" + platform, true);
    ajaxRequest.send(null); 
}   

function addGametowishlist( gameid, spanid )
{
  if( gameid.length == 0 ) return;

     document.getElementById( spanid ).innerHTML = "Adding game....";

  var ajaxRequest;  // The variable that makes Ajax possible!
  try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
  } catch (e){
      // Internet Explorer Browsers
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
  }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
             document.getElementById( spanid ).innerHTML = "Added!";
        }
    }
// TODO you need to html encode the following parameter
    ajaxRequest.open("GET", "addwish.php?wishid=" + gameid, true);
    ajaxRequest.send(null); 
}   

function checkUsername( username_box, spanid )
{
  if( username_box.value.length < 3 )
  {
     document.getElementById( spanid ).innerHTML = "3 or more characters"
     return;
  }

  var ajaxRequest;  // The variable that makes Ajax possible!
  try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
  } catch (e){
      // Internet Explorer Browsers
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
  }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
             document.getElementById( spanid ).innerHTML = ajaxRequest.responseText;
        }
    }
    ajaxRequest.open("GET", "checkusername.php?username=" + username_box.value, true);
    ajaxRequest.send(null);
}
function checkEmail( email_box, spanid )
{
  if( email_box.value.length < 3 )
  {
     document.getElementById( spanid ).innerHTML = "";
     return;
  }

  var ajaxRequest;  // The variable that makes Ajax possible!
  try{
      // Opera 8.0+, Firefox, Safari
      ajaxRequest = new XMLHttpRequest();
  } catch (e){
      // Internet Explorer Browsers
      try{
         ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (e) {
         try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e){
            // Something went wrong
            alert("Your browser broke!");
            return false;
        }
    }
  }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
             document.getElementById( spanid ).innerHTML = ajaxRequest.responseText;
        }
    }
    ajaxRequest.open("GET", "checkemail.php?email=" + email_box.value, true);
    ajaxRequest.send(null);
}

