//////////////////////////////////////////////////////////////////////
//                                                                  //
//  File name:   js.js                                              //
//  Description: Javascript for mariagomori.com                     //
//  Last edited: July 26, 2009                                      //
//  Author:      Stephen Gomori                                     //
//  Contact:     webmaster at vividwind dot com                     //
//                                                                  //
//////////////////////////////////////////////////////////////////////

addOnLoadEvent(loadImages);
addOnLoadEvent(prepareNav);
addOnLoadEvent(prepareForm);
addOnLoadEvent(preparePopUps);
addOnLoadEvent(prepareFlipBook);

function addOnLoadEvent(newFunc){
  var oldOnload = window.onload;
  if (typeof window.onload != 'function'){
    window.onload = newFunc;
  }else{
    window.onload = function(){
      if (oldOnload) {
        oldOnload();
      }
      newFunc();
    };
  }
}

function prepareNav(){
  
  if (!document.getElementById){
    return;
  }

  if (!document.getElementById('navigation')){
    return;
  }
  
  var links = document.getElementById('navigation').getElementsByTagName('a');
  
  for (var i=0; i<links.length; i++){
    links[i].onmouseover = function(){
      var id = this.getAttribute('class');
      swapImage(id, true);
    }
    links[i].onmouseout = function(){
      var id = this.getAttribute('class');
      swapImage(id, false);
    }
  }
}

function prepareForm(){
  
  if (!document.getElementById){
    return;
  }
  
  if (!document.getElementById('contact_form')){
    return;
  }
  
  document.getElementById('message').onkeyup = function(){
    getCharCount();
  }
  document.getElementById('reset').onclick = function(){
    document.getElementById('char_count').innerHTML='Character count: 0, Max: 1500';
  }
  document.getElementById('contact_form').onsubmit = function(){
    return validateForm();
  }
}

function preparePopUps(){

  if (!document.getElementsByTagName('a')){
    return;
  }
  
  var links = document.getElementsByTagName('a');
  
  for (var i=0; i<links.length; i++){
  
   if (links[i].getAttribute('class') == 'popup'){
     links[i].onclick = function(){
       var pop_up = open(this.getAttribute('href'));
	     pop_up.focus;
	     return false;
     }
   }
  }	
}

// Preload images and flyout menu rollovers.
function loadImages(){
	
  if (document.images){
		
		var page_array = new Array('home','story','books','links','contact');
		var roll_over = new Array(page_array.length);
		var here_indicator = new Array(page_array.length);
	
		for (var i=0; i<page_array.length; i++){
			roll_over[i] = new Image();
		  here_indicator[i] = new Image();
			
			roll_over[i].src = 'images/' + page_array[i] + '_rollover.jpg';
			here_indicator[i].src = 'images/' + page_array[i] + '_here.jpg';	
		}
		
		if ((document.getElementById) && (document.getElementById('flipBook'))){	
  		var book_back = new Image();
  	  book_back.src = 'images/book_back.jpg';	
    }
		
  }
}

// Rollover image swap.
function swapImage(image, hovering){

  if (document.images){ 
    if ((document.getElementById(image).src).indexOf('rollover') == -1 && (hovering)){
	    document.getElementById(image).src = 'images/' + image + '_rollover.jpg';    
	  }else{
      document.getElementById(image).src = 'images/' + image + '.jpg';
	  }
  }
}

function prepareFlipBook(){

  if ((document.getElementById) && (document.images) && (document.getElementById('flipBook'))){ 
    document.getElementById('flipBook').onclick = function(){
      if ((document.getElementById('book').src).indexOf('back') == -1){
  	    document.getElementById('book').src = 'images/book_back.jpg';    
  	  }else{
        document.getElementById('book').src = 'images/book_front.jpg';
  	  }
  	  return false;
  	}
  }
}

// Validates form against following regexs, and required fields.
function validateForm(){
	
	var contact_form = document.getElementById('contact_form');
	var email = /^\w[\w\_\&\-\.\~\%]*\@\w[\w\_\-\&\.\~]*\.[a-zA-Z]{2,7}$/;
	var badtext = /[^\w\!\@\#\$\%\^\&\*\ \(\)\-\+\=\{\[\}\]\"\'\:\;\<\,\>\.\?\/\\\~\`\n\r\t]/;
	
	if (badtext.test(contact_form.f_name.value)){
    alert('Sorry, I can\'t accept some of the characters you\'ve entered in the first name field.');
    contact_form.f_name.focus();
    return false;
  }
	
	if (badtext.test(contact_form.l_name.value)){
    alert('Sorry, I can\'t accept some of the characters you\'ve entered in the last name field.');
    contact_form.l_name.focus();
    return false;
  }
	
	if ((contact_form.l_name.value.length == 0) && (contact_form.f_name.value.length < 2)){
		alert('Please enter your name.');
		contact_form.f_name.focus();
		return false;
	}
	
	if (contact_form.message.value.length == 0){
		alert('Please enter a message.');
		contact_form.message.focus();
		return false;
	}
	
	if (badtext.test(contact_form.message.value)){
    alert('Sorry, I can\'t accept some of the characters you\'ve entered in the message area.');
    contact_form.message.focus();
    return false;
  }
	
	if (!email.test(contact_form.email.value)){
    alert('You seem to have entered an incorrect email address.');
    contact_form.email.focus();
    return false;
  }
}

// Gets string length of textarea and prints back above form. If too big, cuts off last character.
function getCharCount(){
	
	var field = document.getElementById('message');
	var max_length = 1500;
	
	if (field.value.length > max_length){
		field.value = field.value.substr(0, max_length); 
	}
	document.getElementById('char_count').innerHTML = 'Character count: '+field.value.length+', Max: '+max_length;
}