function enableEmailFrom(control)
{
  if (control == 0) {
    document.sendshortlistform.radioEmail[0].checked = true;
    document.sendshortlistform.emailFromTextBox.disabled = true;
    document.sendshortlistform.emailFromDropDown.disabled = true;
  }
  if (control == 1) {
    document.sendshortlistform.emailFromTextBox.disabled = false;
    document.sendshortlistform.emailFromDropDown.disabled = true;
  }
  if (control == 2) {
    document.sendshortlistform.emailFromTextBox.disabled = true;
    document.sendshortlistform.emailFromDropDown.disabled = false;
  }
}

function doAdvSearch(page) {
  createCookie("page", page, 0);
  document.searchForm.submit();
}

function genderChanged() {
  var gender = document.getElementById("gender");
  var topMaleFrom = document.getElementById("topMaleFrom");
  var topMaleTo = document.getElementById("topMaleTo");
  var topFemaleFrom = document.getElementById("topFemaleFrom");
  var topFemaleTo = document.getElementById("topFemaleTo");

  var shoeMaleFrom = document.getElementById("shoeMaleFrom");
  var shoeMaleTo = document.getElementById("shoeMaleTo");
  var shoeFemaleFrom = document.getElementById("shoeFemaleFrom");
  var shoeFemaleTo = document.getElementById("shoeFemaleTo");

  var shoeSizeDesc = document.getElementById("shoeSizeDesc");

  if (gender) {
    switch(gender.selectedIndex)
    {
      case 0:
        topMaleFrom.disabled = true;
        topMaleTo.disabled  = true;
        topFemaleFrom.disabled  = true;
        topFemaleTo.disabled  = true;

        shoeMaleFrom.disabled = true;
        shoeMaleTo.disabled  = true;
        shoeFemaleFrom.disabled  = true;
        shoeFemaleTo.disabled  = true;

        resetDropDown("topMaleFrom");
        resetDropDown("topMaleTo");
        resetDropDown("topFemaleFrom");
        resetDropDown("topFemaleTo");

        resetDropDown("shoeMaleFrom");
        resetDropDown("shoeMaleTo");
        resetDropDown("shoeFemaleFrom");
        resetDropDown("shoeFemaleTo");

        break;
      case 1:
        topMaleFrom.style.display = 'block';
        topMaleTo.style.display = 'block';
        topMaleFrom.disabled = false;
        topMaleTo.disabled  = false;
        topFemaleFrom.style.display = 'none';
        topFemaleTo.style.display = 'none';
        shoeSizeDesc.innerHTML = 'Shoe';

        shoeMaleFrom.style.display = 'block';
        shoeMaleTo.style.display = 'block';
        shoeMaleFrom.disabled = false;
        shoeMaleTo.disabled  = false;
        shoeFemaleFrom.style.display = 'none';
        shoeFemaleTo.style.display = 'none';

        resetDropDown("topFemaleFrom");
        resetDropDown("topFemaleTo");

        resetDropDown("shoeFemaleFrom");
        resetDropDown("shoeFemaleTo");

        break;
      case 2:
        topMaleFrom.style.display = 'none';
        topMaleTo.style.display = 'none';
        topFemaleFrom.style.display = 'block';
        topFemaleTo.style.display = 'block';
        topFemaleFrom.disabled  = false;
        topFemaleTo.disabled  = false;

        shoeMaleFrom.style.display = 'none';
        shoeMaleTo.style.display = 'none';
        shoeFemaleFrom.style.display = 'block';
        shoeFemaleTo.style.display = 'block';
        shoeFemaleFrom.disabled  = false;
        shoeFemaleTo.disabled  = false;
        shoeSizeDesc.innerHTML = 'Shoe';

        resetDropDown("topMaleFrom");
        resetDropDown("topMaleTo");

        resetDropDown("shoeMaleFrom");
        resetDropDown("shoeMaleTo");

        break;
      case 3:
        topMaleFrom.disabled = true;
        topMaleTo.disabled  = true;
        topFemaleFrom.disabled  = true;
        topFemaleTo.disabled  = true;

        shoeMaleFrom.disabled = true;
        shoeMaleTo.disabled  = true;
        shoeFemaleFrom.disabled  = true;
        shoeFemaleTo.disabled  = true;

        resetDropDown("topMaleFrom");
        resetDropDown("topMaleTo");
        resetDropDown("topFemaleFrom");
        resetDropDown("topFemaleTo");

        resetDropDown("shoeMaleFrom");
        resetDropDown("shoeMaleTo");
        resetDropDown("shoeFemaleFrom");
        resetDropDown("shoeFemaleTo");
        break;
      default:

    }
  }
}

function resetSearchFields() {
  resetDropDown("gender");
  resetDropDown("ethnicity");
  resetDropDown("eyeColour");
  resetDropDown("hairColour");
  resetDropDown("nationality");
  resetDropDown("workType");

  resetTextbox("ageFrom");
  resetTextbox("ageTo");
  resetTextbox("heightFrom");
  resetTextbox("heightTo");
  resetTextbox("chestFrom");
  resetTextbox("chestTo");
  resetTextbox("hipsFrom");
  resetTextbox("hipsTo");
  resetTextbox("waistFrom");
  resetTextbox("waistTo");

  resetDropDown("topMaleFrom");
  resetDropDown("topMaleTo");
  resetDropDown("topFemaleFrom");
  resetDropDown("topFemaleTo");

  resetDropDown("shoeMaleFrom");
  resetDropDown("shoeMaleTo");
  resetDropDown("shoeFemaleFrom");
  resetDropDown("shoeFemaleTo");

  resetCheckbox("noTattoos");
  resetCheckbox("noScars");
  resetCheckbox("untreatedHair");
  resetCheckbox("ownTransport");

  resetDropDown("joined");
  resetDropDownWithDefault("office", 26323);
  resetTextbox("keywords");

  genderChanged();

  HideContent("filtersapply");
}

