function deletelang(id)
{
var tr = document.getElementById('lang'+id);
tr.parentNode.removeChild(tr);
}

function addlanguage()
{
var tbl = document.getElementById('Languages');
var lastRow = tbl.rows.length;
 
var LangTitle = document.getElementById('addlang');
var i = 0;
var k = 0;
var b = 1;
var key = 0;

if(LangTitle.options[LangTitle.selectedIndex].value=="") return;

for (i=0; i<LangTitle.options.length; i++)
  if (LangTitle.options[i].selected){ 
      for (k=0; k<lastRow; k++)
      {
	if (LangTitle.options[i].value == tbl.rows[k].cells[1].childNodes[0].value) {b = 0; break;}
      }
   break;
  }

if (b!=0) { 
 var row = tbl.insertRow(lastRow);
 row.setAttribute("id", "lang"+lastRow);
 var cell1 = row.insertCell(0);
 cell1.width = 160;
 cell1.innerHTML = LangTitle.options[i].innerHTML;
 key = i; 
 var cell2 = row.insertCell(1);
 cell2.innerHTML = "<input type=\"hidden\" name=\"Languages[]\" value=\""+LangTitle.options[key].value+"\"><input type=\"image\" class=\"delbutton\" src=\"/img/delbtn.gif\" value=\"Delete\" onclick=\"deletelang('"+(lastRow)+"');\">";
}

}

function FillRegionOnLoad(){
 if(document.forms['joinForm'].SelectedRegion.value != '')
  {
   var Region = document.forms['joinForm'].RegionToGo;
   Region.selectedIndex = document.forms['joinForm'].SelectedRegion.value;
   FillCountryOnLoad();
  }
}

function FillCountryOnLoad(){
if(document.forms['joinForm'].SelectedCountry.value != '') {
   var Country = document.forms['joinForm'].CountryToGo;
   Country.selectedIndex = document.forms['joinForm'].SelectedCountry.value;
   handleOnChangeCountry(Country);
   FillStateOnLoad();
   }
}

function FillStateOnLoad(){
  if(document.forms['joinForm'].SelectedState.value != '') {
   var State = document.forms['joinForm'].StateToGo;
   State.selectedIndex = document.forms['joinForm'].SelectedState.value;
  }
}

function FillPropertyCountryOnLoad(){
  var Country = document.forms['joinForm'].PropertyCountry;
  Country.selectedIndex = document.forms['joinForm'].SelectedPropertyCountry.value;
}

function FillPropertyStateOnLoad(){
  var Country = document.forms['joinForm'].PropertyCountry;
  handleOnChangePropertyCountry(Country);
}

function FillCalendar(calName, date){
var cal = document.getElementById(calName);
cal.value = date;
}

function createDirName(){
 if (!checkEmail()) {alert('Please enter your Email address'); return false;}
 else
 {
  if (document.forms['uploadForm'].filename.value!="") document.forms['uploadForm'].dirname.value=document.forms['joinForm'].Email.value;  else return false;
  return true;
 }
}

function checkEmail(){
 if( document.forms[0].Email.value.search(/[\-\_0-9a-zA-Z]+\@[\-\_0-9a-zA-Z]+\.[a-zA-Z]+/) == -1 )
   return false; 
 else return true; 
}

function setVisibleDate1(frm) {
  var el = document.getElementById('date1');
  el.style.display = (frm.date1Check.options.selectedIndex==1) ? '': 'none';
  image1 = new Image();
  image1.src = "http://kapranchikov.com/ft/img/join_disabled.png";
}

function setVisibleDate2(frm) {
  var el = document.getElementById('date2');
  el.style.display = (frm.date2Check.options.selectedIndex==1) ? '': 'none';
}

function checkFields(form)
 {
 var el;
 if(!form.agreement.checked) {alert("Please read Terms of Use and indicate that you agree with them"); return false;}
 disablesubmitbtn();
 return true;
  
 }


