/***
 * Excerpted from "Advanced Rails Recipes",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/fr_arr for more book information.
***/

document.observe('dom:loaded', function() {	
when('notice', function(notice) {
    new Effect.Fade(notice, {delay: 3});
  });

});

// --- school registration step-2
function srStep2(){
	if($('academic_year').getValue()==''){
		alert('Academic Year cannot be blank.');
		return false;
	}else if($('start_date').getValue()==''){
	alert('From Date cannot be blank');
	return false;
	}else if($('end_date').getValue()==''){
		alert('End Date cannot be blank');
		return false;
	}else if($('no_students').getValue()==''){
		alert('No of students cannot be blank');
		return false;
	}else if(!isNumber($('no_students'))){
		return false;
	}
	else{
		return true;
	}
}

// 
function checkExamsSchedule(){
		if($('exam_id').selectedIndex==0){
			  alert('Please select exams');
			return false;
		}else if($('std_class_id').selectedIndex==0){
			alert('Please select standard');
			return false;
		}else if($('division_id').selectedIndex==0){
			alert('Please select division');
			return false;
		}else if($('subject_id').selectedIndex==0){
			alert('Please select subject');
			return false;
		}else{			
			return true;
		}			
}

// --- quiz, questions
function checkExamsSchedule_1(){
		if($('std_class_id').selectedIndex==0){
			alert('Please select standard');
			return false;
		}else if($('division_id').selectedIndex==0){
			alert('Please select division');
			return false;
		}else if($('subject_id').selectedIndex==0){
			alert('Please select subject');
			return false;
		}else{			
			return true;
		}			
}

// --- results 
function checkExamsSchedule_2(){
		if($('std_class_id').selectedIndex==0){
			alert('Please select standard');
			return false;
		}else if($('division_id').selectedIndex==0){
			alert('Please select division');
			return false;
		}else{			
			return true;
		}			
}

function changeName(linkid,divid){	
	
	Effect.toggle(divid,'blind');	
	if (linkid.innerHTML == 'Open') {	
		linkid.innerHTML = 'Close';
	}else if(linkid.innerHTML == 'Close'){
		linkid.innerHTML = 'Open';
	}
	
}

// --- school registration step-1
function checkfields(){
	var name='';
	var zip='';
	var re = /^[-]?\d*\.?\d*$/;
	name=$('user_school_name').getValue();
	zip=$('user_zip').getValue();
   if(name == ""){
      alert('School Name Cannot be blank');
	  return false;
   }else if(zip == ""){
      alert('Zip Cannot be blank');
	  return false;
   }else if(zip.length > 6 || zip.length < 6){
      alert('Zip should be 6 digits.');
			return false;
	 }else if(!isNumber($('user_zip'))){
		 return false;
   }else{
      return true;
   }
}

function faq(elem){
	if(elem=="general"){
		Effect.Fade($('admin'));
    Effect.Fade($('teacher'));
    Effect.Fade($('student'));
		Effect.Appear($('general'));
	}else if(elem=="admin"){		
    Effect.Fade($('teacher'));
    Effect.Fade($('student'));
		Effect.Fade($('general'));
		Effect.Appear($('admin'));
	}else if(elem=="teacher"){
		Effect.Fade($('admin'));    
    Effect.Fade($('student'));
		Effect.Fade($('general'));
		Effect.Appear($('teacher'));
	}else if(elem=="student"){
		Effect.Fade($('admin'));
    Effect.Fade($('teacher'));    
		Effect.Fade($('general'));
		Effect.Appear($('student'));
	}

}


// --- to check weather elem2 is greater than elem1 and warn
function isGreater(elem1, elem2){	
	if(Number(elem2.value)>Number(elem1.value)){
		alert(elem2.name +" cannot be greater than "+ elem1.name);
    return false;
	}else{
	  return true;
	}	
}


// --- checks weather a field is zero
function checkZero(elem3){
	if(number(elem3.value)==0){
		alert(elem3.name + " cannot be zero.");
		return false;
	}else{
		return true;
	}
}


