// JavaScript Document

//------------------------
// Load event function
//------------------------

/**
 * Adds an onload event
 * 
 * @param func
 * @return
 */
function addLoadEvent(func) 
{
	var oldonload = window.onload;
  	if (typeof window.onload != 'function')
  	{
    	window.onload = func;
  	}
  	else 
  	{
    	window.onload = function() 
    	{
      		if (oldonload) 
      		{
        		oldonload();
      		}
      		func();
    	}
  	}
}

//------------------------
// Initialiser scripts
//------------------------


/**
 * Function calls button enablers
 * 
 *
 */
function addButtonActions()
{ 
	activateSubscribeButton();
	activateUnSubscribeButton();
	activateContactFormButton();
	activateSourceContactFormButton();
}

//------------------------
// End of Initialiser scripts
//------------------------


//------------------------
// Button activation scripts
//------------------------

/**
 * This changes the subscribe button to an image
 * 
 */
function activateSubscribeButton()
{
	// Add the validation function firing for the submit button
	var node = document.getElementById("subscribeButton");
	if(!node)
	{
		return;
	}
	else
	{
		var html = '<img src="/images/page/subscribe-button.gif" class="form-button" id="nlSubscribe" />';
		node.innerHTML = html;
		addSubscribeAction();
	}
}

/**
 * This adds an onclick action to the subscribe button
 * 
 */
function addSubscribeAction()
{
	var node = document.getElementById("subscribeButton");
	if(!node)
	{
		return;
	}
	else
	{
		node.onclick = function()
		{
			validateSubscribeForm();
		}
	}
}

/**
 * This changes the unsubscribe button to an image
 * 
 */
function activateUnSubscribeButton()
{
	// Add the validation function firing for the submit button
	var node = document.getElementById("unSubscribeButton");
	if(!node)
	{
		return;
	}
	else
	{
		var html = '<img src="/images/page/unsubscribe-button.gif" class="form-button" id="nlUnSubscribe"/>';
		node.innerHTML = html;
		addUnSubscribeAction();
	}
}

/**
 * This adds an onclick action to the unsubscribe button
 * 
 */
function addUnSubscribeAction()
{
	var node = document.getElementById("nlUnSubscribe");
	if(!node)
	{
		return;
	}
	else
	{
		node.onclick = function()
		{
			validateUnSubscribeForm();
		}
	}
}

/**
 * This changes the submit button to an image
 * 
 */
function activateContactFormButton()
{
	// Add the validation function firing for the submit button
	var node = document.getElementById("submitButton");
	if(!node)
	{
		return;
	}
	else
	{
		
		var html = '<img src="/images/page/send-button.gif" class="form-button" id="enquirySubmit" />';
		node.innerHTML = html;
		addContactSubmitAction();
	}
}

/**
 * This adds an onclick action to the contact submit button
 * 
 */
function addContactSubmitAction()
{
	var node = document.getElementById("enquirySubmit");
	if(!node)
	{
		return;
	}
	else
	{
		node.onclick = function()
		{
			validateContactForm();
		}
	}
}


/**
 * This changes the submit button to an image for the source contact form
 * 
 */
function activateSourceContactFormButton()
{
	// Add the validation function firing for the submit button
	var node = document.getElementById("sourceFormSubmit");
	if(!node)
	{
		return;
	}
	else
	{
		
		var html = '<img src="/images/page/send-button.gif" class="send-button" id="sourceEnquiryButton"/>';
		node.innerHTML = html;
		addSourceContactSubmitAction();
	}
}

/**
 * This adds an onclick action to the contact submit button
 * 
 */
function addSourceContactSubmitAction()
{
	var node = document.getElementById("sourceEnquiryButton");
	if(!node)
	{
		return;
	}
	else
	{
		node.onclick = function()
		{
			validateSourceContactForm();
		}
	}
}

//------------------------
// Functions start here
//------------------------


/**
 * This validates the subscriber form
 *
 */
function validateSubscribeForm()
{
	var valid = true;
	var message = "";
	
	if(document.subscribeForm.name.value == "")
	{
		valid = false;
		alert("Please enter your name to subscribe to our newsletter.");
		return;
	}
	
	if(validateEmail(document.subscribeForm.subEmail.value) == false)
	{
		valid = false;
		message = "Please enter a valid email address to subscribe from our newsletter.";
		alert(message);
		return;
	}
	
	if(document.subscribeForm.subEmail.value == "")
	{
		valid = false;
		message = "Please enter your email address to subscribe from our newsletter.";
		alert(message);
		return;
	}
	
	if(valid == true)
	{
		document.subscribeForm.submit();
	}
	else
	{
		return;
	}
}

/**
 * This validates the unSubscribe form
 *
 */
