// JavaScript Document

var menuDescTO = ""; // global var for menu description Timeout
var forsideSlideshow = 1; // global var for current Slideshow image
var forsideSlideshowSize = 0; // global var for total Slideshow images
var mousePos = new Array(0,0); // global var to track mouse position to start/stop slideshow
var highlightTime = 10; // Time in seconds to change between each highlight

$(document).ready(function(){

	// Reset Font Size
	var originalFontSize = $('body').css('font-size');
	// Set Font Size
	$("#setFontSize").click(function(){
		if($(this).attr('src').indexOf('plus') >= 0){
			$(this).attr('src', $(this).attr('src').replace('plus', 'less'));
			var currentFontSizeNum = parseFloat(originalFontSize, 10);
			var newFontSize = currentFontSizeNum*1.2;
			$('body').css('font-size', newFontSize);
		} else{
			$(this).attr('src', $(this).attr('src').replace('less', 'plus'));
			$('body').css('font-size', originalFontSize);
		}
	});

	// Set action to hide menu descriptions on document click
	$(document).click(function(){
		closeMenuDesc();
	});


	// Display menu content
	$('ul#menu li').hover(function(){
		if(!$(this).hasClass('search'))
			$(this).addClass('on');
	}, function(){
		if(!$(this).hasClass('search'))
			$(this).removeClass('on');
	});

	// Display menu content
	$('ul#menu li a, ul#menu li span').mouseover(function(){
		var myid = $(this).parent().attr('id');
		clearTimeout(menuDescTO);
		$('ul#menu_desc li.desc').hide();
		if($('ul#menu_desc li#desc_'+myid).length){
			$('ul#menu_desc li#desc_'+myid).show();
			if($('ul#menu_desc').css('display') == 'none'){
				$('ul#menu_desc').slideDown();
			}
			menuDescTO = setTimeout("closeMenuDesc()", 3000);
		} else{
			clearTimeout(menuDescTO);
			$('ul#menu_desc').hide();
		}
	});

	// Display menu content
	$('ul#menu_desc li a, ul#menu_desc li span').hover(function(){
		var myimg = $(this).parent().attr('img');
		var mydesc = $(this).parent().attr('resume');
		if(myimg.length > 0 || mydesc.length > 0){
			var mydesccont = $(this).parent().parent().parent().find('div.men_desc_txt');
			var myhtml = "";
			if(myimg.length > 0)
				myhtml += '<img src="/uploads/menu/medium/'+myimg+'" />'+"\n";
			myhtml += "<h3>"+$(this).html()+"</h3>\n";
			myhtml += "<div>"+mydesc+"</div>\n";
			mydesccont.html(myhtml);
			mydesccont.show();
		}
	}, function(){
		$(this).parent().parent().parent().find('div.men_desc_txt').hide();
		var myid = $(this).parent().parent().parent().attr('id').split('_')[2];
		var myimg = $('li#men_'+myid).attr('img');
		var mydesc = $('li#men_'+myid).attr('resume');
		if(myimg.length > 0 || mydesc.length > 0){
			var mydesccont = $(this).parent().parent().parent().find('div.men_desc_txt');
			var myhtml = "";
			if(myimg.length > 0)
				myhtml += '<img src="/uploads/menu/medium/'+myimg+'" />'+"\n";
			myhtml += "<h3>"+$('ul#menu li#men_'+myid+' .tit').html()+"</h3>\n";
			myhtml += "<div>"+mydesc+"</div>\n";
			mydesccont.html(myhtml);
			mydesccont.show();
		}
	});


	$('ul#menu_desc').hover(function(){
		clearTimeout(menuDescTO);
	}, function(){
		menuDescTO = setTimeout("closeMenuDesc()", 3000);
	});
	
	
	// Add functions for Home slideshow
	if($('div#ewc_forside').length){
		forsideSlideshowSize = $("#forside_slideshow li").length;
		
		if(forsideSlideshowSize > 1){
			forsideSlideshowTO = setInterval("changeSlideShow(0, false)", (highlightTime*1000));

			$(document).mousemove(function(e){
				mousePos[0] = e.pageX;
				mousePos[1] = e.pageY;
			}); 

			$('div#forside_nav li img').click(function(){
				var target = $(this).parent().attr('id').split("_")[1];
				changeSlideShow(target, true);
			});
		}
	}

	// Scroll check for positioning Feedback button
	$(document).scroll(function(){
		if($(document).scrollTop()>425){
			$('div#ewc_feedback').css('top', $(document).scrollTop()+'px');
		} else{
			$('div#ewc_feedback').css('top', '425px');
		}
	});

	// Set same height to all columns
	$('ul.ewc_3col_parts').each(function(){
		var theul = $(this);
		var maxHeight = 0;
		theul.find('> li').each(function(){
			if($(this).height() > maxHeight)
				maxHeight = $(this).height();
		});
		theul.find('> li').css('minHeight', maxHeight+"px");
	});
	
	$('form#search_network div#moresearchopts a.show_search_opt').click(function(e){
		e.preventDefault();
		if($('form#search_network div.search_net_adv').css('display') == 'block'){
			$('form#search_network div.search_net_adv').slideUp();
			$('form#search_network div#moresearchopts a.show_search_opt img').attr('src', 'assets/img/icn_plus.png').attr('alt', 'More search options');
			$('form#search_network div#moresearchopts a.show_search_opt span').html('More search options');
		} else{
			$('form#search_network div.search_net_adv').slideDown();
			$('form#search_network div#moresearchopts a.show_search_opt img').attr('src', 'assets/img/icn_less.png').attr('alt', 'Less search options');
			$('form#search_network div#moresearchopts a.show_search_opt span').html('Less search options');
		}
	});
	
	/* Gallery */
	
	var numPics = 0;
	var currPic = 0;
	var currNumPic = 1;
	var currPlace = 5;

	if($('div#gallcontent').length){
		numPics = $('div#gallery ul.gall_imgs li').length;
		currPic = $('div#gallery ul.gall_imgs li:first').attr('id').split("_")[1];
		if(numPics > 5){
			$('div#gallery_items div#gall_next img').show();
		}
		$('div#gallery_items div.gall_thumbs ul').width((101*numPics));
		$('div#gallery div#gallery_items div.gall_thumbs ul li a').click(function(e){
			e.preventDefault();
			var targetID = $(this).parent().attr('id').split("_")[2];
			var myDesc = $(this).attr('alt');
			$('div#gall_overlabel').html(myDesc);
			$('div#gallery div#gallery_items div.gall_thumbs ul li img').removeClass('active');
			$('div#gallery div#gallery_items div.gall_thumbs ul li#gall_pic_'+targetID+' img').addClass('active');
			$('div#gallery ul.gall_imgs li#pic_'+currPic).fadeOut(function(){
				$('div#gallery ul.gall_imgs li#pic_'+targetID).fadeIn(function(){
					currPic = targetID;
					currNumPic = $('div#gallery ul.gall_imgs li#pic_'+targetID).attr('class').split("_")[1];
				});
			});
		});
		$('div#gallery_items div#gall_next img').click(function(e){
			e.preventDefault();
			var currMargin = parseInt($('div#gallery_items div.gall_thumbs ul').css('marginLeft').replace('px', ''));
			var newMargin = currMargin - 101;
			$('div#gallery_items div.gall_thumbs ul').animate({
				'marginLeft': newMargin
			}, 'fast');
			currPlace++;
			if(currPlace >= numPics){
				$(this).hide();
			}
			$('div#gallery_items div#gall_prev img').show();
		});
		$('div#gallery_items div#gall_prev img').click(function(e){
			e.preventDefault();
			var currMargin = parseInt($('div#gallery_items div.gall_thumbs ul').css('marginLeft').replace('px', ''));
			var newMargin = currMargin + 101;
			$('div#gallery_items div.gall_thumbs ul').animate({
				'marginLeft': newMargin
			}, 'fast');
			currPlace--;
			if(currPlace <= 5){
				$(this).hide();
			}
			$('div#gallery_items div#gall_next img').show();
		});
		
		$('div#gallery div#btns_nav').hover(function(){
			if(currNumPic > 1)
				$('div#gallery div#btns_nav div#gallnavprev').show();
			if(currNumPic < numPics)
				$('div#gallery div#btns_nav div#gallnavnext').show();
		}, function(){
			$('div#gallery div#btns_nav div').hide();
		});
		
		$('div#gallery div#btns_nav div').click(function(e){
			e.preventDefault();
			var targNumPic = 0;
			if($(this).attr('id') == "gallnavprev"){
				targNumPic = parseInt(currNumPic) - 1;
			} else{
				targNumPic = parseInt(currNumPic) + 1;
			}
			var targetID = $('div#gallery ul.gall_imgs li.pic_'+targNumPic).attr('id');
			$('li#gall_'+targetID+' a').trigger('click');
			if(targNumPic > 1){
				$('div#gallery div#btns_nav div#gallnavprev').show();
			} else{
				$('div#gallery div#btns_nav div#gallnavprev').hide();
			}
			if(targNumPic < numPics){
				$('div#gallery div#btns_nav div#gallnavnext').show();
			} else{
				$('div#gallery div#btns_nav div#gallnavnext').hide();
			}
			if($(this).attr('id') == "gallnavprev"){
				if(targNumPic < (currPlace-4)){
					$('div#gall_prev img').trigger('click');
				}
			} else{
				if(targNumPic > currPlace){
					$('div#gall_next img').trigger('click');
				}
			}
		});
	}
	
	$('input.date').datepicker({ dateFormat: 'yy-mm-dd' });
	
	$('div.ewc_news_search a.clear').click(function(e){
		e.preventDefault();
		$(this).parent().parent().find('input, textarea, select').each(function(){
			if(this.nodeName.toLowerCase() == 'input'){
				if($(this).attr('type') == 'checkbox'){
					$(this).attr('checked', false);
				} else{
					if(typeof $(this).attr('title') != "undefined"){
						$(this).val($(this).attr('title'));
					} else{
						$(this).val('');
					}
				}
			} else if(this.nodeName.toLowerCase() == 'textarea'){
				$(this).val('');
			} else if(this.nodeName.toLowerCase() == 'select'){
				$(this).val(0);
			}
		});
	});
	
	$('form#ewc_newsletter_frm input#nlsignup').focus(function(){
		if($(this).val() == 'Your e-mail address')
			$(this).val('');
	});
	$('form#ewc_newsletter_frm input#nlsignup').blur(function(){
		if($(this).val() == '')
			$(this).val('Your e-mail address');
	});
	$('form#ewc_newsletter_frm').submit(function(){
		if($('form#ewc_newsletter_frm input#nlsignup').val() == 'Your e-mail address')
			$('form#ewc_newsletter_frm input#nlsignup').val('');
	});
	
	$('form#searchFrm input#searchtxt').focus(function(){
		if($(this).val() == 'Search')
			$(this).val('');
	});
	$('form#searchFrm input#searchtxt').blur(function(){
		if($(this).val() == '')
			$(this).val('Search');
	});
	$('form#searchFrm').submit(function(){
		if($('form#searchFrm input#nlsignup').val() == 'Search')
			$('form#searchFrm input#nlsignup').val('');
	});
	

	$('form.checkFrm').submit(function(){
		return checkForm($(this).attr('id'));
	});
	
	
	$('.opt_profile').live('click', function(){
		$('#frm_img_profile1').hide();
		$('#frm_profile1').hide();
		$('#frm_img_profile2').hide();
		$('#frm_profile2').hide();
		$('#frm_img_profile3').hide();
		$('#frm_profile3').hide();
		$('#frm_img_profile4').hide();
		$('#frm_profile4').hide();
		$('#frm_img_profile5').hide();
		$('#frm_profile5').hide();
		$('#frm_img_profile' + $(this).val()).show();
		$('#frm_profile' + $(this).val()).show();
    });
	
	$('#employer_txt').live('keyup', function(){
		var mypos = $(this).offset();
		$('ul#'+$(this).attr('id').replace('_txt', '_list')).css('top', (mypos.top + 24)+"px").css('left', mypos.left+"px");
		
		$('#employer_list').html('').hide();
		if($(this).val().length>1) {
			$('#employer_list').load("/network/ajax-employers/?employer_txt=" + escape($(this).val()) );
			$('#employer_list').show();
		}		
	});
	$('ul#employer_list li.sel_employer').live('click', function(){ 
		$('#employer_txt').val($(this).attr("title"));
		$('#id_employer').val($(this).attr("id"));
		$('#employer_list').html('').hide();
	});
	$('#employer_txt_2').live('keyup', function(){
		var mypos = $(this).offset();
		$('ul#'+$(this).attr('id').replace('_txt', '_list')).css('top', (mypos.top + 24)+"px").css('left', mypos.left+"px");

		$('#employer_list_2').html('').hide();
		if($(this).val().length>1) {
			$('#employer_list_2').load("/network/ajax-employers/?employer_txt=" + escape($(this).val()) );
			$('#employer_list_2').show();
		}		
	});
	$('ul#employer_list_2 li.sel_employer').live('click', function(){ 
		$('#employer_txt_2').val($(this).attr("title"));
		$('#id_employer_2').val($(this).attr("id"));
		$('#employer_list_2').html('').hide();
	});
	$('#employer_txt_3').live('keyup', function(){
		var mypos = $(this).offset();
		$('ul#'+$(this).attr('id').replace('_txt', '_list')).css('top', (mypos.top + 24)+"px").css('left', mypos.left+"px");

		$('#employer_list_3').html('').hide();
		if($(this).val().length>1) {
			$('#employer_list_3').load("/network/ajax-employers/?employer_txt=" + escape($(this).val()) );
			$('#employer_list_3').show();
		}		
	});
	$('ul#employer_list_3 li.sel_employer').live('click', function(){ 
		$('#employer_txt_3').val($(this).attr("title"));
		$('#id_employer_3').val($(this).attr("id"));
		$('#employer_list_3').html('').hide();
	});
	$('#employer_txt_5').live('keyup', function(){
		var mypos = $(this).offset();
		$('ul#'+$(this).attr('id').replace('_txt', '_list')).css('top', (mypos.top + 24)+"px").css('left', mypos.left+"px");

		$('#employer_list_5').html('').hide();
		if($(this).val().length>1) {
			$('#employer_list_5').load("/network/ajax-employers/?employer_txt=" + escape($(this).val()) );
			$('#employer_list_5').show();
		}		
	});
	$('ul#employer_list_5 li.sel_employer').live('click', function(){ 
		$('#employer_txt_5').val($(this).attr("title"));
		$('#id_employer_5').val($(this).attr("id"));
		$('#employer_list_5').html('').hide();
	});
	
	$('#academic_txt_res').live('keyup', function(){
		var mypos = $(this).offset();
		$('ul#'+$(this).attr('id').replace('_txt', '_list')).css('top', (mypos.top + 24)+"px").css('left', mypos.left+"px");

		$('#academic_list_res').html('').hide();
		if($(this).val().length>1) {
			$('#academic_list_res').load("/network/ajax-academics-res/?academic_txt_res=" + escape($(this).val()) );
			$('#academic_list_res').show();
		}		
	});
	
	$('ul#academic_list_res li.sel_academic_res').live('click', function(){ 
		$('#academic_txt_res').val($(this).attr("title"));
		$('#id_academic_institution_res').val($(this).attr("id"));
		$('#academic_list_res').html('').hide();
	});
	
	$('#academic_txt').live('keyup', function(){
		var mypos = $(this).offset();
		$('ul#'+$(this).attr('id').replace('_txt', '_list')).css('top', (mypos.top + 24)+"px").css('left', mypos.left+"px");
		
		$('#academic_list').html('').hide();
		if($(this).val().length>1) {
			$('#academic_list').load("/network/ajax-academics/?academic_txt=" + escape($(this).val()) );
			$('#academic_list').show();
		}		
	});
	
	$('#academic_list .sel_academic').live('click', function(){ 
		$('#academic_txt').val($(this).attr("title"));
		$('#id_academic_institution').val($(this).attr("id"));
		$('#academic_list').html('').hide();
	});
	
	
	$('.prevpositions').click(function(){
		theid = $(this).attr('id');
		thisnewchecked = $(this).attr('checked');
		$('.prevpositions').attr('checked', false);
		$('#'+theid).attr('checked', thisnewchecked);
	});

	$('#educational_field_max').keyup(function(){
		chars_left = 200 - $(this).val().length;
		if(chars_left < 0) {
			chars_left = 0;
			$(this).val($(this).val().substring(0, 200));			
		}
		if(chars_left == 0) {
		    $('#char_left').css({color: 'red' });
		} else {
			$('#char_left').css({color: 'gray' });
		}
		$('#char_left').text(chars_left);
	});
	
	$('#summary_max').keyup(function(){
		chars_left = 400 - $(this).val().length;
		if(chars_left < 0) {
			chars_left = 0;
			$(this).val($(this).val().substring(0, 400));
		}
		if(chars_left == 0) {
		    $('#char_left_2').css({color: 'red' });
		} else {
			$('#char_left_2').css({color: 'gray' });
		}
		$('#char_left_2').text(chars_left);
	});
	
	$('#add_lang').live('click', function(e){
		e.preventDefault();
		if($('#working_language').val()==0) {
			alert('Please select a working language');
		} else {
			$('#working_lang_area').load("/network/ajax-worklang/?id_member=" + $(this).attr('alt') + "&var=1&id_language=" + $('#working_language').val() );
			$('#working_language').val('0');
		}
    });
	$('.rm_lang').live('click', function(e){
		e.preventDefault();
		$('#working_lang_area').load("/network/ajax-worklang/?id_member=" + $(this).attr('alt') + "&var=2&id_language=" + $(this).attr('rel') );		
	});
	
	$('form input#search_library').focus(function(){
		if($(this).val() == $(this).attr('title'))
			$(this).val('');
	});
	$('form input#search_library').blur(function(){
		if($(this).val() == '')
			$(this).val($(this).attr('title'));
	});
	
	$('button#searchResultsFiltersBtn').click(function(){
		$("input[name='do']").val(1);
		$('form.searchResultsFiltersFrm').trigger('submit');
	});


	$('form#joinShareMailing').submit(function(){ return false; });
	$('form#joinShareMailing input').change(function(){
		var joinmailing = 0;
		if($(this).is(':checked'))
			joinmailing = 1;
		$.get($(this).parent().parent().attr('action'), {
			'join': joinmailing,
			'usr' : $('input#mailingjoinUsr').val()
		}, function(data){
			if(data.result == 1){
				alert("You have been added to the Share & Connect mailing list");
			} else {
				alert("You have been removed from the Share & Connect mailing list");
			}
		}, 'json');
	});
});

