/*
*  Autofill Function
*/
var bizdir_yourInfo_autofill = "For us to contact you";
var bizdir_description_autofill = "Please enter a brief (1000 characters or less) description of the organisation";
var bizdir_cat_description_autofill = "Please enter a brief description of the category (text only, no HTML)";
var bizdir_keywords_autofill = "Words for the organisation to be found by";
var bizdir_companyInfo_autofill = "To be displayed with your listing";
var bizdir_geoInfo_autofill = "To be displayed with your listing";
var bizdir_postCode_autofill = "To allow a map of your location to be displayed";
var bizdir_backLinkUrl_autofill = "Please enter the URL of the page linking to this site";
var bizdir_categorySuggestion_autofill = "Suggest a category name for your listing (if required) ";
var bizdir_categoryInfo_autofill = "The name of the category";
var bizdir_openingTimes_autofill = "Please enter the opening times for the organisation if applicable";

var $j = jQuery.noConflict();

function sortCategoryTable()
{
  $j("#cat_table").tablesorter({sortList: [[0,0]]}); 
}

$j(document).ready(function() 
  { 
    sortCategoryTable(); 
    
    $j("#bizdir_backlink_para").hide();
    $j("#bizdir_backlink_hint").hide();       
    $j("#bizdir_link_sample_para").hide();    
    
    $j("#bizdir_website").focus(function () { 
      $j("#bizdir_backlink_para").slideDown(); 
      $j("#bizdir_backlink_hint").slideDown();       
      $j("#bizdir_link_sample_para").slideDown();     
      $j("#bizdir_link_sample_para").attr("readonly", "yes");       
    });

    // Convert '"Real Name" <name@host>' to 'name@host'
    $j("#bizdir_email,#bizdir_cEmail").blur(function () { 
      var v = $j(this).val();
      var re = /<(.+)>/;
      if (re.test(v))
      {
        $j(this).val(re.exec(v)[1]);
      }
    });
  } 
); 

function bizdir_clearAutoFill(id,type) {
	if(id == null || id == "" || type == null || type == "")
		return;
	var input = document.getElementById(id);
	if(input != null && type != null)
		switch(type) {
			case "YourInfo": if(input.value == bizdir_yourInfo_autofill) input.value = ""; input.style.color = "#000000"; break;
			case "Description": if(input.value == bizdir_description_autofill) input.value = ""; input.style.color = "#000000"; break;
			case "Cat_Description": if(input.value == bizdir_cat_description_autofill) input.value = ""; input.style.color = "#000000"; break;
			case "Keywords": if(input.value == bizdir_keywords_autofill) input.value = ""; input.style.color = "#000000"; break;
			case "CompanyInfo": if(input.value == bizdir_companyInfo_autofill) input.value = ""; input.style.color = "#000000"; break;
			case "GeoInfo": if(input.value == bizdir_geoInfo_autofill) input.value = ""; input.style.color = "#000000"; break;
			case "PostCode": if(input.value == bizdir_postCode_autofill) input.value = ""; input.style.color = "#000000"; break;			
      case "CategoryInfo": if(input.value == bizdir_categoryInfo_autofill) input.value = ""; input.style.color = "#000000"; break;
      case "OpeningTimes": if(input.value == bizdir_openingTimes_autofill) input.value = ""; input.style.color = "#000000"; break;				
      case "CategorySuggestion": if(input.value == bizdir_categorySuggestion_autofill) input.value = ""; input.style.color = "#000000"; break;				
      case "BackLinkUrl": if(input.value == bizdir_backLinkUrl_autofill) input.value = ""; input.style.color = "#000000"; break;				
    }
}

function bizdir_clearAllAutoFill() 
{
	// Clear text fields
	var all_inputs = document.getElementsByTagName('input');
	for(var i=0;i<all_inputs.length;i++) {
		var input = all_inputs[i];
		switch(input.getAttribute('bizdir_autofill')) {
			case 'YourInfo': 
				if(input.value == bizdir_yourInfo_autofill || input.value == "" || input.value == null) {
					input.value = "";
					input.style.color = "#000000"; 
				}
			break;
			case 'Keywords': 
				if(input.value == bizdir_keywords_autofill || input.value == "" || input.value == null) {
					input.value = "";
					input.style.color = "#000000"; 
				}
			break;
			case 'CompanyInfo':
				if(input.value == bizdir_companyInfo_autofill || input.value == "" || input.value == null) {
					input.value = "";
					input.style.color = "#000000";
				}
			break;
			case 'GeoInfo':
				if(input.value == bizdir_geoInfo_autofill || input.value == "" || input.value == null) {
					input.value = "";
					input.style.color = "#000000";
				}
			break;
			case 'PostCode':
				if(input.value == bizdir_postCode_autofill || input.value == "" || input.value == null) {
					input.value = "";
					input.style.color = "#000000";
				}
			break;			
      case 'CategoryInfo':
        if(input.value == bizdir_categoryInfo_autofill || input.value == "" || input.value == null) {
					input.value = "";
					input.style.color = "#000000";
				}
      break;
      case 'CategorySuggestion':
        if(input.value == bizdir_categorySuggestion_autofill || input.value == "" || input.value == null) {
					input.value = "";
					input.style.color = "#000000";
				}
      break;
      case 'BackLinkUrl':
        if(input.value == bizdir_backLinkUrl_autofill || input.value == "" || input.value == null) {
					input.value = "";
					input.style.color = "#000000";
				}
      break;            
		}
	}
	var all_textareas = document.getElementsByTagName('textarea');
	for(var i=0;i<all_textareas.length;i++) {
		var input = all_textareas[i];
		if(
		   	input.getAttribute('bizdir_autofill') == 'Description' && 
			(input.value == "" || input.value == null || input.value == bizdir_description_autofill)
		) {
			input.style.color = '#000000';
			input.value = "";
		} else if(
		   	input.getAttribute('bizdir_autofill') == 'Cat_Description' && 
			(input.value == "" || input.value == null || input.value == bizdir_cat_description_autofill)
		) {
			input.style.color = '#000000';
			input.value = "";
		} else if(
		   	input.getAttribute('bizdir_autofill') == 'OpeningTimes' && 
			(input.value == "" || input.value == null || input.value == bizdir_openingTimes_autofill)
		) {
			input.style.color = '#000000';
			input.value = "";
		}
	}
}

