$(document).ready(function() {
	/* FOR THE VERTICAL TABS OF INV GUIDES ON HOME PAGE */
	investmentTopicClickListener();
	$('.investmentTopics a').bind('click', function() {  
		investmentTopicClickListener();
	});
	setForm();
	
	/* FOR MAIN NAVIGATION DROP DOWNS */
	$('.mainmenus').click(function() {
		$('.mainmenus').removeClass('selected');
		$(this).addClass('selected');
		var index = $('.mainmenus').index(this);
		$('.submenu').hide(); 
		$('#submenu'+index).show();
	});
	
	$('.mainmenus').hover(function() {
		$(this).addClass('active');
	}, function() {
		$(this).removeClass('active');
	});

	$('body').bind('click', function(e) {
		var $clicked=$(e.target);
		if($clicked.is('ul#nav-menu li') || $clicked.parents().is('ul#nav-menu') || $clicked.parents().is('.submenu')) {
																
		} else {
			$('.submenu').hide();
			$('ul#nav-menu li').removeClass('selected');
		}
	});
	
	/* FOR THE LOGIN BOX */
    $('.loginlnk').click(function() {$("div#registfrm").hide('fast');
	$("#alert").html('');																 
	$("div#loginfrm").slideToggle();
		
	});
		
		$('.reg').click(function() {$("div#loginfrm").hide('fast');
		$("div#registfrm").slideToggle();
		
	});
	
	$('#forgot_link').toggle(function() 
		{
			$('#forgot_form').show();
		}, 
		function()
		{
			$('#forgot_form').hide();		
		}
		);
	
	
	/* FOR THE BANNER SLIDESHOW */
	bannerSlideShow();
	
	$('.cycleBanner img').bind('click', function() {
		var bannerIndex = $('.cycleBanner img').index(this);
		bannerIndex += 1;
		cycleBanners(bannerIndex);
	});

	// FOR THE FORM FIELDS
	$(".label_fields").focus(function()
	{
		v = $(this).val();	
		if (v == 'User Name' || v == 'Password' || v=='Your Email ID')
			$(this).val('');
								
	});
	$(".label_fields").blur(function()
	{
		v = $(this).val();	
		if (v == '')
			$(this).val($(this).attr('id'));
								
	 });
	
	
	$(".search_field").focus(function()
	{
		v = $(this).val();	
		if (v == 'Search Investor First')
			$(this).val('');
								
	});
	$(".search_field").blur(function()
	{
		v = $(this).val();	
		if (v == '')
			$(this).val('Search Investor First');
								
	 });
	

	
/* FOR + and - sign in the second level navigation */
$('.floatL .li-icon').click(function() {
	
	if($(this).parent().children('ul').css('display') == 'block')
	{
		$(this).attr('src','http://www.investorfirst.in/nism/images/plus.gif');
		$(this).parent().children('ul').hide('fast');
	}
	else
	{ 
		$(this).attr('src','http://www.investorfirst.in/nism/images/minus.gif');
		$(this).parent().children('ul').show('fast');
	}
									   
	
});

$('#sitemap .li-icon').click(function() {
												 
	if($(this).parent().children('ul').is(':hidden')==true)
		$(this).attr('src','http://www.investorfirst.in/nism/images/minus.gif');
	else
		$(this).attr('src','http://www.investorfirst.in/nism/images/plus.gif');
									   
	
    $(this).parent().children('ul').toggle('slow');
	$(this).removeClass('plus-icon').addClass('minus-icon');
	
  
  });

$('#forgotsubmit').click(function(){
													if( trim( $('#forgot').val() ) == "" || isEMailAddr($("#forgot").val())==false )
													{
														$("#alert").html('Please enter a valid email ID');
														return false;
													}
													
																								
								  });

$('#newsletter_button').click(function(){
									   				
													if(trim($("#newsletter_email").val())=="" || isEMailAddr($("#newsletter_email").val())==false)
													{
														alert('Please enter a valid email ID');
														return false;
													}
									   	
									   });

$("#registsubmit").click(function(){
												
											 		if(trim($("#txtName").val())=="" )
													{ 
														$("#err_msg").html('Please enter your Name');
														$("#txtName").focus();
														return false;
													}
													if(trim($("#txtEmail").val())=="" || isEMailAddr($("#txtEmail").val())==false)
													{
														$("#err_msg").html('Please enter a valid Email Id');
														$("#txtEmail").focus();
														return false;
													}
							   });

$("#loginsubmit").click(function(){
												
											 		if(trim($("#txtUsername").val())=="" )
													{ 
														$("#alert").html('Please enter your Username');
														$("#txtUsername").focus();
														return false;
													}
													if(trim($("#txtUserpass").val())=="" )
													{
														$("#alert").html('Please enter your Password');
														$("#txtUserpass").focus();
														return false;
													}
							   });

$("#btnregist").click(function(){							   
							    
								if(trim($("#txtUseremail").val())=="" )
								{ 
														$("#reg_err").html('<img src="images/incorrect.gif"> Please enter your Username');
														$("#txtUseremail").focus();
														window.scroll(0,0);
														return false;
								}
							   
							   if(trim($("#txtPassword").val())=="" )
								{ 
														$("#reg_err").html('<img src="images/incorrect.gif"> Please enter your Password');
														$("#txtPassword").focus();
														window.scroll(0,0);
														return false;
								}
								
								 if(trim($("#txtPassword").val()).length<6 )
								{ 
														$("#reg_err").html('<img src="images/incorrect.gif"> Password should be of minimum 6 characters.');
														$("#txtPassword").focus();
														window.scroll(0,0);
														return false;
								}
								
								if(trim($("#txtPassword").val())!=trim($("#txtConpassword").val()) )
								{
														$("#reg_err").html('<img src="images/incorrect.gif"> Please re-enter your passwords as they do not match');
														$("#txtConpassword").focus();
														window.scroll(0,0);
														return false;
								}
								
								if( $('#termscheck').is(':checked') != 1)
								{	
														$("#reg_err").html('<img src="images/incorrect.gif"> You have to agree to the terms of use to move forward');
														window.scroll(0,0);
														return false;
								}
								
								/* VALIDATE CAPTCHA */
								
								var lnum = $('#lnum').html();
								var rnum = $('#rnum').html();
								var sum = parseInt(lnum) + parseInt(rnum); 
								var answer = $('#answer').val();
								//alert (sum + " " + answer);
								if(answer == "" || answer != sum)
								{
									$("#reg_err").html('<img src="images/incorrect.gif"> Well, you failed on simple math. The anwer below is incorrect.');
									$("#answer").css('background-color','#ffb6b6')
									$("#answer").focus();
									window.scroll(0,0);
									return false;
								}  
				});
								
$("#btnprofile").click(function(){	 
						   
							  /* if(trim($("#txtPassword").val())=="" )
								{ 
										$("#reg_err").html('<img src="images/incorrect.gif"> Please enter your Password');
										$("#txtPassword").focus();
										window.scroll(0,0);
										return false;
								} */
								
								 if(trim($("#txtPassword").val()).length<6 && trim($("#txtPassword").val()).length>0)
								{ 
										$("#reg_err").html('<img src="images/incorrect.gif"> Password should be of minimum 6 characters.');
										$("#txtPassword").focus();
										window.scroll(0,0);
										return false;
								}
								
								if(trim($("#txtPassword").val())!=trim($("#txtConpassword").val()) )
								{
										$("#reg_err").html('<img src="images/incorrect.gif"> Please re-enter your passwords as they do not match');
										$("#txtConpassword").focus();
										window.scroll(0,0);
										return false;
								}
								
 });


//Financial service box in registeration long form module & update profile form
var bfsi_info = $("#bfsi_info").html();
$("input[name=chkBfsi]:radio").change(function(){
										   		//alert("Reached here" + $("input[name='chkBfsi']:checked").val() );
												if( $("input[name='chkBfsi']:checked").val() == 1 )
													{
														$("#bfsi_box").show();
														$("#bfsi_info").html(bfsi_info);
													}
												else
													{
														$("#bfsi_info").html("");
														$("#bfsi_box").hide();														
													}
										   
										   });

$('.poll-submit').click(function()
			{	
				
				var quesid=$("input[name=quesid]").val();
				$(".temp").html($("#graph").html());
				var opt=$("input[name=poll_choice]:checked").val();
				if(opt==undefined)
				{
					alert('Please select an option before submitting');
					return false;
				}
				$("#graph").html('<img src="images/indicator.gif">')
				var dataString = 'quesid='+ quesid + '&poll_choice=' + opt; 
				
						$.ajax({
								type: "POST",
								url: "pollinsert.php",
								data: dataString,  
								success: function(msg){
								$("#graph").html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"  id="Column2D" width="220" height="200"><param name="movie" value="Charts/Column2D.swf" /><param name="FlashVars" value="&dataURL=data.xml"><param name="quality" value="high" /><embed src="Charts/Column2D.swf" flashVars="&dataURL=data.xml" quality="high"  height="200"  name="Column2D" type="application/x-shockwave-flash" width="220" /></object>');
								
								$(".submitbtn .res").html("Total votes:"+msg);
								$("#poll-result").html('Back to Poll');
								$(".submitbtn .btn").hide();
								$(".submitbtn .res").show();
			
								}
							});
			});
$('#poll-result').click(function(){	
					 
		if($(this).html() == "View results"){
			
				var quesid=$("input[name=quesid]").val();
				$(".temp").html($("#graph").html());
				$("#graph").html('<img src="images/indicator.gif">')
				var dataString = 'quesid='+ quesid + '&result=1'; 
				
						$.ajax({
								type: "POST",
								url: "pollinsert.php",
								data: dataString,  
								success: function(msg){
								$("#graph").html('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"  id="Column2D" width="220" height="200"><param name="movie" value="Charts/Column2D.swf" /><param name="FlashVars" value="&dataURL=data.xml"><param name="quality" value="high" /><embed src="Charts/Column2D.swf" flashVars="&dataURL=data.xml" quality="high"  height="200"  name="Column2D" type="application/x-shockwave-flash" width="220" /></object>');
								
								$(".submitbtn .res").html("Total votes:"+msg);
								$("#poll-result").html('Back to Poll');
								$(".submitbtn .btn").hide();
								$(".submitbtn .res").show();
								}
							});
		}
		else if($(this).html() == "Back to Poll"){
			$("#graph").html($(".temp").html());
			$("#poll-result").html('View results');
			$(".submitbtn .btn").show();
			$(".submitbtn .res").hide();
		}
		
});

  
			
			$('#btnquery').click(function()
			{	
				
				var mail=$("input[name=txtEmailid]").val();
				var query=$("#txtQuery").val();
				if(trim(mail)=="" || isEMailAddr(mail)==false)
				{
					$("#query_msg").html('Please enter a valid Email Id');
					$("input[name=txtEmailid]").focus();
					return false;
				}
				if(trim(query)=="" )
				{
					$("#query_msg").html('Please enter your question');
					$("#txtQuery").focus();
					return false;
				}
				
				var dataString = 'email='+ mail + '&query=' + query; 
				
						$.ajax({
								type: "POST",
								url: "pollinsert.php",
								data: dataString,  
								success: function(msg){
								$("input[name=txtEmailid]").val('');
								$("#txtQuery").val('');
								$("#query_msg").html('<span class="thanksmsg">Thank you for submitting your question. We will get back to you.</span>');
								
								}
							});
			});
});





