// JavaScript Document
jQuery(document).ready(function() {
/************************************************************\
* Email product details box
\************************************************************/	 
		 jQuery("#email-product-form-box").hide();
		
		 jQuery(".email-product-button").click(function()
		 	{
			var browserWidth2 = (jQuery(window).width()/2)-(jQuery("#email-product-form-box").width()/2);
			jQuery( 'html, body' ).animate( { scrollTop: 0 }, 1000 );
			jQuery('#email-product-form-box').before('<div class="over-wrap" />');
			jQuery(".over-wrap").fadeTo("normal", 0.33);			
			jQuery("#email-product-form-box").css( "left",browserWidth2 );	
			jQuery("#email-product-form-box").show();}
		 );
		 
		 jQuery("#email-product-close").click(function()
		 	{jQuery("#email-product-form-box").hide();
			jQuery(".over-wrap").remove();
			}
		 );
/************************************************************\
* Download brochure box
\************************************************************/	 
		 jQuery("#download-brochure-form-box").hide();
		
		 jQuery(".download-brochure-link").click(function()
		 	{
			var browserWidth2 = (jQuery(window).width()/2)-(jQuery("#download-brochure-form-box").width()/2);
			jQuery( 'html, body' ).animate( { scrollTop: 0 }, 1000 );
			jQuery('#download-brochure-form-box').before('<div class="over-wrap" />');
			jQuery(".over-wrap").fadeTo("normal", 0.33);			
			jQuery("#download-brochure-form-box").css( "left",browserWidth2 );	
			jQuery("#download-brochure-form-box").show();}
		 );
		 
		 jQuery("#download-brochure-close").click(function()
		 	{jQuery("#download-brochure-form-box").hide();
			jQuery("#br_link").val('');
			jQuery(".over-wrap").remove();
			}
		 );
});
/************************************************************\
* Email product details funtion
\************************************************************/
function email_product_details(){
	
	var ep_link  = document.getElementById('ep_link');
    var ep_sent  = document.getElementById('ep_sent');
    var ep_name  = document.getElementById('ep_name');
	var ep_email = document.getElementById('ep_email');
    var ep_friend = document.getElementById('ep_friend');
    var success  = true;
    
		if(notEmpty(ep_name, "Please enter your name",'#errorMsg1')){ 
          if(notEmpty(ep_email, "Please enter your email",'#errorMsg1')){
			if(emailValidator(ep_email, "Please enter a valid email",'#errorMsg1')){ 
				if(notEmpty(ep_friend, "Please enter friend name",'#errorMsg1')){
					if(notEmpty(ep_sent, "Please enter friend email",'#errorMsg1')){					
               		 	if(emailValidator(ep_sent, "Please enter a valid email",'#errorMsg1')){  
							jQuery('#errorMsg1').hide('fast');
							var data = ep_link.value + ";" + ep_sent.value + ";" + ep_name.value + ";" + ep_email.value + ";" + ep_friend.value;							
									//remove all the class add the messagebox classes and start fading
									jQuery("#errorMsg1").removeClass().addClass('messagebox').text('Sending...').fadeIn("slow");
									//check the username exists or not from ajax
									jQuery.post("wp-content/themes/isha/js/ajax-sent-friends.php",{ user_name:data } ,function(data)
									{
										
									  if(data=='no') //if username not avaiable
									  {
										  jQuery("#errorMsg1").fadeTo(200,0.1,function() //start fading the messagebox
										{
										  //add message and change the class of the box and start fading
										  jQuery(this).html('Project recommendation cannot be sent').addClass('failure_msg').fadeTo(900,1);
										});       
									  }
									
									  else 
									  {
										  jQuery("#errorMsg1").fadeTo(200,0.1,function()  //start fading the messagebox
										{
										  //add message and change the class of the box and start fading
										  jQuery(this).html('Thank you for recommending us').addClass('success_msg').fadeTo(900,1); 
										  jQuery("#errorMsg1").removeClass('messagebox');
										    jQuery("#ep_sent").val('');
											jQuery("#ep_name").val('');
										  	jQuery("#ep_email").val('');
										  	jQuery("#ep_friend").val('');
										});
									  }
										   
									});
						
                            return true;
                        
                }
            
				}
			} 
			}
		}
    }
    return false;
}
/************************************************************\
* Populate download link for brochure
\************************************************************/
function populate_download_link(brochurelink){
	var downlink = brochurelink;
	jQuery("#br_link").val(downlink);
}

