﻿// JavaScript Document
$(document).ready(function(){

	$('#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').livequery('click', function(){
		if($('#working_language').val()==0) {
			alert('Please select a working language');
			return 0;
		}
		
		$('#working_lang_area').load("/network/ajax-worklang/?id_member=" + $(this).attr('alt') + "&var=1&id_language=" + $('#working_language').val() );
		$('#working_language').val('0');
    });
	
	$('.prevpositions').click(function(){
		theid = $(this).attr('id');
		thisnewchecked = $(this).attr('checked');
		$('.prevpositions').attr('checked', false);
		$('#'+theid).attr('checked', thisnewchecked);
	});
	$('.rm_lang').livequery('click', function(){
		$('#working_lang_area').load("/network/ajax-worklang/?id_member=" + $(this).attr('alt') + "&var=2&id_language=" + $(this).attr('rel') );		
	});
	
	$('.opt_profile').livequery('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();
    });
	
	$('.show_search_opt').livequery('click', function(){
		if($('#show_search').val()=='') {
			$('div#content div.listing div.search_box_network div.search_net_adv').css({display: 'block' });
			$('#show_search').val('1');
			$('#img_search_add').attr("src", "img/remove.png");
			$('#txt_search_more').text('Less search options');
		} else {
			$('div#content div.listing div.search_box_network div.search_net_adv').css({display: 'none' });
			$('#show_search').val('');
			$('#img_search_add').attr("src", "img/add.png");
			$('#txt_search_more').text('More search options');
		}		
	});												   

	$('#academic_txt_res').livequery('keyup', function(){
		$('#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();
		}		
	});
	
	$('.sel_academic_res').livequery('click', function(){ 
		$('#academic_txt_res').val($(this).attr("title"));
		$('#id_academic_institution_res').val($(this).attr("id"));
		$('#academic_list_res').html('').hide();
	});

	$('#academic_txt').livequery('keyup', function(){
		$('#academic_list').html('').hide();
		if($(this).val().length>1) {
			$('#academic_list').load("/network/ajax-academics/?academic_txt=" + escape($(this).val()) );
			$('#academic_list').show();
		}		
	});
	
	$('.sel_academic').livequery('click', function(){ 
		$('#academic_txt').val($(this).attr("title"));
		$('#id_academic_institution').val($(this).attr("id"));
		$('#academic_list').html('').hide();
	});
	
	$('#employer_txt').livequery('keyup', function(){
		$('#employer_list').html('').hide();
		if($(this).val().length>1) {
			$('#employer_list').load("/network/ajax-employers/?employer_txt=" + escape($(this).val()) );
			$('#employer_list').show();
		}		
	});
	
	$('.sel_employer').livequery('click', function(){ 
		$('#employer_txt').val($(this).attr("title"));
		$('#id_employer').val($(this).attr("id"));
		$('#employer_list').html('').hide();
	});
	
	$('#employer_txt_2').livequery('keyup', function(){
		$('#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();
		}		
	});
	
	$('.sel_employer').livequery('click', function(){ 
		$('#employer_txt_2').val($(this).attr("title"));
		$('#id_employer_2').val($(this).attr("id"));
		$('#employer_list_2').html('').hide();
	});	
	
	$('#employer_txt_3').livequery('keyup', function(){
		$('#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();
		}		
	});
	
	$('.sel_employer').livequery('click', function(){ 
		$('#employer_txt_3').val($(this).attr("title"));
		$('#id_employer_3').val($(this).attr("id"));
		$('#employer_list_3').html('').hide();
	});
	
	
	$('#employer_txt_5').livequery('keyup', function(){
		$('#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();
		}		
	});
	
	$('.sel_employer').livequery('click', function(){ 
		$('#employer_txt_5').val($(this).attr("title"));
		$('#id_employer_5').val($(this).attr("id"));
		$('#employer_list_5').html('').hide();
	});	

	$("#search_library").click(function() {
		if($(this).val()=='Search in library') {
			$(this).val('');
		}
		return false;
	});

	$('.sub').click(function(){	
		$curr = $(this).attr('alt');						 
							 
		$('.li_sub2').each(function(index){									
			
			if($('.li_sub2').eq(index).attr('alt')==$curr) {
						
				if($('.li_sub2').eq(index).attr('title')=='hide') {					
					$(this).attr('title','show');
					//add in open sub
					$text = $('#open_sub').val().split("|");
					$found = 0;
					for($i=0; $i<$text.length; $i++) {
						if($text[$i]==$curr) {
							$found = 1;
						}
					}
					if($found == 0) {
						$('#open_sub').val($('#open_sub').val() + '|' + $curr);
					}
					
				} else {
					$(this).attr('title','hide');
					//del in open sub
					$text = $('#open_sub').val().split("|");
					$text_new = '';
					$found = 0;
					for($i=0; $i<$text.length; $i++) {
						if($text[$i]==$curr) {
							$text[$i] = '';
						}
						if($i==0 || $i==$text.length-1) {
							$var_extra = '';
						} else {
							$var_extra = '|';
						}
						
						$text_new = $text_new + $var_extra + $text[$i];
					}
					$('#open_sub').val($text_new);
				}
			}
			
			if($('.li_sub2').eq(index).attr('title')=='hide') {
				$('.li_sub2').eq(index).removeClass("show");
				$('.li_sub2').eq(index).addClass("hide");
				$('.li_sub2').eq(index).find("input").attr('checked', false);				
				
			} else {
				$('.li_sub2').eq(index).removeClass("hide");
				$('.li_sub2').eq(index).addClass("show");
			}			
			
		});
		
	});							 
						   						   
	$('button.sitebtn').mouseover(function(){
		$(this).addClass('sitebtnover');
	});
	$('button.sitebtn').mouseout(function(){
		$(this).removeClass('sitebtnover');
	});
	$('button.submit').mouseover(function(){
		$(this).addClass('sitebtnover');
	});
	$('button.submit').mouseout(function(){
		$(this).removeClass('sitebtnover');
	});
	
	$('#homecont #home .box').mouseover(function(){
		$(this).addClass('mover');
	});
	$('#homecont #home .box').mouseout(function(){
		$(this).removeClass('mover');
	});

	$('#homecont #home .box').click(function(){
		var theurl = $(this).find("a").attr('href');
		self.location.href = theurl;
	});

// Gallery

	var galleryItems = new Array();
	var galleryActItem = 0;
	var galleryNumItems = 0;
	var galleryItemActive = 1;
	var galleryNav = new Array();
	galleryNav['from'] = 1;
	$('#gallery #gallery_items ul li').each(function(){
		galleryItems[galleryNumItems] = $(this).find('a').attr('href');
		galleryNumItems++;
	});

	$('#gallery #gallery_items ul li a').click(function(){
		$('#gallery div.gallimg img').attr('src', $(this).attr('href'));
		$('#gallery div.gallimg div#overlabel').html($(this).attr('alt'));
		thisid = $(this).attr('id');
		$('#gallery_items img').removeClass('active');
		$('#'+thisid+' img').addClass('active');
		galleryItemActive = thisid.replace('gallGoTo_', '');
		if(galleryItemActive == 1){
			$('#gallnavprev').hide();
		} else{
			$('#gallnavprev').show();
		}
		if(galleryItemActive < galleryNumItems){
			$('#gallnavright').show();
		} else{
			$('#gallnavright').hide();
		}
		return false;
	});
	$('div#gallery div#gall_prev').addClass('disabled');
	if(galleryNumItems < 6){
		$('div#gallery div#gall_next').addClass('disabled');
	} else{
		galleryActItem = 5;
		$('div#gallery div#gall_next').removeClass('disabled');
	}
	galleryNav['to'] = galleryActItem;
	$('div#gallery div#gall_next').click(function(){
		if(!$(this).hasClass('disabled')){
			thismarg = $('div#gallery div#gallery_items ul').css('marginLeft');
			thismarg = (thismarg.replace('px', '')*1);
			newmargin = thismarg - 101;
			$('div#gallery div#gallery_items ul').animate({
				marginLeft: newmargin+"px"
			}, 50);
			galleryActItem++;
			if(galleryActItem == galleryNumItems){
				$('div#gallery div#gall_next').addClass('disabled');
			} else{
				$('div#gallery div#gall_next').removeClass('disabled');
			}
			$('div#gallery div#gall_prev').removeClass('disabled');
			galleryNav['from']++;
			galleryNav['to']++;
		}
	});
	$('div#gallery div#gall_prev').click(function(){
		if(!$(this).hasClass('disabled')){
			thismarg = $('div#gallery div#gallery_items ul').css('marginLeft');
			thismarg = (thismarg.replace('px', '')*1);
			newmargin = thismarg + 101;
			$('div#gallery div#gallery_items ul').animate({
				marginLeft: newmargin+"px"
			}, 50);
			galleryActItem--;
			if(galleryActItem < 6){
				$('div#gallery div#gall_prev').addClass('disabled');
			} else{
				$('div#gallery div#gall_prev').removeClass('disabled');
			}
			$('div#gallery div#gall_next').removeClass('disabled');
			galleryNav['from']--;
			galleryNav['to']--;
		}
	});


// Reset Font Size
	var originalFontSize = $('html').css('font-size');
	$(".resetFont").click(function(){
		$('html').css('font-size', originalFontSize);
	});
	// Set Font Size
	$("#setFontSize").click(function(){
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		if($(this).find("img").attr("src")=="img/icn_font.png") {
			$(this).find("img").attr("src", "img/icn_fontless.png");
			var newFontSize = currentFontSizeNum*1.2;
		} else {
			$(this).find("img").attr("src", "img/icn_font.png");
			var newFontSize = originalFontSize;
		}
		
		$('html').css('font-size', newFontSize);
		return false;
	});
	
	
	$('a.print').click(function(){
		window.print();
		return false;
	});
	$('#searchResultsFiltersBtn').click(function(){
		$("input[name='do']").val(1);
		$('#searchResultsFiltersFrm').trigger('submit');
	});
	$('#searchTopBtn').click(function(){
		$('#searchFrm').trigger('submit');
	});
	
	$("#tabFeedback").mouseover(function(){
		$(this).css("margin-left", "0px");
	});
	$("#tabFeedback").mouseout(function(){
		$(this).css("margin-left", "-2px");
	});
	if($("#signupStatus").length) {
		setTimeout('$("#signupTrigger").trigger("click")', 1000);
	}
	
	
	$('#frmFilters .date').attr('readonly', true);
	$('#frmFilters .date').datepicker({ dateFormat: 'yy-mm-dd' });

	$('#frmFilters .clear').click(function(){
		$('#frmFilters input, #frmFilters textarea, #frmFilters select').each(function(){
				if(this.tagName == 'INPUT'){
					if($(this).attr('type') == "checkbox"){
						$(this).attr('checked', false);
					} else if($(this).attr('type') != "hidden") {
						$(this).attr('value', '');
					}
				}
				if(this.tagName == 'SELECT'){
					$(this).attr('selectedIndex', 0);
				}
				if(this.tagName == 'TEXTAREA'){
					$(this).attr('value', '');
				}
		});
		return false;
	});

/*
	$('#frmFilters').submit(function(){
		var data = $(this).attr('action');
		$(this).find('input, textarea, select').each(function(){
			if($(this).attr('type') == 'checkbox'){
				if($(this).attr('checked'))
					data += $(this).attr('name')+'/'+$(this).attr('value')+'/';
			} else{
				data += $(this).attr('name')+'/'+$(this).attr('value')+'/';
			}
		});
		self.location.href=data;
		return false;
	});
*/
	$('.checkFrm').bind('submit', function(){
		result = checkForm($(this).attr('id'));
		return result;
	});

	if($('#gallery').length >0){
		gallpos = $('#gallery').offset();
		$('#btns_nav').css('top', (gallpos.top+16));
		$('#btns_nav').css('left', (gallpos.left+16));
	}

	$('#gallnavright').mouseover(function(){
		$(this).addClass('over');
	});
	$('#gallnavright').mouseout(function(){
		$(this).removeClass('over');
	});
	$('#gallnavprev').mouseover(function(){
		$(this).addClass('over');
	});
	$('#gallnavprev').mouseout(function(){
		$(this).removeClass('over');
	});
	$('#gallnavright').click(function(){
		$('#gallGoTo_'+galleryItemActive+' img').removeClass('active');
		galleryItemActive++;
		$('#gallGoTo_'+galleryItemActive).trigger('click');
		if(galleryItemActive < galleryNav['from']){
			$('#gall_prev').trigger('click');
		}
		if(galleryItemActive > galleryNav['to']){
			$('#gall_next').trigger('click');
		}
	});
	$('#gallnavprev').click(function(){
		galleryItemActive--;
		$('#gallGoTo_'+galleryItemActive).trigger('click');
		if(galleryItemActive < galleryNav['from']){
			$('#gall_prev').trigger('click');
		}
		if(galleryItemActive > galleryNav['to']){
			$('#gall_next').trigger('click');
		}
	});
	$('#reloadcaptcha').click(function(){
	   	$('#captcha').attr('src', 'captcha/captcha.php?rand='+rand(100));
		return false;
	});
	
});

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' && !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;
}
