﻿// JavaScript Document

(function() {
		  
	if (window.addEventListener) window.addEventListener("load", wws_js_init, false);
	else if (window.attachEvent) window.attachEvent("onload", wws_js_init);

	function wws_js_init() {
		var cur_form;
		if (document.eng_tellafriend) { document.eng_tellafriend.onsubmit = eng_validate_taf_on_submit; cur_form = document.eng_tellafriend; }
		if (document.heb_tellafriend) { document.heb_tellafriend.onsubmit = heb_validate_taf_on_submit; cur_form = document.heb_tellafriend; }
		if (document.eng_contactform) { document.eng_contactform.onsubmit = eng_validate_ctf_on_submit; cur_form = document.eng_contactform; }
		if (document.heb_contactform) { document.heb_contactform.onsubmit = heb_validate_ctf_on_submit; cur_form = document.heb_contactform; }
		// set focus on form's first element
		if (cur_form) cur_form.fname.focus();
	}
	
	// validate english tell-a-friend form

	function eng_validate_taf_on_submit() {
		var invalid = false;
		
		var f = document.eng_tellafriend;
		
		// verify friend name
		var e1 = f.fname;
		var v1 = e1.value;
		if (v1.search("\\S") == -1) {
			alert("Fields marked with an asterisk are mandatory");
			e1.focus();
			return false;
		}

		// verify friend email
		var e2 = f.femail;
		var v2 = e2.value;
		if (v2.search("\\S") == -1) {
			alert("Fields marked with an asterisk are mandatory");
			e2.focus();
			return false;
		}

		// allowed email format: "*@*.*"
		if (v2.search("\\S") != -1) {
			// non-empty email
			if ((v2.search("^[^@]{1,64}@[^@]{1,255}$") == -1) ||
				(v2.search("^\\S{1,64}@\\S{1,253}\\.[a-zA-z0-9]{2,6}$") == -1)) {
				alert("Illegal email address");
				e2.focus();
				return false;
			}
		}

		// verify sender name
		var e3 = f.yname;
		var v3 = e3.value;
		if (v3.search("\\S") == -1) {
			alert("Fields marked with an asterisk are mandatory");
			e3.focus();
			return false;
		}

		// verify sender email
		var e4 = f.yemail;
		var v4 = e4.value;
		if (v4.search("\\S") == -1) {
			alert("Fields marked with an asterisk are mandatory");
			e4.focus();
			return false;
		}

		// allowed email format: "*@*.*"
		if (v4.search("\\S") != -1) {
			// non-empty email
			if ((v4.search("^[^@]{1,64}@[^@]{1,255}$") == -1) ||
				(v4.search("^\\S{1,64}@\\S{1,253}\\.[a-zA-z0-9]{2,6}$") == -1)) {
				alert("Illegal email address");
				e4.focus();
				return false;
			}
		}

		// verify message
		var e5 = f.content;
		var v5 = e5.value;
		// cannot exceed 1024 characters
		if (v5.length > 1024) {
			alert("The comments field cannot exceed 1024 characters");
			e5.focus();
			return false;
		}

	}

	// validate hebrew tell-a-friend form

	function heb_validate_taf_on_submit() {
		var invalid = false;
		
		var f = document.heb_tellafriend;
		
		// verify friend name
		var e1 = f.fname;
		var v1 = e1.value;
		if (v1.search("\\S") == -1) {
			alert("שדות המסומנים בכוכבית הינם שדות חובה");
			e1.focus();
			return false;
		}

		// verify friend email
		var e2 = f.femail;
		var v2 = e2.value;
		if (v2.search("\\S") == -1) {
			alert("שדות המסומנים בכוכבית הינם שדות חובה");
			e2.focus();
			return false;
		}

		// allowed email format: "*@*.*"
		if (v2.search("\\S") != -1) {
			// non-empty email
			if ((v2.search("^[^@]{1,64}@[^@]{1,255}$") == -1) ||
				(v2.search("^\\S{1,64}@\\S{1,253}\\.[a-zA-z0-9]{2,6}$") == -1)) {
				alert("כתובת לא חוקית של דואר אלקטרוני");
				e2.focus();
				return false;
			}
		}

		// verify sender name
		var e3 = f.yname;
		var v3 = e3.value;
		if (v3.search("\\S") == -1) {
			alert("שדות המסומנים בכוכבית הינם שדות חובה");
			e3.focus();
			return false;
		}

		// verify sender email
		var e4 = f.yemail;
		var v4 = e4.value;
		if (v4.search("\\S") == -1) {
			alert("שדות המסומנים בכוכבית הינם שדות חובה");
			e4.focus();
			return false;
		}

		// allowed email format: "*@*.*"
		if (v4.search("\\S") != -1) {
			// non-empty email
			if ((v4.search("^[^@]{1,64}@[^@]{1,255}$") == -1) ||
				(v4.search("^\\S{1,64}@\\S{1,253}\\.[a-zA-z0-9]{2,6}$") == -1)) {
				alert("כתובת לא חוקית של דואר אלקטרוני");
				e4.focus();
				return false;
			}
		}

		// verify message
		var e5 = f.content;
		var v5 = e5.value;
		// cannot exceed 1024 characters
		if (v5.length > 1024) {
			alert("אורכו של שדה ההערות אינו יכול לעלות על 1024 תווים");
			e5.focus();
			return false;
		}
	}

	// validate english contact form (ctf)

	function eng_validate_ctf_on_submit() {
		var invalid = false;
		
		var f = document.eng_contactform;
		
		// verify name
		var e1 = f.fname;
		var v1 = e1.value;
		if (v1.search("\\S") == -1) {
			alert("Fields marked with an asterisk are mandatory");
			e1.focus();
			return false;
		}

		// verify phone
		var e2 = f.phone;
		var v2 = e2.value;
		if (v2.search("\\S") == -1) {
			alert("Fields marked with an asterisk are mandatory");
			e2.focus();
			return false;
		}

/*
		if (v2.search("^[0-9]{9,12}$") == -1) {
			alert("Invalid phone number - include full number, digits only");
			return false;
		}
*/

		// verify email
		var e3 = f.email;
		var v3 = e3.value;
		if (v3.search("\\S") == -1) {
			alert("Fields marked with an asterisk are mandatory");
			e3.focus();
			return false;
		}
		
		if (v3.search("\\S") != -1) {
			// non-empty email
			if ((v3.search("^[^@]{1,64}@[^@]{1,255}$") == -1) ||
				(v3.search("^\\S{1,64}@\\S{1,253}\\.[a-zA-z0-9]{2,6}$") == -1)) {
				alert("Illegal email address");
				e3.focus();
				return false;
			}
		}

		// verify message
		var e4 = f.mtext;
		var v4 = e4.value;
		if (v4.search("\\S") == -1) {
			alert("Fields marked with an asterisk are mandatory");
			e4.focus();
			return false;
		}

		if (v4.length > 1024) {
			alert("The text field cannot exceed 1024 characters");
			e4.focus();
			return false;
		}
	}

	// validate hebrew contact form (ctf)

	function heb_validate_ctf_on_submit() {
		var invalid = false;
		
		var f = document.heb_contactform;
		
		// verify name
		var e1 = f.fname;
		var v1 = e1.value;
		if (v1.search("\\S") == -1) {
			alert("שדות המסומנים בכוכבית הינם שדות חובה");
			e1.focus();
			return false;
		}

		// verify phone
		var e2 = f.phone;
		var v2 = e2.value;
		if (v2.search("\\S") == -1) {
			alert("שדות המסומנים בכוכבית הינם שדות חובה");
			e2.focus();
			return false;
		}

		// validate phone (hebrew form only)
		if (v2.search("^[0-9]{9,12}$") == -1) {
			alert("מספר הטלפון אינו חוקי - אזור חיוג חובה, ספרות בלבד");
			e2.focus();
			return false;
		}

		// verify email
		var e3 = f.email;
		var v3 = e3.value;
		if (v3.search("\\S") == -1) {
			alert("שדות המסומנים בכוכבית הינם שדות חובה");
			e3.focus();
			return false;
		}

		if (v3.search("\\S") != -1) {
			// non-empty email
			if ((v3.search("^[^@]{1,64}@[^@]{1,255}$") == -1) ||
				(v3.search("^\\S{1,64}@\\S{1,253}\\.[a-zA-z0-9]{2,6}$") == -1)) {
				alert("כתובת לא חוקית של דואר אלקטרוני");
				e3.focus();
				return false;
			}
		}

		// verify message
		var e4 = f.mtext;
		var v4 = e4.value;
		if (v4.search("\\S") == -1) {
			alert("שדות המסומנים בכוכבית הינם שדות חובה");
			e4.focus();
			return false;
		}

		if (v4.length > 1024) {
			alert("אורכו של שדה ההודעה אינו יכול לעלות על 1024 תווים");
			e4.focus();
			return false;
		}
	}

})(); 