function resetDropDownWithDefault(id, value) {
  var ddl= document.getElementById(id);
  if (ddl)
    ddl.selectedIndex = 0;
  createCookie(id,"26324",-1);
}

function resetDropDown(id) {
  var ddl= document.getElementById(id);
  if (ddl)
    ddl.selectedIndex = 0;
  createCookie(id,"",-1);
}

function resetCheckbox(id) {
  var cb= document.getElementById(id);
  if (cb)
    cb.checked = false;
  createCookie(id,"",-1);
}

function resetTextbox(id) {
  var tb= document.getElementById(id);
  if (tb)
    tb.value = "";
  createCookie(id,"",-1);
}

function createCookie(name,value,days) {
	if (days > 0) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function deleteItemFromCookie(name,item) {
	var oldStr = readCookie(name);
      var newStr = "";
	var ca = oldStr.split(',');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		if (ca[i] != item) {
                        if (newStr != "") {
				newStr = newStr + "," + ca[i];
			}
			else
			{
				newStr = ca[i];
			}
              }
	}
	createCookie(name,newStr,1);
}

function countCookieItems(name) {
	var oldStr = readCookie(name);
        if (oldStr) {
	  var ca = oldStr.split(',');
	  return ca.length;
        }
	else {
	  return 0;
	}
}

function peoplePerson(name) {
	if (countCookieItems(name) == 1)
          return "person";
        else
         return "people";
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function setShortlistCount() {
  var status = document.getElementById("shortliststatus");
  if (status) {
    var statuspeople = document.getElementById("shortliststatuspeople");
    status.innerHTML = countCookieItems("shortlist");
    statuspeople.innerHTML = peoplePerson("shortlist");
  }
}

function setShortlistCount2() {
  var status2 = document.getElementById("shortliststatus2");
  if (status2) {
    var statuspeople2 = document.getElementById("shortliststatuspeople2");
    status2.innerHTML = countCookieItems("shortlist");
    statuspeople2.innerHTML = peoplePerson("shortlist");
  }
}

function AddRemove(d) {
var n = document.getElementById("addremoveimage"+d);
var status = document.getElementById("shortliststatus");
if (n.className == "add")
{
        var shortlist = readCookie("shortlist");
        if (shortlist) {
          deleteItemFromCookie("shortlist",d)
          shortlist = shortlist + "," + d;
          createCookie("shortlist",shortlist,0);
        }
        else
        {
          createCookie("shortlist",d,0);
        }
	n.setAttribute("src", "/images/tick.gif");
	n.setAttribute("class", "remove"); 
	n.setAttribute("className", "remove");
}
else
{
        deleteItemFromCookie("shortlist",d)
	n.setAttribute("src", "/images/add.gif");
	n.setAttribute("class", "add"); 
	n.setAttribute("className", "add"); 
}
setShortlistCount();
}

function doSelectAll(ds) {
  var memberIds = ds.split(",");
  var shortlist = readCookie("shortlist");
  var status = document.getElementById("shortliststatus");

  for(i = 0; i < (memberIds.length)-1; i++){

    var n = document.getElementById("addremoveimage"+memberIds[i]);
    if (n) {
      if (n.className == "add")
      {
        if (shortlist) {
          deleteItemFromCookie("shortlist",memberIds[i])
          shortlist = readCookie("shortlist");
          shortlist = shortlist + "," + memberIds[i];
          createCookie("shortlist",shortlist,0);
        }
        else
        {
          shortlist = memberIds[i];
          createCookie("shortlist",memberIds[i],0);
        }
	n.setAttribute("src", "/images/tick.gif");
	n.setAttribute("class", "remove"); 
	n.setAttribute("className", "remove");
      }
    }
    else {
        if (shortlist) {
          deleteItemFromCookie("shortlist",memberIds[i])
          shortlist = readCookie("shortlist");
          shortlist = shortlist + "," + memberIds[i];
          createCookie("shortlist",shortlist,0);
        }
        else
        {
          shortlist = memberIds[i];
          createCookie("shortlist",memberIds[i],0);
        }
    }
  }
  setShortlistCount();
}


function RemoveFromShortlist(d) {
var n = document.getElementById("shortlistcontainer"+d);
var status = document.getElementById("shortliststatus");
var status2 = document.getElementById("shortliststatus2");

        deleteItemFromCookie("shortlist",d)
	n.style.display = 'none'; 

setShortlistCount();
setShortlistCount2();

if (countCookieItems("shortlist") == 0) {
  var o = document.getElementById("shortlistcontainer0");
  o.style.display = 'block';
}
}

function showShortlist() {
  HideContent('sendemailcontainer');
  HideContent('sendtxtcontainer');
  ShowContent('sendshortlistcontainer');
}

function showEmail() {
  HideContent('sendshortlistcontainer');
  HideContent('sendtxtcontainer');
  ShowContent('sendemailcontainer');
}

function showTxt() {
  HideContent('sendshortlistcontainer');
  HideContent('sendemailcontainer');
  ShowContent('sendtxtcontainer');
}

function HideContent(d) {
var d = document.getElementById(d);
if (d)
d.style.display = 'none';
}

function ShowContent(d) {
var d = document.getElementById(d);
if (d)
d.style.display = 'block';
}

function ReverseDisplay(d) {
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}