var count = 1,countNext = 2,alertTimerId;
function bannerSlideShow(){
	for(i=2;i<=4;i++)
	{
		$('#banner'+i).hide();
	}
	alertTimerId = setInterval ( "cycleBanners(0)", 6000 );
}
function cycleBanners(no){

if(no!=0)
{
	countNext=no;	
}
	
	if(countNext == 1) 
		count = 5;
	else 
		count = countNext - 1;
		
		for(var i=1; i<=5; i++){
			
			$('#banner'+i).hide();
			$('#cycleBannerImg'+i).attr('src',"images/bannerDactive.gif");
			
		}
		$('#banner'+countNext).show();
		$('#cycleBannerImg'+countNext).attr('src',"images/bannerActive.gif");
		//clearInterval(alertTimerId);
		
		
	count = countNext;
	countNext++;
	if(countNext == 6)
		countNext = 1;
}

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;
}
function isEMailAddr(elem) 
 {
  
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!elem.match(re)) 
	return false;
    else 
    return true;
         
 }
 

function investmentTopicClickListener() {
	$('.investmentTopics').bind('click', function() {
		$('.investmentTopics').removeClass('active');
		$(this).addClass('active');
		$('.divider').show();
		$(this).next().hide();
		if($(this).prev().attr('class') == "divider"){
			$(this).prev().hide();
		}
		var index = $('.investmentTopics').index(this);
		$('.investmentTopicContent').hide();
		document.getElementById('div'+index).style.display = 'block';
	});
}
function selectReplacement(obj,count) {
	// append a class to the select
	obj.className += ' replaced';
	// create list for styling
	var ul = document.createElement('ul');
	ul.className = 'selectReplacement';
	ul.id = 'selectReplacement' + count;
	var opts = obj.options;
	for (var i=0; i<opts.length; i++) {
		var selectedOpt;
		if (opts[i].selected) {
			selectedOpt = i;
			break;
		} 
		else {
			selectedOpt = 0;
		}
	}
	for (var i=0; i<opts.length; i++) {
		var li = document.createElement('li');
		var txt = document.createTextNode(opts[i].text);
		var div = document.createElement('div');
		div.appendChild(txt);
		li.appendChild(div);
		li.selIndex = opts[i].index;
		li.selectID = obj.id;
		li.onclick = function() {
			selectMe(this, count);
		}
		if (i == selectedOpt) {
			li.className = 'selected';
			li.onclick = function() {
				//this.parentNode.className += ' selectOpen';
				this.onclick = function() {
					selectMe(this, count);
				}
			}
		}
		ul.appendChild(li);
	}
	// add the input and the ul
	obj.parentNode.appendChild(ul);
	
	$('ul#selectReplacement'+count).hover(function() {
		$('ul#selectReplacement' + count).show();
	}, function() {
		$('ul#selectReplacement' + count).hide();
	});
	
	$('ul#selectReplacement' + count + ' li').mouseout(function(){
		$(this).removeClass('hover');
	}).mouseover(function(){
		$(this).addClass('hover');
	});
	
	$('.fakeSelect' + count).bind('click', function() {
		$('#selectReplacement' + count).show();
	});
}
function selectMe(obj, count) {
	var lis = obj.parentNode.getElementsByTagName('li');
	for (var i=0; i<lis.length; i++) {
		if (lis[i] != obj) { // not the selected list item
			lis[i].className='';
			lis[i].onclick = function() {
				selectMe(this, count);
			}
		} 
		else {
			setVal(count, obj.selIndex);
			obj.className='selected';
			obj.onclick = function() {
				this.onclick = function() {
					selectMe(this, count);
				}
			}
		}
	}
	$('.fakeSelect'+count)[0].innerHTML = obj.innerHTML;
	$('#selectReplacement'+count).hide();
}
function setVal(count, selIndex) {
	var obj = document.getElementById('fakeSelect'+count);
	obj.selectedIndex = selIndex;
        if(obj.value.substring(0,8)=="quesans_")
	{
		$('.quesans').hide();
		$('#'+obj.value).show();

                var titlediv=obj.value.replace('quesans_','');
		$('.faqtitle').hide();
		$('#faqtitle_'+titlediv).show();
	}
        if(obj.value.substring(0,6)=="event_")
	{
		$('.m-content').hide();
		$('#'+obj.value).show();
	}
}
function setForm() {
	var s = document.getElementsByTagName('select');
	var count;
	for (var i=0, count=1; i<s.length; i++, count++) {
		selectReplacement(s[i],count);
	}
}


function toggle(showHideDiv, switchTextDiv) {
	var ele = document.getElementById(showHideDiv);
	var text = document.getElementById(switchTextDiv);
	if(ele.style.display == "block") {
    		ele.style.display = "none";
		text.innerHTML = "<img src='images/plus.gif'> ";
  	}
	else {
		ele.style.display = "block";
		text.innerHTML = "<img src='images/minus.gif'> ";
	}
} 

function citytxtbox(val)
{
	
	if(val==35)
	{
	document.getElementById('citybox').style.display="block";
	document.getElementById('ocityname').style.display="block";
	}
	else
	{
		document.getElementById('citybox').style.display="none";
	document.getElementById('ocityname').style.display="none";
	}
	
}

function showstate(val)
{
	if(val==102)
	{
		document.getElementById('txtState').style.display="none";
		document.getElementById('ostate').style.display="block";
		document.getElementById('txtCity').style.display="none";
		document.getElementById('ocity').style.display="block";
	}
	else
	{
		document.getElementById('txtState').style.display="block";
		document.getElementById('ostate').style.display="none";
		document.getElementById('txtCity').style.display="block";
		document.getElementById('ocity').style.display="none";
	}
}
