// JavaScript Document

function externalLinks(){
	var links = $$('a');
	links.each (function(el){
		var rel = el.getProperty('rel');
		if (rel == 'external'){
			el.setProperty ('target', '_blank');
		}
	});
}

function _paypal_link(){
	var paypal_logo = $$('#paypal_logo');
	if (paypal_logo.length > 0){
		$('paypal_logo').setStyle ('margin-left', '-20px');
		$('paypal_logo').addEvent ('mouseover', function(el){
			this.setStyle('cursor', 'pointer');
		});
		$('paypal_logo').addEvent ('click', function(el){
			window.open('https://www.paypal.com/uk/cgi-bin/webscr?cmd=xpt/cps/popup/OLCWhatIsPayPal-outside','olcwhatispaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=350');
		});
	}
}

// JavaScript Document

function startforms(){
	var forms = $$('form');
	forms.each (function(el){
		el.addEvent('submit', function(e){
			if (!validateForm(el.id)){
				//alert(e);
				e = new Event(e);
				e.stop();
			}
		});
	});
	var req_fields = $$('form .req');
	req_fields.each (function(el){
		el.addEvent('blur', function(e){
			Error = verifyField(el.id);
		});
	});
}

function verifyField(id){
	var error = '';
	var type = '';
	var rel_content  = '';
	var match = false;

	rel_content = $(id).getProperty('rel');
	if (rel_content){
		if (rel_content.toLowerCase().indexOf('email') != -1){
			type = 'email';
		}
		if (rel_content.indexOf('|') != -1){
			match = rel_content.substring(rel_content.indexOf('|')+1);
			rel_content = rel_content.substring(0,rel_content.indexOf('|'));
		}
		if ($(id).getProperty('type')=='checkbox') {
			type = 'checkbox'
		}
	}
	switch (type){
		case 'email':
			var value = $(id).value;
			apos = value.indexOf("@");
			dotpos = value.lastIndexOf(".");
			if (apos<1 || dotpos-apos<2){
				error += rel_content + (value?' (invalid)':'')+'\r\n';
			}else{
				if(match){
					if(value != $(match).value){
						error += rel_content + (value?' (doesn\'t match '+$(match).getProperty('rel')+')':'')+'\r\n';

					}
				}
			}
			break;
		case 'checkbox':
			var checked = $(id).checked;
			if (!checked){
				error += rel_content + '\r\n';
			}
			break;
		default:
			var value = $(id).value;
			if (!value){
				if (rel_content){
					error += rel_content + '\r\n';
				} else {
					error = $(id).getProperty('name');
				}
			}
			break;
	}
	var p = $(id).getParent();
	if (error){
		error = '- '+error;
		p.addClass ('invalid');
		p.removeClass('valid');
		/*var div = new Element('div');
		div.appendText('Error! Please complete this field!');
		div.injectAfter($(id));*/
	} else {
		p.addClass('valid');
		p.removeClass('invalid');
	}
	return error;
}

function validateForm(id){
	main_error = '';
	firstError = '';
	var fields = $$('form#' + id + ' .req');
	for (a=0; a<fields.length; a++){
		var Return = verifyField(fields[a].id)
		if (Return){
			main_error += Return;
			firstError = firstError ? firstError : fields[a].id;
		}
	}
	if (main_error){
		$(firstError).focus();
		main_error = "Please complete the following required fields: \n\n"+main_error;
		alert (main_error);
		return false;
	}
	return true;
}
function _getmap(){
	var map_exists = $$('#gmap');
	if (map_exists.length > 0){
		window.addEvent('load',function(){
			load('gmap');
		});	
		window.addEvent('unload',function(){
			GUnload();
		});	
	}
}

function orderform(){
	//var order
}

window.addEvent('domready', function(){
	externalLinks();
	_paypal_link();
	_getmap();
	startforms();
	orderform();
});