function URLEncode(str) {
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";
	var plaintext = str;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";	// x-www-urlencoded, rather than 
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert("Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted.");
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	}
	return encoded;
}

function chkJoin() {
  ff = eval("document.joinform");
  if (ff.full_name.value == "") {
    alert("Please enter your full name.");
    ff.full_name.focus();
    return false;
  }
  else if (ff.email.value.indexOf('@') <= 0) {
    alert("Please enter a valid email address.");
    ff.email.focus();
    ff.email.select();
    return false;
  }
  else if (ff.user.value == "") {
    alert("Please enter a User ID.");
    ff.user.focus();
    return false;
  }
  else if (ff.pass.value == "") {
    alert("Please enter a password.");
    ff.pass.focus();
    return false;
  }
  else if (ff.pass_check.value == "") {
    alert("Please repeat your password.");
    ff.pass_check.focus();
    return false;
  }
  else if (ff.pass.value != ff.pass_check.value) {
    alert("Your passwords did not match, please re-enter");
    ff.pass_check.value = "";
    ff.pass.focus();
    ff.pass.select();
    return false;
  }
  else if (!ff.agree.checked) {
    alert("Please check that you agree to the terms and conditions.");
    ff.agree.focus();
    return false;
  }
  else if (ff.recaptcha_response_field.value == "") {
    alert("Please enter the words you see.");
    ff.recaptcha_response_field.focus();
    return false;
  }
  else {
    ff.subtn.value = "Saving...";
    ff.subtn.disabled = true;
  	return true;
  }
}

function chkInfoSave() {
  ff = eval("document.joinform");
  if (ff.full_name.value == "") {
    alert("Please enter your full name.");
    ff.full_name.focus();
    return false;
  }
  else if (ff.email.value.indexOf('@') <= 0) {
    alert("Please enter a valid email address.");
    ff.email.focus();
    ff.email.select();
    return false;
  }
  else if (ff.user.value == "") {
    alert("Please enter a User ID.");
    ff.user.focus();
    return false;
  }
  else if (ff.pass.value == "") {
    alert("Please enter a password.");
    ff.pass.focus();
    return false;
  }
  else if (ff.pass_check.value == "") {
    alert("Please repeat your password.");
    ff.pass_check.focus();
    return false;
  }
  else if (ff.pass.value != ff.pass_check.value) {
    alert("Your passwords did not match, please re-enter");
    ff.pass_check.value = "";
    ff.pass.focus();
    ff.pass.select();
    return false;
  }
  else {
    ff.subtn.value = "Saving...";
    ff.subtn.disabled = true;
  	return true;
  }
}


function chkLogin() {
  ff = eval("document.mainlogin");
  if (ff.login_id.value == "") {
    alert("Please enter your screen name or email address.");
    ff.login_id.focus();
    return false;
  }
  else if (ff.pass.value == "") {
    alert("Please enter your password.");
    ff.pass.focus();
    return false;
  }
  else {
    ff.subtn.value = "Signing in...";
    ff.subtn.disabled = true;
    return true;
  }
}

function chkForget() {
  ff = eval("document.forgetform");
  if (ff.login_id.value == "") {
    alert("Please enter your screen name or email address.");
    ff.login_id.focus();
    return false;
  }
  else {
    ff.subtn.disabled = true;
    return true;
  }
}

function slim_pop(img_file,img_txt) {
  Slimbox.open(img_file, img_txt);
}

function rn() {
  return Math.floor(Math.random()*9999999);
}

function init_photo_sort() {
 new Sortables('photos', {
	constrain: false,
	revert: true,
	clone: true,
	opacity: .5,
	handle: '.handle',
	onStart: function() {
		this.clone.setStyles({
			'width':'90px', 
			'height':'90px',
			'background-color':'#FFF',
			'border':'1px solid #CCC',
			'list-style':'none'
		});
		if (Browser.Engine.trident) {
			this.clone.setStyles({'font':'8.5pt Tahoma','font-weight':'bold'});
			this.clone.set('html','Click to Drop');
		}
	},
	onComplete: function() {
		str = this.serialize();
	    var myAjax = new Request.JSON({
	    	url:	'/ajax/save_sort_order',
	    	data:	'&tbl=photos&str='+str,
	    	onSuccess:	function(arsp, html) {
	    	  // null
	    	},
	    	onFailure: 	function() {
	    	  alert('Operation Failed!')
	    	}
	    }).get();
	}
  });
}

function delete_photo(id,thru) {
  var kk = confirm("Are you sure you want to delete this photo?");
  if (kk) {
    new Request({
      url: '/ajax/delete_photo?rn='+rn()+'&photo_id='+id,
      onSuccess: function(txt) {
	    if (thru) {
	      window.location = '/my_photos';
	    }
	    else {
		  $('photo'+id).destroy();
	    }
      }
    }).send();
  }
}

