// JavaScript Document

////////////////////////
/// Initialize onloads
////////////////////////

addOnload(initForms);


function addOnload(newFunction){
    var oldOnload = window.onload;

    if(typeof oldOnload == "function"){
        window.onload = function(){
            if(oldOnload){
                oldOnload();
            }
            newFunction();
        }
    }else{
        window.onload = newFunction;
    }

}


/************************************/

function initForms() {
    for (var i=0; i< document.forms.length; i++) {
		if(document.forms[i].id == "mainform"){	
        	document.forms[i].onsubmit = function() {		
            	return validForm();
		
        	}
		}
    }
}

function validForm() {
    var allGood = true;
    var allTags = document.getElementsByTagName("*");

    for (var i=0; i<allTags.length; i++) {
        if(!validTag(allTags[i])) {
            allGood = false;
        }
    }
    return allGood;
	

    function validTag(thisTag) {
        var outClass = "";
        var allClasses = thisTag.className.split(" ");
        
        for (var j=0; j< allClasses.length; j++) {
           outClass += validBasedOnClass(allClasses[j]) + " ";   
        }

        thisTag.className = outClass;

        if(outClass.indexOf("invalid") > -1) {
			invalidLabel(thisTag.parentNode);
            thisTag.focus();
            if(thisTag.nodeName == "INPUT") {
                thisTag.select();
            }
            return false;
        }
        return true;
        
        function validBasedOnClass(thisClass) {
            var classBack = "";

            switch(thisClass) {
                case "":
                case "invalid":
                     break;
                case "reqd":
                    if((allGood && thisTag.value == "")||(allGood && thisTag.value == " ")){
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
			
                case "radio":
                    if(allGood && !radioPicked(thisTag.name)){
                        classBack = "invalid ";
                    }    
                    classBack += thisClass;
                    break;
				
                case "email":
                    if(allGood && !validEmail(thisTag.value)){
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
					
			    case "phone":
                    if(allGood && !validPhone(thisTag.value)){
                        classBack = "invalid ";
                    }
                    classBack += thisClass;
                    break;
	
                default:
                   /* if(allGood && !crossCheck(thisTag,thisClass)){
						classBack = "invalid ";
					}*/
                    classBack += thisClass;
			}
            return classBack;
      }

		

           
               function validEmail(email){
                   var invalidChars = " /:,;";

                   if(email == ""){
                       return false;
                   }
                   for(var k=0; k<invalidChars.length; k++){
                       var badChar = invalidChars.charAt(k);
                       if(email.indexOf(badChar) > -1){
                           return false;
                       }
                   }
                   var atPos = email.indexOf("@",1);
                   if(atPos == -1){
                       return false;
                   }
                   if(email.indexOf("@", atPos+1) != -1){
                       return false;
                   }
                   var periodPos = email.indexOf(".",atPos);
                   if(periodPos == -1){
                       return false;
                   }
                   if(periodPos+3 > email.length){
                       return false;
                   }
                   return true;
               }
			   
			   function validEmail2(email){
                   var invalidChars = " /:,;";

                   if(email == ""){
                       return false;
                   }
                   for(var k=0; k<invalidChars.length; k++){
                       var badChar = invalidChars.charAt(k);
                       if(email.indexOf(badChar) > -1){
                           return false;
                       }
                   }
                   var atPos = email.indexOf("@",1);
                   if(atPos == -1){
                       return false;
                   }
                   if(email.indexOf("@", atPos+1) != -1){
                       return false;
                   }
                   var periodPos = email.indexOf(".",atPos);
                   if(periodPos == -1){
                       return false;
                   }
                   if(periodPos+3 > email.length){
                       return false;
                   }
                   return true;
               }
			 
			    function validPhone(phone){

                  
                   if(phone.length < 10){
                       return false;
                   }
				   
                   return true;
               }
            
               /*
               function crossCheck(inTag,otherFieldID){     
                   if(!document.getElementById(otherFieldID)){
                       return false;
                   }
                   return(inTag.value == document.getElementById(otherFieldID).value);
               }
			   */

               function radioPicked(radioName){
                   var radioSet = "";
            
                   for(var k=0; k<document.forms.length; k++){
                       if(!radioSet){
                           radioSet = document.forms[k][radioName];
                       }
                   }
                   if(!radioSet){
                       return false;
                   }      
               
       
               for(k=0; k<radioSet.length; k++){
                   if(radioSet[k].checked){
                       return true;
                   }
               }
               return false;
            }


        function invalidLabel(parentTag){
            if(parentTag.nodeName == "LABEL"){
                parentTag.className += " invalid";
            }
        }
	}
}


function isresult(){
	if(document.getElementById("isres").checked == true)
		document.getElementById("email").disabled = false;
		
	if(document.getElementById("isres").checked == false){
		document.getElementById("email").disabled = true;
		document.getElementById("email").value = "";
	}
}

// On campus map page


function display_continue(){

	var continue_1; 
		continue_1 = document.getElementById("continue_1");	
		
	if(document.getElementById("complex")){
		var complex; 
			complex = document.getElementById("complex");	
		if(complex.value != ""){
			continue_1.disabled = false;
		}else{
			continue_1.disabled = true;	
		}
	}
	if(document.getElementById("interface_type1")){
		var interface_type; 
			interface_type = document.getElementById("interface_type1");	
		if(interface_type.value > 0){
			continue_1.disabled = false;
		}else{
			continue_1.disabled = true;	
		}
	}
	if(document.getElementById("interface_type2")){
		var interface_type; 
			interface_type = document.getElementById("interface_type2");	
		if(interface_type.value > 0){
			continue_1.disabled = false;
		}else{
			continue_1.disabled = true;	
		}
	}
}

function select_complex(id){
	
	document.location="complex.php?complex=" + id;
	
}

function select_hall(id){
	
	document.location="hall.php?hall=" + id;
	
}

function select_room(id){
	
	document.location="confirm.php?room=" + id;
	
}

// trim whitespace function

function trim(str) { 
    str.replace(/^\s*/, '').replace(/\s*$/, ''); 

   return str;
} 


// confirmation scripts

function doConfirm (name,id)
{	
	result = confirm("If you already have a roommate, confirming \"" + name + "\" as your roommate will replace your existing roommate. Continue?");
	
	if (result)
	{
		
		field = document.getElementById('action' + id);
		field.value = 'confirm';
		
		form = document.getElementById(id);
		form.submit();		
	}
}

function doDenyRm (name,id)
{	
		
		field = document.getElementById('action' + id);
		field.value = 'deny_rm';
		
		form = document.getElementById(id);
		form.submit();		

}

// confirmation scripts

function doConfirmRes ()
{	
	result = confirm("Confirming this room will replace any other room selection you may have previously made.");
	
	if (result)
	{
		field = document.getElementById('action');
		field.value = 'confirm_res';
		
		form = document.getElementById('confirmform');
		form.submit();		
	}
}



// confirmation scripts

function doCancelRoom ()
{	
	result = confirm("Canceling your room reservation will remove you from the room and cancel your roommate selection. Your current roommate will remain in this room unless s/he cancels the room reservation as well.");
	
	if (result)
	{
		field = document.getElementById('action_cancel');
		field.value = 'cancel_res';
		
		form = document.getElementById('cancelform');
		form.submit();		
	}
}

// confirmation scripts

function doCancelRoommate ()
{	
	result = confirm("Are you sure you want to cancel your current roommate selection?");
	
	if (result)
	{
		field = document.getElementById('action_cancel_rm');
		field.value = 'cancel_rm';
		
		form = document.getElementById('cancelroommate');
		form.submit();		
	}
}




// application

function what_sport(){
	
	var sport;
	sport = document.getElementById('sport');
		
	if(document.getElementById('arbs').checked == true){
	
		if((sport.value == 'sport')||(sport.value == ''))
			sport.focus();
	}
	
	if(document.getElementById('arbs').checked == false){
		sport.value = 'sport';	
	}
	
}

// admin report

function disable_room_sort(appchecked){
	
	if(appchecked == 1){
		document.getElementById("room_sort").checked = false;
		document.getElementById("room_sort").disabled = true;
	}else{
		document.getElementById("room_sort").disabled = false;
	}
	
}

// popup window for confirm.php matching questions

function popwindow (doc){

opener = window.open(doc,"Window1",
"menubar=no,width=600,height=600,toolbar=yes,scrollbars=1");

opener.focus()
	
}



//////////////////////////////////
/// Fixed Tooltip
//////////////////////////////////

var tipwidth='250px' //default tooltip width
var tipbgcolor='#EFEFEF'  //tooltip bgcolor
var disappeardelay=0  //tooltip disappear speed onMouseout (in miliseconds)
var vertical_offset="0px" //horizontal offset of tooltip from anchor link
var horizontal_offset="-3px" //horizontal offset of tooltip from anchor link

/////No further editting needed

var ie4=document.all
var ns6=document.getElementById&&!document.all

if (ie4||ns6)
document.write('<div id="fixedtipdiv" style="visibility:hidden;width:'+tipwidth+';background-color:'+tipbgcolor+'" ></div>')

function getposOffset(what, offsettype){
var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
var parentEl=what.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}


function showhide(obj, e, visible, hidden, tipwidth){
if (ie4||ns6)
dropmenuobj.style.left=dropmenuobj.style.top=-500
if (tipwidth!=""){
dropmenuobj.widthobj=dropmenuobj.style
dropmenuobj.widthobj.width=tipwidth
}
if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover")
obj.visibility=visible
else if (e.type=="click")
obj.visibility=hidden
}

function iecompattest(){
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
if (whichedge=="rightedge"){
var windowedge=ie4 && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-15 : window.pageXOffset+window.innerWidth-15
dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure-obj.offsetWidth
}
else{
var windowedge=ie4 && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
edgeoffset=dropmenuobj.contentmeasure+obj.offsetHeight
}
return edgeoffset
}

function fixedtooltip(menucontents, obj, e, tipwidth){
if (window.event) event.cancelBubble=true
else if (e.stopPropagation) e.stopPropagation()
clearhidetip()
dropmenuobj=document.getElementById? document.getElementById("fixedtipdiv") : fixedtipdiv
dropmenuobj.innerHTML=menucontents

if (ie4||ns6){
showhide(dropmenuobj.style, e, "visible", "hidden", tipwidth)
dropmenuobj.x=getposOffset(obj, "left")
dropmenuobj.y=getposOffset(obj, "top")
dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+"px"
dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+obj.offsetHeight+"px"
}
}

function hidetip(e){
if (typeof dropmenuobj!="undefined"){
if (ie4||ns6)
dropmenuobj.style.visibility="hidden"
}
}

function delayhidetip(){
if (ie4||ns6)
delayhide=setTimeout("hidetip()",disappeardelay)
}

function clearhidetip(){
if (typeof delayhide!="undefined")
clearTimeout(delayhide)
}