﻿/// <reference path="jquery-1.4.4-vsdoc.js" />
/*
Copyright in this document remains the property of Rapp Limited and this document may not be reproduced in whole or part without our prior written consent.  All information of whatever nature contained in this document is strictly confidential and may not be used, implemented, adapted or communicated to any third party without the consent of Rapp Limited.
*/
var fieldErrors = {title:"Please select a valid title",
			  forename:"Please enter a valid forename",
			  surname:"Please enter a valid surname",
			  name:"Please enter a valid name",
			  address1:"Please enter address line 1",
			  company:"Please enter a Company Name",
			  position:"Please enter a position",
			  jobtitle:"Please enter a jobTitle",
			  postcode:"Please enter a valid postcode",
			  telephone:"Please enter a valid telephone number",
			  email:"Please enter a valid email address",
			  emailFail:"This email address is already registered",
			  compulsory:"This is a compulsory field",
			  nonBlank:"This field must not be left blank"};
var tagTranslate = {INPUT:"val",
			  		SELECT:"val"}
var invalid = false;
var mouseDown = false;

function fieldAlertUpdate(obj){
	$.extend(fieldErrors,obj);	
}

$(document).ready(function() {
	
	var isCtrl = false;					   
	$(document).keyup(function (e) {
		if(e.which == 17) isCtrl=false;
	})
	$(document).keydown(function (e) { 
		if(e.which == 17) isCtrl=true;
	}); 	
	$(".justNo").keypress(function(event) {

		var key = event.which;
		//allows ctrl+v ctrl+c ctrl+x backspace tab
		if ((isCtrl && (key=118 || key == 86 || key == 120 || key == 88 || key == 99 || key == 67)) || key == 8 || key == 0) {
			return true;	
		}

		//allows all keys 0 – 9 plus space
		var thisReg = /^[0-9 ]+$/;
		if (!thisReg.test(String.fromCharCode(key))) {
			event.preventDefault()
		}
	});

	$('#submit').mousedown(function(){mouseDown=true}).click(function() {
		$('div.error').remove();
		errorCheckAll();
		mouseDown=false;
		if (invalid) {
			invalid=false;
			return false;
		}
	});
	$('.simpleErrorBlur').blur(function (){
		if ($(this).next().attr('class') == "error siefl") {
		   	$(this).next().remove();	
		}
		if (!mouseDown) {
			errorCheckBlur($(this));
		} /*else {
			$('div.error').remove();
			errorCheckAll();
		}*/
	});
});

function startFromNet(e) {
    $('div.error').remove();
    errorCheckAll();
    if (invalid) {
        invalid = false;
        return false;
        e.stopPropagation();
    }
}

function errorCheckAll() {
	$('[class^="simpleError"]').each(function(n) {
		//var field = this[tagTranslate[this.tagName]];
		var compulsory = false;
		var attributes = $(this).attr('dataError').split(' ');
		var messageType = attributes[attributes.length - 1];
		var message = fieldErrors[messageType];
		if (attributes[0] == 'compulsory') {
			compulsory = true;
		}
		var obj = $(this);
		testField(obj,compulsory,message,messageType);
	});
}

function errorCheckBlur(a) {
		var obj = a;
		var compulsory = false;
		var attributes = obj.attr('dataError').split(' ');
		var messageType = attributes[attributes.length - 1];
		var message = fieldErrors[messageType];
		if (attributes[0] == 'compulsory') {
			compulsory = true;
		}
		testField(obj,compulsory,message,messageType);
}

function testField(a,b,c,d) {
	var thisInvalid = false;
	var obj = a;
	var com = b;
	var mes = c;
	var mType = d;

	if (com) {
		if (obj.val() == '') {
			invalid=true;
			thisInvalid=true;
			a.after('<div class="error siefl"><span class="error-icon">^</span><strong>Alert - </strong>' + c + '</div>');
		}
	}
	
	if (!thisInvalid & obj.val() > '') {
		console.log(mType);
		switch (mType) {
			case 'email':
				var thisReg = /^[-a-zA-Z0-9][-.a-zA-Z0-9]*@[-.a-zA-Z0-9]+(\.[-.a-zA-Z0-9]+)*\.(com|edu|info|gov|int|mil|net|org|biz|name|museum|coop|aero|pro|tv|[a-zA-Z]{2})$/;
				if (!thisReg.test(obj.val())) {
					invalid=true;
					a.after('<div class="error siefl"><span class="error-icon">^</span><strong>Alert - </strong>' + c + '</div>');
				}
				break;
			case 'postcode':
				var postcode = $(obj).val().replace(/ /g,'').toUpperCase();
				var thisReg = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$/;
				if (!thisReg.test(postcode) && !(postcode == 'SANTA1' || postcode == 'GIR0AA')) {
					invalid=true;
					a.after('<div class="error siefl"><span class="error-icon">^</span><strong>Alert - </strong>' + c + '</div>');
				}
				break;
			case 'surname':  //alpha only (plus space, hyphen or single quote) min 3 bytes
			case 'jobtitle':
			case 'name':
				var thisReg = /^[a-zA-Z '-]+$/;
				if (!thisReg.test(obj.val()) || obj.val().length < 3) {
					invalid=true;
					a.after('<div class="error siefl"><span class="error-icon">^</span><strong>Alert - </strong>' + c + '</div>');
				}
				break;
			case 'telephone':   //numeric only (plus space)
				var telno = $(obj).val().replace(/ /g,'');   
				var thisReg = /^[0]{1}[0-9]{9,}$/;
				if (!thisReg.test(telno)) {
					invalid=true;
					a.after('<div class="error siefl"><span class="error-icon">^</span><strong>Alert - </strong>' + c + '</div>');
				}
				break;
			case 'forename':   //alpha only (plus space)   
				var thisReg = /^[a-zA-Z ]+$/;
				if (!thisReg.test(obj.val())) {
					invalid=true;
					a.after('<div class="error siefl"><span class="error-icon">^</span><strong>Alert - </strong>' + c + '</div>');
				}
				break;
			default:
				break;
		}
	}
}

