$(document).ready( function() {

if(parent.document.getElementById('main-saas')!=null||parent.document.getElementById('main-paas')!=null)
   $('#edit-interests-pop-wrapper .jquery-selectbox-list .item-3').insertBefore($('#edit-interests-pop-wrapper .jquery-selectbox-list .item-1'));

$('#send_contact_button').click(
		function () { 
			$('#block-block-32').hide();
			$('#block-block-33').hide();
		})


	$('.open-register').click(
		function () { 
			$('#block-block-32').slideToggle('slow');
			$('#block-block-33').slideToggle('slow');
		})


/*	if($.browser.msie) { 	// see if browser is IE
		$("input[type='text']").click(
			function () { //solve IE BG image bug partially
			$(this).css("background-position","100% 0");
		}).blur(
			function(){
			 $(this).css("overflow","hidden");
                        // $(this).css("background-position","0 0");
		});
	}*/	
	$(".captcha div.description").append('<span>*</span>');
	$(".captcha div.description span").addClass('form-required');
	$(".captcha div.description span").attr("title","This Field is required.");
	
	$('#gigaspaces-salesforce-contact-form input').each(function() {
		var $this = $(this);
		var t = $this.attr('value');		
		$(this).focus(function() {
			if($this.attr('value') == t) {$this.attr('value','');}
			$this.css('color','#666666');
		});
		$(this).blur(function() {
			if($this.attr('value') == '') {$this.attr('value',t);$this.css('color','#DADADA');}
			
		});
	});
	$('#gigaspaces-salesforce-contact-form select').each(function() {
		var $this = $(this);
		var t = $this.attr('value');		
		$(this).focus(function() {
			$this.css('color','#666666');
		});
		$(this).blur(function() {
			if($this.attr('value') == 'Country') {$this.css('color','#DADADA');}
			
		});
	});
	$('#gigaspaces-salesforce-contact-form #edit-question').each(function() {
		var $this = $(this);
		var t = $this.attr('value');
		$width = $(this).css('width');
		$height = $(this).css('height');
		$(this).click(function() {
			if($this.attr('value') == t) {$this.attr('value','');}
			//$(this).css('width','200px');
			$(this).css('height','37px');
			$('#edit-name').hide();
			$this.css('color','#666666');
		});
		$(this).blur(function() {
			if($this.attr('value') == '') {$this.attr('value',t);$this.css('color','#DADADA');}
			//$(this).css('width',$width);
			$(this).css('height',$height);
			$('#edit-name').show();
			
		});
	});

	//style the select boxes
	$('.form-item > select.other-action, .select-other-wrapper > select.other-action').each(function() {
		var other = $(this.parentNode.parentNode.parentNode).find('#edit-other-new-wrapper, #edit-other-pop-wrapper, #edit-other-wrapper,#edit-other-demo-wrapper');
		Effects.slideSelect.apply(this,[228, ($.browser.msie && $.browser.version == "6.0" ? 78 : 85),'Other',other]);
		//Effects.slideSelect.apply(this,[228, ($.browser.msie && $.browser.version == "6.0" ? 0 : 0),'Other',other]);
	});
/*Added By Avital*/
	$('.form-item > select.other-action1, .select-other-wrapper-1 > select.other-action1').each(function() {	
		var other1 = $(this.parentNode.parentNode.parentNode).find('#edit-other1-new-wrapper, #edit-other1-pop-wrapper, #edit-other1-wrapper,#edit-other1-demo-wrapper,#edit-other1-cloud-wrapper,#edit-other1-cloud1-wrapper,#edit-other1-land-wrapper');
		Effects.slideSelect1.apply(this,[228, ($.browser.msie && $.browser.version == "6.0" ? 78 : 85),'Other',other1]);
	});
/**/

	$('.form-item > select.styled,.select-other-wrapper > select.styled').each(function() {
		if ($.browser.msie && $.browser.version == "6.0") {
		//	return;
		}
		$(this).selectbox({animationSpeed:'fast'});
	});
/*Added By Avital*/
	$('.form-item > select.styled,.select-other-wrapper-1 > select.styled').each(function() {
		if ($.browser.msie && $.browser.version == "6.0") {
			return;
		}
		$(this).selectbox({animationSpeed:'fast'});
	});
/**/
	$('.form-item  > select.styled2,.select-other-wrapper > select.styled2').each(function() {
		if ($.browser.msie && $.browser.version == "6.0") {
		//	return;
		}
		$(this).sSelect({ddMaxHeight: '100px'});
	});
	$('select.other-action').each(function() {
				$(this).change();
	});
/*Added By Avital*/
	$('select.other-action1').each(function() {
				$(this).change();
	});
/**/	
	if( $(".messages.error").get(0) ) {
		$(".messages.error").css('display',"block");
	}
	if(location.hash.match('contact_sent')) {
		location.hash = location.hash.replace('contact_sent','');
		window.open('/contact/thankyou?popup=' + location.pathname.replace(/\//g,"-"),'mywindow','width=400,height=400,resizable=0,location=1,status=1,scrollbars=1');
	}


	/*$('form').each(function() {
		var self = this;
		$(this).find('#edit-message-wrapper').each(function() {
			$(this).remove();
			$(self).find('#edit-submit').before($(this));
		});
	});*/

 /* new contact*/
 if($('#edit-country-new-wrapper .jquery-selectbox .selectedTxt').text()=='United States')
 {
  $('#edit-state-new-wrapper').show('slow');
   if($('#edit-state-new').val()==''){
 	 $('.form-text-wrapper').addClass("Error-Text-Input");
 	 $('#edit-state-new-wrapper label').addClass("Error-Text-Label");
	}
 }


$('#edit-country-new-wrapper .jquery-selectbox ul li').click(function() {
 if($(this).text()=='United States')
    $('#edit-state-new-wrapper').show('slow');
 else
    $('#edit-state-new-wrapper').hide();

});

 /* contact us*/
if($('#edit-country-wrapper .jquery-selectbox .selectedTxt').text()=='United States')
 {
  $('#edit-state-wrapper').show('slow');
  if($('#edit-state').val()=='')
  $('.form-text-wrapper').addClass("Error-Text-Input");
 }


$('#edit-country-wrapper .jquery-selectbox ul li').click(function() {
 if($(this).text()=='United States')
   $('#edit-state-wrapper').show('slow');
 else
    $('#edit-state-wrapper').hide();


}); 
 /*landing contact us*/
if($('#edit-country-land-wrapper .jquery-selectbox .selectedTxt').text()=='United States')
 {
  $('#edit-state-land-wrapper').show('slow');
  if($('#edit-state-land').val()=='')
  $('.form-text-wrapper').addClass("Error-Text-Input");
 }


$('#edit-country-land-wrapper .jquery-selectbox ul li').click(function() {
 if($(this).text()=='United States')
   $('#edit-state-land-wrapper').show('slow');
 else
    $('#edit-state-land-wrapper').hide();


}); 
 /* pop contact me*/

if($('#edit-country-pop-wrapper .jquery-selectbox .selectedTxt').text()=='United States')
 {
 
  $('#edit-state-pop-wrapper').show('slow');
  if($('#edit-state-pop').val()==''){
  $('.form-text-wrapper').addClass("Error-Text-Input");
   $('#edit-state-pop-wrapper label').addClass("Error-Text-Label");
}
 }


$('#edit-country-pop-wrapper .jquery-selectbox ul li').click(function() {
 if($(this).text()=='United States')
   $('#edit-state-pop-wrapper').show('slow');
 else
    $('#edit-state-pop-wrapper').hide();


});




});	

/**
 * effect object for creating javascript  effects
 */
Effects = {};
/**
 * slide  select  effect to allow  an option to ask for text input in another field
 * (should be run when "this" is the select box it self (use apply))
 * @param orgWidth the original width of the select
 * @param smallWidth the small width of the select
 * @param keyWord the keyWord that the select is chosen to active  the slide
 * @param other the jquery object of the input field
 */
Effects.slideSelect = function( orgWidth, smallWidth ,keyWord, other ){		
	other.remove();			
		var leftOffset = smallWidth - orgWidth;
		 $(this).wrap('<div class="select-other-wrapper"></div');
		$(this).after(other);
		var orgOverflow = 'visible';
		//other.width(orgWidth -smallWidth -1);
		$(this).change(function(){
			if($(this).val() == keyWord ) {
				this.isOther = true;				
				$(this.parentNode.parentNode.parentNode).css('overflow','hidden');
				other.show();
				$(this.parentNode).animate({width:smallWidth+'px'},'normal','swing',function(){
					$(this).addClass('small-select-background');
					$(this.parentNode.parentNode).css('overflow',orgOverflow);										
				});
			} else if(this.isOther){
				$(this.parentNode.parentNode.parentNode).css('overflow','hidden');
				$(this.parentNode).removeClass('small-select-background');
				$(this.parentNode).animate({width: orgWidth+"px"},'normal','swing',function(){
						other.hide();
						$(this.parentNode.parentNode).css('overflow',orgOverflow);
				
				});				
				this.isOther= false;
			}
			
		});
};

/**
 * style the select boxes  
 * (should be run when "this" is the select box it self (use apply)) 
 */
Effects.styleSelect = function(){
	if($.browser.msie && $.browser.version == "6.0"){
		$(this).wrap("<div class='select-style-wrapper'></div>");
		return;
	}

	var div = $('<div class="select-background" style="position:absolute;"> </div>');
	$(this).wrap("<div class='select-style-wrapper'></div>");
	$(this).before(div);
	div.html($(this).find('option:selected').text());
	$(this).change(function(){
			div.html($(this).find('option:selected').text());
	});
};
/***addes by Avital*/
Effects.slideSelect1 = function( orgWidth, smallWidth ,keyWord, other){	
	other.remove();		
		var leftOffset = smallWidth - orgWidth;
		 $(this).wrap('<div class="select-other-wrapper-1"></div');
		$(this).after(other);
		var orgOverflow = 'visible';
		//other.width(orgWidth -smallWidth -1);
		$(this).change(function(){
			if($(this).val() == keyWord ) {
				this.isOther = true;				
				$(this.parentNode.parentNode.parentNode).css('overflow','hidden');
				other.show();
				$(this.parentNode).animate({width:smallWidth+'px'},'normal','swing',function(){
					$(this).addClass('small-select-background');
					$(this.parentNode.parentNode).css('overflow',orgOverflow);										
				});
			} else if(this.isOther){
				$(this.parentNode.parentNode.parentNode).css('overflow','hidden');
				$(this.parentNode).removeClass('small-select-background');
				$(this.parentNode).animate({width: orgWidth+"px"},'normal','swing',function(){
						other.hide();
						$(this.parentNode.parentNode).css('overflow',orgOverflow);
				
				});				
				this.isOther= false;
			}
			
		});
};
/**
 * style the select boxes  
 * (should be run when "this" is the select box it self (use apply)) 
 */
Effects.styleSelect1 = function(){	
	if($.browser.msie && $.browser.version == "6.0"){
		$(this).wrap("<div class='select-style-wrapper-1'></div>");
		return;
	}

	var div = $('<div class="select-background" style="position:absolute;"> </div>');
	$(this).wrap("<div class='select-style-wrapper-1'></div>");
	$(this).before(div);
	div.html($(this).find('option:selected').text());
	$(this).change(function(){
			div.html($(this).find('option:selected').text());
	});
};
/****/


function contactFormSubmit() {
	if(trim($('#edit-question').attr('value')) != '' &&  trim($('#edit-question').attr('value')) != 'Question' && validate_name(trim($('#edit-name').attr('value'))) != '' &&  trim($('#edit-name').attr('value')) != 'Full name' && $('#edit-country').attr('value') != 'Country' && validate_email($('#edit-email').attr('value')) ) {
	window.open('/contact/thankyou','mywindow','width=400,height=400,resizable=0,location=1,status=1,scrollbars=1');		
	}
}

function newContactFormSubmit() {	
	if(validate_name(trim($('#edit-name-new').attr('value'))) && $('#edit-company-new').attr('value') != '' && $('#edit-country-new').attr('value') != '0' && validate_email($('#edit-email-new').attr('value')) && $('#edit-interests-new').attr('value') != 0 && $('#edit-captcha-response-new').attr('value') != '' &&
		($('#edit-interests-new').attr('value') != 'Other' || $('#edit-other-new').attr('value') != '')) {
			window.open('/contact/thankyou','mywindow','width=400,height=400,resizable=0,location=1,status=1,scrollbars=1');		
	}
}

function popContactFormSubmit() {
	return(validate_name(trim($('#edit-name-pop').attr('value'))) && 
						$('#edit-company-pop').attr('value') != '' && 
						$('#edit-country-pop').attr('value') != '0' && 
						validate_email($('#edit-email-pop').attr('value')) && 
						$('#edit-interests-pop').attr('value') != 0 && 
						$('#edit-captcha-response-pop').attr('value') != '' &&
						($('#edit-role-pop').attr('value') != 'Other' || $('#edit-other1-pop').attr('value') != '') &&
						($('#edit-interests-pop').attr('value') != 'Other' || $('#edit-other-pop').attr('value') != ''));
}
function demoContactFormSubmit() {
	return(validate_name(trim($('#edit-name-demo').attr('value'))) && 
						$('#edit-company-demo').attr('value') != '' && 
						$('#edit-country-demo').attr('value') != '0' && 
						validate_email($('#edit-email-demo').attr('value')) && 
						$('#edit-interests-demo').attr('value') != 0 && 
						$('#edit-captcha-response-demo').attr('value') != '' &&
						($('#edit-role-demo').attr('value') != 'Other' || $('#edit-other1-demo').attr('value') != ''));
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}
function validate_email(email) {
  apos=email.indexOf("@");
  dotpos=email.lastIndexOf(".");
  if (apos<1||dotpos-apos<2)
    {return false;}
  else {return true;}
}

function validate_name(name) {
  apos=name.indexOf(" ");
  if (apos<1)
    {return false;}
  else {return true;}
}





