// ok, so there's no way to have js perform the dns itself, meaning we are going 
// to have to ajax it in the background.
// that's ok, as long as the request happens after doc load, AND
// a cookie gets set as External_Domain (so we never request it again).
// that reduces the "size" of the affected file, and prevents this js from stalling
// rendering...

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function getIP()
{
	var ip = getCookie('External_IP'); //''; // '69.64.36.111';

	return ip;
}

// alert(getIP());

// this is consuming too much CPU resource -- looking up each hostname.
// we need to offload&asnyc the dns lookup to clients.
function getHostname()
{
	var host = ''; // '69.64.36.111';
	// if cookie is set, return that.
	// else, execute http request.
	// loadXMLDoc('www2.studentsreview.com');	
	return host;
}


// color stuff
function setColor(clr,el)
{
        el.style.backgroundColor=clr;
}




// SAVE GET COOKIE

// borrowed from: http://www.netspade.com/articles/javascript/cookies.xml
// put these in the js, so we don't have to keep redl all this code; etc.
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}





/*
in the background choose a particular type of test
        - colors
        - information-gathering (clicks on page length)
        - position
        * label the user with a cookie so they see a consistent test.

        - return an array with the type of ad, channel, colors, anything else changing.

*/
function splitABGoogle()
{
	// alert("hey");
        var testtype = getCookie('splitABTest');
        var testval =  getCookie('splitABTestVal');


        var gslot = "7237135280";
        var gchannel = "";
        var gtitle = "";
        var gurl = "";


        // choose a test
        if (! testtype)
        {
                var d = new Date();
                d.setTime(d.getTime() + 2592000);

                // choose a test at random
                testtype = Math.round(2*Math.random()) + 1;

                setCookie('splitABTest', testtype, d,'/','studentsreview.com',0);
        }



        // run selected test

        // pagelength click data gathering
        if (testtype == 1)
        {
                setCookie('splitABTestVal', 0, d,'/','studentsreview.com',0);

                // detect the page length and choose the channel based on it.
                var x = getDocHeight();

		// alert(x);

                if (x <= 1600)  // short page
                { gchannel = "5902777349"; }
                else            // long page
                { gchannel = "0041883666"; }

		

        }

        // TITLE colors
        else if (testtype == 2)
        {
        // 3 colors (2-4)

                if (! testval ) // choose a color for this user
                {
                        var testval=Math.random();
                        setCookie('splitABTestVal', testval, d,'/','studentsreview.com',0);
                }

                if (testval < .33) // blue
                {
                        gtitle = "0000ff";
                        gchannel = "1087088314";
                }
                else if (testval < .66) // green
                {
                        gtitle = "008800";
                        gchannel = "2865777528";
                }
                else // red
                {
                        gtitle = "880000";
                        gchannel = "4568866828";
                }

        }

        // URL/text colors
        else
        {
                // test values
        }

        var testArray = new Array(4);  // ad-slot, channel, title color, link color
        testArray[0] = gslot;
        testArray[1] = gchannel;
        testArray[2] = gtitle;
        testArray[3] = gurl;

	// alert(testArray);

        return testArray;
}






// CALL HTTP IN BACKGROUND
var xmlhttp

function loadXMLDoc(url)
{
/*
   try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
   } catch (e) {
    alert("Permission UniversalBrowserRead denied.");
   }
*/

// code for Mozilla, etc.
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest()
  xmlhttp.onreadystatechange=xmlhttpChange
  xmlhttp.open("GET",url,true)
  xmlhttp.send(null)
  }
// code for IE
else if (window.ActiveXObject)
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
    if (xmlhttp)
    {
    xmlhttp.onreadystatechange=xmlhttpChange
    xmlhttp.open("GET",url,true)
    xmlhttp.send()
    }
  }
}