function validateUnSubscribeForm()
{
	var valid = true;
	var message = "";
	
	if(validateEmail(document.unsubscribeForm.unSubEmail.value) == false)
	{
		valid = false;
		message = "Please enter a valid email address to unsubscribe to our newsletter.";
		alert(message);
		return;
	}
	
	if(document.unsubscribeForm.unSubEmail.value == "")
	{
		valid = false;
		message = "Please enter your email address to unsubscribe to our newsletter.";
		alert(message);
		return;
	}
	
	if(valid == true)
	{
		document.unsubscribeForm.submit();
	}
	else
	{
		return;
	}
}

/**
 * This validates the Contact form
 * 
 */
function validateContactForm()
{
	var valid = true;
	var message = "";
	
	if(document.contactForm.name.value == "")
	{
		valid = false;
		message = "Please enter your name";
		alert(message);
		return;
	}
	
	if(document.contactForm.company.value == "")
	{
		valid = false;
		message = "Please enter your company name";
		alert(message);
		return;
	}
	
	if(document.contactForm.address.value == "")
	{
		valid = false;
		message = "Please enter your address details";
		alert(message);
		return;
	}
	
	if(document.contactForm.city.value == "")
	{
		valid = false;
		message = "Please enter your city";
		alert(message);
		return;
	}
	
	if(document.contactForm.postcode.value == "")
	{
		valid = false;
		message = "Please enter your postcode";
		alert(message);
		return;
	}
	
	if(document.contactForm.telephone.value == "")
	{
		valid = false;
		message = "Please enter your telephone number";
		alert(message);
		return;
	}
	
	if(document.contactForm.email.value == "")
	{
		valid = false;
		message = "Please enter your email address";
		alert(message);
		return;
	}
	
	if(validateEmail(document.contactForm.email.value) == false)
	{
		valid = false;
		message = "Please enter a valid email address";
		alert(message);
		return;
	}
	
	if(valid == true)
	{
		document.contactForm.submit();
	}
	else
	{
		return;
	}
}


/**
 * This validates the Sourcing Guide Contact form
 * 
 */
function validateSourceContactForm()
{
	var valid = true;
	var message = "";
	
	if(document.sourceContactForm.name.value == "")
	{
		valid = false;
		message = "Please enter your name";
		alert(message);
		return;
	}
	
	
	if(document.sourceContactForm.telephone.value == "")
	{
		valid = false;
		message = "Please enter your telephone number";
		alert(message);
		return;
	}
	
	if(document.sourceContactForm.email.value == "")
	{
		valid = false;
		message = "Please enter your email address";
		alert(message);
		return;
	}
	
	if(validateEmail(document.sourceContactForm.email.value) == false)
	{
		valid = false;
		message = "Please enter a valid email address";
		alert(message);
		return;
	}
	
	if(valid == true)
	{
		document.sourceContactForm.submit();
	}
	else
	{
		return;
	}
}

/**
 * This validates the email address
 *
 */
function validateEmail(str) 
{
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1)
		{
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		    return false
		}

		if (str.indexOf(at,(lat+1))!=-1)
		{
		    return false
		}

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		 {
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1)
		 {
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1)
		 {
		    return false
		 }

 		 return true					
}

//------------------------
// Old functions here
//------------------------

function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }

function getVal(obj)
{
        
	if (parseInt(document.forms['mainForm'][obj].value, 10) == 'NaN')
	{
		return -1;
	} 
	else 
	{
		return new Number(document.forms['mainForm'][obj].value);
	}        
}

function round(number,x) 
{
	// rounds number to x decimal places, defaults to 2
    x = (!x ? 2 : x);
    return Math.round(number*Math.pow(10,x))/Math.pow(10,x);
}        
		
function calc()
{
	var f = document.mainForm;
	var vol = getVal('thickness') * getVal('width') * getVal('length') * getVal('pieces'); 
	var volMCubed, volPerPiece, priceTotal, pricePerPiece,priceLinearM ,price100LinerM;
	
	
	volMCubed = vol/1000000000;
	volPerPiece = volMCubed / getVal('pieces');
	priceTotal = getVal('pricePerMCubed') * volMCubed;
	pricePerPiece = priceTotal / getVal('pieces');
	priceLinearM = priceTotal /( getVal('length')/1000 * getVal('pieces'));
	price100LinearM = priceLinearM * 100;
	
	f.volMCubed.value = round(volMCubed, 3);
	f.volPerPiece.value = round(volPerPiece, 4);
	f.priceTotal.value = round(priceTotal, 2);
	f.pricePerPiece.value = round(pricePerPiece, 2);
	f.priceLinearM.value = round( priceLinearM,2);
	f.price100LinerM.value = round( price100LinearM , 2);
	return;

}

//------------------------
// End of functions
//------------------------


//------------------------
// Load Events here
//------------------------

addLoadEvent(addButtonActions);

//------------------------
// End of Load Events
//------------------------