function bizdir_populateAutofill() {
	var override = arguments[0] == true;
	var all_inputs = document.getElementsByTagName('input');
	for(var i=0;i<all_inputs.length;i++) {
		var input = all_inputs[i];
		switch(input.getAttribute('bizdir_autofill')) {
			case 'YourInfo': 
				if(override || input.value == bizdir_yourInfo_autofill || input.value == "" || input.value == null) {
					input.value = bizdir_yourInfo_autofill;
					input.style.color = "#999999"; 
				}
			break;
			case 'Keywords': 
				if(override || input.value == bizdir_keywords_autofill || input.value == "" || input.value == null) {
					input.value = bizdir_keywords_autofill;
					input.style.color = "#999999"; 
				}
			break;
			case 'CompanyInfo':
				if(override || input.value == bizdir_companyInfo_autofill || input.value == "" || input.value == null) {
					input.value = bizdir_companyInfo_autofill;
					input.style.color = "#999999";
				}
			break;
			case 'GeoInfo':
				if(override || input.value == bizdir_geoInfo_autofill || input.value == "" || input.value == null) {
					input.value = bizdir_geoInfo_autofill;
					input.style.color = "#999999";
				}
			break;
			case 'PostCode':
				if(override || input.value == bizdir_postCode_autofill || input.value == "" || input.value == null) {
					input.value = bizdir_postCode_autofill;
					input.style.color = "#999999";
				}
			break;						
      case 'CategoryInfo':
        if(override || input.value == bizdir_categoryInfo_autofill || input.value == "" || input.value == null) {
					input.value = bizdir_categoryInfo_autofill;
					input.style.color = "#999999";
				}
      break;
      case 'CategorySuggestion':
        if(override || input.value == bizdir_categorySuggestion_autofill || input.value == "" || input.value == null) {
					input.value = bizdir_categorySuggestion_autofill;
					input.style.color = "#999999";
				}
      break;
      case 'BackLinkUrl':
        if(override || input.value == bizdir_backLinkUrl_autofill || input.value == "" || input.value == null) {
					input.value = bizdir_backLinkUrl_autofill;
					input.style.color = "#999999";
				}
      break;            
		}
	}
	var all_textareas = document.getElementsByTagName('textarea');
	for(var i=0;i<all_textareas.length;i++) {
		var input = all_textareas[i];
		if(
		   	input.getAttribute('bizdir_autofill') == 'Description' && 
			(input.value == "" || input.value == null || input.value == bizdir_description_autofill || override)
		) {
			input.style.color = '#999999';
			input.value = bizdir_description_autofill;
		} else if(
		   	input.getAttribute('bizdir_autofill') == 'Cat_Description' && 
			(input.value == "" || input.value == null || input.value == bizdir_cat_description_autofill || override)
		) {
			input.style.color = '#999999';
			input.value = bizdir_cat_description_autofill;
		} else if(
		   	input.getAttribute('bizdir_autofill') == 'OpeningTimes' && 
			(input.value == "" || input.value == null || input.value == bizdir_openingTimes_autofill || override)
		) {
			input.style.color = '#999999';
			input.value = bizdir_openingTimes_autofill;
		}
	}
}

/*
 * ON LOAD
 */
function bizdir_onload() {
	bizdir_populateAutofill();
	var bizdir_submit = document.getElementById("bizdir_submit")
	if(bizdir_submit != null)
		bizdir_submit.disabled = false;
}

window.onload = bizdir_onload;

/*
 *  HELPER FUNCTIONS
 */
function clearMessage() 
{
	var messages = document.getElementById('bizdir_messages');
	if (messages != null) 
  {
		messages.className = '';
		messages.innerHTML = '';
	}
	if (typeof stopSpinner === 'function')
  {
    stopSpinner();
  }
}



