// contact.js
// Byde Heating & Plumbing Ltd

// Globals
var INITMSG = 'Type your message here...';

window.onload = initialize; 

// onload
function initialize () {
	if (!W3CDOM) return;

	var obj, frm;	
	var rimg = new Array(1);

	// Common (main.js)
	setCommonElements();

	// Rollovers
/*		
	obj = document.getElementById('hd_showerwater');
	if (obj) {
		rimg['hd_showerwater'] = new RolloverImg(104,104,'/img/hd_showerwater.gif','/img/hd_showerwater_off.gif');	
		obj.onmouseover = function() {this.src = rimg['hd_showerwater'].imgOver.src;}
		obj.onmouseout = function() {this.src = rimg['hd_showerwater'].imgOut.src;}			
	}
*/

	// Form...
	frm = document.forms['contactform'];

	// Init Email Event
	frm.yremail.onkeyup = checkEmail;
	frm.sendemail.onmouseup = frm.sendemail.onkeyup = function() {document.contactform.yremail.onkeyup = null;}

	// Init Form Text
	if ((frm.details.value.length == 0) || (frm.details.value == INITMSG)) {
  	frm.details.value = INITMSG;			
  	obj = document.getElementById('details');
  	if (obj) {
  		//obj.innerHTML = INITMSG;
  		obj.style.color = '#cccccc';
  		obj.onfocus = initTextArea;
  	}
	}

	// Make sure first field of form has focus
	frm.yrname.focus();	
}

// Checks email address field and if something then 'checks' the email checkbox 
function checkEmail() {
	var obj = document.getElementById('sendemail');
	if (obj) {
		obj.checked = (this.value > ' ');
	}
}

// Clears textarea and changes style to black when clicked on for the first time
function initTextArea() {
	this.onfocus = null;	// So won't call again
	this.value = '';
	this.style.color = '#000000';
}

// Pass [F]ield[N]ame
function alertField(FN) {
	var obj;
	obj = document.getElementById('lbl'+FN);
	if (obj) { obj.style.color = '#ff0000'; }
	obj = document.getElementById(FN+'_arrow');
	if (obj) { obj.style.visibility = 'visible'; }
}

// Pass [F]ield[N]ame
function okField(FN) {
	var obj;
	obj = document.getElementById('lbl'+FN);
	if (obj) { obj.style.color = '#000000'; }
	obj = document.getElementById(FN+'_arrow');
	if (obj) { obj.style.visibility = 'hidden'; }
}

function okAllFields() {
	var flds = new Array('yrname','yrphone','yremail','subject','details');
	var i;
	for (i=0; i<flds.length; i++) {
		okField(flds[i]);
	}
}

// Validates contact form
// - All problem fields are highlighted/alerted
// - Focus is set to first problem field
// Returning False prevents form submit
function checkForm() {
	var ok = true;
	var frm = document.forms['contactform'];
	var msg = '';
	var focusctrl = null;
	
	// Ok/Reset all fields
	okAllFields(); 
	
	// No Name
	if (frm.yrname.value < ' ') {
		ok = false;
		focusctrl = frm.yrname;
		msg += '\n- You have not entered your name.';
		alertField('yrname');		
	}
	
	// No Phone or Email
	if ((frm.yrphone.value < ' ') && (frm.yremail.value < ' ')) {
		ok = false;
		if (focusctrl == null) { focusctrl = frm.yrphone; }		
		msg += '\n- You must enter either a phone number or valid email address.';
		alertField('yrphone');
		alertField('yremail');				
	}
	
	// Valid Phone
	if ((frm.yrphone.value > ' ') && (frm.yrphone.value.length < 5)) {
		ok = false;
		if (focusctrl == null) { focusctrl = frm.yrphone; }		
		msg += '\n- The phone number you have entered does not appear to be valid.';
		alertField('yrphone');		
	}
	
	// Valid Email
	if ((frm.yremail.value > ' ') && (!isValidEmail(frm.yremail.value))) {
		ok = false;
		if (focusctrl == null) { focusctrl = frm.yremail; }		
		msg += '\n- The email address you have entered does not appear to be valid.';
		alertField('yremail');		
	}
	
	// No Subject
	if (frm.subject.value < ' ') {
		ok = false;
		if (focusctrl == null) { focusctrl = frm.subject; }		
		msg += '\n- You must enter a "Subject" for the message.';
		alertField('subject');
	}

	// No Details
	if ((frm.details.value < ' ') || (frm.details.value == INITMSG)) {
		ok = false;
		if (focusctrl == null) { focusctrl = frm.details; }		
		msg += '\n- You have not entered any message in the "Details" text box.';
		alertField('details');
	}
	
	if (!ok) {
		alert('Please correct the following information before sending...\n' + msg + '\n\nClick [Ok] to return to the contact form.');
		focusctrl.focus();
	}
	return ok;
}

// Returns true if email ok, false otherwise
function isValidEmail(Email) {
	var badch = ' ,#!';	// Invalid Chars
	var at = "@"
	var dot = "."
	var lat = Email.indexOf(at);
	var ldot = Email.indexOf(dot);
	var lstr = Email.length - 1;
	var i;
	
	// No '@' OR '@' is at start OR '@' is at end
	if ((lat <= 0) || (Email.lastIndexOf(at) == lstr)) {
		return false;
	}

	// No '.' OR '.' is at start OR '.' is near end (must have at least 2 char TLD) 
	if ((ldot <= 0) || (Email.lastIndexOf(dot) >= (lstr - 1))) {
		return false;
	}

	// Presence of a 2nd '@'
	if (Email.indexOf(at,(lat+1)) != -1) {
		return false;
	}

	// If there is a dot either side of the '@'
	if ((Email.substring(lat-1,lat) == dot) || (Email.substring(lat+1,lat+2) == dot)) {
		return false;
	}

	// No '.' after the 1 char after the '@'
	if (Email.indexOf(dot,(lat+2)) == -1) {
		return false;
	}
		
	// Check for invalid characters
	for (i=0; i<badch.length; i++) {
		if (Email.indexOf(badch.charAt(i)) != -1) {
			return false;
		}
	}

	return true;					
}

// Using regExp...
function x_isValidEmail(str){
  var filter=/^.+@.+\..{2,3}$/
  return (filter.test(str))
	
	
//validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
//strEmail = document.form1.email.value;
	
}	

// Simple Version...
function xx_isValidEmail(str) {
	return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
}
