// -*- mode: Java; -*-

function validatePhotoUpload () {
    var missing_fields = new Array();

    $('#select_gallery, #enter_tags, #select_category').hide();
  
    if (($('#gallery').val() == 0 || $('#gallery').val() == undefined) && $('#newgallery').val() == "")
	missing_fields.push('select_gallery');
    if ($('#photo_category').val() == 0)
	missing_fields.push('select_category');
    if ($('#photo_tags').val() == "")
	missing_fields.push('enter_tags');
    if (missing_fields.length > 0) {
	for (var i=0; i < missing_fields.length; i++)
	    $('#'+missing_fields[i]).show();
	return (false);
    } else {
	return (true);
    }
}

function validateProductUpload() {
	
	var missing_fields = new Array();
	
	$('#select_product_title, #select_product_condition, #select_product_colour, #select_product_description, #select_product_photo, #select_product_qty, #select_product_price, #select_product_postage, #select_product_tags').hide();
	if ($('#product_title').val() == "")
		missing_fields.push('#select_product_title');
	if ($('#product_style').val() == 0 || $('#product_style') == undefined || $('#product_style') == '')
		missing_fields.push('#select_product_style	');	
	if ($('#product_condition').val() == 0 || $('#product_condition') == undefined || $('#product_condition') == '')
		missing_fields.push('#select_product_condition');
	if ($('#product_colour').val() == "")
		missing_fields.push('#select_product_colour');
	if ($('#product_description').val() == "")
		missing_fields.push('#select_product_description');		
	if ($('#product_photo').val() == 0 || $('#product_photo').val() == undefined)
		missing_fields.push('#select_product_photo');
	if ($('#product_qty').val() == "")
		missing_fields.push('#select_product_qty');
	if ($('#product_postage_destination').val() == 0 || $('#product_postage_destination') == undefined || $('#product_postage_destination') == '')
		missing_fields.push('#select_product_postage_destination');
	if ($('#product_postage_cost').val() == 0 || $('#product_postage_cost') == undefined || $('#product_postage_cost') == '')
		missing_fields.push('#select_product_postage_cost');
	if ($('#product_price').val() == "")
		missing_fields.push('#select_product_price');
	if ($('#product_postage').val() == "")
		missing_fields.push('#select_product_postage');
	if ($('#product_tags').val() == "")
		missing_fields.push('#select_product_tags');

	if (missing_fields.length > 0) {
	for (var i=0; i < missing_fields.length; i++)
	    $(missing_fields[i]).show();
	$('#product_title').focus();
	return (false);
	
    } else {
	return (true);
	
    }
}

function validateManageShop() {
	
	var missing_fields = new Array();

	$('#select_about_shop, #select_product_condition, #select_product_colour, #select_product_description, #select_product_photo, #select_product_qty, #select_product_price, #select_product_postage, #select_product_tags').hide();
	if ($('#seller_aboutshop').val() == "")
		missing_fields.push('#select_about_shop');
	if ($('#seller_address1').val() == 0 || $('#seller_address1') == undefined || $('#seller_address1') == '')
		missing_fields.push('#select_address');	
	if ($('#seller_town').val() == 0 || $('#seller_town') == undefined || $('#seller_town') == '')
		missing_fields.push('#select_town');
	if ($('#seller_state').val() == "")
		missing_fields.push('#select_state');
	if ($('#seller_country').val() == "")
		missing_fields.push('#select_country');		
	if ($('#seller_phone').val() == "" || $('#seller_phone').val() == '')
		missing_fields.push('#select_phone');
		if ($('#seller_paypal').val() == "" || $('#seller_paypal').val() == '')
			missing_fields.push('#select_paypal');
	if ($('#agreeterms').get(0).checked == 0)
		missing_fields.push('#select_terms');
	
	if (missing_fields.length > 0) {
	for (var i=0; i < missing_fields.length; i++)
	    $(missing_fields[i]).show();
	$('#seller_aboutshop').focus();
        $('b.infotip').remove();
	return (false);
	
    } else {
	return (true);
	
    }
}

