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');
	});
  });
}

var swfu;
function init_swfUpload() {
  swfu = new SWFUpload({
	flash_url : "/flash/swfupload/swfupload.swf",
	upload_url: "/photo_contest_upload_handler.php?",
	file_post_name: "photo_file",
	file_size_limit : "15 MB",
	file_types : "*.jpg; *.jpeg; *.jpe; *.psd; *.png; *.pdf; *.gif; *.tiff; *.bmp; *.eps; *.ps;",
	file_types_description : "Document Files",
	file_upload_limit : 0,
	file_queue_limit : 1,
	custom_settings : {
	  progress_target : "fsUploadProgress",
	  upload_successful : false
    },
	debug: false,
	// Button settings
	button_image_url: "/images/swfupload/XPButtonUploadText_61x22.png",	// Relative to the Flash file
	button_width: "61",
	button_height: "22",
	button_placeholder_id: "spanButtonPlaceHolder",
	button_text: "",
	button_text_style: "",
	button_text_left_padding: 0,
	button_text_top_padding: 0,
	swfupload_loaded_handler : swfUploadLoaded,
	file_dialog_start_handler: fileDialogStart,
	file_queued_handler : fileQueued,
	file_queue_error_handler : fileQueueError,
	file_dialog_complete_handler : fileDialogComplete,
	upload_progress_handler : uploadProgress,
	upload_error_handler : uploadError,
	upload_success_handler : uploadSuccess,
	upload_complete_handler : uploadComplete
  });
}

function chkPhotoForm() {
  formPass = validatePhotoForm(true);
  if (formPass) {
     $('subtn').value = "Submitting...please wait";
     $('subtn').disabled = true;
     return true;
  }
  else {
    return false;
  }
}

function validatePhotoForm(showAlert) {
  if ($('first_name').value == "") {
	if (showAlert) {
	  alert("Please enter your first name.");
	  $('first_name').focus();
	}
    return false;
  }
  else if ($('last_name').value == "") {
	if (showAlert) {
	  alert("Please enter your last name.");
	  $('last_name').focus();
	}
    return false;
  }
  else if ($('address1').value == "") {
	if (showAlert) {
	  alert("Please enter your street address.");
	  $('address1').focus();
	}
	return false;
  }
  else if ($('city').value == "") {
	if (showAlert) {
	  alert("Please enter your city.");
	  $('city').focus();
	}
	return false;
  }
  else if ($('state').value == "") {
	if (showAlert) {
	  alert("Please select your state.");
	  $('state').focus();
	}
	return false;
  }
  else if ($('zip').value == "") {
	if (showAlert) {
	  alert("Please enter your zip code.");
	  $('zip').focus();
	}
	return false;
  }
  else if ($('phone').value == "") {
	if (showAlert) {
	  alert("Please enter your phone number.");
	  $('phone').focus();
	}
	return false;
  }
  else if ($('email').value.indexOf('@') <= 0) {
	if (showAlert) {
	  alert("Please enter a valid email address.");
	  $('email').focus();
	  $('email').select();
	}
	return false;
  }
  else if (($('legacy')) && ($('photo_file').value == "")) {
	if (showAlert) {
	  alert("Please select a photo file from your computer to upload.");
	  $('photo_file').focus();
    }
	return false;
  }
  else if ((!$('legacy')) && ($('txtFileName').value == "")) {
	if (showAlert) {
	  alert("Please select a photo file from your computer to upload.");
    }
	return false;
  }
  else if ($('photo_title').value == "") {
	if (showAlert) {
	  alert("Please enter a title for your photo.");
	  $('photo_title').focus();
    }
	return false;
  }
  else if ($('photo_description').value == "") {
	if (showAlert) {
	  alert("Please enter a short description of your photo.");
	  $('photo_description').focus();
	}
	return false;
  }
//  else if ($('photo_tags').value == "") {
//	if (showAlert) {
//	  alert("Please enter one or more tag (or keyword) that describes your photo. Seperate by comma.");
//	  $('photo_tags').focus();
//    }
//	return false;
//  }
  else if (!$('agree').checked) {
	if (showAlert) {
	  alert("Please check that you have read and agree to the Terms of Use.");
	  $('agree').focus();
    }
	return false;
  }
  else if (document.photoForm.recaptcha_response_field.value == "") {
	if (showAlert) {
      alert("Please enter the two words you see.");
      document.photoForm.recaptcha_response_field.focus();
    }
    return false;
  }
  else {
    return true;
  }
}


