function setHighlights() {									
	for (i=0;i<document.forms.length;i++) {
		//for (j=0;j<document.forms[i].length;j++){
		var arryInput = document.forms[i].getElementsByTagName('input');
		for (j=0;j<arryInput.length;j++) {
			objElement = arryInput[j];
			if(objElement.getAttribute("type").toLowerCase() == "text" || objElement.getAttribute("type").toLowerCase() == "password"){
				addEvent( objElement, 'focus', function(){ cName=this.className; this.className='ffActive'; } );
				addEvent( objElement, 'blur', function(){ this.className=cName; } );
			}
		}
	}
}

function addEvent( obj, type, fn ) {
	if ( obj.attachEvent ) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
		obj.attachEvent( 'on'+type, obj[type+fn] );
	} else
		obj.addEventListener( type, fn, false );
}

function removeEvent( obj, type, fn ) {
	if ( obj.detachEvent ) {
		obj.detachEvent( 'on'+type, obj[type+fn] );
		obj[type+fn] = null;
	} else
	obj.removeEventListener( type, fn, false );
}

function addLoadEvent(func) {

  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
	window.onload = func;
  } else {
	window.onload = function() {
	  if (oldonload) {
		oldonload();
	  }
	  func();
	}
  }

}

/*auto included load events*/
//addLoadEvent(setHighlights)

function displayNotice(m,w,n,e) {
	
	if (m.length > 0) {	$('messageList').innerHTML = '<li>' + m + '</li>'; Element.show('messageOut'); Effect.BlindUp('messageOut', {delay: 5.0})}
	if (w.length > 0) {	$('warningList').innerHTML = '<li>' + w + '</li>'; Element.show('warningOut'); Effect.BlindUp('warningOut', {delay: 5.0})}
	if (n.length > 0) {	$('noticeList').innerHTML = '<li>' + n + '</li>'; Element.show('noticeOut'); Effect.BlindUp('noticeOut', {delay: 5.0})}
	if (e.length > 0) {	$('errorList').innerHTML = '<li>' + e + '</li>'; Element.show('errorOut'); Effect.BlindUp('errorOut', {delay: 5.0})}
		
}

function setPrimaryImage(id) {
	$('div.primary').removeClass('primary');
	$('div#image_' + id).addClass('primary').highlight("slow");
}

function addCollection(productCategory) {
	var brandId = document.getElementById('product_brand_id').value;
	if (!brandId) {
		alert("Please select a brand before adding a collection.")
		return
	}
	
	var collection = prompt("Please enter a collection name:","");
	if (collection && collection != "") { // if they enterend something
		$.ajax({
			data:'brand_id='+brandId+'&collection='+collection+'&product='+productCategory, 
			success:function(request){
				$('#collections').html(request);
			}, 
			type:'post', 
			url:'/admin/'+productCategory+'s/add_collection'
		});
	}
	else if (collection == ""){ // if they entered nothing and hit OK
		alert('Nothing was entered.');
		addCollection(productCategory);
	}
	else { // if they hit cancel
		// alert('cancel');
	}
	

}

var nav_title = "";
$(document).ready(function () {
    $(".mainMenu ul a").hover(function(e){
		nav_title = $(this).attr("title");
		$(this).removeAttr("title");
	}, function(){
		$(this).attr({title:nav_title});	
	});
	
	$(".mainMenu li li:has(ul)").addClass("hasChildren");
	
});

// ================================
// = inserts <p></p> in MCE field if first text block does not have formatting applied=
// ================================

function mceTagWrap(element_id, html, body) {
 html = trim(html);
 /*check that text starts and ends with tags if not wrap it in <p>s.  
   This only happens for single line text messages*/
  if(html.charAt(0) != "<" || (html.charAt(html.length - 1) != ">" || html.charAt(html.length -2) == "/")) {
	html = "<p>"+html+"</p>";	
  }
 return html;
}
function trim(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}

// ==========================
// = listing page functions =
// ==========================

function toggle_list_display(element, filter_type, link_text) {
	$('#' + filter_type + '_more').toggle();
	$('.' + filter_type + 'Filtered').toggle();
	$(element).toggleText('Fewer ' + link_text, 'More ' + link_text );
}

// ==========================
// = Product page functions =
// ==========================

function CalculateSqft() {
	if(isNaN($('#sqft').val()) || $('#sqft').val().length == 0) {
		alert("Please enter a number for the square feet.");
		$('#sqft').focus();
	}
	else if(isNaN($('#sqft_over').val()) || $('#sqft_over').val().length == 0 || $('#sqft_over').val() < 0) {
		alert("Please enter a number for the overage percent.");
		 $('#sqft_over').focus();
	}
	else {
		qty = ($('#sqft').val() * (1 + $('#sqft_over').val()/100)) / $('#sqft_box').val();
		$('#boxes_number').val(Math.ceil(qty));
		$.fn.fancybox.close(); 
	}
}

function test() {
	alert('here');
}