var form;
var tb;
var tr;
var dowhat;
var product_id;
var id;
var rowid;
var btn;

function save_change(value,pro_id)
{
    showmsg(1,"&nbsp;");
    dowhat = "save";
    id     = value;
    setform();
    form.s_product_id.value = pro_id;
    btn = document.getElementById("save_" + id);
    do_ajax();
}

function del_domain(value,pro_id)
{
   showmsg(1,"&nbsp;");
   if(!(confirm("delete ?")))
   { return; }
   dowhat = "del";
   id     = value;
   setform();
   form.s_product_id.value = pro_id;
   btn = document.getElementById("del_" + id);
   do_ajax();
}

function add_domain()
{
   showmsg(1,"&nbsp;");
   //if(!(confirm("I confirm that I have read the User Agreement and that I am  the rightful owner of this Domain and/ or website. I understand that if I list my domains for sale, ODNAM will actively market my domains, and that in the case of a successful sale I will pay a commission of 10% of the sales price (min. $50 for .com/.net/.org, for other minimums please see price list), which includes ODNAM's secure escrow service.")))
   if(!(confirm("I confirm that I have read the User Agreement and that I'm the rightful owner of this domain. I understand that if I list my domains for sale,I will pay a commission of 10% of the sale price, which includes ODNAM's secure escrow service.")))
   { return; }
   dowhat = "add";
   setform();
   btn = document.getElementById("add_new");
   do_ajax();
}

function do_ajax()
{
   btn.value    = "Wait ";
   btn.disabled = true;
   var pars = $("form1").serialize(true);
   var myAjax = new Ajax.Request("do_ajax.php",{method: 'post',parameters: pars,onComplete: showResponse});

}



function showResponse(originalRequest)
{
   var result = String(originalRequest.responseText).trim();
   var index  = tr.rowIndex; 

   if ( "del" == dowhat)
   {
      btn.value ="delete";
      btn.disabled = false;
   }
   else if ( "save" == dowhat)
   {
      btn.value ="Save";
      btn.disabled = false;
   }

   else if ( "add" == dowhat && isNaN(originalRequest.responseText))
   {
       btn.value ="Add";
       btn.disabled = false;
   }

   if( ("1" != result && "add" != dowhat) | (("add" == dowhat) && (isNaN(originalRequest.responseText))) )
   {
      showmsg("0",result);
      return;
   }

   if( "del" == dowhat)
   {
       delrow();
       showmsg("1","delete domain sucess");
       return;
    } 

   if( "save" == dowhat)
   {
       tr.cells[5].innerHTML = '<input type="button" name="save[]" value="Save" class="button_del" id="save_' + index +
                              '" onclick="save_change(\'' + index + '\',\'' + form.s_product_id.value + '\');" />'
       showmsg("1","save domain sucess");
       return;
   }

   if( "add" == dowhat)
   {
       product_id = result;
       tr.id      = "tr_" + index;
       document.getElementById("new_domain_name").readonly = true;;
       document.getElementById("new_domain_name").id       = "domain_name_" + index;
       document.getElementById("new_exchage_type").id      = "exchage_type_" + index;
       document.getElementById("new_start_price").id       = "start_price_" + index; 
       document.getElementById("new_category").id          = "category_" + index; 
       document.getElementById("new_details").id           = "details_" + index;
     
       tr.cells[5].innerHTML = '<input type="button" name="save[]" value="Save" class="button_del" id="save_' + index +
                              '" onclick="save_change(\'' + index + '\',\'' + product_id + '\');" />' 
       tr.cells[6].innerHTML = '<input type="button" name="del[]" value="Delete" class="button_del" id="del_' + index +
                              '" onclick="del_domain(\'' + index + '\',\'' + product_id + '\');" />'
       newrow();
       showmsg("1","add domain sucess");
       return;
   }
}


