/*------------------------------------------------------------------------------
 
	* 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');
				
				// Multifield phone numbers
				if(thisID.substr(0, 6) == 'iphone') {
					thisID = 'iphone';
				}
				
				try {
					messages[id] = $('label[for='+thisID+']').attr('is').replace('|','\n');
				}
				catch (e) {
					
				}
			});

			alert(messages.unique().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;
		
		var value = obj.val();
		var myId = obj.attr('id');
		var phoneRegEx = "^[0-9\)\( .\\/#]{1,50}$";
		
		if (obj.attr('rel') && obj.attr('id') != 'iphone_phone' && obj.attr('id') != 'iphone_area' && obj.attr('id') != 'iphone_country') {
			pattern = new RegExp(stripslashes('^' + obj.attr('rel') + '$'));
		}
		if (obj.attr('rel') && (obj.attr('id') == 'iphone_phone' || obj.attr('id') == 'iphone_area' || obj.attr('id') == 'iphone_country')) {
			pattern = new RegExp(phoneRegEx);
		}
		if ((pattern && pattern.test(value)) || (!pattern && value != '')) {
			
			if (obj.attr('id') == 'iphone_phone') {
				if (!validPhone(value,"4","17","yes")) { 
					obj.addClass('error').removeClass('ok');
					return false;
				}
			}
			else if (obj.attr('id') == 'iphone_area') {
				if (!validPhone(value,1,4)) { 
					obj.addClass('error').removeClass('ok');
					return false;
				}
			}
			else if (obj.attr('id') == 'iphone_country') {
				if (!validPhone(value,1,3)) { 
					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,min,max,long) {
		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 < min || valueCount > max) { 
			return false;
		}
		//validate this only if phone number field is being validated
		if (long == "yes") {
			if (value.indexOf("#") == -1 && valueCount > 12) {
				return false;
				
			} 
			if (value.indexOf("#") != -1 && valueCount > 16) {
				return false;
			}
			// Check if phone is the same character repeated
	        if (numsOnly.replace(noRepeat , "") == "") {
	        	long = "no";
	            return false;
	        }
	
	        // Check that there are no characters other than digits and "#"
	        if (value.replace(/[0-9#]/g,'') != '') {
	        	long = "no";
	            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 ) {
	        	long = "no";
	        	return false;
	        }
		}
		return true;
	}
	
/* Toggle for footer links */
/* Author: Lior Iluz */
	
	$(function() {
		var tabContainers = $('div.tabs > div');
		tabContainers.hide().filter(':first').show();

		$('div.tabs ul.tabNav a').click(function() {
			
			//checks if user clicked on a regular link like "About Us" to allow it open in a new window
			if ($(this).hasClass('untabbed')) {
				tabContainers.hide();
				return true;
			}
			tabContainers.hide();
			
			//check if the link clicked is already open and collapse it back.
			if ($(this).hasClass('selected')) { 
				tabContainers.hide();
				$('div.tabs ul.tabNav a').removeClass('selected');
			}
			//if not, collapse the clicked link.
			else {		
				tabContainers.filter(this.hash).show();
				
				$('div.tabs ul.tabNav a').removeClass('selected');
				$(this).addClass('selected');
			}		
			return false;
		});
		//hide on load and remove "selected" from all links to avoid double clicking on the first link to expand it
		tabContainers.hide();
		$('div.tabs ul.tabNav a').removeClass('selected');
	});
	

	Array.prototype.unique = function () {
		var r = new Array();
		o:for(var i = 0, n = this.length; i < n; i++)
		{
			for(var x = 0, y = r.length; x < y; x++)
			{
				if(r[x]==this[i])
				{
					continue o;
				}
			}
			r[r.length] = this[i];
		}
		return r;
	}


/* Chatlink for Widget */
/* Author: Lior Iluz */
function popupChat() {
    
    if (typeof chatlink != "undefined" && chatlink != "") {
        window.open(chatlink+'&referrer='+escape(document.location),'chat34252572','width=475,height=400,resizable=yes');
        return false;
    }

}
