$(document).ready( function() {
//	var options = { 
//			target: 			'#pop_errors',
//			success:			showResponsePop
//	};
//	$('#gigaspaces-pop-contact-page-form').ajaxForm(options);
/*	if( $('#gigaspaces-new-contact-page-form').get(0) ) {
		var options2 = { 
				target: 			'#new_errors',
				success:			showResponseNew				
		};	    
		$('#gigaspaces-new-contact-page-form').ajaxForm(options2);*/
	//clear all text fields upon refreshing - to prevent the browser from "autocomplete"
/*	$("input[type='text']").attr('value',"");
	$("#gigaspaces-pop-contact-page-form select").each(function(){
		$(this).val("0").triggerHandler('change');
	});
	$("#gigaspaces-new-contact-page-form select").each(function(){
			$(this).val("0").triggerHandler('change');
	});*/
	$('.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');
		Effects.slideSelect.apply(this,[228, ($.browser.msie && $.browser.version == "6.0" ? 78 : 85),'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-cloud1-wrapper, #edit-other');
		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'});
	});
/***/
	if( $(".messages.error").get(0) ) {
		$(".messages.error").css('display',"none");
		if( $("#gigaspaces-new-contact-page-form").get(0)){					
			serverCheck($(".messages.error").html(),"","#gigaspaces-new-contact-page-form");
			$('html, body').animate({
				scrollTop: $("#gigaspaces-new-contact-page-form").offset().top
				}, 2000);					
		}
		if( $("#gigaspaces-cloud-contact-page-form").get(0)){	
			$('#block-block-32').show();				
			serverCheck($(".messages.error").html(),"","#gigaspaces-cloud-contact-page-form");
			$('html, body').animate({
				scrollTop: $("#gigaspaces-cloud-contact-page-form").offset().top
				}, 2000);					
		}
		if( $("#gigaspaces-cloud1-contact-page-form").get(0)){	
			$('#block-block-33').show();				
			serverCheck($(".messages.error").html(),"","#gigaspaces-cloud1-contact-page-form");
			$('html, body').animate({
				scrollTop: $("#gigaspaces-cloud1-contact-page-form").offset().top
				}, 2000);					
		}
		if( $("#gigaspaces-content-contact-page-form").get(0)){					
			serverCheck($(".messages.error").html(),"","#gigaspaces-content-contact-page-form");
			$('html, body').animate({
				scrollTop: $("#gigaspaces-content-contact-page-form").offset().top
				}, 2000);
	}
	}	
	
	if($('#pop_errors').get(0) &&  $("#gigaspaces-pop-contact-page-form").get(0) ) {
		serverCheck($('#pop_errors').html(),null,'#gigaspaces-pop-contact-page-form');
		
	
	}
/*	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");
				});
	}*/
});

function popFinished(){
	$("#hidden_popup_html h2").remove();
	$("#hidden_popup_html div").remove();
	$("#hidden_popup_html iframe").attr('src',"/contact/thankyou");
	$("#hidden_popup_html iframe input").remove();
	$("#hidden_popup_html iframe").show();
}



function showRequest(formData, jqForm, options){
	/*	var queryString = $.param(formData);
alert('About to submit: \n\n' + queryString);*/
	return true;
}

function showResponsePop(responseText, statusText)  {
	serverCheck(responseText, statusText, "#gigaspaces-pop-contact-page-form");
}

function showResponseNew(responseText, statusText){
	serverCheck(responseText, statusText, "#gigaspaces-new-contact-page-form");	
}