function delete_photo_admin(photo_id) {
  var kk = confirm("Are you sure you want to delete this photo?");
  if (kk) {
    window.location = '/vs_photos?action=delete&photo_id='+photo_id;
  }
}

function opac_items(elem,opcv) {
  $$(elem).each(function(item){
	item.setStyle('opacity',opcv);
  });
}

function edit_photo_caption(id) {
  var new_caption = prompt("Add/Edit Caption:",$('photo'+id+'_caption').get('html'));
  if (new_caption) {
    new Request({
      url: '/ajax/save_photo_caption?rn='+rn()+'&photo_id='+id+'&caption='+URLEncode(new_caption),
      onSuccess: function(txt) {
	    $('photo'+id+'_caption').set('html',new_caption);
      }
    }).send();
  }
}

function init_pink_box() {
  $$('div.pb').each(function(item){
	contentz = item.get('html');
	item.set('html','');
	w1 = new Element('div').set('class','boxThinkWrapper').inject(item);
	w2 = new Element('div').set('class','boxThink').inject(w1);
	w3 = new Element('div').set('class','boxThinkInner').inject(w2);
	w3.set('html',contentz);
	new Element('div').set('class','clear').inject(w3);
  });

  $$('div.pb2').each(function(item){
	contentz = item.get('html');
	item.set('html','');
	w1 = new Element('div').set('class','boxSundialWrapper').inject(item);
	w2 = new Element('div').set('class','boxSundial').inject(w1);
	w3 = new Element('div').set('class','boxSundialInner').inject(w2);
	w3.set('html',contentz);
	new Element('div').set('class','clear').inject(w3);
  });  

  $$('div.pinkbox').each(function(item){
	contentz = item.get('html');
	item.set('html','');
	new Element('div').set('class','pinkbox_top').inject(item);
	new Element('div').set('class','pinkbox_content').set('html',contentz+'&nbsp;').inject(item);
	new Element('div').set('class','pinkbox_bottom').inject(item);
	new Element('div').set('class','clear').inject(item);
  });

}

function chk_video_form() {
  ff = eval("document.vform");
  if (ff.video_title.value == "") {
    alert("Please enter a video title.");
    ff.video_title.focus();
    return false;
  }
  else if (ff.video_description.value == "") {
    alert("Please enter a video description.");
    ff.video_description.focus();
    return false;
  }
  else if (ff.video_file.value == "") {
    alert("Please select a movie file to upload.");
    ff.video_file.focus();
    return false;
  }
  else {
    ff.subtn.value = "Uploading...please wait";
    ff.subtn.disabled = true;
    return true;
  }
}

function chk_video_save_form() {
  ff = eval("document.vform");
  if (ff.video_title.value == "") {
    alert("Please enter a video title.");
    ff.video_title.focus();
    return false;
  }
  else if (ff.video_description.value == "") {
    alert("Please enter a video description.");
    ff.video_description.focus();
    return false;
  }
  else {
    ff.subtn.value = "Saving...please wait";
    ff.subtn.disabled = true;
    return true;
  }
}

function delete_video(video_id) {
  var kk = confirm("Are you sure you want to delete this video?");
  if (kk) {
    window.location = '/my_videos?action=delete&video_id='+video_id;
  }
}

function delete_video_admin(video_id) {
  var kk = confirm("Are you sure you want to delete this video?");
  if (kk) {
    window.location = '/vs_videos?action=delete&video_id='+video_id;
  }
}

function inapp_flag(item_id) {
  var cc = confirm("Are you sure you want to flag this material as inappropriate? This is a claim we take seriously and will immediately review this content.");
  if (cc) {
    window.location = "?action=inapp&item_id="+item_id;
  }
}

function toggle_comment_form() {
  cf = $('comment_form');
  if (cf.getStyle('display') == "none") {
    cf.setStyle('display','');
    $('comment').focus();
    $('comment_btn').setStyle('display','none');
  }
  else {
    cf.setStyle('display','none');
    $('comment_btn').setStyle('display','');
  }
}

function chkComment() {
  ff = eval("document.cform");
  if (ff.comment.value == "") {
    alert("Please enter a comment to post.");
    ff.comment.focus();
    return false;
  }
  else {
    ff.subtn.value = "Saving...";
    ff.subtn.disabled = true;
    return true;
  }
}

function delete_comment(video_id, comment_id) {
  var kk = confirm("Are you sure you want to delete this comment?");
  if (kk) {
    window.location = "?action=delete_comment&video_id="+video_id+"&comment_id="+comment_id;
  }
}

function hover_thumbs() {
  $$('li.pi img, li.vi img').each(function(item){
	item.addEvent('mouseover', function(e) {
	  item.setStyle('opacity','0.3');
	});
	item.addEvent('mouseout', function(e) {
	  item.setStyle('opacity','1');
	});
  });
}