$('document').ready(function()
{
	$("#thanks").hide()
	$("#error").hide()
	$("#refreshform").hide()
	$('#smsmobileno').numeric({allow:"+- "});
	$("#refreshform").click( function()
	{
		$("#pop").show()
		$("#error").hide()
		$("#refreshform").hide()
		generateCaptcha()
		$("#smsmobileno").val('');		
	})
	$("#smsmobileno").keypress( function(evt){
		if(evt.which!=0){var charCode = (evt.which) ? evt.which : event.keyCode}
		if(charCode == 95)
		{			
			return false;
		}
		else if(!(charCode > 47 &&  charCode < 58) && charCode != 32 && charCode != 43 && charCode != 45)
		{
			return true;
		}
	});

	$('#smscountrycode').focus(function (){
		$('#smsmobileno').focus();
	});	
	$("#contactCaptcha").html('<img src="'+path+'images/loader.gif" align="absmiddle">');
	generateCaptcha()
	$("#pop").submit(function()
	{
		if(trim($("#smsmobileno").val())=="")
		{
			alert("Bitte geben Sie Ihre Handynummer ein");
			$('#smsmobileno').focus();
			return false;
		}
		else if(trim($("#smsmobileno").val())!="" && isNaN(trim($("#smsmobileno").val())))
		{
			alert("Numeric only allowed ");
			$('#smsmobileno').val('');
			$('#smsmobileno').focus();
			return false;
		}
		else if($("#contactcaptchaValue").val() == '?' || $("#contactcaptchaValue").val() == '')
		{
			alert('Bitte geben Sie den Sicherheitscode ein, wie in der Bild.');
			$('#contactcaptchaValue').focus();
			return false;
		}
		else
		{
			$.ajax(
				{
					type: 'POST',
					url: path+"captcha.php",
					data:{'action':'validateCaptcha', 'result':$('#contactcaptchaValue').val()},
					success: function(data) 
					{
						
						if(data!='')
						{	
							contactcaptchdata = data;					
							dispCaptcha(data, 'contactCaptcha', 'contactcaptchaValue');
							alert("Die Sicherheits-Code, den Sie eingegeben haben ist nicht korrekt. Bitte versuchen Sie es erneut.");
							$('#contactcaptchaValue').focus();
							return false;
						}
						else
						{
							tel = trim($("#smsmobileno").val());
							cap = $("#contactcaptchaValue").val();
							$.post(path+"sms.php", {captcha:cap,phone:tel},function(data)
							{
								if(data == 100)
								{
									$("#pop").hide()
									$("#thanks").show()
								}
								else
								{
									$("#pop").hide()
									$("#error").show()
									$("#refreshform").show()
								}
									
								return true;
							});
							return false;
						}
						
					 }
				});
				return false;
		}



		
	});
})

function trim(str)
{
        if(!str || typeof str != 'string')
        return '';
        return ltrim(rtrim(str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ')));
        
}
function ltrim(str, chars) {
        chars = chars || "\\s";
        return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
function rtrim(str, chars) {
        chars = chars || "\\s";
        return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function changeWaterMarkStylesmall(controlId)
{

	$('#'+controlId).removeClass('tboxdummysmall');
	$('#'+controlId).addClass('tboxsmall');
}
function changeWaterMarkStyleDefaultsmall(controlId,controlValue)
{	
	if($('#'+controlId).val()== controlValue)
	{
		$('#'+controlId).addClass('tboxdummysmall');
		$('#'+controlId).removeClass('tboxsmall');
	}	
}
function removeStylesmall(controlId)
{
	$('#'+controlId).removeClass('tboxredsmall');
	$('#'+controlId).addClass('tboxsmall');
}

function validkey(evt)
{		

	if(evt.which!=0){var charCode = (evt.which) ? evt.which : event.keyCode}

		if(charCode == 95)
		{
			return false;
		}
		else if(charCode == 13)
		{
			validatecontactForm();
			return false;
		}			
		else
		{			
			return true;
		}

}
function validatecaptchanumeric()
{
  $('#contactcaptchaValue').numeric();
}
function dispCaptcha(data , spanid, controlId)
{	
	$('#'+spanid).html(data);
	var captchaText = document.createElement("input")  
	captchaText.setAttribute("type", "text")
	captchaText.setAttribute("autocomplete", "off")
	captchaText.setAttribute("name", controlId)
	captchaText.setAttribute("value", "?")
	captchaText.setAttribute("id", controlId)
	captchaText.setAttribute("title", "Enter right value")
	captchaText.setAttribute("onKeyPress", "return validkey(event)")
	var captchaLabel = document.createElement("label")
	captchaLabel.setAttribute("id", spanid+1)    
	$('#'+spanid).append(captchaText);
	$('#'+spanid).append(captchaLabel);
	$('#'+spanid+1).html('(Spam-Schutz)') 
  	$('#'+controlId).width(20);
  	$('#'+controlId).addClass("tboxdummysmall");
 	$('#'+controlId).focus( function() {if(this.value=="?")this.value=''; changeWaterMarkStylesmall(this.id)});
  	$('#'+controlId).blur( function() {if(this.value=='')this.value="?";changeWaterMarkStyleDefaultsmall(this.id, this.defaultValue);removeStylesmall(this.id)});
  	$('#'+controlId).keydown( function() {validatecaptchanumeric()});
}

function generateCaptcha()
{
	$.ajax(
	{
		type: 'POST',
		url: path+"captcha.php",
		data:{'action':'createCaptcha'},
		success: function(data) 
		{
			contactcaptchdata = data
			dispCaptcha(data,'contactCaptcha', 'contactcaptchaValue')


		}

	});
}
function formsub(evt)
{
  if(evt.which!=0){var charCode = (evt.which) ? evt.which : event.keyCode}
  if (charCode ==13)
  {  
	 validatecontactForm()
	 return false;
  }
  
}
