/*------------------------------------------------------------------------------
 
	* Filename:		script.js
	* Dated:		Dec 22 2009
	* Author:		Andy Keren
	* Project:		Learn Trade LP

------------------------------------------------------------------------------*/
/* ========================================================================== */

	document.myFormSubmitted = false;
	
	function form_validate() {
		$('#commit input[type=text]').each(function(){
			validators(this);
		});
		
		fields = $('#commit input.error');
		if (fields.length) {
			messages = [];
			fields.each(function(id, field){
				thisID = $(this).attr('id');
				messages[id] = $('label[for='+thisID+']').attr('is').replace('|','\n');
			});
			alert(messages.join('\n\n'));
			return false;
		}

		if (! document.myFormSubmitted) {
			document.myFormSubmitted = true;
			$('#commit input[type=submit]').attr('disabled','');
			return true;
		} 
		
		return false;
		
	}
	
	/* Tooltip */
	$(function() {
		
		$(".question").hover(function () {
			$(this).attr("innerHTML", '<div class="tooltip"><span>' + $(this).attr('tooltip') + '</span></div>');
		}, function () {
			$(this).attr("innerHTML", '');
		});
		
		$('label').each(function(){
			errorString = $(this).attr('title');
			if (errorString != '') {
				$(this).attr('title','').attr('errorString',errorString);
			}
		});
		/* END Tooltip */
		
		$('#gobtn').bind('click',clickSubmit).bind('dblclick',clickSubmit);
		
		$('#commit').submit(function (){ return form_validate(); });
		$('#commit input[type=text]').bind('blur',function(){
			validators(this);
		});
	});
	
		
	function clickSubmit (e) {
		e.preventDefault();
		$('#commit').submit();
	}
	
	function validators (obj) {
		obj = $(obj);
		pattern = false;
		
		if (obj.attr('id') == 'customer-site') {
			return;
		}
		
		var value = obj.val();
		var myId = obj.attr('id');
		var phoneRegEx = "^[0-9\)\( .\\/+_-]{1,50}$";
		
		if (obj.attr('rel') && obj.attr('id') != 'customer-phone') {
			pattern = new RegExp(stripslashes('^' + obj.attr('rel') + '$'));
		}
		if (obj.attr('rel') && obj.attr('id') == 'customer-phone' ) {
			pattern = new RegExp(phoneRegEx);
		}
		if ((pattern && pattern.test(value)) || (!pattern && value != '')) {
			
			if (obj.attr('id') == 'customer-phone') {
				if (!validPhone(value)) { 
					obj.addClass('error').removeClass('ok');
					return false;
				}
			}
			obj.addClass('ok').removeClass('error');
		}
		
		else {
			obj.addClass('error').removeClass('ok');
		}
	}
	
	function stripslashes( str ) {
		// http://kevin.vanzonneveld.net
		// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
		// +   improved by: Ates Goral (http://magnetiq.com)
		// +      fixed by: Mick@el
		// +   improved by: marrtins
		// +   bugfixed by: Onno Marsman
		// +   improved by: rezna
		// *     example 1: stripslashes('Kevin\'s code');
		// *     returns 1: "Kevin's code"
		// *     example 2: stripslashes('Kevin\\\'s code');
		// *     returns 2: "Kevin\'s code"
	 
		return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
	}

	function validPhone(value) {
		
		var value = $.trim(value);
        var numsOnly = value.replace(/[^0-9]/g,'');
        var noRepeat = RegExp(numsOnly.substr(0,1), "g");
        
		var valueCount = numsOnly.length;
		

        // Check if phone is in the right length
		if (valueCount < 6 || valueCount > 15) { 
			return false;
		}
		
		// Check if phone is the same character repeated
        if (numsOnly.replace(noRepeat , "") == "") {
            return false;
        }

        // Check that there are no characters other than digits and "()-.#+ "
        if (value.replace(/[0-9()-.#+ ]/g,'') != '') {
            return false;
        }

        // Check if string is not part of "0987654321" or "1234567890"
        if ("1234567890".indexOf(numsOnly) != -1 || "0987654321".indexOf(numsOnly) != -1 || "0123456789".indexOf(numsOnly) != -1 ) {
            return false;
        }

		return true;
	}
