var base_url = "http://www.ipfonline.com/";

$(document).ready(function() {
	
	initTabs();
	
	$("#searchparam").keyup(function(){
		
		var searchparam = $(this).val();
		
		
		
		$(".table-column-a").each(function(){
			
			
			$(this).children('ul').each(function(){
				
				
				
				$(this).children('li.parent').each(function(){
					
					if(searchparam == ""){
						
						$(this).removeClass('green');
										
					}
					
					var searchstring = $(this).html();
					if(searchstring.search(new RegExp(searchparam, "i"))!=-1 && searchparam != ""){
						
						$(this).addClass('green');
						
					}
					else{
						$(this).removeClass('green');
					}
				});
				
			
				$(this).children('li').each(function(){
					
					if(searchparam == ""){
						
						$(this).children('a').removeClass('green');
						
					}
					
					var searchstring = $(this).html();
					if(searchstring.search(new RegExp(searchparam, "i"))!=-1 && searchparam != ""){
						
						$(this).children('a').addClass('green');
						
						
					}
					else{
						$(this).children('a').removeClass('green');
					}
				});
			
			});	
			
		});
		
		
		
	});
	
	$("#username").blur(function(){
		
		if(trim($(this).val()) != ""){

				var dataString = 'username='+ $(this).val(); 
				$.ajax({
					type: "POST",
					url:base_url+"register/checkusername",
					data: dataString,  
					success: function(msg){
					if(msg != 0){
						$("#username_err").html('Sorry! This username is already in use');
						$("#username_err").show();
					}
					else
						$("#username_err").hide();
					}
				});
		}
	});	
	
	$("#email").blur(function(){
		
			if(trim($(this).val()) != ""){
			
				var email = $(this).val();
				
				var dataString = 'email='+ $(this).val(); 
				$.ajax({
					type: "POST",
					url:base_url+"index.php/register/checkemailid",
					data: dataString,  
					success: function(msg){
						if(msg != 0){
							$('span.error').css('background','url("'+base_url+'images/cross_icon1.gif") no-repeat');
							$("#email_err").css('color','#962830');
							$("#email_err").html('Sorry! This email id is already in use');
							$("#email_err").show();
							$("#emailexisted").val(msg);
							$("#email_err").parent().parent().css('background','#F5FD6E');
						}
						else{
							if(isEMailAddr(email) == false){
								$('span.error').css('background','url("'+base_url+'images/cross_icon1.gif") no-repeat');
								$("#email_err").css('color','#962830');
								$("#email_err").html('Please enter a valid email id');
								$("#email_err").parent().parent().css('background','#F5FD6E');
							}
							else{	
								$('span.error').css('background','url("'+base_url+'images/tick-mark.gif") no-repeat');
								$("#email_err").css('color','#658c28');
								$("#email_err").html('This email id is available');
								$("#email_err").parent().parent().css('background','none');
							}
							$("#email_err").show();
							$("#emailexisted").val(msg);
						}	
					}
				});
		}
	});	
	
	$("#captchainput").blur(function(){
		captcha = trim($(this).val());
		
		if(captcha != ""){

			var dataString = 'captcha='+ captcha; 
			
			$(".loadmsg")
			.ajaxStart(function(){
				$(this).show();
			})
			.ajaxComplete(function(){
				$(this).hide();
				
			});
			
			$.ajax({
				type: "POST",
				url:base_url+"captcha/captcheck.php",
				data: dataString,  
				success: function(msg){
					if(msg == 0){
						$("#captcha_err").html('The words did not match');
						$("#captcha_err").parent().parent().css('background','#F5FD6E');
						$("#captcha_err").show();
						$("#captchacheck").val(msg);
					}
					else
					{	
						$("#captcha_err").parent().parent().css('background','none');
						$("#captcha_err").hide();
						$("#captchacheck").val(msg);
					}	
				}
			});
		}
	});	
	
	$("#resetemail").blur(function(){
		
		if(trim($(this).val()) != ""){

				var dataString = 'email='+ $(this).val(); 
				$.ajax({
					type: "POST",
					url:base_url+"index.php/register/checkemailid",
					data: dataString,  
					success: function(msg){
						if(msg == 0){
							$("#reset_err").html('This email id is not yet registered');
							$("#reset_err").show();
							$("#resetemailval").val(msg);
						}
						else
							$("#reset_err").hide();
							$("#resetemailval").val(msg);
						}
				});
		}
	});	
	
	$("#resetsubmit").click(function(){
	
		var reset = trim($('input[name=resetemailval]').val());
		var email = trim($('input[name=resetemail]').val());
		
		if(reset == 0)
		{
			$("#reset_err").html('This email id is not yet registered');
			$("#reset_err").show();
		}
		else
		{
			$("#reset_err").hide();
			var dataString = 'email='+ email; 
			$.ajax({
				type: "POST",
				url:base_url+"index.php/register/resetpass",
				data: dataString,  
				success: function(msg){
					$("#resetmsg").html(msg);
				}
			});
		}	
		
	});
	
	$("#newpassword").click(function(){
		
		var password = trim($('input[name=password]').val());
		var passwordcopy = trim($('input[name=passwordcopy]').val());
		var userid = trim($('input[name=userid]').val());
		var activcode = trim($('input[name=activcode]').val());
		
		if(password == ""){
			
			$("#password_err").show();
			return false;			
		}
		else if(password.length < 6){
			
			$("#password_err").html('Password should be atleast 6 characters');
			$("#password_err").show();
			return false;	
		}	
		else{
			
			$("#password_err").hide();
		}
		
		if(passwordcopy == ""){
			
			$("#cpassword_err").html('Re-entering password is mandatory');
			$("#cpassword_err").show();
			return false;			
		}
		else{
			
			$("#cpassword_err").hide();
		}
		
		if(password != passwordcopy){
			$("#cpassword_err").html('Passwords do not match');
			$("#cpassword_err").show();
			return false;
		}
		else{
			$("#cpassword_err").hide();
			var dataString = 'userid='+userid+'&activcode='+activcode+'&password='+password; 
			$.ajax({
				type: "POST",
				url:base_url+"index.php/register/newpassword",
				data: dataString,  
				success: function(msg){
					$("#newpwdmsg").html(msg);
				}
			});
		}	
		
	});
	
	$("#register").click(function() { 
		
		var fname = trim($('input[name=firstname]').val());
		var password = trim($('input[name=password]').val());
		var passwordcopy = trim($('input[name=passwordcopy]').val());
		var email = trim($('input[name=email]').val());
		var emailexisted = trim($('input[name=emailexisted]').val());
		var mcountrycode = trim($('input[name=mcountrycode]').val());
		var mobile = trim($('input[name=mobile]').val());
		var company = trim($('input[name=company]').val());
		var address = trim($('input[name=address]').val());
		var designation = trim($('input[name=designation]').val());
		var pincode = trim($('input[name=pincode]').val());
		var country = trim($('select[name=country]').val());
		var city = trim($('select[name=city]').val());
		var captcha = trim($('input[name=captchainput]').val());
		var captchacheck = trim($('input[name=captchacheck]').val());
		
		if($('input[name=newsletters]').is(':checked')){
			$('input[name=newsletters]').val('1');
		}
		else
			$('input[name=newsletters]').val('0');
		
		if(email == "" || isEMailAddr(email) == false ){
			$("#email_err").html('Email ID is invalid');
			$("#email_err").parent().parent().css('background','#F5FD6E');
			$("#email_err").show();
			return false; 	
		}
		
		else{
			
			$("#email_err").hide();
		}
		
		if(emailexisted != 0){
			$("#email_err").html('Sorry! This email id is already in use');
			$("#email_err").show();
			return false; 	
		}
		
		else{
			$("#email_err").hide();
			$('span.error').css('background','url("'+base_url+'images/cross_icon1.gif") no-repeat');
		}

		if(password == ""){
			$("#password_err").parent().parent().css('background','#F5FD6E');
			$("#password_err").show();
			return false;			
		}
		else if(password.length < 6){
			$("#password_err").parent().parent().css('background','#F5FD6E');
			$("#password_err").html('Password should be atleast 6 characters');
			$("#password_err").show();
			return false;	
		}	
		else{
			$("#password_err").parent().parent().css('background','none');
			$("#password_err").hide();
		}
		
		if(passwordcopy == ""){
			
			$("#cpassword_err").html('Re-entering password is mandatory');
			$("#cpassword_err").parent().parent().css('background','#F5FD6E');
			$("#cpassword_err").show();
			return false;			
		}
		else{
			$("#cpassword_err").parent().parent().css('background','none');
			$("#cpassword_err").hide();
		}
		
		if(password != passwordcopy){
			$("#cpassword_err").html('Passwords do not match');
			$("#cpassword_err").parent().parent().css('background','#F5FD6E');
			$("#cpassword_err").show();
			return false;
		}
		else{
			$("#cpassword_err").parent().parent().css('background','none');
			$("#cpassword_err").hide();
		}
		
		if(fname == ""){
			$("#fname_err").parent().parent().css('background','#F5FD6E');
			$("#fname_err").show();
			return false;			
		}
		else{
			$("#fname_err").parent().parent().css('background','none');
			$("#fname_err").hide();
		}	
		
		if(company == ""){
			$("#company_err").parent().parent().css('background','#F5FD6E');
			$("#company_err").show();
			return false;			
		}
		else{
			$("#company_err").parent().parent().css('background','none');
			$("#company_err").hide();
		}
		
		if(address == ""){
			$("#address_err").parent().parent().css('background','#F5FD6E');
			$("#address_err").show();
			return false;			
		}
		else{
			$("#address_err").parent().parent().css('background','none');
			$("#address_err").hide();
		}
		
		if(country == "" || country == 0){
			$("#country_err").parent().parent().css('background','#F5FD6E');
			$("#country_err").show();
			return false;			
		}
		else{
			$("#country_err").parent().parent().css('background','none');
			$("#country_err").hide();
		}
		
		if(city == "" || city == 0){
			$("#city_err").parent().parent().css('background','#F5FD6E');
			$("#city_err").show();
			return false;			
		}
		else{
			$("#city_err").parent().parent().css('background','none');
			$("#city_err").hide();
		}
		
		if(pincode == "" || Validate_no(pincode) == false){
			$("#pincode_err").parent().parent().css('background','#F5FD6E');
			$("#pincode_err").show();
			return false;			
		}
		else{
			$("#pincode_err").parent().parent().css('background','none');
			$("#pincode_err").hide();
		}
		
		if(mcountrycode == "" ){
			
				$("#mobile_err").html('Valid Country code is mandatory');
				$("#mobile_err").parent().parent().css('background','#F5FD6E');
				$("#mobile_err").show();
				return false; 
		}	
		if(mobile == "" || Validate_no(mobile) == false ){
			
			$("#mobile_err").html('Valid Contact no. is mandatory');
			$("#mobile_err").parent().parent().css('background','#F5FD6E');
			$("#mobile_err").show();
			return false; 	
		}
		else{
			$("#mobile_err").parent().parent().css('background','none');
			$("#mobile_err").hide();
		}	
		
		if(captcha == ""){
			
			$("#captcha_err").html('Please enter the above verification code');
			$("#captcha_err").parent().parent().css('background','#F5FD6E');
			$("#captcha_err").show();
			return false; 	
		}
		else{
			$("#captcha_err").parent().parent().css('background','none');
			$("#captcha_err").hide();
		}
		
		if(captchacheck == 0){
			$("#captcha_err").html('The words did not match');
			$("#captcha_err").parent().parent().css('background','#F5FD6E');
			$("#captcha_err").show();
			return false; 	
		}
		
		else{
			$("#captcha_err").parent().parent().css('background','none');
			$("#captcha_err").hide();
		}
		
		if($('input[name=tc]').is(':checked')){
			$('#terms_err').hide();
			$("#terms_err").parent().parent().css('background','none');
			return true;
		}
		else
		{
			$('#terms_err').show();
			$("#terms_err").parent().parent().css('background','#F5FD6E');
			return false;
		}	
		
	});
	
		$(".label_fields").focus(function()
			{
				v = $(this).val();	
				if (v == '91')
					$(this).val('');
										
		});
		$(".label_fields").blur(function()
			{
				v = trim($(this).val());	
				if (v == '')
					$(this).val('91');
										
		 });
	
			$("#sendenquiry").click(function() { 
			
			var companyid = trim($('input[name=supplier_id]').val());
			var productid = trim($('input[name=product_id]').val());
			var productname = trim($('input[name=product_name]').val());
			var name = trim($('input[name=user_name]').val());
			var email = trim($('input[name=user_email]').val());
			var company = trim($('input[name=user_company]').val());
			var phone = trim($('input[name=user_phone]').val());
			var userip = trim($('input[name=userip]').val());
			var requirement = $('.lead_req').val();
			
			if(name == ""){
				$("#err_name").html('Name is a required field');
				$('#err_name').show();
				return false;
			}
			else{
				$('#err_name').hide();
			}
			
			if(email == "" ){
				$("#err_email").html('Email is a required field');
				$('#err_email').show();
				return false;
			}
			else if(isEMailAddr(email)==false){
				$("#err_email").html('Email ID is invalid');
				$('#err_email').show();
				return false;
			}
			else{
				$('#err_email').hide();
			}
			
			if(company == ""){
				$("#err_company").html('Company name is a required field');
				$('#err_company').show();
				return false;
			}
			else{
				$('#err_company').hide();
			}
			
			if(phone == ""){
				$("#err_phone").html('Phone number is a required field');
				$('#err_phone').show();
				return false;
			}
			else if(Validate_no(phone) == false){
				$("#err_phone").html('Phone number is invalid');
				$('#err_phone').show();
				return false;
			}
			else{
				$('#err_phone').hide();
			}
			
			
			$(".loadmsg")
			.ajaxStart(function(){
				$(this).show();
			})
			.ajaxComplete(function(){
				$(this).hide();
				
			});
			
			var dataString = 'lead_name='+ name +'&lead_email=' +email +'&lead_company=' +company+'&lead_phone=' +phone+ '&lead_requirement='+escape(requirement)+'&supplier_id=' +companyid+'&product_id='+productid+'&product_name='+escape(productname)+'&userip='+userip;
			$.ajax({
				type: "POST",
				url:base_url+"index.php/users/insertlead",
				data: dataString,  
				success: function(msg){
					
					$("#resultmsg").show();
					$('input[name=user_name]').val('');
					$('input[name=user_email]').val('');
					$('input[name=user_company]').val('');
					$('input[name=user_phone]').val('');
					$('.lead_req').val('');
					
				}
			});
			
			return false;
			
		});
	
	$("#sendcompenquiry").click(function() { 
		
		var companyid = $('input[name=supplier_id]').val();
		var name = trim($('input[name=user_name]').val());
		var email = trim($('input[name=user_email]').val());
		var company = trim($('input[name=user_company]').val());
		var phone = trim($('input[name=user_phone]').val());
		var requirement = $('.lead_req').val();
		var userip = trim($('input[name=userip]').val());
		
		if(name == ""){
			$("#err_name").html('Name is a required field');
			$('#err_name').show();
			return false;
		}
		else{
			$('#err_name').hide();
		}
		
		if(email == "" ){
			$("#err_email").html('Email is a required field');
			$('#err_email').show();
			return false;
		}
		else if(isEMailAddr(email)==false){
			$("#err_email").html('Email ID is invalid');
			$('#err_email').show();
			return false;
		}
		else{
			$('#err_email').hide();
		}
		
		if(company == ""){
			$("#err_company").html('Company name is a required field');
			$('#err_company').show();
			return false;
		}
		else{
			$('#err_company').hide();
		}
		
		if(phone == ""){
			$("#err_phone").html('Phone number is a required field');
			$('#err_phone').show();
			return false;
		}
		else if(Validate_no(phone) == false){
			$("#err_phone").html('Phone number is invalid');
			$('#err_phone').show();
			return false;
		}
		else{
			$('#err_phone').hide();
		}
		
		
		$(".loadmsg")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
			
		});
		
		var dataString = 'lead_name='+ name +'&lead_email=' +email +'&lead_company=' +escape(company)+'&lead_phone=' +phone+ '&lead_requirement='+escape(requirement)+'&supplier_id='+companyid+'&userip='+userip;
		$.ajax({
			type: "POST",
			url:base_url+"index.php/users/insertleadcompany",
			data: dataString,  
			success: function(msg){
				
				$("#resultmsg").show();
				$('input[name=user_name]').val('');
				$('input[name=user_email]').val('');
				$('input[name=user_company]').val('');
				$('input[name=user_phone]').val('');
				$('.lead_req').val('');
				
			}
		});
		
		return false;
		
	});
		
	
	$('.categories-li').click(function(){
		
		$('.separator').hide();
		$('.popup-content').hide();
		
		$(this).parents('.li-tbl').next('.separator').show();
		$(this).parents('.li-tbl').next('.separator').next('.popup-content').show();
	});
	
	$('.categorymenu').click(function(){
		
		$('.separator').hide();
		$('.popup-content').hide();
		
		$(this).parents('.li-tbl').next('.separator').show();
		$(this).parents('.li-tbl').next('.separator').next('.popup-content').show();
		
		var popupid = $(this).parents('.li-tbl').next('.separator').next('.popup-content').attr('id');
		var myarr = new Array();
		myarr = popupid.split("_");
		var parentcategoryid = myarr[1];
		
		var content = $("#products_"+parentcategoryid).html();

		if(content == "" ){
			
			$(".load")
			.ajaxStart(function(){
				$(this).show();
			})
			.ajaxComplete(function(){
				$(this).hide();
				
			});
			
			var dataString = 'parentcategoryid='+ parentcategoryid; 
			$.ajax({
				type: "GET",
				url:base_url+"home/topproducts/"+ parentcategoryid,

				success: function(msg){
					
					$("#products_"+parentcategoryid).html(msg);
					
				}
			});
		}
		
		
	});
		
	$('body').click(function(e){
		var $clicked=$(e.target);
		if($clicked.is('.popup-content') || $clicked.parents().is('.popup-content') || $clicked.parents().is('.categories li')) {
																
		} else {
			$('.separator').hide();
			$('.popup-content').hide();
		}
	});
	
	
	$(".selectall").click(function() {
		
		if($(this).is(":checked") == true)
			$(".productid").attr('checked', true);
		else
			$(".productid").attr('checked', false);
	});
	
	$(".narrow-hide").click(function() {
		
		$('.narrow-search').hide();
		$('.collapsed-search').show();
		
	});
	
	$(".narrow-show").click(function() {
		
		$('.narrow-search').show();
		$('.collapsed-search').hide();
	});
	
	$(".searchbox").focus(function()
	{
		v = $(this).val();	
		if (v == 'Type a product or company name here...')
			$(this).val('');
								
	});
	
	$(".searchbox").blur(function()
	{
		v = trim($(this).val());	
		if (v == '')
			$(this).val('Type a product or company name here...');
								
	 });
	
	$('.tree li span.symbol-close').click(function(){
		
		$(this).next('ul').toggle('slow');
		
		if($(this).children('img').attr('src') == base_url+'themes/icon_minus.jpg')
			$(this).children('img').attr('src',base_url+'themes/icon_plus.jpg');
		else
			$(this).children('img').attr('src',base_url+'themes/icon_minus.jpg');
	});
	
	$('.subcategory').click(function(){
		
		document.filterform.submit();
	});
	
	$('.other').click(function(){
		
		document.filterform.submit();
	});
	
	$('#submit_form').click(function(){
		
		var searchparam = trim(document.form.search.value);
		if (searchparam == "" || searchparam == 'Type a product or company name here...'){
			alert("Please enter your search keyword");
			return false;
		}
	});
	
	$('#submit_formsearch').click(function(){
		
		var searchparam = trim(document.formsearch.search.value);
		if (searchparam == "" || searchparam == 'Type a product or company name here...'){
			alert("Please enter your search keyword");
			return false;
		}
	});
	
	var pagenos = $("#prime_page_count").val();
	
	$('#prod-rotate li.num').click(function() {
	
		var liIndex = $('#prod-rotate li').index(this);
		rotateproducts(liIndex,pagenos);
	});
	$('#prod-rotate li.next').click(function() {
		
		var liIndex = $('#prod-rotate li').index($('#prod-rotate li.act'));
		liIndex += 1;
		if(liIndex <= pagenos){
			
			for(var i=1; i<=pagenos; i++){
			
			
			$('#prod-rotate li:eq('+i+')').removeClass('act');
			$('#prod-rotate li:eq('+i+')').html('<a href="javascript:void(0)">'+i+'</a>');
			$('.primediv'+i).hide();
			
			}
		
			$('#prod-rotate li:eq('+(liIndex)+')').addClass('act');
			$('#prod-rotate li:eq('+(liIndex)+')').children('a').remove();
			$('#prod-rotate li:eq('+(liIndex)+')').html(liIndex);
			$('.primediv'+(liIndex)).show();
		
			
		}	
		
		
	});
	
	$('#prod-rotate li.prev').click(function() {
			
			var liIndex = $('#prod-rotate li').index($('#prod-rotate li.act'));
			liIndex -= 1;
			if(liIndex > 0){
								
				for(var i=1; i<=pagenos; i++){
					
					$('#prod-rotate li:eq('+i+')').removeClass('act');
					$('#prod-rotate li:eq('+i+')').html('<a href="javascript:void(0)">'+i+'</a>');
					$('.primediv'+i).hide();
					
				}
				
				$('#prod-rotate li:eq('+(liIndex)+')').addClass('act');
				$('#prod-rotate li:eq('+(liIndex)+')').children('a').remove();
				$('#prod-rotate li:eq('+(liIndex)+')').html(liIndex);
				$('.primediv'+(liIndex)).show();
			}	
	});
	
	
	//productSlideShow(pagenos);
	
	/*$('.company-showing ul li').click(function() {
	
		
		if($(this).children('a').html() == 'Products'){
			
			$('.companydescription').hide();
			$('.companydetails').hide();
			$('.productsdiv').show();
			
			$('.company-showing ul li').attr('class','a');
			$(this).removeClass('a').addClass('b');
		}
		else{
			
			$('.companydescription').show();
			$('.companydetails').show();
			$('.productsdiv').hide();
			
			$('.company-showing ul li').attr('class','a');
			$(this).removeClass('a').addClass('b');
			
		}	
		
	});*/
	
	$('.company-showing ul li').click(function() {
	
		
		if($(this).children('a').html() == 'Products'){
			
			$('.companydescription').hide();
			$('.companydetails').hide();
			$('.productsdiv').show();
			
			$('.company-showing ul li').attr('class','a');
			$(this).removeClass('a').addClass('b');
		}
		else{
			
			$('.companydescription').show();
			$('.companydetails').show();
			$('.productsdiv').hide();
			
			$('.company-showing ul li').attr('class','a');
			$(this).removeClass('a').addClass('b');
			
		}	
		
	});	
	
	$('#specs').click(function() {
	
		$('.specs').show();
	});	
	
	$('#specsclose').click(function() {
		
		$('.specs').hide();
	});
	
	
	$('#myipf-tab1').click(function() {
		
		$(".search-div").hide();
		$(".profile-div").hide();
		$(".product-div").show();
		$(this).attr('class','active');
		$('#myipf-tab2').attr('class','inactive');
		$('#myipf-tab3').attr('class','inactive');
		
	});
	$('#myipf-tab2').click(function() {
		
		$(".product-div").hide();
		$(".profile-div").hide();
		$(".search-div").show();
		$(this).attr('class','active');
		$('#myipf-tab1').attr('class','inactive');
		$('#myipf-tab3').attr('class','inactive');
		
	});
	
	$('#myipf-tab3').click(function() {
		
		$(".product-div").hide();
		$(".search-div").hide();
		$(".profile-div").show();
		$(this).attr('class','active');
		$('#myipf-tab1').attr('class','inactive');
		$('#myipf-tab2').attr('class','inactive');
		
	});
	
	
	$(".submitcontact").click(function() {
		
		var flag = 0;
		for(var i=0; i < document.products.productid.length; i++){
			if(document.products.productid[i].checked)
				flag=1;
		}
		
		if(flag ==1){
			
				var productids = "";
				for( i=0; i < document.products.productid.length; i++){
					if(document.products.productid[i].checked)
						productids = productids + document.products.productid[i].value+"||";
				}
				
				name = trim($('#name').val());
				email = trim($('#email').val());
				company = trim($('#company').val());
				phone = trim($('#phone').val());
				requirement = trim($('#requirement').val());
				var userip = trim($('input[name=userip]').val());
				
				if(name == "" || name == "Your Name"){
					$('#errmsg').html('Please enter your name');
					$('#errmsg').show();
					return false;
				}	
				else{
					$('#errmsg').hide();
				}	
				
				if(email == "" || isEMailAddr(email) == false || email == 'Your Email'){
					$('#errmsg').html('Please enter a valid email');
					$('#errmsg').show();
					return false;
				}	
				else{
					$('#errmsg').hide();
				}	
				
				if(company == "" || company == 'Your Company'){
					$('#errmsg').html('Please enter your company name');
					$('#errmsg').show();
					return false;
				}	
				else{
					$('#errmsg').hide();
				}	
				
				if(phone == "" || Validate_no(phone) == false || phone == 'Your Phone'){
					$('#errmsg').html('Please enter a valid phone no.');
					$('#errmsg').show();
					return false;
				}	
				else{
					$('#errmsg').hide();
				}	
				
				$(".load")
				.ajaxStart(function(){
					$(this).show();
				})
				.ajaxComplete(function(){
					$(this).hide();
					
				});
				
				var dataString = 'name='+ name +'&email=' +email +'&company=' +escape(company)+'&phone=' +phone+ '&requirement='+escape(requirement)+'&productids=' +productids+'&userip=' +userip;  
				$.ajax({
					type: "POST",
					url:base_url+"users/savelead",
					data: dataString,  
					success: function(msg){
						
						alert('Your enquiry is sent to the manufacturer/supplier. Thank you!');
						$('#name').val("Your Name");
						$('#email').val("Your Email");
						$('#company').val("Your Company");
						$('#phone').val("Your Phone");
						$('#requirement').val("Your Requirement");
						$('.contactsupp').hide('slow');
					
						
						
					}
				});
		}
		else
			alert('Please select the products to contact supplier');
		
	});
	
	$("#contactsupp-btn").click(function() {
		
		$('.eachcontactsupp').hide();
		$(".contactsupp").show('slow');
	});
	
	$(".closeform").click(function() {
		
		$(".contactsupp").hide();
	});
	
	$(".closeeachfrm").click(function() {
		
		$(this).parents('.eachcontactsupp').hide('slow');
	});
	
	$(".showeachform").click(function() {
		
		$('.eachcontactsupp').hide();
		$('.contactsupp').hide();
		$(this).parents('.products-view-contact').children('.eachcontactsupp').show('slow');
	});
	
	$(".showadenquiryform").click(function() {
		$('.eachcontactsupp').show('slow');
	});
	
	$(".submitadenquiry").click(function() {
		
		var adid = $('#adid').val();
		var adcompname = $('#adcompanyname').val();
		var adbasename = $('#adbasename').val();
		var adissue = $('#adissuename').val();
		var adpage = $('#adpageno').val();
		var curissue = $('#currentissue').val();
		var adcompanyid = $('#adcompanyid').val();
		var name = $('#name').val();
		var email = $('#email').val();
		var company = $('#company').val();
		var phone = $('#phone').val();
		var requirement = $('#requirement').val();
		var userip = trim($('input[name=userip]').val());
				
		if(name == "" || name == "Your Name"){
			$('#errmsg').html('Please enter your name');
			$('#errmsg').show();
			return false;
		}	
		else{
			$('#errmsg').hide();
		}	
		
		if(email == "" || isEMailAddr(email) == false || email == 'Your Email'){
			$('#errmsg').html('Please enter a valid email');
			$('#errmsg').show();
			return false;
		}	
		else{
			$('#errmsg').hide();
		}	
		
		if(company == "" || company == 'Your Company'){
			$('#errmsg').html('Please enter your company name');
			$('#errmsg').show();
			return false;
		}	
		else{
			$('#errmsg').hide();
		}	
		
		if(phone == "" || Validate_no(phone) == false || phone == 'Your Phone'){
			$('#errmsg').html('Please enter a valid phone no.');
			$('#errmsg').show();
			return false;
		}	
		else{
			$('#errmsg').hide();
		}
		if(requirement == "" || requirement == 'Your Requirement'){
			$('#errmsg').html('Please enter your requirement');
			$('#errmsg').show();
			return false;
		}	
		else{
			$('#errmsg').hide();
		}
		
		$("#load")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
			
		});
		var dataString = 'name='+ name +'&email=' +email +'&company=' +escape(company)+'&phone=' +phone+ '&requirement='+escape(requirement)+'&adid=' +adid+'&adcompanyid=' +adcompanyid+'&adcompanyname=' +escape(adcompname)+'&adissue=' +adissue+'&adpage=' +adpage+'&curissue=' +curissue+'&userip='+userip+'&adbasename='+adbasename;
		$.ajax({
			type: "POST",
			url:base_url+"index.php/users/saveleadad",
			data: dataString,  
			success: function(msg){
				
				alert('Your enquiry is sent to the manufacturer/supplier. Thank you!');
				$('#name').val('Your Name');
				$('#email').val('Your Email');
				$('#company').val('Your Company');
				$('#phone').val('Your Phone');
				$('#requirement').val('Your Requirement');
				
				$('.eachcontactsupp').hide('slow');
				
			}
		});
		
		
		
	});
	
	$(".submiteachprod").click(function() {
		var temp = $(this).attr('id');
		var temp = temp.split('_');
		var productid = temp[0];
		var productname = $('#productname_'+productid).val();
		
		var name = $('#name_'+productid).val();
		var email = $('#email_'+productid).val();
		var company = $('#company_'+productid).val();
		var phone = $('#phone_'+productid).val();
		var requirement = $('#requirement_'+productid).val();
		var userip = trim($('input[name=userip]').val());
				
		if(name == "" || name == "Your Name"){
			$('#errmsg_'+productid).html('Please enter your name');
			$('#errmsg_'+productid).show();
			return false;
		}	
		else{
			$('#errmsg_'+productid).hide();
		}	
		
		if(email == "" || isEMailAddr(email) == false || email == 'Your Email'){
			$('#errmsg_'+productid).html('Please enter a valid email');
			$('#errmsg_'+productid).show();
			return false;
		}	
		else{
			$('#errmsg_'+productid).hide();
		}	
		
		if(company == "" || company == 'Your Company'){
			$('#errmsg_'+productid).html('Please enter your company name');
			$('#errmsg_'+productid).show();
			return false;
		}	
		else{
			$('#errmsg_'+productid).hide();
		}	
		
		if(phone == "" || Validate_no(phone) == false || phone == 'Your Phone'){
			$('#errmsg_'+productid).html('Please enter a valid phone no.');
			$('#errmsg_'+productid).show();
			return false;
		}	
		else{
			$('#errmsg_'+productid).hide();
		}	
		
		var obj = $(this).parents('.eachcontactsupp');
		
				
		$("#load_"+productid)
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
			
		});
		var dataString = 'name='+ name +'&email=' +email +'&company=' +escape(company)+'&phone=' +phone+ '&requirement='+escape(requirement)+'&productid=' +productid+'&productname=' +escape(productname)+'&userip=' +userip;
		$.ajax({
			type: "POST",
			url:base_url+"index.php/users/saveleadperprod",
			data: dataString,  
			success: function(msg){
				
				alert('Your enquiry is sent to the manufacturer/supplier. Thank you!');
				$('#name_'+productid).val('Your Name');
				$('#email_'+productid).val('Your Email');
				$('#company_'+productid).val('Your Company');
				$('#phone_'+productid).val('Your Phone');
				$('#requirement_'+productid).val('Your Requirement');
				
				obj.hide('slow');
				
			}
		});
		
		
		
	});
	$('.delproduct').click(function() {
		
		var result = confirm("Are you sure you want to delete this item from your MyIPF list? ");
		if(result == true){
			
			var thisid = $(this).attr('id');
			var myarr = thisid.split("_");
			$("#delprod").val(myarr[0]);
			$("#delsearch").val('');
			
			document.formresults.submit();
			
		}
		else
			return false;
	});	
	
	$('.delsearchitem').click(function() {
		
		var result = confirm("Are you sure you want to delete this item from your MyIPF list? ");
		if(result == true){
			
			var thisid = $(this).attr('id');
			var myarr = thisid.split("_");
			$("#delsearch").val(myarr[0]);
			$("#delprod").val('');
			
			
			document.formresults.submit();
		}
		else
			return false;
	});	
	
	$('.startswith').click(function(){
		
		
		$('.exhibitor-list').html('');
		var content = '';
		
		var popupid = $(this).attr('id');
		var tradefairid = $('#tradefairid').val();
		
		if(content == "" ){
			
			$(".load")
			.ajaxStart(function(){
				$(this).show();
			})
			.ajaxComplete(function(){
				$(this).hide();
				
			});
			
			var dataString = tradefairid+"/"+popupid; 
			$.ajax({
				type: "GET",
				url:base_url+"tradefairs/locateexhibitor/"+ dataString,

				success: function(msg){
					
					$('.exhibitor-list').html(msg);
					
				}
			});
		}
		
	});
	
	
	$("#adissue").change(function(){
		
		var issuebasename = $("#adissue").val();
		window.location = base_url+'ads/ipfads/'+issuebasename;
		
	});
	
	$("#seladv").click(function(){
		
		var issuebasename = $("#adissue").val();
		$('.adcompanynav').show();
		$(".load")
			.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
				
		});
			
		var dataString = issuebasename; 
		$.ajax({
			type: "GET",
			url:base_url+"ads/getadcompanies/"+ dataString,

			success: function(msg){
					
			$('.adcompanynav').html(msg);
			$('.adcompanynav').show();		
			}
		});
			
	});
	
	$("#addproduct").click(function(){
		
		var productcount = $('#productnum').val();
		var product = $('#productcompany').val();
		var productid = $('#compproductid').val();
		
		if(product == "" || product == "type few letters of the product")
		{
			alert('Please choose a product to add');
		}
		else{
			productcount++;
			$("#productsselected").prepend('<span id="product'+productid+'"><font>'+product+'</font><span><a href="javascript:void(0)" onclick="removeProduct('+productid+')">[-] Remove</a></span><input type="hidden" value="'+productid+'" name="productcompany'+productcount+'"/></span>'); 
			$('#productnum').val(productcount);
			$('#productcompany').val('');
			$('#compproductid').val('');
		}	
	});
	
	$("#productcompany").focus(function()
			{
				v = $(this).val();	
				if (v == 'type few letters of the product')
					$(this).val('');
											
			});
		

	$("#productcompany").blur(function()
		{
			v = trim($(this).val());	
			if (v == '')
			$(this).val('type few letters of the product');
										
		});
	
	$(".myipfselectall").click(function() {
		
		if($(this).is(":checked") == true)
			$(".myipfproductid").attr('checked', true);
		else
			$(".myipfproductid").attr('checked', false);
	});
	
	$("#myipfcontactsupp-btn").click(function() {
		
		$('.eachcontactsupp').hide();
		$(".contactsupp").attr('class','contactsupp');
		$(".contactsupp").show('slow');
	});
	
	$("#myipfcontactsupp-btntop").click(function() {
		
		$('.eachcontactsupp').hide();
		$(".contactsupp").attr('class','contactsupp posabs');
		$(".contactsupp").show();
	});
	
	$(".submitmyipf").click(function() {
		
		var flag = 0;
		for(var i=0; i < document.formresults.myipfproductid.length; i++){
			if(document.formresults.myipfproductid[i].checked)
				flag=1;
		}
		
		if(flag ==1){
			
				var productids = "";
				for( i=0; i < document.formresults.myipfproductid.length; i++){
					if(document.formresults.myipfproductid[i].checked)
						productids = productids + document.formresults.myipfproductid[i].value+"||";
				}
				
				name = trim($('#myipfname').val());
				email = trim($('#myipfemail').val());
				company = trim($('#myipfcompany').val());
				phone = trim($('#myipfphone').val());
				requirement = trim($('#myipfrequirement').val());
				var userip = trim($('input[name=userip]').val());
				
				if(name == "" || name == "Your Name"){
					$('#errmsg').html('Please enter your name');
					$('#errmsg').show();
					return false;
				}	
				else{
					$('#errmsg').hide();
				}	
				
				if(email == "" || isEMailAddr(email) == false || email == 'Your Email'){
					$('#errmsg').html('Please enter a valid email');
					$('#errmsg').show();
					return false;
				}	
				else{
					$('#errmsg').hide();
				}	
				
				if(company == "" || company == 'Your Company'){
					$('#errmsg').html('Please enter your company name');
					$('#errmsg').show();
					return false;
				}	
				else{
					$('#errmsg').hide();
				}	
				
				if(phone == "" || Validate_no(phone) == false || phone == 'Your Phone'){
					$('#errmsg').html('Please enter a valid phone no.');
					$('#errmsg').show();
					return false;
				}	
				else{
					$('#errmsg').hide();
				}	
				
				$(".load")
				.ajaxStart(function(){
					$(this).show();
				})
				.ajaxComplete(function(){
					$(this).hide();
					
				});
				
				var dataString = 'name='+ name +'&email=' +email +'&company=' +company+'&phone=' +phone+ '&requirement='+requirement+'&productids=' +productids+'&userip=' +userip;  
				$.ajax({
					type: "POST",
					url:base_url+"index.php/users/savelead",
					data: dataString,  
					success: function(msg){
						
						alert('Your enquiry is sent to the manufacturer/supplier. Thank you!');
						$('#name').val("Your Name");
						$('#email').val("Your Email");
						$('#company').val("Your Company");
						$('#phone').val("Your Phone");
						$('#requirement').val("Your Requirement");
						$('.contactsupp').hide('slow');
					
						
						
					}
				});
		}
		else
			alert('Please select the products to contact supplier');
		
	});
	
	$('#changepwd').click(function() {
		$('#newpwd').show('slow');
		$('#cancelpwd').show();
		$(this).hide();
		
	});
	
	$('#cancelpwd').click(function() {
		$('#newpwd').hide('slow');
		$('#changepwd').show();
		$(this).hide();
		
	});
	
	$('#addphone').click(function() {
		$('#newphone').show('slow');
		$('#addphone').hide();
		$('#cancelphone').show();
	});
	
	$('#editemail').click(function() {
		var phone = $('#existedemail').html();
		$('#editedemail').val(phone);
		$('#existedemail').hide();
		$('#editedemail').show('slow');
		$('#editemail').hide();
		$('#cancelemail').show();
		
	});
	
	$('#cancelemail').click(function() {
		var phone = $('#editedemail').val();
		
		$('#editedemail').hide();
		$('#existedemail').html(phone);
		$('#existedemail').show();
		$('#editemail').show();
		$('#cancelemail').hide();
	});
	
	$('#editphone').click(function() {
		var phone = $('#existedphone').html();
		$('#newphone').hide();
		$('#editedphone').val(phone);
		$('#existedphone').hide();
		$('#editedphone').show('slow');
		$('#editphone').hide();
		$('#addphone').hide();
		$('#separator').hide();
		$('#cancelphone').show();
		
	});
	
	$('#cancelphone').click(function() {
		var phone = $('#editedphone').val();
		
		$('#newphone').hide();
		$('#editedphone').hide();
		//$('#existedphone').html(phone);
		$('#existedphone').show();
		$('#editphone').show();
		$('#separator').show();
		$('#addphone').show();
		$('#cancelphone').hide();
	});
	
	$('#editwebsite').click(function() {
		var phone = $('#existedwebsite').html();
		$('#newwebsite').hide();
		$('#editedwebsite').val(phone);
		$('#existedwebsite').hide();
		$('#editedwebsite').show('slow');
		$('#editwebsite').hide();
		$('#addwebsite').hide();
		$('#websiteseparator').hide();
		$('#cancelwebsite').show();
		
	});
	
	$('#cancelwebsite').click(function() {
		var phone = $('#editedwebsite').val();
		
		$('#newwebsite').hide();
		$('#editedwebsite').hide();
		$('#existedwebsite').html(phone);
		$('#existedwebsite').show();
		$('#editwebsite').show();
		$('#websiteseparator').show();
		$('#addwebsite').show();
		$('#cancelwebsite').hide();
	});
	
	$('#editcompphone').click(function() {
		var phone = $('#existedcompphone').html();
		$('#newcompphone').hide();
		$('#editedcompphone').val(phone);
		$('#existedcompphone').hide();
		$('#editedcompphone').show('slow');
		$('#editcompphone').hide();
		$('#addcompphone').hide();
		$('#compseparator').hide();
		$('#cancelcompphone').show();
		
	});
	
	$('#cancelcompphone').click(function() {
		var phone = $('#editedcompphone').val();
		
		$('#newcompphone').hide();
		$('#editedcompphone').hide();
		$('#existedcompphone').html(phone);
		$('#existedcompphone').show();
		$('#editcompphone').show();
		$('#compseparator').show();
		$('#addcompphone').show();
		$('#cancelcompphone').hide();
	});
	
	$('#addwebsite').click(function() {
		$('#newwebsite').show('slow');
		$('#addwebsite').hide();
		$('#cancelwebsite').show();
		
	});
	
	$('#addcompphone').click(function() {
		$('#newcompphone').show('slow');
		$('#addcompphone').hide();
		$('#cancelcompphone').show();
	});
	
	$('#addcompany').click(function() {
		$('#newcompany').show('slow');
		$('#addcompany').hide();
		$('#cancelcompany').show();
	});
	
	$('#cancelcompany').click(function() {
		$('#newcompany').hide();
		$('#addcompany').show();
		$('#cancelcompany').hide();
		$('#existedcompany').show();
		$('#editcompany').show();
	});
	
	$('#editcompany').click(function() {
		$('#existedcompany').hide();
		$('#editcompany').hide();
		$('#newcompany').show('slow');
		$('#cancelcompany').show();
	});
	
	$('#adddesig').click(function() {
		$('#newdesig').show('slow');
		$('#adddesig').hide();
		$('#canceldesig').show();
	});
	
	$('#canceldesig').click(function() {
		$('#newdesig').hide();
		$('#adddesig').show();
		$('#canceldesig').hide();
		$('#existeddesig').show();
		$('#editdesig').show();
	});
	
	$('#editdesig').click(function() {
		$('#existeddesig').hide();
		$('#editdesig').hide();
		$('#newdesig').show('slow');
		$('#canceldesig').show();
	});
	
	$('#addadd').click(function() {
		$('#newadd').show('slow');
		$('#addadd').hide();
		$('#canceladd').show();
	});
	
	$('#canceladd').click(function() {
		$('#newadd').hide();
		$('#addadd').show();
		$('#canceladd').hide();
		$('#existedadd').show();
		$('#editadd').show();
	});
	
	$('#editadd').click(function() {
		$('#existedadd').hide();
		$('#editadd').hide();
		$('#newadd').show('slow');
		$('#canceladd').show();
	});
	
	$('#addpin').click(function() {
		$('#newpin').show('slow');
		$('#addpin').hide();
		$('#cancelpin').show();
	});
	
	$('#cancelpin').click(function() {
		$('#newpin').hide();
		$('#addpin').show();
		$('#cancelpin').hide();
		$('#existedpin').show();
		$('#editpin').show();
	});
	
	$('#editpin').click(function() {
		$('#existedpin').hide();
		$('#editpin').hide();
		$('#newpin').show('slow');
		$('#cancelpin').show();
	});
	
	$('#saveprofile').click(function(){
		var userid = $('#userid').val();
		var password = $('#newpwd').val();
		var mobile = $('#editedphone').val();
		var newmobile = $('#newphone').val();
		var website = $('#editedwebsite').val();
		var newwebsite = $('#newwebsite').val();
		var compphone = $('#editedcompphone').val();
		var newcompphone = $('#newcompphone').val();
		var newcompany = $('#newcompany').val();
		var newdesig = $('#newdesig').val();
		var newadd = $('#newadd').val();
		var newpin = $('#newpin').val();
		var companyid = $('#companyid').val();
		var country = $('select[name=country]').val();
		var city = $('select[name=city]').val();
		
		
		if(password == "" && $('#newpwd').is(':visible'))
		{
			$("#password_err").show();
			return false;			
		}
		else if(password.length < 6 && $('#newpwd').is(':visible')){
			
			$("#password_err").html('Password should be atleast 6 characters');
			$("#password_err").show();
			return false;	
		}	
		else{
			$("#password_err").hide();
		}
		
		if($('#editedemail').is(':visible')){
			if(email == "" || isEMailAddr(email) == false){
				$("#email_err").html('Email ID is invalid');
				$("#email_err").show();
				return false; 	
			}
			
			else{
				
				$("#email_err").hide();
			}
		}
		
		if($('#editedphone').is(':visible')){
			if(mobile == ""){
				
				$("#mobile_err").html('Valid Mobile no. is mandatory');
				$("#mobile_err").show();
				return false; 	
			}
			else{
				$("#mobile_err").hide();
			}
		}
		if($('#newphone').is(':visible')){
			if(newmobile == ""){
				
				$("#mobile_err").html('Valid Mobile no. is mandatory');
				$("#mobile_err").show();
				return false; 	
			}
			else{
				$("#mobile_err").hide();
			}
		}
		
		if($('#editedwebsite').is(':visible')){
			if(website == ""){
				
				$("#website_err").html('Website is mandatory');
				$("#website_err").show();
				return false; 	
			}
			else{
				$("#website_err").hide();
			}
		}
		if($('#newwebsite').is(':visible')){
			if(newwebsite == ""){
				
				$("#website_err").html('Website is mandatory');
				$("#website_err").show();
				return false; 	
			}
			else{
				$("#website_err").hide();
			}
		}
		
		if($('#editedcompphone').is(':visible')){
			if(compphone == "" || Validate_no(compphone) == false ){
				
				$("#phone_err").html('Valid Phone no. is mandatory');
				$("#phone_err").show();
				return false; 	
			}
			else{
				$("#phone_err").hide();
			}
		}
		if($('#newcompphone').is(':visible')){
			if(newcompphone == "" || Validate_no(newcompphone) == false ){
				
				$("#phone_err").html('Valid Phone no. is mandatory');
				$("#phone_err").show();
				return false; 	
			}
			else{
				$("#phone_err").hide();
			}
		}
		
		var dataString = 'userid='+userid+'&password='+ password +'&mobile=' +mobile+'&newmobile=' +newmobile+ 
						'&compphone='+compphone+'&newcompphone=' +newcompphone+'&website=' +website+'&newwebsite='+newwebsite+
						'&newcompany='+newcompany+'&companyid='+companyid+'&country='+country+'&city='+city+'&desig='+newdesig+'&address='+newadd+'&pin='+newpin;  
		$.ajax({
			type: "POST",
			url:base_url+"index.php/users/saveprofile",
			data: dataString,  
			success: function(msg){
				window.location.reload(true);
			}
		});
		
	});
	
	
	$(".submittfenquiry").click(function() {
		
		var tfid = $('#tradefairid').val();
		var tfname = $('#tradefairname').val();
		var name = $('#name').val();
		var email = $('#email').val();
		var company = $('#company').val();
		var phone = $('#phone').val();
		var requirement = $('#requirement').val();
		var userip = trim($('input[name=userip]').val());
				
		if(name == "" || name == "Your Name"){
			$('#errmsg').html('Please enter your name');
			$('#errmsg').show();
			return false;
		}	
		else{
			$('#errmsg').hide();
		}	
		
		if(email == "" || isEMailAddr(email) == false || email == 'Your Email'){
			$('#errmsg').html('Please enter a valid email');
			$('#errmsg').show();
			return false;
		}	
		else{
			$('#errmsg').hide();
		}	
		
		if(company == "" || company == 'Your Company'){
			$('#errmsg').html('Please enter your company name');
			$('#errmsg').show();
			return false;
		}	
		else{
			$('#errmsg').hide();
		}	
		
		if(phone == "" || Validate_no(phone) == false || phone == 'Your Phone'){
			$('#errmsg').html('Please enter a valid phone no.');
			$('#errmsg').show();
			return false;
		}	
		else{
			$('#errmsg').hide();
		}
		if(requirement == "" || requirement == 'Your Requirement'){
			$('#errmsg').html('Please enter your requirement');
			$('#errmsg').show();
			return false;
		}	
		else{
			$('#errmsg').hide();
		}
		
		$("#load")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
			
		});
		var dataString = 'name='+name+'&email='+email+'&company='+escape(company)+'&phone='+phone+'&requirement='+escape(requirement)+'&tfid='+tfid+'&tfname='+escape(tfname)+'&userip='+userip;
		$.ajax({
			type: "POST",
			url:base_url+"index.php/users/saveleadtradefair",
			data: dataString,  
			success: function(msg){
				
				alert('Your enquiry is sent to the Organizers. Thank you!');
				$('#name').val('Your Name');
				$('#email').val('Your Email');
				$('#company').val('Your Company');
				$('#phone').val('Your Phone');
				$('#requirement').val('Your Requirement');
				
				$('.eachcontactsupp').hide('slow');
				
			}
		});
		
		
		
	});
	
	
});


	function trim(s) {
	  while (s.substring(0,1) == ' ') {
	    s = s.substring(1,s.length);
	  }
	  while (s.substring(s.length-1,s.length) == ' ') {
	    s = s.substring(0,s.length-1);
	  }
	  return s;
	}

	var count = 1,countNext = 2,alertTimerId;
	function productSlideShow(pagenos){
		for(i=2;i<=pagenos;i++)
		{
			$('.primediv'+i).hide();
		}
		alertTimerId = setInterval ( "rotateproducts(0,"+pagenos+")", 4000 );
	}
	
	function rotateproducts(no,pagenos){
	
		var count;
		if(no!=0)
		{
			countNext=no;	
		}
		
		if(countNext == 1) 
			count = pagenos;
		else 
			count = countNext - 1;
			
			for(var i=1; i<=pagenos; i++){
				
				
				$('#prod-rotate li:eq('+i+')').removeClass('act');
				$('#prod-rotate li:eq('+i+')').html('<a href="javascript:void(0)">'+i+'</a>');
				$('.primediv'+i).hide();
				
			}
			
			$('#prod-rotate li:eq('+(countNext)+')').addClass('act');
			$('#prod-rotate li:eq('+(countNext)+')').children('a').remove();
			$('#prod-rotate li:eq('+(countNext)+')').html(countNext);
			$('.primediv'+(countNext)).show();
			
			//clearInterval(alertTimerId);
			
			
		count = countNext;
		countNext++;
		if(countNext == (pagenos))
			countNext = 1;
	}
	
	function isEMailAddr(elem) 
	{
	 
		var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
		if (!elem.match(re)) 
			return false;
		else 
		   return true;
	        
	}

	function Validate_no(phone)
	{ 
		count = phone.length;
		var valid_str="0123456789-+() ";
		
		for(var pos=0; pos<=count; pos++)
		{	
			if(valid_str.search(phone.charAt(pos)) == -1)
				return false;
		}
			
	}
	
	function addto_myipf(username, productid){
		
		if(username != ""){
			
			var dataString = 'productid='+productid+'&username='+username; 
			$.ajax({
				type: "POST",
				url:base_url+"index.php/users/saveproduct",
				data: dataString,  
				success: function(msg){
					if(msg == 1){
						alert('This product has been saved to your MyIPF page');
						window.location.reload(true);
					}	
					else{
						alert('This product already exists in your Saved Products list');
					}
				}
			});
			
		}
		else
			alert('Please login to add this product to your MyIPF page');
	
	}
	
	function addto_mylist(username, productid){
	
		if(username != ""){
			
					var flag = 0;
					for(var i=0; i < document.products.productid.length; i++){
						if(document.products.productid[i].checked)
							flag=1;
					}
					
					if(flag ==1){
						
						var productids = "";
						
							
							for( i=0; i < document.products.productid.length; i++){
								if(document.products.productid[i].checked)
									productids = productids + document.products.productid[i].value+"||";
							}
							
							var dataString = 'username='+ username +'&productids=' +productids; 
							$.ajax({
								type: "POST",
								url:base_url+"index.php/users/saveallproducts",
								data: dataString,  
								success: function(msg){
									
										alert('Products selected have been saved to your MyIPF page');
										window.location.reload(true);
									
								}
							});
							
					}	
					else{
							
						alert('You did not select any products to add to your MyIPF page');	
					}	
		}
		else
			alert('Please login to add products to your MyIPF page');
					
	}
	
	function savesearch(username){
		
		if(username != ""){
			
			var searchparam = $("#searchparam").val();
			
			var dataString = 'username='+ username +'&searchparam=' +searchparam; 
			$.ajax({
				type: "POST",
				url:base_url+"users/savesearch",
				data: dataString,  
				success: function(msg){
					
					if(msg == 1){
						alert('Search string has been saved to your MyIPF list');
						window.location.reload(true);
					}	
					else
						alert('Search string already exists in your MyIPF list');
					
				}
			});
			
		}
		else
			alert('Please login to save this search');
		
	}
	
	function initTabs()
	{
		var sets = document.getElementsByTagName("ul");
		for (var i = 0; i < sets.length; i++)
		{
			if (sets[i].className.indexOf("tabsettf") != -1)
			{
				var tabs = [];
				var links = sets[i].getElementsByTagName("a");
				for (var j = 0; j < links.length; j++)
				{
					if (links[j].className.indexOf("tabtf") != -1)
					{
						tabs.push(links[j]);
						links[j].tabs = tabs;
						var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

						//reset all tabs on start
						if (c) if (links[j].parentNode.className.indexOf("active") != -1) c.style.display = "block";
						else c.style.display = "none";

						links[j].onclick = function ()
						{
							var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
							if (c)
							{
								//reset all tabs before change
								for (var i = 0; i < this.tabs.length; i++)
								{
									var tab = document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1));
									if (tab)
									{
											tab.style.display = "none";
										
									}
									this.tabs[i].parentNode.className = this.tabs[i].parentNode.className.replace("active", "");
								}
								this.parentNode.className += " active";
									c.style.display = "block";
									$(this).blur();
								return false;
							}
						}
					}
				}
			}
		}
		return false;
	}

	function closeadcomp(){
		
		$('.adcompanynav').hide();
	}
	
	function compstartswith(id){
		
		$('.adexhibitor-list').html('');
		var content = '';
		
		var popupid = id;
		var issuename = $('#adid').val();
		
		if(content == "" ){
			
			$(".load")
			.ajaxStart(function(){
				$(this).show();
			})
			.ajaxComplete(function(){
				$(this).hide();
				
			});
			
			if(popupid == '_')
			{
				var dataString = issuename; 
				$.ajax({
					type: "GET",
					url:base_url+"ads/getadcompanies/"+ dataString,

					success: function(msg){
							
					$('.adcompanynav').html(msg);
					$('.adcompanynav').show();		
					}
				});
			}
			else
			{	
				var dataString = issuename+"/"+popupid; 
				$.ajax({
					type: "GET",
					url:base_url+"ads/adcompaniesstarts/"+ dataString,
	
					success: function(msg){
						
						$('.adexhibitor-list').html(msg);
						
					}
				});
			}
		}
		
	}
	
	function compnav(month,perpage,id){
		
		var offset = id;
		var per_page = perpage;
		var cur_month = month;
		
		$(".load")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
			
		});
		
		var dataString = cur_month+"/"+offset+"/"+per_page; 
		$.ajax({
			type: "GET",
			url:base_url+"ads/getadcompaniesnav/"+ dataString,

			success: function(msg){
				
				$('.adcompanynav').html(msg);
				
			}
		});
	}
	
	function showcities(name,val){
		dataString = "stateid="+val+"&name="+name;
		$("select[name="+name+"]").parents('tr').next('tr').children().next().children('.temp').html("<select name='city'><option>Loading....</option></select>");
		$.ajax({
				type: "POST",
				url:base_url+'index.php/companies/getCitiesReg',
				data: dataString,  
				success: function(msg){
				$("select[name="+name+"]").parents('tr').next('tr').children().next().children('.temp').html(msg);
			  }
		});
		
		dataString1 = "stateid="+val;
		
		$.ajax({
			type: "POST",
			url:base_url+'index.php/companies/getISDCode',
			data: dataString1,  
			success: function(msg){
			$("input[name=mcountrycode]").val(msg);
			$("input[name=ccountrycode]").val(msg);
		  }
	});	
		
	}
	
	function removeProduct(x)
	{
		$('#product'+x).remove('');
	}