function validateSimplePhotoUpload() {
  var missing_fields = new Array();

  $('#select_gallery, #enter_tags, #select_category, #enter_photo_path').hide();
  
  if (($('#gallery').val() == 0 || $('#gallery').val() == undefined) && $('#newgallery').val() == "")
    missing_fields.push('#select_gallery');
  if ($('#photo_category').val() == 0)
    missing_fields.push('#select_category');
  if ($('#photo_tags').val() == "")
    missing_fields.push('#enter_tags');
  if ($('#photo1').val() == "" && $('#photo2').val() == "" &&
      $('#photo3').val() == "" && $('#photo4').val() == "")
  	missing_fields.push('#enter_photo_path');
  if (missing_fields.length > 0) {
      for (var i=0; i < missing_fields.length; i++)
	  $(missing_fields[i]).show();
      return (false);
  } else {
      return (true);
  }
}

function validateBlogUpload() {
  var missing_fields = new Array();
  
  $('#enter_title, #select_category').hide();
  
  if ($('#blog_title').val() == '')
    missing_fields.push('#enter_title');
  if ($('#blogcategory').val() == 0)
    missing_fields.push('#select_category');
  // XXX: Ensure content is provided.
  if (missing_fields.length > 0) {
    for (var i=0; i < missing_fields.length; i++)
      $(missing_fields[i]).show();
    return (false);
  } else {
    return (true);
  }
}

/* evaluate stylepic/hotgood upload */
function validateHotGoodUpload(){
  var missing_fields = new Array();
  
  $('#provide_path, #enter_title, #select_category,' +
     '#enter_content, #enter_tags').hide();

  if ($('#hotgood_photo').val() == "")
    missing_fields.push('#provide_path');
  if ($('#hotgood_title').val() == "")
    missing_fields.push('#enter_title');
  if ($('#hotgood_subcategory').val() == 0)
    missing_fields.push('#select_category');
  if ($('#hotgood_content').val() == "")
    missing_fields.push('#enter_content');
  if ($('#hotgood_tags').val() == "")
    missing_fields.push('#enter_tags');

  if (missing_fields.length > 0) {
      for (var i=0; i < missing_fields.length; i++)
	  $(missing_fields[i]).show();
      return (false);
  } else {
      return (true);
  }
}

/* evaluate threads upload */
function evaluate_threads_upload(){

  var missing_fields = new Array();
  
  if (document.upload_threads.threads_photo.value == ""){
    missing_fields.push('enter_path');
  }
  if (document.upload_threads.threads_what.value == ""){
    missing_fields.push('enter_what');
  }
  if (document.upload_threads.threads_where.value == ""){
    missing_fields.push('enter_where');
  }
  if (document.upload_threads.threads_tags.value == ""){
    missing_fields.push('enter_threads_tags');
  }
    return missing_fields;
}

function wakeFCKeditor(instance) {
    var oFCKEd = FCKeditorAPI.GetInstance(instance);
    try {
	oFCKEd.MakeEditable();
    } catch (e) {
	oFCKEd.Focus();
    }
}

$(document).ready(function() {

    // $("#mypics").editInPlace({ });

    $("#editPlayer").click(function () {
        $("#player").editable("stream.php", {
	    type: 'textarea',
	    cancel: 'Cancel',
	    submit: 'Save',
	    rows: 10,
	    id: 'element_id',
	    name: 'update_value',
	    event: 'dblclick',
	    tooltip: 'Double click to edit...'
        });
	$("#player").dblclick();
    });

    if ($('#colourpicker').length) {
        // The following mysterious statements can silently break whatever
	// follows. Take care.
        var picker = $.farbtastic('#colourpicker');
        $('.profilecolour').each(function () { picker.linkTo(this) })
	                   .focus(function() {
			       picker.linkTo(this);
		           });
    }

    if ($('form[name=galleryedit]').length) {
        // Build an array of fields that have been modified.
	// This is used to prevent submitting data that is unchanged.
	fields = $('input[name^=caption], input[name^=order], input[name=title], select[name=category]');
        var changedFields = new Array();

	fields.change(function () {
	    changedFields.push(this);
	});

	// Disabled unchanged fields to prevent them being submitted.
	$('form[name=galleryedit]').submit(function () {
	    fields.not(changedFields).attr('disabled', 'disabled');
	});
    }

    if ($('form[name=hotgoodedit]').length) {
        // Build an array of fields that have been modified.
	// This is used to prevent submitting data that is unchanged.
        fields = $('input[name^=title], input[name^=description], select[name^=category]');
        var changedFields = new Array();

        fields.change(function () {
            changedFields.push(this);
	});

	// Disabled unchanged fields to prevent them being submitted.
        $('form[name=hotgoodedit]').submit(function () {
            fields.not(changedFields).attr('disabled', 'disabled');
        });
    }
});