function xmlhttpChange()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4)
  {
  // if "OK"
  if (xmlhttp.status==200)
    {
    // ...some code here...
    }
  else
    {
    alert("Problem retrieving XML data")
    }
  }
}


// WRITE TO A DIV
function writit(text,id)
{
        if (document.getElementById)
        {
               x = document.getElementById(id);
               x.innerHTML = '';
               x.innerHTML = text;
        }
        else if (document.all)
        {
               x = document.all[id];
               x.innerHTML = text;
        }
        else if (document.layers)
        {
               x = document.layers[id];
               // text2 = '<P CLASS="testclass">' + text + '</P>';
               // alert(text2)
               x.document.open();
               x.document.write(text); //(text2);
               x.document.close();

        }

}



// temporary hack until new version is loaded in.
function resize(id,newheight, newwidth)
{
        x = getobj(id)
        x.style.height = newheight;
        x.style.width = newwidth;
//  border: solid; border-width:1px; border-color:#e4e4e4;
}
// we are ending up with lots of duplicate functions...
function getobj(id)
{
        if (document.getElementById)
        {
               x = document.getElementById(id);
        }
        else if (document.all)
        {
               x = document.all[id];
        }
        else if (document.layers)
        {
               x = document.layers[id];
        }
        return x
}


// forwarding scripts..
// actually, we want to make it really easy for ppl to "forward" pages by including a js on their page.

// functionality for emailing comments
var forward_email = '';	// preserve the email address across the page...
// save as a cookie, so we can load it up on other pages.
// proxy as a background image request.
function forward_page(email)
{
	// get the url
	var page = document.URL;

	// check for a valid email address first.
	var cref = 'http://www.studentsreview.com/active/forward_page.php3?email=' + email + '&page=' + page;
	new Image().src = cref;

	// set a cookie, so user doesn't keep inputting their friend's email address...
	var d = new Date();
	d.setTime(d.getTime() + 2592000);
	setCookie('forward_email',email,d,'/','studentsreview.com',0);
}
function overlib_forward_page()
{
	var text = "Their Email Address:<br><input type=textbox name=forward_email value='" + forward_email +"'  onChange='forward_email = this.value;' ><input type=submit value=send style='font-size:10px' ";
	text += " onClick='forward_page(forward_email); nd(100);' >"; 
	return text;
}

// active response
function AddResponse(formname)
{

        form = document.forms[formname];

        var source = form.datasource.value;
        var sourceid = form.sourceid.value;

        var response = form.response.value;
        var uid = form.uid.value;

        var email = form.email.value;

        var response_href = form.response_href.value;

        for (i=0;i<form.rtype.length;i++) {
                if (form.rtype[i].checked) {
                        var rtype = form.rtype[i].value;
                }
        }

		// display response inline, if an area exists
		if (getobj('blank'+formname)) {
			writit(response,'blank'+formname);
		}

		// response 1 area - notify of successful submission.
		if (getobj('submit_notify1_'+formname))
		{
			writit("<font color=green size=4><b>OK!!  Submitted</b></font>",'submit_notify1_'+formname);
		}

        var q = "ds="+source+
                "&sid="+sourceid+
                "&type="+rtype+
                "&email="+email+
                "&uid="+uid+
                "&response=" + escape(response) +
			 "&response_href=" + escape(response_href);

        // alert(q);
        (new Image).src = 'http://www.studentsreview.com/active/add_response.php3?'+q;

        // add the comment.  change the form to show the comment was added.
	// add the comment into a blank div.



}




function contact_form(time_key)
	{
		var sessionid = getCookie("sessionid");
		if (sessionid)
		{
			var mw; // Message Window
			mw=window.open('http://www.studentsreview.com/test/contact.php3?TIME='+time_key,
				'Contact',
				'toolbar=no,status=no,personalbar=no,menubar=no,resizable=yes,width=600,height=300,scrollbars=yes');
	
			if (window.focus) {
				mw.focus();
			}
		}
		else
		{
		// this will automatically either signup or log in.
		// automatically sign in, but preserve comment..
			document.location = "http://www2.studentsreview.com/members/free/contact.php3?TIME="+time_key+"&dest=" + location.pathname;
		// /DC/George_Washington_University.html";
		}
	}	