//validates that the entry is a positive or negative number
function isNumber(elem) {
  var str = elem.value;
    var re = /^[-]?\d*\.?\d*$/;
    str = str.toString();
    if (!str.match(re)) {
        alert("Enter only numbers into the "+ elem.name +" field.");
        //setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    }
    return true;
}

// validates that the field value string has one or more characters in it
function isNotEmpty(elem) {
  var str = elem.value;
    var re = /.+/;
    if(!str.match(re)) {
        alert("Please fill in the required field.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}

// validates that the entry is 16 characters long
function isLen16(elem) {
  var str = elem.value;
    var re = /\b.{16}\b/;
    if (!str.match(re)) {
        alert("Entry does not contain the required 16 characters.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
// validates that the entry is formatted as an e-mail address
function isEMailAddr(elem) {
  var str = elem.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) {
        alert("Verify the e-mail address format.");
        setTimeout("focusElement('" + elem.form.name + "', '" + elem.name + "')", 0);
        return false;
    } else {
        return true;
    }
}
// validate that the user made a selection other than default
function isChosen(select) {
    if (select.selectedIndex == 0) {
        alert("Please make a choice from the list.");
        return false;
    } else {
        return true;
    }
}

// validate that the user has checked one of the radio buttons
function isValidRadio(radio) {
    var valid = false;
    for (var i = 0; i < radio.length; i++) {
        if (radio[i].checked) {
            return true;
        }
    }
    alert("Make a choice from the radio buttons.");
    return false;
}

function focusElement(formName, elemName) {
    var elem = document.forms[formName].elements[elemName];
    elem.focus();
    elem.select();
}

function checkInteger(val){
	for(var i;i<val.length;i++){
		var c = val.charAt(i)
		if(Number(c)<0 || Number(c)>9){
			return false;
		}
		else{
		  return true;
		}
	}	
}


function initialize() { 
  if (typeof users != 'undefined') {
    var map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    map.addControl(new GLargeMapControl());
  
    // Clicking the marker will hide it
    function createMarker(latlng, user) {
      var marker = new GMarker(latlng);
      var html="<table style='font-size:11px'><tr><td style='width:50%' valign='top'><strong>"+user.name+"</strong><br /><img src='"+user.photo+"' /></td><td style='width:50%;' valign='top'>"+user.address1+"<br />"+user.address2+"<br />"+user.city+"</td></tr></table>";
      GEvent.addListener(marker,"click", function() {
        map.openInfoWindowHtml(latlng, html);
      });
      return marker;
    }

    var bounds = new GLatLngBounds;
    for (var i = 0; i < users.length; i++) {
	  if (users[i].lng > 0) {
	  	var latlng = new GLatLng(users[i].lat, users[i].lng)
	  	bounds.extend(latlng);
	  	map.addOverlay(createMarker(latlng, users[i]));
	  }
    }
    map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));
  }
}  
window.onload=initialize;

function initialize() { 
  if (typeof users != 'undefined') {
    var map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 13);
    map.addControl(new GLargeMapControl());
  
    // Clicking the marker will hide it
    function createMarker(latlng, user) {
      var marker = new GMarker(latlng);
      var html="<table style='font-size:11px'><tr><td style='width:50%' valign='top'><strong>"+user.name+"</strong><br /><img src='"+user.photo+"' /></td><td style='width:50%;' valign='top'>"+user.address1+"<br />"+user.address2+"<br />"+user.city+"</td></tr></table>";
      GEvent.addListener(marker,"click", function() {
        map.openInfoWindowHtml(latlng, html);
      });
      return marker;
    }

    var bounds = new GLatLngBounds;
    for (var i = 0; i < users.length; i++) {
	  if (users[i].lng > 0) {
	  	var latlng = new GLatLng(users[i].lat, users[i].lng)
	  	bounds.extend(latlng);
	  	map.addOverlay(createMarker(latlng, users[i]));
	  }
    }
    map.setCenter(bounds.getCenter(),map.getBoundsZoomLevel(bounds));
  }
}  
window.onload=initialize;

/* ----------------------------------------------------------- */
// -------------------------
// Popup screen
// -------------------------

function showAreaSelector(link_id, div_id){	
//getting the link
	var link = $(link_id);
//getting the div
	var div = $(div_id);	
//determining it's location
	var link_x = getX(link);
	var link_y = getY(link);
//moving the div to the location of the link, slightly under and to the right
	
var div_x = link_x + link.width;
var div_y = link_y + link.height;
	
//setting the div's visibility and position
div.style.display = 'block';
//Effect.BlindDown(div, { duration: 2.0 })
}

function hideAreaSelector(div_id){
//get the div
div = $(div_id)	
//set the div visibility to 'hidden'
div.style.display = 'none';
//Effect.BlindUp(div, { duration: 2.0 })
}

function getY(element){
	var y = 0;
	for( var e = element; e; e = e.offsetParent)
		//iterate the offset Parents
	{
		y += e.offsetTop; 
		//add up the offsetTop values
	}
	return y;
}
function getX(element){
	var x = 0;
	for( var e = element; e; e = e.offsetParent)
//iterate the offset Parents
	{
		x += e.offsetLeft; 
//add up the offsetTop values
	}
	return x;
}
/* -------------------------main menu---------------------------------- */
// --- menus onmouseover and onmouseout
var active_menu = 0;
function hidesubmenu(){
	submenu=$('s'+active_menu);
	submenu.style.display="none";
}

function showsubmenu(id){	
	active_menu = id
	submenu=$('s'+id);
	for(i=1;i<=6;i++){
		if(i==id){
			submenu.style.display="block";
		}//else{
			//document.getElementById('s'+i).style.display="none";
		//}
	}
}
/* ---------------------------main menu-------------------------------- */
//--- This fun checks the entered marks while update
function marks_text(dv){	
	var div = $(dv);
	var elms = div.getElementsByTagName("*");	
	for(var i=0;i<elms.length;i++){		
		if(elms[i].type == 'text'){			
			if(Number(elms[i].value)>Number($('exam_date_max_marks').getValue())){				
				alert('Obtained marks cannot exceed max marks');
				return false;
			}			
		}
	}		
}