function changeSlideShow(target, chg){
	if(chg || (mousePos[1] < 110 || mousePos[1] > 425)) {		
		if(target == 0){
			if(forsideSlideshow < forsideSlideshowSize)
				target = forsideSlideshow + 1;
			else
				target = 1;
		}
		$('ul#forside_slideshow li:nth-child('+forsideSlideshow+')').fadeOut(function(){
			$('ul#forside_slideshow li:nth-child('+target+')').fadeIn(function(){
				$('div#forside_nav li#sld_'+forsideSlideshow+' img').attr('src', $('div#forside_nav li#sld_'+forsideSlideshow+' img').attr('src').replace('_on', '_off'));
				forsideSlideshow = parseInt(target);
				$('div#forside_nav li#sld_'+forsideSlideshow+' img').attr('src', $('div#forside_nav li#sld_'+forsideSlideshow+' img').attr('src').replace('_off', '_on'));
			});
		});
	}
}

function closeMenuDesc(){
	$('ul#menu_desc').slideUp();
}

function checkForm(frmID){
		var frmError = false;				
		var thisid = frmID;
		$('form#'+thisid+' input, form#'+thisid+' textarea, form#'+thisid+' select').each(function(){
			if(($(this).attr('required') == 'true' || $(this).attr('req') == 'true') && !frmError){
				if(this.tagName == 'INPUT'){
					if($(this).attr('value') == ''){
						$(this).addClass('required');
						alert($(this).attr('title'));
						$(this).focus();
						frmError = true;
					}
					if($(this).attr('reqtype') == 'email' && frmError == false){
						if(!check_email($(this).attr('value'))){
							$(this).addClass('required');
							alert("Enter a valid Email address");
							$(this).focus();
							frmError = true;
						}
					}
				}
				if(this.tagName == 'SELECT'){
					if($(this).attr('selectedIndex') == 0){
						$(this).addClass('required');
						alert($(this).attr('title'));
						$(this).focus();
						frmError = true;
					}
				}
				if(this.tagName == 'TEXTAREA'){
					if($(this).attr('value') == ''){
						$(this).addClass('required');
						alert($(this).attr('title'));
						$(this).focus();
						frmError = true;
					}
				}
			}
		});		
		
		//validate a profile was selected
		if($(".opt_profile").val() && frmError == false) {
			
			frmError = true;
			$(".opt_profile").each(function(){
				if($(this).attr('checked')) {
					frmError = false;
				}
			});
			
			if(frmError) {
				alert($(".opt_profile").attr('title'));
				$(".opt_profile").focus();
			}
			
		}
		
		//validate a gender was selected
		/*if($(".opt_gender").val() && frmError == false) {
			
			frmError = true;
			$(".opt_gender").each(function(){
				if($(this).attr('checked')) {
					frmError = false;
				}
			});
			
			if(frmError) {
				alert($(".opt_gender").attr('title'));
				$(".opt_gender").focus();
			}
			
		}*/
		
		//validate a working language was selected
		if($("#has_wl") && frmError == false) {
			if($("#has_wl").val()==0) {
				alert('Select a working language');
				$("#working_language").focus();
				frmError = true;
			}
		}
		
		//validate area of expertise
		if($(".opt_ae").val() && frmError == false) {
			
			frmError = true;
			$(".opt_ae").each(function(){
				if($(this).attr('checked')) {
					frmError = false;
				}
			});
			
			if(frmError) {
				alert('Select at least one Area of expertise');
				$(".opt_ae").focus();
			}
			
		}
		
		//validate academic level
		if($(".opt_al").val() && frmError == false) {
			
			frmError = true;
			$(".opt_al").each(function(){
				if($(this).attr('checked')) {
					frmError = false;
				}
			});
			
			if(frmError) {
				alert('Select your academic level');
				$(".opt_al").focus();
			}
			
		}		
		
		
		//validate new pass
		if(($("#new_password").val() && frmError == false) || ($("#conf_password").val() && frmError == false)) {
			if($("#new_password").val()=='') {
				alert($("#new_password").attr('title'));
				$("#new_password").focus();
				frmError = true;
			}
			if($("#conf_password").val()=='') {
				alert($("#conf_password").attr('title'));
				$("#conf_password").focus();
				frmError = true;
			}
			if($("#new_password").val().length<4 && frmError == false) {
				alert("At least 4 characters for password");
				$("#new_password").val('');
				$("#conf_password").val('');
				frmError = true;				
			}			
			if($("#new_password").val()!=$("#conf_password").val() && frmError == false) {
				alert("The passwords didn't match");
				$("#new_password").val('');
				$("#conf_password").val('');
				frmError = true;
			}			
		}
		
		//special validations
		if($('.opt_profile:checked').val() ==2) {
			
			//validate tt
			if($(".opt_tt").val() && frmError == false) {
				
				frmError = true;
				$(".opt_tt").each(function(){
					if($(this).attr('checked')) {
						frmError = false;
					}
				});
				
				if(frmError) {
					alert('Select at least one Level taught');
					$(".opt_tt").focus();
				}
				
			}			
		}
		
		if($('.opt_profile:checked').val() ==3) {
			
			//validate tt
			if($(".opt_taught").val() && frmError == false) {
				
				frmError = true;
				$(".opt_taught").each(function(){
					if($(this).attr('checked')) {
						frmError = false;
					}
				});
				
				if(frmError) {
					alert('Select at least one Level taught');
					$(".opt_taught").focus();
				}
				
			}			
		}
		
		
		if($('.opt_profile:checked').val() ==4) {

			if($("#academic_txt_res").val()=='' && frmError == false) {
				alert('Academic Institution where your research is registered');
				$("#academic_txt_res").focus();
				frmError = true;
			}
			if($("#thesis_title").val()=='' && frmError == false) {
				alert('Title of thesis/dissertation');
				$("#thesis_title").focus();
				frmError = true;
			}
			
			//validate fields covered in thesis
			if($(".opt_aet").val() && frmError == false) {
				
				frmError = true;
				$(".opt_aet").each(function(){
					if($(this).attr('checked')) {
						frmError = false;
					}
				});
				
				if(frmError) {
					alert('Select at least one field covered in the thesis');
					$(".opt_aet").focus();
				}
				
			}			
		}
		
		
		if($('.opt_profile:checked').val() ==5) {

			if($("#chk_position").val()=='' && frmError == false) {
				alert('Position is required');
				$("#chk_position").focus();
				frmError = true;
			}
		}
		
		//validate accept terms
		if($(".accept").val() && frmError == false) {
			
			frmError = true;
			$(".accept").each(function(){
				if($(this).attr('checked')) {
					frmError = false;
				}
			});
			
			if(frmError) {
				alert('Please accept Terms of Service and Privacy Policy');
				$(".accept").focus();
			}
			
		}
		
		
		if(frmError)
			return false;
		else
			return true;
}

function rand( n ){
	return ( Math.floor ( Math.random ( ) * n + 1 ) );
}

function check_email(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
	    return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false
			}
		}
		return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		return false
	}
	if (len<2) {
		alert(errStr)
		return false
	}
	return true;
}
