function playVideo(url) {
  var width = 980;    var height = 570;    var winleft = (screen.width - width) / 2;    var winUp = (screen.height - height) / 2;    window.open(url,"videoplayer","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width="+width+", height="+height+", left="+winleft+", top="+winUp);
}

function openWindow(url, w, h) {
  var width = w;    var height = h;    var winleft = (screen.width - width) / 2;    var winUp = (screen.height - height) / 2;    window.open(url,"videoplayer","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, copyhistory=no, width="+width+", height="+height+", left="+winleft+", top="+winUp);
}

function isEmailValid(address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return reg.test(address);
}

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}

function g(val) {
	return document.getElementById(val);
}

function getFormParams(form) {
  var params = new Array();

  for (var i=0; i<form.elements.length; i++) {
    var e = form.elements[i];
    if (e.name.length == 0) continue;
    if (e.type == "checkbox" && !e.checked) continue;

    params.push(encodeURIComponent(e.name) + "=" + encodeURIComponent(e.value));
  }

  return params;
}

function asyncReq(url, disableId, enableId, params) {
  xmlhttp.open("POST", url, true);
  xmlhttp.onreadystatechange=asyncReqCB;

  if (!params) {
    params = [];
  }

  xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  xmlhttp.send(params.join("&"))

  var disable = g(disableId);
  if (disable) {
    disable.style.display = 'none';
  }

  var enable = g(enableId);
  if (enable) {
    enable.style.display = '';
  }
}

function asyncReqCB() {
  if (xmlhttp.readyState==4) {
    if (xmlhttp.responseXML != null) {
      var de = xmlhttp.responseXML.documentElement;
      var resultNode = de.getElementsByTagName("status");

      if (resultNode.length == 1 && resultNode[0].firstChild.data == "ok") {
        return;
      }
    }

    alert("There was a problem with your request. Please refresh the page and try again.");
    return;
  }
}

function $G(v,o) { return((typeof(o)=='object'?o:document).getElementById(v)); }
function $S(o) { return((typeof(o)=='object'?o:$G(o)).style); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function abPos(o) { var o=(typeof(o)=='object'?o:$G(o)), z={X:0,Y:0}; while(o!=null) { z.X+=o.offsetLeft; z.Y+=o.offsetTop; o=o.offsetParent; }; return(z); }
function XY(e,v) { var o=agent('msie')?{'X':event.clientX+document.documentElement.scrollLeft,'Y':event.clientY+document.documentElement.scrollTop}:{'X':e.pageX,'Y':e.pageY}; return(v?o[v]:o); }

function onStarMove(e,o) {
  var p=abPos(o), x=XY(e), oX=x.X-p.X, oY=x.Y-p.Y;

  var n = o.id.substr(4);
  if (hasVoted(n)) return;

  if (oX < 0) oX = 0;
  if (oX > 84) oX = 84;

  var w = Math.floor(((oX+17) / 85)*5)*17;
  $S('starCur'+n).width=w+'px';
};

function onStarOut(e,o) {
  starRevert(o);
}

function onStarUpdate(url, disableId, enableId, o) {
  var n = o.id.substr(4);
  if (hasVoted(n)) return;
  var w = $S('starCur'+n).width;
  w = w.substr(0, w.length - 2);
  var score = Math.floor((w / 85) * 100);
  $G('starCur'+n).title = 'Thank you for voting!';
  asyncReq(url, disableId, enableId, ['rating='+score]);
};

function hasVoted(n) {
  return $G('starCur'+n).title == 'Thank you for voting!';
}

function starRevert(o) {
  var n = o.id.substr(4);
  if (hasVoted(n)) return;
  var v = $G('starCur'+n).title;
  $S('starCur'+n).width=Math.round(v*84/100)+'px';
};


function submitComment() {
  var form = g('comment_form');
  if (!form) return;

  var m = g('comment_msg');
  if (!m) return;

  if (m.value.length == 0) {
    alert("Please enter a comment.");
    return;
  }
  if (m.value.length > 255) {
    alert("Please limit your comment to 255 characters.");
    return;
  }

  var params = getFormParams(form);
  asyncReq(form.action, 'comment_submit', 'comment_thankyou', params);
}


function searchSubmit() {
  var q = g('search');

  var type = g('searchtype');

  var url = '';

  if (type.value == "model") {
    url += "?page=themen";
  } else if (type.value == "video") {
    url += "?page=videos";
  } else if (type.value == "photoset") {
    url += "?page=photos";
  }

  var sort = g('sort');

  if (sort.value == "rated") {
    url += "&sort=ratingavgscore";
  } else if(sort.value == "release") {
    if (type.value == "model") {
      alert("Release date is only for content.");
      return false;
    }
    url += "&sort=release_date";
  } else if(sort.value == "views") {
    url += "&sort=viewcount";
  } else if(sort.value == "favorites") {
    url += "&sort=favoritecount";
  } else if(sort.value == "comments") {
    url += "&sort=commentcount";
  } else if(sort.value == "az") {
    if (type.value == "model") {
      url += "&sort=username&order=asc";
    } else {
      url += "&sort=title&order=asc";
    }
  } else if(sort.value == "za") {
    if (type.value == "model") {
      url += "&sort=username&order=desc";
    } else {
      url += "&sort=title&order=desc";
    }
  }

  if (q) {
    url += "&q="+encodeURIComponent(q.value);
  }

  document.location = url;

  return false;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_setTextOfTextfield(objId,x,newText) { //v9.0
  with (document){ if (getElementById){
    var obj = getElementById(objId);} if (obj) obj.value = newText;
  }
}

function loginPrompt() {
  alert('Become a member or login with your account!');
}

function upgradePrompt() {
  alert('Upgrade to a premium membership, or buy purchase credits from the Specials page.');
}

function signupWidget() {
  var form = g('form1');
  if (!form) {
    alert("Missing signup form.");
    return;
  }

  var username = g('username');
  if (!username) {
    alert("Missing username on signup form.");
    return;
  }

  var password = g('password');
  if (!password ) {
    alert("Missing password on signup form.");
    return;
  }

  var email = g('email');
  if (!username) {
    alert("Missing email on signup form.");
    return;
  }

  if (username.value.length < 6 || username.value.length > 16) {
    alert("Please enter a username between 6 and 16 characters.");
    return;
  }


  if (password.value.length < 6 || password.value.length > 16) {
    alert("Please enter a password between 6 and 16 characters.");
    return;
  }

  if (!isEmailValid(email.value)) {
    alert("Please enter a valid email address.");
    return;
  }

  var i = 0;
  var vopt = '';
  while(true) {
    var el = g('vopt' + i);
    if (!el) break;

    if (el.checked) {
      vopt = el.value;
      break;
    }
    i++;
  }

  if (vopt.length == 0) {
    alert("Unable to get viewing option.");
    return;
  }

  form.action += '&continue='+encodeURIComponent('http://www.manifestmen.com/?page=specials&vopt=' + vopt);
  form.submit();
}