function handleOnChangeRegion(dd1)
{
  var idx = dd1.selectedIndex;
  var val = dd1[idx].value;
  var par = document.forms["joinForm"];
  var parelmts = par.elements;
  var countrysel = parelmts["CountryToGo"];
  var region = val;
  if (region != "Any region")
  {
   Http.get({
		url: "countries.php?Region=" + region,
		callback: fillCountries,
		cache: Http.Cache.GetNoCache
	}, [countrysel]);
  }else
  {
   Http.get({
		url: "countries.php?Region=" + "all",
		callback: fillCountries,
		cache: Http.Cache.GetNoCache
	}, [countrysel]);
   
  }
}
function fillCountries(xmlreply, elmt)
{
  if (xmlreply.status == Http.Status.OK)
  {
   var response = xmlreply.responseText;
   var arr = response.split("|");
   elmt.length = arr.length;
   var countryinfo;
   var o = 0;
   for (o=1; o < arr.length; o++)
   {
        countryinfo = arr[o].split(">"); 
	elmt[o].text = countryinfo[1];
	elmt[o].value = countryinfo[0];
   }
  }
  else
  {

   alert("Cannot handle the Ajax call. - "+result.status.toString());
  }
}

function handleOnChangeCountry(dd1)
{
  var idx = dd1.selectedIndex;
  var val = dd1[idx].value;
  var par = document.forms["joinForm"];
  var parelmts = par.elements;
  var prezsel = parelmts["StateToGo"];
  var country = val;
  var statetr = document.getElementById('States');
  if (country != "Any country")
  {
   Http.get({
		url: "states.php?Country=" + country,
		callback: fillStates,
		cache: Http.GetNoCache
	}, [prezsel]);
  }else
  {
   //statetr.style.display = 'none';
   Http.get({
		url: "states.php?Country=" + "all",
		callback: fillStates,
		cache: Http.GetNoCache
	}, [prezsel]);
   
  }
}
function fillStates(xmlreply, elmt)
{
  if (xmlreply.status == Http.Status.OK)
  {
   var response = xmlreply.responseText;
   var arr = response.split("|");
   elmt.length = 1;
   elmt.length = arr.length;
   var countryinfo;
   var tr =  document.getElementById("States");
   if (arr.length > 1 ) tr.style.display = ''; else tr.style.display = 'none';
   for (o=1; o < arr.length; o++)
   {
        countryinfo = arr[o].split(">"); 
	elmt[o].text = countryinfo[1];
	elmt[o].value = countryinfo[0];
   }
  SelectedState = document.forms['joinForm'].SelectedState.value;
  var i=0;
  for (i = 0; i<elmt.options.length; i++ )
  {
   if (SelectedState == elmt.options[i].value)
   {
    elmt.selectedIndex = i;
    break;
   }
  }
  }
  else
  {
   alert("Cannot handle the Ajax call.");
  }
}

function handleOnChangePropertyCountry(dd1)
{
  var idx = dd1.selectedIndex;
  var val = dd1[idx].value;
  var par = document.forms["joinForm"];
  var parelmts = par.elements;
  var prezsel = parelmts["PropertyState"];
  var country = val;
  var statetr = document.getElementById('PropertyStates');
  if (country != "Not selected")
  {
   Http.get({
		url: "states.php?Country=" + country,
		callback: fillPropertyStates,
		cache: Http.GetNoCache
	}, [prezsel]);
  }else
  {
   statetr.style.display = 'none';
   Http.get({
		url: "states.php?Country=" + "all",
		callback: fillPropertyStates,
		cache: Http.GetNoCache
	}, [prezsel]);
   
  }
}
function fillPropertyStates(xmlreply, elmt)
{
  if (xmlreply.status == Http.Status.OK)
  {
   var response = xmlreply.responseText;
   var arr = response.split("|");
   elmt.length = 1;
   elmt.length = arr.length;
   var countryinfo;
   var tr =  document.getElementById('PropertyState');
   if (arr.length > 1 ) { document.joinForm.PropertyState.disabled =false;  }else document.joinForm.PropertyState.disabled =true;
   for (o=1; o < arr.length; o++)
   {
        countryinfo = arr[o].split(">"); 
	elmt[o].text = countryinfo[1];
	elmt[o].value = countryinfo[0];
   }
  SelectedState = document.forms['joinForm'].SelectedPropertyState.value;
  var i=0;
  for (i = 0; i<elmt.options.length; i++ )
  {
   if (SelectedState == elmt.options[i].value)
   {
    elmt.selectedIndex = i;
    break;
   }
  }
  }
  else
  {
   alert("Cannot handle the Ajax call.");
  }
}

function disablesubmitbtn()
{
 btnObj = document.getElementById('submitbtn');
 btnObj.parentNode.removeChild(btnObj);
 var newImg = document.createElement('img');
 newImg.src = "http://kapranchikov.com/ft/img/join_disabled.png";
 document.joinForm.appendChild(newImg);
}