//提示信息
function showmsg(type,msg)
{
   if(msg.indexOf("login.php")!=-1) {
      window.location.href("login.php");
      return;
   }
   if( "1" == type) return;
   alert(msg);
   return;

   var divmsg  = document.getElementById("ajax_result");
   var color   = "red";
   if( "1" == type)
   {
        color  = "blue";
   }

   divmsg.innerHTML = '<span style="color:' + color + ';">' + msg + '</span>';
}


//设置form的值
function setform()
{
    form  = document.getElementById("form1");
    tb    = document.getElementById("content_table");
    rowid = "tr_" + id;
    if( "save" == dowhat)
   {
       form.s_dowhat.value       = "save";
       form.s_domain_name.value  = document.getElementById("domain_name_" + id).value;
       form.s_exchage_type.value = document.getElementById("exchage_type_" + id).value;
       form.s_start_price.value  = document.getElementById("start_price_" + id).value;
       form.s_category.value     = document.getElementById("category_" + id).value;
       form.s_details.value      = document.getElementById("details_" + id).value;
   }
   else if( "del" == dowhat)
   {
       form.s_dowhat.value       = "del";
   }
   else if( "add" == dowhat)
   {
       form.s_dowhat.value       = "save";
       form.s_domain_name.value  = document.getElementById("new_domain_name").value;
       form.s_exchage_type.value = document.getElementById("new_exchage_type").value;
       form.s_start_price.value  = document.getElementById("new_start_price").value;
       form.s_category.value     = document.getElementById("new_category").value;
       form.s_details.value      = document.getElementById("new_details").value;

       rowid = "newdomain";
       form.s_dowhat.value       = "add";
   }
   tr = document.getElementById(rowid);
}

function delrow()
{
   trIndex = tr.rowIndex;
   tb.deleteRow(trIndex);
}

function newrow()
{

   var newtr       = tb.insertRow(tr.rowIndex+1);
   newtr.id        = "newdomain";
   newtr.className = "blue";
   if ( "blue" == tr.className )
   { newtr.className = "while"; }

   var td0= newtr.insertCell(0);
   var td1= newtr.insertCell(1);
   var td2= newtr.insertCell(2);
   var td3= newtr.insertCell(3);
   var td4= newtr.insertCell(4);
   var td5= newtr.insertCell(5);
   var td6= newtr.insertCell(6);

   td0.align = "center";
   td1.align = "center";
   td2.align = "center";
   td3.align = "center";
   td4.align = "center";
   td5.align = "center";
   td6.align = "center";

   td0.innerHTML = '<input type="text" class="input145" id="new_domain_name" />';
   td1.innerHTML =' <select style="width:86px;" id="new_exchage_type"><option value="fixed" selected>Fixed</option>' +
                 '<option value="auction">Auction</option></select>';
   td2.innerHTML ='<input type="text" style="width:100px;" value="10" id="new_start_price"/>';
   td3.innerHTML ='<select style="width:86px; " id="new_category">' +
                 '<option value="1" selected>Adult</option>' +
                 '<option value="2" >Business/Law</option>' +
                 '<option value="3" >Careers/Education</option>' +
                 '<option value="4" >Consumer Goods</option>' +
                 '<option value="5" >Health</option>' +
                 '<option value="6" >Internet</option>' +
                 '<option value="7" >Leisure/Culture</option>' +
                 '<option value="8" >Nature</option>' +
                 '<option value="9" >Science</option>' +
                 '<option value="10" >Society</option>' +
                 '<option value="11" >Sports</option>' +
                 '<option value="12" >Technology</option>' +
                 '<option value="13" >Travel/Traffic</option>' +
                 '<option value="14" >Other</option>' +
                 '</select>';
   td4.innerHTML='<input type="text" class="input67" id="new_details">';
   td5.innerHTML='';
   td6.innerHTML='<input type="button" id="add_new" value="Add" class="button_del" onclick="add_domain()" />';
}