function PH_loadPhotos() {
  PH_initZoom();
  $$('#contestPhotos li').each(function(item){
	item.setStyle('display','block');
  });
}

function PH_initZoom() {
  thisPos = $('sliderPos').get('value').toInt();
  new Slider($('photoSize'), $('photoSizeSlider'), {
	steps: 200,
	range: [50],
	onChange: function(value){
		PH_updateZoomPhotos(value);
	}
  }).set(thisPos);
  PH_updateZoomPhotos(thisPos);
}

function PH_updateZoomPhotos(vv) {
  $('sliderPos').set('value',vv);
  // set frames
  $$('#contestPhotos li').each(function(item){
  	item.setStyles({
  		'height':vv+'px',
  		'width':vv+'px'
  	});
  });
  // set landscape images
  $$('#contestPhotos li.h img.i').each(function(item){
	osize  = eval(item.get('rel'));
	orig_h = osize[0].toInt();
	orig_w = osize[1].toInt();
	rsize  = ((orig_h * vv) / orig_w);
	iadj   = ((vv / 2) - (rsize / 2)).toInt();
	if (iadj < 0) { iadj = 0; }
  	item.setStyles({
  		'height':rsize+'px',
  		'width':vv+'px',
  		'margin-top':iadj+'px'
  	});
  });
  // set vertical images
  $$('#contestPhotos li.v img.i').each(function(item){
	osize = eval(item.get('rel'));
	orig_h = osize[0].toInt();
	orig_w = osize[1].toInt();
	rsize = ((orig_w * vv) / orig_h);
	iadj   = ((vv / 2) - (rsize / 2)).toInt();
	if (iadj < 0) { iadj = 0; }
  	item.setStyles({
  		'height':vv+'px',
  		'width':rsize+'px',
  		'margin-left':iadj+'px'
  	});
  });
}

function PH_deletePhoto(photoId) {
  var kk = confirm("Are you sure you want to delete this photo?");
  if (kk) {
    new Request({
      url: '/ajax/deleteContestPhoto.php?photo_id='+photoId+'&rn='+rn(),
      onSuccess: function(txt) {
	    if (txt == "error") {
	      alert("Error deleting photo");
	    }
	    else {
	      new Fx.Morph($(photoId), {}).start({'opacity':[1,0]});
	      (function(){ $(photoId).destroy(); }).delay(750);
	    }
      }
    }).send();
  }
}

function PH_managePhoto(pid) {
  MOOdalBox_I.open(
    "/ajax/manageContestPhoto.php?photo_id="+pid+"&rn="+rn(),
    "MANAGE PHOTO",
    "860 440"
  );
}

function PH_chkPIform() {
  if ($('photo_title').value == "") {
	alert("Please enter a title for your photo.");
    $('photo_title').focus();
	return false;
  }
  else if ($('photo_description').value == "") {
	alert("Please enter a short description of your photo.");
	$('photo_description').focus();
	return false;
  }
//  else if ($('photo_tags').value == "") {
//	alert("Please enter one or more tag (or keyword) that describes your photo. Seperate by comma.");
//	$('photo_tags').focus();
//	return false;
//  }
  else {
    $('subtn').value = "Saving...";
    $('subtn').disabled = true;
    return true;
  }
}

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

function PH_PIchkAppr(pid) {
  ichk = 0;
  if ($('approved').checked) {
    ichk = 1;
  }
  window.location = "?photo_id="+pid+"&action=approve&stat="+ichk;
}

function PH_PIchkFeat(pid) {
  ichk = 0;
  if ($('featured').checked) {
    ichk = 1;
  }
  window.location = "?photo_id="+pid+"&action=feature&stat="+ichk;
}

function PH_featured(ff,pn,pid) {
  window.location = "?pagenum="+pn+"&action=featured&photo_id="+pid+"&featured="+ff;
}

function PH_approve(pn,pid) {
  var aa = confirm("Are you sure you wish to approve this photo?");
  if (aa) {
    window.location = "?pagenum="+pn+"&action=approve&photo_id="+pid;
  }
}

function PH_applyFilter() {
  if ($('photoFilter').get('value') == "") {
    window.location = "?";
  }
  else {
    window.location = "?photoFilter="+$('photoFilter').get('value');
  }
}

function PH_viewPhoto(pid) {
  MOOdalBox_I.open(
    "/ajax/viewContestPhoto.php?photo_id="+pid+"&rn="+rn(),
    "VIEW PHOTO",
    "860 440"
  );
}