function serverCheck(responseText, statusText, formId){
	var formType;
	var selector;
	var errorExist;
	var AllEmpty = false;
	var EmptyTexts;
	var EmptyCombos;	
	
	if (formId.indexOf("pop")!=-1)
		formType = "pop";
	else if(formId.indexOf("cloud1")!=-1)
		formType = "cloud1";
	else if(formId.indexOf("cloud")!=-1)
		formType = "cloud";
	else
		formType = "new";

	var fields = {'name':{label:" #edit-name-"+formType+"-wrapper label",input:" #edit-name-"+formType+"-wrapper .form-text-wrapper"},
			'Company':{label:" #edit-company-"+formType+"-wrapper label",input:" #edit-company-"+formType+"-wrapper .form-text-wrapper"},
			'mail':{label:" #edit-email-"+formType+"-wrapper label",input:" #edit-email-"+formType+"-wrapper .form-text-wrapper"},
			'Country':{label:" #edit-country-"+formType+"-wrapper label",input:" #edit-country-"+formType+"-wrapper .jquery-selectbox"},
			'Interest':{label:" #edit-interests-"+formType+"-wrapper label",input:" #edit-interests-"+formType+"-wrapper .jquery-selectbox"},
			'<li> field is required.</li>':{label:formId+" .captcha .description",input:formId+" #edit-captcha-response-"+formType+"-wrapper .form-text-wrapper"},
			'CAPTCHA':{label:formId+" .captcha .description",input:formId+" #edit-captcha-response-"+formType+"-wrapper .form-text-wrapper"},
			'Role':{label:" #edit-role-"+formType+"-wrapper label",input:" #edit-role-"+formType+"-wrapper .jquery-selectbox"},
			'other interest':{label:" #edit-interests-"+formType+"-wrapper label",input:" #edit-interests-"+formType+"-wrapper #edit-other-"+formType+"-wrapper "},
			'other role':{label:" #edit-role-pop-wrapper label",input:" #edit-role-pop-wrapper #edit-other1-pop-wrapper"}	
	};
	for(var i in fields) {		
		$(fields[i]['label']).removeClass('Error-Text-Label');
		$(fields[i]['input']).removeClass('Error-Text-Input');
	}
	
	for(var i in fields) {				
		if (responseText.indexOf(i)!=-1){
			errorExist = true;			
			$(fields[i]['label']).addClass('Error-Text-Label');			
			$(fields[i]['input']).addClass('Error-Text-Input');
		}
	}
	
	EmptyTexts = ($(formId+" input[type='text'][value=''].form-text").length);
	EmptyCombos = ($(formId+" .jquery-selectbox span:contains('SELECT')").length);
	if (EmptyTexts==5 && EmptyCombos==4)
		AllEmpty=true;

	if (formType=="pop")
		selector = "#hidden_popup_iframe .pop_contact_block_description";
	else if(formType=="cloud" || formType=="cloud")
		selector = ".cloud_contact_block_description";
	else
		selector = ".new_contact_block_description";
	
	if (errorExist){		
		if (!AllEmpty){
			$(selector).html("There is missing or invalid data in the boxes indicated below. Please revise:");
			$(selector).addClass('desc-error');
		}
	}

	else{
		$(selector).html("Send us your contact information and let us know your interest, so we can forward your request to the right person at GigaSpaces.");
		$(selector).removeClass('desc-error');
		
		$(formId+" input[type='text']").attr('value',"");		
		$(formId+" select").each(function(){
			$(this).val("0").triggerHandler('change');
		});
		
		if (formType=="pop")
			popFinished();
		else{			
			window.open('/contact/thankyou','mywindow','width=400,height=400,resizable=0,location=1,status=1,scrollbars=1');
		}
	}
}

function showResponse(responseText, statusText)  {
	/*	alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
    	'\n\nThe output div should have already been updated with the responseText.');*/
} 
function showError(responseText, statusText)  { 
	/*	alert('show error: ' + statusText + '\n\nresponseText: \n' + responseText + 
    '\n\nThe output div should have already been updated with the responseText.');*/ 
}
function ScrollToElement(theElement){
	  var selectedPosX = 0;
	  var selectedPosY = 0;
	  
	  while(theElement != null){
	    selectedPosX += theElement.offsetLeft;
	    selectedPosY += theElement.offsetTop;
	    theElement = theElement.offsetParent;
	  }
	                        		      
	 window.scrollTo(selectedPosX,selectedPosY);

}
function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

