/*

  To Dos:
    - abstract functions where ever possible
    
*/

window.onload = function() {
    positionNavs();
    setActiveMenuTab();
}

window.onresize = function() {
  positionNavs();
}

function setActiveMenuTab() {

    /*
      
      If the menus change, this code will not work.
      
      This is an ugly hack to highlight the active
      menu tab. I intend to fix this as soon as I have time.
      This is only a temporary fix.
      
      The code needs to be abstracted to handle an arbitrary
      number of menus and not rely on the element id. In order
      to do this, I need to significantly change the way
      the CSS is written.
      
      The best way to correct this is to implement the all-CSS
      drop down menus for Mambo.
      
    */

	var nav        = document.getElementById( 'primNav' );
	var docHREF    = document.location;
	var docHREF    = new String( docHREF );
	docHREF        = docHREF.toLowerCase();
	var menuTab    = '';
	
	if ( docHREF.indexOf( 'index.php' ) == -1 || 
             docHREF.indexOf( '?' ) == -1 ) {
		menuTab = document.getElementById( 'tab1' );
		noDim = 1;
	}
	else if ( docHREF.indexOf( 'com_content' ) != -1 && 
	          docHREF.indexOf( 'task=view' ) != -1) {
		menuTab = document.getElementById( 'tab2' );
		noDim = 2;
	}
	else if ( docHREF.indexOf( 'com_collections' ) != -1 || 
	          docHREF.indexOf( 'com_borders' ) != -1 ) {
		menuTab = document.getElementById( 'tab3' );
		menuTab = menuTab.childNodes[0];
		noDim = 3;
	}
	else if ( docHREF.indexOf( 'com_fabrics' ) != -1 ) {
		menuTab = document.getElementById( 'tab4' );
		noDim = 4;
	}
	else if ( docHREF.indexOf( 'com_accessories' ) != -1 ) {
		menuTab = document.getElementById( 'tab5' );
		noDim = 5;
	}
	else if ( docHREF.indexOf( 'com_trends' ) != -1 ) {
		menuTab = document.getElementById( 'tab6' );
		noDim = 6;
	}
	else if ( docHREF.indexOf( 'blogsection' ) != -1 ) {
		menuTab = document.getElementById( 'tab7' );
		menuTab = menuTab.childNodes[0];
		noDim = 7;
	}
	else if ( docHREF.indexOf( 'com_contact' ) != -1 ) {
		menuTab = document.getElementById( 'tab8' );
		menuTab = menuTab.childNodes[0];
		noDim = 8;
	}
	if ( menuTab != '' ) {
		menuTab.style.backgroundColor = "#EFC4AB";
	}
}


/* MY PROTOTYPE FUNCTIONS */

Array.prototype.inArray = function ( value )
{
	var i;
	for (i=0; i < this.length; i++) {
		if ( this[i] === value ) {
			return true;
		}
	}
	return false;
};

function vowelCheck( theChar ) {
	vowels = new Array( 'a', 'e', 'i', 'o', 'u' );
	theChar = theChar.toLowerCase();
	if ( vowels.inArray( theChar ) ) {
     return true;
	}
	return false;
}
String.prototype.isVowel = vowelCheck;


/* END PROTOTYPE FUNCTIONS */

function validateEmailForm( theform ) {
	var isOk = validateField( theform, 'name' );
	if ( isOk == true ) {
		isOk = validateField( theform, 'email' );
	}
	if ( isOk == true ) {
	  isOk = validateEmailAddress( theform );
	}
	if ( isOk == true ) {
		isOk = validateField( theform, 'subject' );
	}
	if ( isOk == true ) {
		isOk = validateField( theform, 'city' );
	}
	if ( isOk == true ) {
		isOk = validateField( theform, 'state' );
	}
	if ( isOk == true ) {
		isOk = validateField( theform, 'phone' );
	}
	if ( isOk == true ) {
     isOk = validateField( theform, 'message' );
   }
	return isOk;
}

function validateLostPassword( theform ) {
	var isOk = validateField( theform, 'username' );
	if ( isOk == true ) {
		isOk = validateEmailAddress( theform );
	}
	return isOk;
}

function validateLogin( theform ) {
	var isOk = validateField( theform, 'username' );
	if ( isOk == true ) {
		isOk = validateField( theform, 'password' );
	}
	return isOk;
}

function validateEmailAddress( theform ) {
	fieldref = document.forms[theform]['email'];
	isOk = validateField( theform, 'email' );
	if ( isOk == true ) {
		if ( ( fieldref.value.search("@") == -1 ) || 
			  ( fieldref.value.search("[.*]" ) == -1 ) ) {
			alert('That doesn\'t appear to be a valid email address.' );
			return false;
		}
	}
	return isOk;
}

function validateField( theform, thefield ) {
	fieldref = document.forms[theform][thefield];
	
	// Really anal message formatting
	
	theChar = thefield.charAt(0);
	n = ' ';
	var vowel = theChar.isVowel;
	if ( vowel == true ) {
		n = 'n ';
	}
	
	if ( fieldref.value == '' ) {
		alert('You did not enter a'+n+thefield+'.');
		return false;
	}
	return true;
}

function submitForm( theform ) {
	document.forms[theform].submit();
	return true;
}

/* ------------------------------------------------------------------
	Use = to validate forms
	      This function tries to check for valid email addresses
	      assuming that the name of the email field will have the
	      string 'email' in it. This approach is used to avoid
	      hard-coding any field names into the function, making
	      it more easily ported.
	@param (string) = name of the form to validate
	@param (array) = an array of fields to check in the form
--------------------------------------------------------------------- */

function formvalidate( myform, myfields ) {
	emptyalert = 'You did not provide a(n) ';
	emailalert = 'The email entered does not appear to be a valid format.';
	for ( i=0; i<myfields.length; i++ ) {
		var myfield = myfields[i];
		if ( document.forms[myform][myfield].value == '' ) {
			alert(emptyalert+myfield);
			return false;
		}
		if ( myfield.indexOf('email') != -1 ) {
			if ( ( document.forms[myform][myfield].value.search("@") == -1 ) || 
				  ( document.forms[myform][myfield].value.search("[.*]" ) == -1 ) ) {
				alert(emailalert);
				return false;
			}
		}
	}
	document.forms[myform].submit();
}

function setimage( which, what ) {
	document.images[which].src = what;
}

function settext ( whichid, whattext ) {
	document.getElementById(whichid).innerHTML=whattext;
}

function changeloc( URL_List ) {
	var URL = URL_List.options[URL_List.selectedIndex].value;
	if ( URL != 0 && URL != '0' ) {
		// URL += '.html';
   	window.location.href = URL;
   }
}

function showlogo() {
	var logoDiv = document.getElementById('headerlogo');
	logoDiv.className = logoDiv.className.replace('hide','');
}
function hidelogo() {
	var logoDiv = document.getElementById('headerlogo');
	logoDiv.className+='hide';
}

function show(which) {
  if (document.all && document.getElementById) {
    for(i=1; i< which.childNodes.length; i++) {
      node = which.childNodes[i];
      if( node.nodeName=="UL" ) {
        node.className+="drop";
      }
    }
  }
}
function hide(which) {
  if (document.all && document.getElementById) {
    for(i=1; i< which.childNodes.length; i++) {
      node = which.childNodes[i];
      if( node.nodeName=="UL" ) {
        node.className=node.className.replace("drop","");
      }
    }
  }
}

