// Some globals
var _result;
var _filename;
var _datadir;
var _downloadname;

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

function pageInit() {
//updateBGColour();
}

function bgPreview() {
 preview_url="preview.php?image="+_datadir+"/"+_filename+".gif";
 window.location=preview_url;
}

function GetCodes() {
 
codes_url="getcodes.php?result="+_result+"&ref="+_datadir+"/"+_filename+".gif";
window.location=codes_url;
}

/*
// Set the form bgcolour
function updateBGColour() {
var bgcolour_sel=document.getElementById('gen_fields').bgcolour_sel;

// bgcolour_sel dropdown
len = bgcolour_sel.length;
for (i = 0; i <len; i++) {
if (bgcolour_sel[i].selected) {
bgcolour_val = bgcolour_sel[i].value;
}
}

document.getElementById('gen_fields').bgcolour.value=bgcolour_val;
}
*/

function setStyle(style) {
// Set the form style
document.getElementById('gen_fields').font_style.value=style;
ProcessGen();
}

function ProcessGenCallback() {
 if(Ajax.CheckReadyState(Ajax.request))
 {
//  alert("callback done");
  var response = Ajax.request.responseXML.documentElement;
  var _image = response.getElementsByTagName('image');
  _result = response.getElementsByTagName('result')[0].firstChild.data;
  _filename = response.getElementsByTagName('filename')[0].firstChild.data;
  _datadir = response.getElementsByTagName('datadir')[0].firstChild.data;
  _downloadname = response.getElementsByTagName('downloadname')[0].firstChild.data;
  _result=RTrim(_result);
  _downloadname=RTrim(_downloadname);
/*  var html_output="<img src='"+_datadir+"/"+_filename+".gif' onload='ShowOutput()'>";*/
  var html_output="<img src='http://static1.pookatoo.com/"+_datadir+"/"+_filename+".gif' onload='ShowOutput()'>";
  document.getElementById('hidden_output').innerHTML=html_output;
/*
  alert("html_output is "+html_output);
  alert("result is "+_result);
  alert("filename is "+_filename);
*/
  //window.location="ecard-sent.php";
 }
}

function ShowOutput() {

  var html_output="<table height='100%'><tr valign='center'><td><img src='http://static1.pookatoo.com/"+_datadir+"/"+_filename+".gif'></td></tr></table>";
/*
  var html_output="<table height='100%'><tr valign='center'><td><img src='http://pookatoo.com/"+_datadir+"/"+_filename+".gif'></td></tr></table>";
*/
  document.getElementById('hidden_output').innerHTML="";
  document.getElementById('output').innerHTML=html_output;

  // Write the embedding code
  var html_output="";
  if (_result=="ok") {
   html_output="<textarea id='embed_textarea' onClick='this.focus();this.select()'>";

   html_output+="<a href='http://pookatoo.com/'>";
   html_output+="<img src='http://static1.pookatoo.com/"+_datadir+"/"+_filename+".gif' border='0' alt='pookatoo.com image'>";
   html_output+="</a>";

//html_output+="Sorry! Link codes are disabled right now until the site move is complete!";
   html_output+="</textarea>";
  } 
  else if (_result=="toobigtolink") {
   html_output="<textarea id='embed_textarea'>";
   html_output+="\n\nImage is too big to link to - please download instead, or try a smaller font size or non-animated version .";
   html_output+="</textarea>";
  }
  else if (_result=="busy") {
   html_output="<textarea id='embed_textarea'>";
   html_output+="Sorry, the server is busy at the moment- please try again in a few minutes.";
   html_output+="</textarea>";
  }
//  document.getElementById('embed_code').innerHTML=html_output;

  html_output="<form name='download_fields' action='download.php' method='POST'>";
  html_output+="<input type='hidden' name='filename' id='filename' value='";
  html_output+=_filename;
  html_output+="'>";
  html_output+="<input type='hidden' name='downloadname' id='downloadname' value='";
  html_output+=_downloadname;
  html_output+="'>";
  html_output+="<input type='submit' value='Get\r\nWeb code' id='codesbutton' onclick='GetCodes(); return false;'>";
  html_output+="<input type='submit' value='Download\r\n Image' ";
  html_output+="id='downloadbutton'>";
  html_output+="<input type='submit' value='Preview\r\nBackground' id='previewbutton' onclick='bgPreview(); return false;'> ";
  html_output+="</form>";

  document.getElementById('embed_code_text').innerHTML=html_output;
}

function ProcessGen() {
var font_style = document.getElementById('gen_fields').font_style;
var font_size = document.getElementById('gen_fields').font_size;
var bgcolour = document.getElementById('gen_fields').color0.value;
var text = document.getElementById('gen_fields').text.value;
//var glow_halo = document.getElementById('gen_fields').glow_halo.checked;
var non_trans = document.getElementById('gen_fields').non_trans.checked;
var anim_type = document.getElementById('gen_fields').anim_type;

// fontstyle dropdown
len = font_style.length
for (i = 0; i <len; i++) {
if (font_style[i].selected) {
font_style_val = font_style[i].value
}
}

// size radio buttons
len = font_size.length
for (i = 0; i <len; i++) {
if (font_size[i].checked) {
font_size_val = font_size[i].value
}
}

// anim type radio buttons
len = anim_type.length
for (i = 0; i <len; i++) {
if (anim_type[i].checked) {
anim_type_val = anim_type[i].value
}
}

glow_halo="true";
var reqstring = 'gentext.php?text='+encodeURIComponent(text);
reqstring += '&font_style='+encodeURIComponent(font_style_val);
reqstring += '&font_size='+font_size_val;
reqstring += '&bgcolour='+encodeURIComponent(bgcolour);
reqstring += '&glow_halo='+glow_halo;
reqstring += '&non_trans='+non_trans;
reqstring += '&anim_type='+anim_type_val;

//alert("calling "+reqstring);
// Ajax animation
var html_output="<img src='ajax-loader2.gif' style='margin:40px 0 0 0'>";
document.getElementById('output').innerHTML=html_output;

Ajax.Request(reqstring, ProcessGenCallback);
}
