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 dataString = 'email='+ $(this).val(); 
				$.ajax({
					type: "POST",
					url:base_url+"register/checkemailid",
					data: dataString,  
					success: function(msg){
						if(msg != 0){
							$("#email_err").html('Sorry! This emaildid is already in use');
							$("#email_err").show();
						}
						else
							$("#email_err").hide();
					}
				});
		}
	});	
	
	$("#register").click(function() { 
		
		var fname = trim($('input[name=firstname]').val());
		var username = trim($('input[name=username]').val());
		var password = trim($('input[name=password]').val());
		var passwordcopy = trim($('input[name=passwordcopy]').val());
		var email = trim($('input[name=email]').val());
		var countrycode = trim($('input[name=countrycode]').val());
		var areacode = trim($('input[name=areacode]').val());
		var phone = trim($('input[name=phone]').val());
		var securityques = trim($('select[name=securityques]').val());
		var securityans = trim($('input[name=securityans]').val());
		
		if(fname == "" || fname =='First Name'){
			
			$("#fname_err").show();
			return false;			
		}
		else{
			
			$("#fname_err").hide();
		}	
		
		if(username == ""){
			$("#username_err").html('Username is  mandatory');
			$("#username_err").show();
			return false;			
		}
		else{
			
			$("#username_err").hide();
		}
		
		if(password == ""){
			
			$("#password_err").show();
			return false;			
		}
		else if(password.length < 6){
			
			$("#password_err").html('Password should be minimum of 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();
		}	
		
		if(email == "" || isEMailAddr(email) == false ){
			$("#email_err").html('Email ID is invalid');
			$("#email_err").show();
			return false; 	
		}
		else{
			
			$("#email_err").hide();
		}	
		
		if(countrycode == "" ){
			
				$("#phone_err").html('Valid Country code is mandatory');
				$("#phone_err").show();
				return false; 
			
		}	
		else if(areacode == "Code" || areacode == "" || Validate_no(areacode) == false){
			
			$("#phone_err").html('Valid Area code is mandatory');
			$("#phone_err").show();
			return false; 	
		}	
		else if(phone == "" || Validate_no(phone) == false ){
			
			$("#phone_err").html('Valid Phone no. is mandatory');
			$("#phone_err").show();
			return false; 	
		}
		else{
			$("#phone_err").hide();
		}	
		
		if(securityques == ""){
			$("#ques_err").show();
			return false; 
		}
		else{
			$("#ques_err").hide();
		}	
		
		if(securityans == ""){
			$("#ans_err").show();
			return false; 
		}
		else{
			$("#ans_err").hide();
		}	
		
		
		
	});
	$(".label_fields").focus(function()
			{
				v = $(this).val();	
				if (v == 'First Name' || v == 'Last Name' || v=='Code')
					$(this).val('');
										
			});
	$(".label_fields").blur(function()
			{
				v = trim($(this).val());	
				if (v == '')
					$(this).val($(this).attr('id'));
										
			 });
	$(".field").focus(function()
			{
				v = $(this).val();	
				if (v == 'Your Name' || v == 'Your Email' || v=='Your Company' || v=='Your Phone' || v=='Your Requirement')
					$(this).val('');
										
			});
	$(".field").blur(function()
			{
				v = trim($(this).val());	
				if (v == '')
					$(this).val($(this).attr('name'));
										
			 });
	
	$("#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='+requirement+'&supplier_id=' +companyid+'&product_id='+productid+'&product_name='+productname+'&userip='+userip;
			$.ajax({
				type: "POST",
				url:base_url+"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=' +company+'&lead_phone=' +phone+ '&lead_requirement='+requirement+'&supplier_id='+companyid+'&userip='+userip;
		$.ajax({
			type: "POST",
			url:base_url+"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();
		$(".product-div").show();
		$(this).attr('class','active');
		$('#myipf-tab2').attr('class','inactive');
		
	});
	$('#myipf-tab2').click(function() {
		
		$(".product-div").hide();
		$(".search-div").show();
		$(this).attr('class','active');
		$('#myipf-tab1').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=' +company+'&phone=' +phone+ '&requirement='+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=' +company+'&phone=' +phone+ '&requirement='+requirement+'&adid=' +adid+'&adcompanyid=' +adcompanyid+'&adcompanyname=' +adcompname+'&adissue=' +adissue+'&adpage=' +adpage+'&curissue=' +curissue+'&userip='+userip+'&adbasename='+adbasename;
		$.ajax({
			type: "POST",
			url:base_url+"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=' +company+'&phone=' +phone+ '&requirement='+requirement+'&productid=' +productid+'&productname=' +productname+'&userip=' +userip;
		$.ajax({
			type: "POST",
			url:base_url+"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();		
			}
		});
			
	});
	
});


	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 = 'username='+ username +'&productid=' +productid; 
			$.ajax({
				type: "POST",
				url:base_url+"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+"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);
				
			}
		});
	}
