﻿var intTextBox=1;

//FUNCTION TO ADD TEXT BOX ELEMENT
function addElement()
{
intTextBox = intTextBox + 1;
var contentID = document.getElementById('content');
var newTBDiv = document.createElement('span');
newTBDiv.setAttribute('id','item'+intTextBox);
newTBDiv.innerHTML = 'گزينه: <input type="text" id="option' + intTextBox + '" name="option' + intTextBox + '" size="30"/><input type="hidden" id="optionid' + intTextBox + '" name="optionid'+ intTextBox +'" value="0"/> <a href="javascript:removeThisElement(\''+intTextBox+'\');">حذف گزينه</a><br/>';
contentID.appendChild(newTBDiv);
}

//FUNCTION TO REMOVE TEXT BOX ELEMENT
function removeElement()
{
if(intTextBox != 0)
{
var contentID = document.getElementById('content');
contentID.removeChild(document.getElementById('strText'+intTextBox));
intTextBox = intTextBox-1;
}
}

function removeThisElement(id)
{
if(intTextBox != 0)
{
var contentID = document.getElementById('content');
if(document.getElementById('optionid'+id).value != 0)
  document.getElementById('idForDel').value = document.getElementById('idForDel').value + document.getElementById('optionid'+id).value + '#';
contentID.removeChild(document.getElementById('item'+id));
intTextBox = intTextBox-1;
//alert(document.getElementById('idForDel').value);
}
}

function changeVal(){
  
if(document.getElementById('published_check').checked)
  document.getElementById('published').value = '1';
else
  document.getElementById('published').value = '0';
        

}

function changePublishedVal(){  

document.getElementById('published').value = document.getElementById('published_combo').value;
alert(document.getElementById('published').value);        

}
function voteItemsVal(){
  
  document.getElementById('items_val').value = document.getElementById('items').value;
  //alert(document.getElementById('items_val').value);      

}

function add(type) {
	//Create an input type dynamically.
	//var tr = document.createElement("tr");
	//var td1 = document.createElement("td");
	//var td2 = document.createElement("td");
	var element = document.createElement("input");
	var p = document.createElement("p");
	//var text = document.createTextNode("option");

	//Assign different attributes to the element.
	element.setAttribute("type", type);
	element.setAttribute("value", "");
	element.setAttribute("name", "option");
	element.setAttribute("id", "option");
	element.setAttribute("size", "80");

	var foo = document.getElementById("fooBar");

	//Append the element in page (in span).	
	//td1.appendChild(text);
	//td2.appendChild(element);
	//tr.appendChild(td1);
	//tr.appendChild(td2);
	
	foo.appendChild(p);
	p.appendChild(element);		
}

function fillOptionsVal(todo){
  document.getElementById('optionsval').value = "";

  var inputs = document.getElementsByTagName('input'); 

  for(var k=0;k<inputs.length;k++)
  {
	var input = inputs[k];	
	if((input.type == 'text')&&(input.name.substring(0,6) == 'option')){
	  if(input.value.replace(/\s/g,"") == ""){
	    document.getElementById('optionsval').value = "";
	    break;		
	  } 
	  document.getElementById('optionsval').value = document.getElementById('optionsval').value + input.value + '#';
	}
	if((input.type == 'hidden')&&(input.name.substring(0,8) == 'optionid')) 
	  document.getElementById('optionsidval').value = document.getElementById('optionsidval').value + input.value + '#';	
  }  

  if(todo==1){   
    document.AddForm.submit();
  }
  else if(todo==0){
    document.EditForm.submit();
	//alert(document.getElementById('optionsidval').value);
	}
	
}

