var $=jQuery.noConflict();
var emailregex=/^([*+!.&#$\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,6})$/i;

var phoneregex=/^[+]|[\d-()]+$/;

function disableEnterKey(e)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox



     if(key == 13 || key == 10)
         { 
		$('#newsletter_submit').click();
		 
		 return false;}
     else
          return true;
}

function submitByEnterKey(e,button_id)
{
     var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

      if(key == 13 || key == 10)
         { 
		$('#'+button_id).click();
		 
		 return false;}
     else
          return true;
}



$(document).ready(function() {
var isvalid=true;   
var isemailvalid=true;
var isphonevalid=true;
	$('#contact_submit').click(function(){
						   
				 val=$('.require');
				 
				 var err = '';
				 
				 val.each(function(i){
								   
								  $(this)
										.next('img[@alt="wrong"]')
											.remove();
								$(this)
									.next('img[@alt="correct"]')
										.remove();
							
									if($(this).val()=='')
									{	 
										$(this).css('border','1px solid red');
										$(this).after('<img src="images/wrong-icon.png" align="absmiddle"  alt="wrong">');
										isvalid=false;
									}
									else
									{
										$(this).css('border','1px solid green');
				

				$(this).after('<img src="images/correct-icon.png" align="absmiddle"  alt="correct">');
				$(this).next('img').fadeOut("slow");
										isvalid=true;
									}
								 
								   });
			$('.emailvalidation')
						.next('img[@alt="wrong"]')
							.remove();
					$('.emailvalidation')
						.next('img[@alt="correct"]')
							.remove();
								
				if(!emailregex.test($('.emailvalidation').val()))
				{
					
					
					$('.emailvalidation').css('border','1px solid red');
					$('.emailvalidation').after('<img src="images/wrong-icon.png" align="absmiddle"  alt="wrong">');

					isemailvalid=false;
				}
				else
				{
					 $('.emailvalidation').css('border','1px solid green');
								   $('.emailvalidation').after('<img src="images/correct-icon.png" align="absmiddle"  alt="correct">');
								 	$('.emailvalidation').next('img').fadeOut("slow");
					isemailvalid=true;
				}

				$('.requirephone')
						.next('img[@alt="wrong"]')
							.remove();
					$('.requirephone')
						.next('img[@alt="correct"]')
							.remove();
				if(!phoneregex.test($('.requirephone').val()))
				{
					
					
					$('.requirephone').css('border','1px solid red');
					$('.requirephone').after('<img src="images/wrong-icon.png" align="absmiddle"  alt="wrong">');

					isphonevalid=false;
				}
				else
				{
					$('.requirephone').css('border','1px solid green');
					$('.requirephone').after('<img src="images/correct-icon.png" align="absmiddle"  alt="correct">');
				  	$('.requirephone').next('img').fadeOut("slow");
					isphonevalid=true;
				}
			//alert('isphonevalid'+isphonevalid);
			//alert('isemailvalid'+isemailvalid);
			//alert('isvalid'+isvalid);
				if(isvalid==true && isemailvalid==true && isphonevalid==true )
				{
					param=$("#frm_contact").serialize();
					
					$('#loader').html('<img src="images/ajax-loader.gif" />');
					
					$.ajax({
							   type: "POST",
							   url: "contactprocess.php",
							   data: param,
							   dataType:'json',
							   success: function(res){
								   $('#loader').html('');
								 if(res.flag)
							 {
								
									 
									 $("#block_frm_contact").animate({
													opacity: 1
													}, 500, "linear",function(){
														$('#block_frm_contact').hide('slow');
														$('#contact_mgs').show('slow',function(){$(this).html('<br /><br />'+res.msg+'<br /><br />');});
														 $("#contact_mgs").animate({
													opacity: 1
													}, 1500, "linear",function(){
																$('#block_frm_contact').show('slow');
																$('#contact_mgs').hide('slow');
																 inputObj=$('.contacttxt');
																																										 
																 inputObj.each(function(i){
																				   
																					if($(this).val())
																					{
																						$(this).val('');
																						$(this).css('border','0px');
																					}
																				 
																				   });
															
																
																if($('#email_subc').attr('checked')==true)
																{
																	$('#email_subc').removeAttr('checked');
																}
																
														} );
														
														
														} );

							 }
							   }
						 });
				}
			   
	});
	
	/*
	$('.require').blur(function(){
				 var err = '';
				 
				$(this)
						.next('img[@alt="wrong"]')
							.remove();
				$(this)
						.next('img[@alt="correct"]')
							.remove();
							
				if($(this).val()=='')
				{
					 $(this).css('border','1px solid red');
					 $(this).after('<img src="images/wrong-icon.png" align="absmiddle"  alt="wrong">');
				 }
				else
				{
				 $(this).css('border','1px solid green');
				

				$(this).after('<img src="images/correct-icon.png" align="absmiddle"  alt="correct">');
				$(this).next('img').fadeOut("slow");

				}
				
								 
		});*/
	
	/*
	$('.emailvalidation').blur(function(){
				$('.emailvalidation')
						.next('img[@alt="wrong"]')
							.remove();
				$('.emailvalidation')
						.next('img[@alt="correct"]')
							.remove();
													
							if(emailregex.test($('.emailvalidation').val()))
							{
								 $('.emailvalidation').css('border','1px solid green');
								   $('.emailvalidation').after('<img src="images/correct-icon.png" align="absmiddle"  alt="correct">');
								 	$('.emailvalidation').next('img').fadeOut("slow");
							}
							else
							{
								$('.emailvalidation').css('border','1px solid red');
								$('.emailvalidation').after('<img src="images/wrong-icon.png" align="absmiddle"  alt="wrong">');
							}		
				});*/
	/*
	$('.requirephone').blur(function(){
									 
				$('.requirephone')
						.next('img[@alt="wrong"]')
							.remove();
				$('.requirephone')
						.next('img[@alt="correct"]')
							.remove();
												 
				if(phoneregex.test($('.requirephone').val()))
				{
					$('.requirephone').css('border','1px solid green');
					$('.requirephone').after('<img src="images/correct-icon.png" align="absmiddle"  alt="correct">');
				  	$('.requirephone').next('img').fadeOut("slow");
				}
				else
				{
					$('.requirephone').css('border','1px solid red');
					$('.requirephone').after('<img src="images/wrong-icon.png" align="absmiddle"  alt="wrong">');
				}
		});
	
	*/
});



function openwindow()
{
	param=$("#frm_contact").serialize();
	
	 window.open("print.html?"+param,"mywindow","location=No,resizable=0,width=600,height=450");
} 
