NS4 = (document.layers);
function toggleBox(szDivID, iState) // 1 visible, 0 hidden
{
    if(document.layers)	   //NN4+
    {
       document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = iState ? "visible" : "hidden";
    }
    else if(document.all)	// IE 4
    {
        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
}

function SYS_trackObject(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=SYS_trackObject(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;}

function doSubmit(form) 
{
	form.submit()
}

function FormCheck(formname,para2)
{
	if (formname == "SaveCollege")
	{
		var collegenamepattern = /\//;
		var phonepattern = /(^none$)|(^\(\d{3}\)\s[A-Z0-9]{3}-[A-Z0-9]{4}$)|(^\(\d{3}\)\s[A-Z0-9]{3}-[A-Z0-9]{4}\s\/\s\(\d{3}\)\s[A-Z0-9]{3}-[A-Z0-9]{4}$)/;
		var faxpattern = /(^none$)|(^\(\d{3}\)\s[A-Z0-9]{3}-[A-Z0-9]{4}$)/;
		country = para2;
		if (country=='United Kingdom') 
		{
			var phonepattern = /(^none$)|(^\+44\s\(0\)\d{2}\s\d{4}\s\d{4}$)|(^\+44\s\(0\)\d{2}\s\d{4}\s\d{4}\s\/\s\+44\s\(0\)\d{2}\s\d{4}\s\d{4}$)/;
			var faxpattern = /(^none$)|(^\+44\s\(0\)\d{2}\s\d{4}\s\d{4}$)/;
		}
		retval = true;
		error = "";
		if (document.thisform.collegename.value=="" || document.thisform.collegename.value==null) 
		{
			error = error + "- please enter College Name!\n";
	    }
	    else
	    {
	    	if (collegenamepattern.test(document.thisform.collegename.value.trim())) error = error + "- Invalid College Name!\n";
	    }
		if (document.thisform.address1.value=="" || document.thisform.address1.value==null) 
		{
			error = error + "- please enter Address!\n";
	    }
		if (document.thisform.city.value=="" || document.thisform.city.value==null) 
		{
			error = error + "- please enter City!\n";
	    }
		if ((country=='USA' || country=='Canada') && document.thisform.statename.value=="" || document.thisform.statename.value==null) 
		{
			error = error + "- please select State!\n";
	    }
		if (document.thisform.zip.value=="" || document.thisform.zip.value==null) 
		{
			error = error + "- please enter Zip!\n";
	    }
	    else
	    {
			if (country=='USA' && validateZIP(document.thisform.zip.value)==false)
			{
				alert ('Invalid Zip Code'); 
				return false;
			}	
			if (country=='Canada' && validateZIPCanada(document.thisform.zip.value)==false)
			{
				alert ('Invalid Zip Code'); 
				return false;
			}	
			if (country=='United Kingdom' && validateZIPUK(document.thisform.zip.value)==false)
			{
				alert ('Invalid Zip Code'); 
				return false;
			}	
	    }
		if (document.thisform.phone.value=="" || document.thisform.phone.value==null) 
		{
			error = error + "- please enter Phone Number!\n";
	    }
	    else
	    {
	    	if (!(phonepattern.test(document.thisform.phone.value.trim()))) error = error + "- Invalid Phone Number!\n";
	    }
	    
		if (document.thisform.fax.value=="" || document.thisform.fax.value==null) 
		{
			error = error + "- please enter Fax Number!\n";
	    }
	    else
	    {
			if (!(faxpattern.test(document.thisform.fax.value.trim()))) error = error + "- Invalid Fax Number!\n";
	    }
		if (document.thisform.email.value=="" || document.thisform.email.value==null) 
		{
			error = error + "- please enter Email!\n";
	    }
	    else
	    {
			if (document.thisform.email.value!="none" && validateEMAIL(document.thisform.email.value)==false)	    
			{
				error = error + "- Invalid Email Address!\n";
			}
	    }
		if (document.thisform.collegeurl.value=="" || document.thisform.collegeurl.value==null) 
		{
			error = error + "- please enter URL!\n";
	    }
	    else
	    {
			if (document.thisform.collegeurl.value!="none" && validateURL(document.thisform.collegeurl.value)==false)	    
			{
				error = error + "- Invalid URL!\n";
			}
	    }
		if (error != "") 
		{
			retval = false;
			alert(error);
		}
		return retval;
	}
	if (formname == "Login")
	{
		if (document.thisform.email.value=="" || document.thisform.email.value==null) 
		{
	    	alert("please enter email!");
	        document.thisform.email.focus();
	        return false;
	    }
		if (document.thisform.password.value=="" || document.thisform.password.value==null) 
		{
	    	alert("please enter password!");
	        document.thisform.password.focus();
	        return false;
	    }
	}
	if (formname == "SearchforColleges")
	{
		if (document.thisform.college_country.value=="" || document.thisform.college_country.value==null) 
		{
	    	alert("Please Select Country");
	        document.thisform.college_country.focus();
	        return false;
	    }
		zipPress('Colleges');
		if (document.thisform.college_zip.value != "") 
		{
			if (validateZIP(document.thisform.college_zip.value)==false)
			{
				alert ('Invalid Zip Code'); 
				return false;
			}	
		}
	}
	if (formname == "SearchforSchools")
	{
		zipPress('Schools');
		if (document.thisform.school_zip.value != "") 
		{
			if (validateZIP(document.thisform.school_zip.value)==false)
			{
				alert ('Invalid Zip Code'); 
				return false;
			}	
		}
	}
	if (formname == "PhotoUpload")
	{
		retval = true;
		error = "";
		if (document.thisform.fupload.value=="" || document.thisform.fupload.value==null) 
		{
			error = error + "- Photo File Name must be Entered!\n";
	    }
		if (document.thisform.email.value=="" || document.thisform.email.value==null) 
		{
			error = error + "- Email Address must be Entered!\n";
	    }
	    else
	    {
			if (validateEMAIL(document.thisform.email.value)==false)	    
			{
				error = error + "- Invalid Email Address!\n";
			}
			else
			{
				if (document.thisform.email.value != document.thisform.confirmemail.value)			
				{
					error = error + "- Email Address and Confirm Email Address must Match!\n";
				}
			}
	    }
		if (error != "") 
		{
			retval = false;
			alert(error);
		}
		return retval;
	}
}

function validateEMAIL(string)
{
	var addressPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	return addressPattern.test(string);
}

function validateURL(string)
{
	if (string.substring(0, 7) != "http://") return false;
}

function validateZIP(zipcode)
{
	var valid = "0123456789";
	if (zipcode.length!=5) return false;
	for (var i=0; i < zipcode.length; i++) {
		temp = "" + zipcode.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") return false;}
	return true;
}

function validateZIPCanada(string)
{
	if (string.length!=7) return false;
	var addressPattern = /[A-Za-z]\d[A-Za-z]\s\d[A-Za-z]\d/;
	return addressPattern.test(string);
}

function validateZIPUK(string)
{
	//N1 6AH
	//LA1 4YW
	//W1U 4RY
	//EC3N 2EY
	//AB10 1JQ
	if (string.length!=6 && string.length!=7 && string.length!=8) return false;
	var addressPattern = /([A-Za-z]\d\s\d[A-Za-z][A-Za-z])|([A-Za-z](\d|[A-Za-z])(\d|[A-Za-z])\s\d[A-Za-z][A-Za-z])|([A-Za-z][A-Za-z](\d|[A-Za-z])(\d|[A-Za-z])\s\d[A-Za-z][A-Za-z])/;
	return addressPattern.test(string);
}

function resetStateCity()
{
	if (document.thisform.college_state)
	{
		document.thisform.college_state.value = "All States";
	}
	if (document.thisform.college_city)
	{
		document.thisform.college_city.value = "All Cities";
	}	
	thisform.submit();
}	

function divopenclose(obj,div,pluscaption,minuscaption) 
{
    if (document.getElementById(obj) && document.getElementById(div)) 
    {	
    	html = document.getElementById(obj).innerHTML;
    	if (html.match("minus"))
    	{
    		innerHTML = '<img src="http://www.directoryofeducation.net/images/plus.gif" width="12" height="10" border="0" hspace="3">'+pluscaption;
    		document.getElementById(div).style.display = "none";
    		set_cookie ( "mycollegeedit", "open", 2015, 12, 31, "/","", "" );
    	}	
    	else
    	{
    		innerHTML = '<img src="http://www.directoryofeducation.net/images/minus.gif" width="12" height="10" border="0" hspace="3">'+minuscaption;
    		document.getElementById(div).style.display = "block";
    		set_cookie ( "mycollegeedit", "close", 2015, 12, 31, "/","", "" );
    	}
		document.getElementById(obj).innerHTML = innerHTML;    
	}
}	

function geo(format,module)
{
	if (module == "Colleges")
	{
		document.thisform.college_zip.value = "";
		if (format == "state")
		{
			thisform.submit();
		}	
	}	
	if (module == "Schools")
	{
		document.thisform.school_zip.value = "";
		if (format == "state")
		{
			thisform.submit();
		}	
	}	
}	

function zipPress(module)
{
	if (module == "Colleges")
	{
		if (document.thisform.college_zip.value != "") 
    	{
			document.thisform.college_state.value = "All States";
			document.thisform.college_city.value = "All Cities";
		}
	}	
	if (module == "Schools")
	{
		if (document.thisform.school_zip.value != "") 
		{
			document.thisform.school_state.value = "All States";
			document.thisform.school_city.value = "All Cities";
		}
	}	
}	

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );
  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}

function delete_cookie ( cookie_name )
{
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

String.prototype.trim = function() 
{
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};

function java(option,div) 
{
	strOption = option;
	strURL = "http://www.directoryofeducation.net/java.php";
	xmlhttpPost(strURL,strOption,div)
} 

function xmlhttpPost(strURL,strOption,div) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText,div);
        }
    }
    self.xmlHttpReq.send(strOption);
}
function updatepage(str,div){
    $(div).innerHTML = str;
}
