/*Javascript trim function*/
function jtrim(stringToTrim) {
          return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function jltrim(stringToTrim) {
        return stringToTrim.replace(/^\s+/,"");
}
function jrtrim(stringToTrim) {
        return stringToTrim.replace(/\s+$/,"");
} 
/*End of the Javascript trim function*/
function changeWaterMarkStyle(controlId)
{

	$('#'+controlId).removeClass('tboxdummysmall');
	$('#'+controlId).addClass('tboxsmall');
}
function changeWaterMarkStyleDefault(controlId,controlValue)
{	
	if($('#'+controlId).val()== controlValue)
	{
		$('#'+controlId).addClass('tboxdummysmall');
		$('#'+controlId).removeClass('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 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("onFocus", "javascript:captchaEmtp();")
	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-Protection)') 
	$('#'+controlId).width(20);
	$('#'+controlId).addClass("tboxdummysmall");
	$('#'+controlId).focus( function() {if(this.value=="?")this.value=''; changeWaterMarkStyle(this.id)});
  	$('#'+controlId).blur( function() {if(this.value=='')this.value="?";changeWaterMarkStyleDefault(this.id, this.defaultValue);});
	$('#'+controlId).keydown( function() {validatecaptchanumeric()});
	var br=document.createElement("br")
	$('#'+spanid).append(br);
	var span = document.createElement("span")
	span.setAttribute("id", controlId+"_warning")
	//span.setAttribute("style", "color:red;font-size:80% ")
	//span.setAttribute("class", red)
	$('#'+spanid).append(span);
}


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


function validatecaptchanumeric()
{
	
	$('#contactcaptchaValue').numeric();


}


/*Number Validate Function */
function isNumberKey_num(evt){

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

	 if(charCode==8)
		return true;

         if (charCode < 48 || charCode > 57)
            return false;

         return true;

	}
}


/*Phone Number Validate Function */
function phoneNumberValidation(evt)
      {
	if(evt.which!=0)
	{
         var charCode = (evt.which) ? evt.which : event.keyCode

		 if(charCode==8 || charCode==32 || charCode==43 || charCode==40 || charCode==41 || charCode==45 ){
			 return true;
		 }else if (charCode < 48 || charCode > 57){
		    	return false;

		 }else{
		    	return true;
		 }
	}
}



function sendContactMail(){

	$.ajax({
	type: 'POST',
	url: path+"sendMail.php",
	data:$("#contactFromId").serialize(),
	success: function(data) 
	{
		//document.getElementById('contactFromId').reset();
		//alert('Contact very soon');
		window.location=thanksPath;

	}

	});
}

function contactFn(){

	var x = jrtrim(jltrim(jtrim($('#email').val())));
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;


	if(jtrim($('#email').val())==""){

		alert('Please enter E-mail');
		$("#email").focus();
		return false;

	}else if(!filter.test(x)){	

		alert('Please enter valid E-mail');
		$("#email").focus();
		return false;
	}else if(jtrim($('#city').val())==""){

		alert('Please enter city');
		$("#city").focus();
		return false;

	}else if(jtrim($('#contactcaptchaValue').val())=="" || jtrim($('#contactcaptchaValue').val())=="?" ){

		alert('Please enter the security code');
		$("#contactcaptchaValue").focus();
		return false;

	}else{
		$.ajax(
		{
			type: 'POST',
			url: path+"captcha.php",
			data:{'action':'validateCaptcha', 'result':$('#contactcaptchaValue').val()},
			success: function(data) 
			{
				if(data!='')
				{	
					$("#captchacheck").removeClass("validate");					
					$("#captchacheck").addClass("validateNo1");						
					dispCaptcha(data, 'captchacheck', 'contactcaptchaValue');
					$("#contactcaptchaValue").val('');						
					alert('Please enter the correct security code');
					$("#contactcaptchaValue").focus();
					return false;	
				}
				else{
					
					sendContactMail();						
					return true;

				}
				
			 }
		});

	}

}

$(document).ready(
function (){

$('.listing li').mouseover(function() {
$(this).addClass("over");
})
$('.listing li').mouseout(function() {
$(this).removeClass("over");
})




});

	function captchaload(){

      $("#captchacheck").html('<img alt="" src="'+ path +'/images/loader.gif" />');  
  
  }	