function change_U_top(){
	var formObj = document.REVIEW
	var univObj = document.REVIEW.UNIVERSITY
	if ( formObj.STATE[formObj.STATE.options.selectedIndex].value)
 	{
		univObj.options.length=1;
		univObj.options[0] = new Option("Loading ... select again when done.",0) ;
		//	name_ = "http://www.studentsreview.com/test/phantomlayer.php3?STATE=" + formObj.STATE[formObj.STATE.options.selectedIndex].value ;
		name_ = "http://www.studentsreview.com/" + formObj.STATE[formObj.STATE.options.selectedIndex].value + ".html";
	//	location = name_;
		alert(name_);
		parent.TEMP.document.location = name_;
	}
	}


// user stuff ---------- 
/*
load up the saved cookie into a variable.  (this will always be 1 page 
load delayed unless the "save" also updates the cookies via js
*/
function check_saved(id)
{
// check if a comment is in the saved list
}
sessionid = getCookie("sessionid");
function topshowable(sessionid)
{
	// if (! sessionid) { return; }
	// write in tuition costs here (from php)
	// check how much money we have
	// load up info to show at the top (finances, saved comment count, saved school, etc.)
	return "hey";
}


// comment page stuff ------
function youcansavethis()
{
	// if cookie has been stored, return
	if (getCookie("savethishelp")) { return; }
	else
	{
		setCookie("savethishelp", 1);	// expiration & domain?
		overlib("<b>You can save this comment!!<///b><p>Click 'save' to the left.",STICKY);
	}
}
function rate_comment(sckey, rating, avg)
{
	// alert(sckey + " " + rating);
	var cref = 'http://www.studentsreview.com/active/rate_comment.php3?sckey=' + sckey + '&rating=' + rating;
	var rim = new Image()
	rim.src = cref;
	// alert(cref);
	
	// hideit('rate_'+sckey);
	// write a thank you note!
	writit("<center><font size='4' color='#c0c0c0'>Thank You! (Avg rating="+ avg +")</font></center>",'rate_'+sckey);
	// set a cookie, so that this rating doesn't show anymore.
	var d = new Date();
	d.setTime(d.getTime() + 2592000);
	setCookie('rated_'+sckey,1,d,'/','studentsreview.com',0);
}
// HIDE A DIV
function hideit(id)
{
	x = getbyname(id);
	x.style.visibility = 'hidden';
}
// get element by name
function getbyname(id)
{
        if (document.getElementById)
        {
               x = document.getElementById(id);
        }
        else if (document.all)
        {
               x = document.all[id];
        }
        else if (document.layers)
        {
               x = document.layers[id];
        }
	return x;
}
// functionality for emailing comments
var forward_email = '';	// preserve the email address across the page...
// save as a cookie, so we can load it up on other pages.
// proxy as a background image request.
function forward_comment(email, sckey)
{
	// check for a valid email address first.
	var cref = 'http://www.studentsreview.com/active/forward_comment.php3?sckey=' + sckey + '&email=' + email;
	var rim = new Image()
	rim.src = cref;
	// set a cookie, so user doesn't keep inputting their friend's email address...
	var d = new Date();
	d.setTime(d.getTime() + 2592000);
	setCookie('forward_email',email,d,'/','studentsreview.com',0);
}
function overlib_forward_text(sckey)
{
	var text = "Their Email Address:<br><input type=textbox name=forward_email value='" + forward_email +"'  onChange='forward_email = this.value;' ><input type=submit value=send style='font-size:10px' ";
	text += " onClick='forward_comment(forward_email," + sckey + "); nd(100);' >"; 
	return text;
}