/************************************************************\
* Download brochure funtion
\************************************************************/
function download_brochure(){
	
	var br_link  = document.getElementById('br_link');
    var br_name  = document.getElementById('br_name');
	var br_phone = document.getElementById('br_phone');
	var br_email = document.getElementById('br_email');
    var success  = true;
		if(notEmpty(br_name, "Please enter your name",'#errorMsg2')){ 			 
				if(notEmpty(br_phone, "Please enter your phone number",'#errorMsg2')){	
					 if(notEmpty(br_email, "Please enter your Email Address",'#errorMsg2')){	
               		 	if(emailValidator(br_email, "Please enter a valid email",'#errorMsg2')){  
							jQuery('#errorMsg2').hide('fast');
							var data = br_link.value + ";" + br_name.value + ";" + br_email.value + ";" + br_phone.value;		
							
									//remove all the class add the messagebox classes and start fading
									jQuery("#errorMsg2").removeClass().addClass('messagebox').text('Sending...').fadeIn("slow");
									//check the username exists or not from ajax
									jQuery.post("wp-content/themes/isha/js/ajax-download-brochure.php",{ user_name:data } ,function(data)
									{
										
									  if(data=='no') //if username not avaiable
									  {
										  jQuery("#errorMsg2").fadeTo(200,0.1,function() //start fading the messagebox
										{
										  //add message and change the class of the box and start fading
										  jQuery(this).html('There is some problem with the download,please try later.').addClass('failure_msg').fadeTo(900,1);
										});       
									  }
									
									  else 
									  {
										  jQuery("#errorMsg2").fadeTo(200,0.1,function()  //start fading the messagebox
										{
										  //add message and change the class of the box and start fading
										  jQuery(this).html('Your can download the brochure now').addClass('success_msg').fadeTo(900,1); 
										  jQuery("#errorMsg2").removeClass('messagebox');
										    jQuery("#br_sent").val('');
											jQuery("#br_name").val('');
										  	jQuery("#br_email").val('');
										  	jQuery("#br_phone").val('');
											jQuery("#download-brochure-form-box").hide();
										  	jQuery(".over-wrap").remove();
										  window.open("/getpdf.php?pdf=" + br_link.value, 'newtab');										 
										 //window.location.href = br_link.value; /*Open in same tab*/
										});
									  }
										   
									});
						
                            return true;
                        
                }
					 	}
					} 			
			}
    return false;
}

/************************************************************\
* Validation for not empty fields
\************************************************************/
function notEmpty(elem, helperMsg, errorBox){	
    if(elem.value.length == 0){
        jQuery(errorBox).addClass('wrong').html(helperMsg);
        elem.focus(); // set the focus to this input
        return false;
    }
    return true;
}
/************************************************************\
* Validation for email
\************************************************************/
function emailValidator(elem, helperMsg, errorBox){
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(elem.value.match(emailExp)){
		return true;
	}else{
		jQuery(errorBox).addClass('wrong').html(helperMsg);
		elem.focus();
		return false;
	}
}
/************************************************************\
* Tab content script
\************************************************************/
/*var countries=new ddtabcontent("countrytabs")
countries.setpersist(true)
countries.setselectedClassTarget("link") //"link" or "linkparent"
countries.init(2)*/

jQuery(document).ready(function() {
	jQuery(".isha_custom_tab").live("click", function(){
		var div_id=jQuery(this).attr("rel");
		jQuery(this).parent().parent().find(".isha_custom_tab").removeClass("selected");
		jQuery(this).addClass("selected")
		jQuery(this).parent().parent().next().find(".tabcontent").hide();
		jQuery(this).parent().parent().next().find("#"+div_id).show();
		return false;
	})
})
