function buildStyleString(){
  //remove # 
  if(bg.substr(0,1) == "#")
    bg = bg.replace('#',"Z");
  font_color = font_color.replace('#',"Z");
  
  //border.replace(/#/g,"");
  border = border.replace('#',"Z");
  
  var str = "{";
  str += '"title":"' + title + '",';
  str += '"bg":"' + bg + '",';
  str += '"font_color":"' + font_color + '",';
  str += '"border":"' + border + '",';
  str += '"width":"' + widget_width + '",';
  str += '"height":"' + widget_height + '",';
  str += '"callback":"' + loader;
  str += '"}' + "";
  return str;
  
}


function insertCodeText(){
  var code_txt = '<div><div id="WR_' + widget_name + '">Loading widget...</div>';
  code_txt += linker + '</div>\n';
  if(extra_script != '')
    code_txt += extra_script + '\n';
  code_txt += '<sc' + 'ript type="text/javascript" src="http://www.widgetranch.com/get_widget.js"></scr' + 'ipt>\n';
  /*if(loader != '')
    code_txt += '<scr' + 'ipt type="text/javascript">addScript("' + widget_name + '", "html", ' + "'" + buildStyleString() + ", '" + loader + "' );</scri" + "pt>";
  else
    code_txt += '<scr' + 'ipt type="text/javascript">addScript("' + widget_name + '", "html", ' + "'" + buildStyleString() + "' );</scri" + "pt>";*/
  code_txt += '<scr' + 'ipt type="text/javascript">addScript("' + widget_name + '", "html", ' + "'" + buildStyleString() + "' );</scri" + "pt>";
  //addScript("wikipedia_widget3", "html", '{' + '"width":"333px"' +'}');
  //document.getElementById("widget_code_text").innerHTML = code_txt;
  document.getElementById("widget_code_text").value = code_txt;
}
/*
function getIt(form){
  if(form != undefined){
    if(form.border[0].checked != true){
      if(parseInt(form.width.value) < MIN_W + PADDING_L + PADDING_R + 2*BORDER_W){
        widget_width = MIN_W + PADDING_L + PADDING_R + 2*BORDER_W + "px";
        form.width.value = MIN_W + PADDING_L + PADDING_R + 2*BORDER_W + "";
      }
      if(widget_height != "none" && parseInt(form.height.value) < MIN_H + PADDING_T + PADDING_B + 2*BORDER_W){
        widget_height = MIN_H + PADDING_T + PADDING_B + 2*BORDER_W + "px";
        form.height.value = MIN_H + PADDING_T + PADDING_B + 2*BORDER_W + "";
      }
    }
    else{
      if(parseInt(form.width.value) < MIN_W){
        widget_width = MIN_W + "px";
        form.width.value = MIN_W + "";
      }
      if(parseInt(widget_height != "none" && form.height.value) < MIN_H){
        widget_height = MIN_H + "px";
        form.height.value = MIN_H + "";
      }
    }
  }
  addScript(widget_name, "html", buildStyleString() );
  insertCodeText();
}
*/
function getIt(form){
  if(form != undefined){
    if(border != "none" && parseInt(form.width.value) < MIN_W + PADDING_L + PADDING_R + 2*BORDER_W){
      widget_width = MIN_W + PADDING_L + PADDING_R + 2*BORDER_W + "px";
      form.width.value = MIN_W + PADDING_L + PADDING_R + 2*BORDER_W + "";
    }
    else if(parseInt(form.width.value) < MIN_W){
      widget_width = MIN_W + "px";
      form.width.value = MIN_W + "";
    }
      
    
    if(widget_height != "none" && parseInt(form.height.value) < MIN_H ){
      widget_height = MIN_H + "px";
      form.height.value = MIN_H + "";
    }
  }
  addScript(widget_name, "html", buildStyleString() );
  insertCodeText();
}
/*function borderOn(){
  border = "1px solid black";
  document.getElementById("widget_border").style.border = "1px solid #2D4516";
  insertCodeText();
}*/

function borderOn(form){
  document.getElementById("border_color").disabled = false;
  document.getElementById("px1border").disabled = false;
  document.getElementById("px2border").disabled = false;
  document.getElementById("px3border").disabled = false;
  border_color = form.border_color.value;
  if(form.border_w[0].checked)
    BORDER_W = 1;
  else if(form.border_w[1].checked)
    BORDER_W = 2;
  else
    BORDER_W = 3;
  border = BORDER_W + "px " + "solid " + border_color;
  getIt(form);
}

function borderOff(form){
  document.getElementById("border_color").disabled = true;
  document.getElementById("px1border").disabled = true;
  document.getElementById("px2border").disabled = true;
  document.getElementById("px3border").disabled = true;
  BORDER_W = 0;
  border = "none";
  //document.getElementById("widget_border").style.border = "none";
  getIt(form);
}

function fontColorOn(form){
  font_color = form.font_color.value;
  document.getElementById("font_color").disabled = false;
  getIt();  
}

function fontColorOff(form){
  font_color = "none";
  document.getElementById("font_color").disabled = true;
  getIt();  
}

function titleOn(){
  title = "1";
  getIt();
  document.getElementById("widget_display_container").style.paddingTop = "3px";
}

function titleOff(){
  title = "0";
  //document.getElementById("widget_border").style.border = "none";
  getIt();
  document.getElementById("widget_display_container").style.paddingTop = "20px";
}


function updateWidgetWidth(form){
  //document.getElementById("widget_border").style.width = form.width.value + "px";
  widget_width = form.width.value + "px";
  /*
  if(form.border[0].checked == true){
    if(parseInt(form.width.value) < MIN_W + 2*PADDING + 2*BORDER_W){
      widget_width = MIN_W + 2*PADDING + 2*BORDER_W + "px";
      form.width.value = MIN_W + 2*PADDING + 2*BORDER_W + "";
    }
  }
  else{
    if(parseInt(form.width.value) < MIN_W){
      widget_width = MIN_W + "px";
      form.width.value = MIN_W + "";
    }
  }
  */
  getIt(form)
}
function updateWidgetHeight(form){
  //document.getElementById("widget_border").style.height = form.height.value + "px";
  widget_height = form.height.value + "px";
  getIt(form);
}

function backgroundToColor(form){
  document.getElementById("bg_color").disabled = false;
  bg = form.bg_color.value;
  getIt();
}

function backgroundToTransparent(){
  document.getElementById("bg_color").disabled = true;
  bg = "transparent";
  getIt();
}



