// ----------------------------------------------
// Open new windows via rel="external"
// ----------------------------------------------

function externalLinks() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && 
       anchor.getAttribute("rel") == "external") 
     anchor.target = "_blank"; 
 } 
} 

// ----------------------------------------------
// High Roller drop-down
// ----------------------------------------------


function toggle( targetId ){
	  if (document.getElementById){
			target = document.getElementById( targetId );
					target.style.display = "";
		}
	}
function toggleOff( targetId ){
	  if (document.getElementById){
			target = document.getElementById( targetId );
					target.style.display = "none";
		}
	}

// ---------------------------------------------
// Form Validation stuff
// ---------------------------------------------

function checkReg(){
	if (valNotNull('reg-username'))
	{
                        errdiv = document.getElementById('reg-username-err');
                        errdiv.style.display = "none";
	}
	else
	{
		return false;
	}
	if (valNotNull('reg-password'))
	{
                        errdiv = document.getElementById('reg-password-err');
                        errdiv.style.display = "none";
	}
	else
	{
		return false;
	}
	if (valNotNull('reg-email'))
	{
                        errdiv = document.getElementById('reg-email-err');
                        errdiv.style.display = "none";
	}
	else
	{
		return false;
	}
}

function valNotNull(inputId){
	if (document.getElementById){
		nput = document.getElementById(inputId);
		if (nput.value != '')
		{
			return true;
		}
		else
		{
			errdiv = document.getElementById(inputId + '-err');
			errdiv.style.display = "block";
			return false;
		}
	}
}

function validateRoll(whichone) {
	var maxUrls = 25;
	var numUrls = 0;
	var textareaId = whichone + '-sources';
	var textareaElement = '';
	if (document.getElementById(whichone + '-name').value == '')
	{
		errdiv = document.getElementById(whichone + '-name-err');
		errdiv.style.display = "block";
		return false;
	}
	else
	{
		document.getElementById(whichone + '-name-err').style.display = "none";
	}
        if (document.getElementById(whichone + '-sources').value == '')
        {
                errdiv = document.getElementById(whichone + '-sources-err');
                errdiv.style.display = "block";
                return false;
        }
	else
	{
		document.getElementById(whichone + '-sources-err').style.display = "none";
	}
        if (document.getElementById){
                textareaElement = document.getElementById(whichone + '-sources');
		for (var i = 0; i < textareaElement.value.length; i++){
			var strChar = textareaElement.value.substring(i, i + 1);
			if (strChar == '\n')
			{
				numUrls += 1;
				if (numUrls > maxUrls)
				{
					errElem = document.getElementById(textareaId + '-err');
					errElem.style.display = "block";
					return false;
				}
			}
		}
		if ((textareaElement.value.search(/(^|[^\.])google.com/i) > -1)||(textareaElement.value.search(/www.google.com/i) > -1)||(textareaElement.value.search(/www.yahoo.com/i) > -1)||(textareaElement.value.search(/(^|[^\.])yahoo.com/i) > -1)||(textareaElement.value.search(/www.ask.com/i) > -1)||(textareaElement.value.search(/(^|[^\.])ask.com/i) > -1)){
			if (confirm('We noticed you have a search engine as one of your sites. You should only put search engines in your Searchroll if you want to search the information on the search site\'s corporate pages. The result pages on search engines like Google, Yahoo, and Ask.com are created dynamically and thus aren\'t searched by Rollyo.\r\nClick OK to continue with Google in your Searchroll, or Cancel to edit your sources list.\r\n'))
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	return true;
	}
}

// ----------------------------------------------
// Searchroll link pop-up boxes
// ----------------------------------------------

function swapme(sid) {
	document.getElementById('rollpop' + sid).className = "show";
 }
 function closeMeOut(sid) {
 	document.getElementById('rollpop' + sid).className = "hide";
 }

// ----------------------------------------------
// Default search box text (thank you, Dunstan)
// ----------------------------------------------

// Is this working? Seems overkill... adding a one-off function to replace the default search for text...

function clearThis(f) {
  if (f.value=="Search for...") f.value = "";
}

// event handler
function addEventToObject(obj,evt,func) {
	var oldhandler = obj[evt];
	obj[evt] = (typeof obj[evt] != 'function') ? func : function(){oldhandler();func();};
}

// search box stuff
var Searchbox = {
	init : function()
		{
		var sBox = document.getElementById('search-box');
		if (sBox)
			{
			addEventToObject(sBox,'onclick',Searchbox.click);
			addEventToObject(sBox,'onblur',Searchbox.blur);
			}	
		},
	click : function()
		{
		var sBox = document.getElementById('search-box');
		if (sBox.value == 'Search for...')
			{
			sBox.value = '';
			}
	  	},
	blur : function()
		{
		var sBox = document.getElementById('search-box');
		if (sBox.value == '' || sBox.value == ' ') {sBox.value = 'Search for...';}
		}
	};

// ----------------------------------------------
// Popup Image Upload
// ----------------------------------------------

function popUpload() {
window.open('/imageupload.html', 'uploader', 'location=0, toolbar=0, directories=0, status=0, menubar=0, width=300, height=250')
return false;
}

// ----------------------------------------------
// Add events onload
// ----------------------------------------------
addEventToObject(window,'onload',Searchbox.init);
addEventToObject(window,'onload',externalLinks);

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}


function Is ()
{   // convert all characters to lowercase to simplify testing
	var agt=navigator.userAgent.toLowerCase()
// --- BROWSER VERSION ---
	this.major = stringToNumber(navigator.appVersion)
	this.minor = parseFloat(navigator.appVersion)

	this.nav  = ((agt.indexOf('mozilla')!=-1) && ((agt.indexOf('spoofer')==-1)
		&& (agt.indexOf('compatible') == -1)))
	this.nav2 = (this.nav && (this.major == 2))
	this.nav3 = (this.nav && (this.major == 3))
	this.nav4 = (this.nav && (this.major == 4))
	
	//Netscape 6
	this.nav5 =	(this.nav && (this.major == 5))
	this.nav6 = (this.nav && (this.major == 5))
	this.gecko = (this.nav && (this.major >= 5))

	this.ie   = (agt.indexOf("msie") != -1)
	this.ie3  = (this.ie && (this.major == 2))
	this.ie4  = (this.ie && (this.major == 3))
	this.ie5  = (this.ie && (this.major == 4))

	this.opera = (agt.indexOf("opera") != -1)

	this.nav4up = this.nav && (this.major >= 4)
	this.ie4up  = this.ie  && (this.major >= 4)
}

function stringToNumber(s)
{
        return parseInt(('0' + s), 10)
}

var is = new Is();



var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/

if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}


function placePop(e)
{
        var x = 0;
        var y = 0;
        if (!e) var e = window.event;
        if (e.pageX || e.pageY)
        {
                x = e.pageX;
                y = e.pageY;
        }
        else if (e.clientX || e.clientY)
        {
                x = e.clientX + document.body.scrollLeft;
                y = e.clientY + document.body.scrollTop;
        }
        pop = document.getElementById('popdiv');
 alert(x + "x" + y);
		if (is.nav4) pop.moveTo(x, y);
		else if (is.ie4up) {
			pop.style.pixelLeft = x;
			pop.style.pixelTop  = y;
		}
		else if (is.gecko) {
			pop.style.left = x;
			pop.style.top  = y;
		}
}

function getMyHTML(serverPage, objID, e) {
var wrap = document.body;
wrap.onclick = closeMe;

if (wrap.captureEvents) wrap.catureEvents(Event.CLICK);
var obj = document.getElementById(objID);

if (!e) var e = window.event;

        var x = 0;
        var y = 0;

        if (e.pageX || e.pageY)
        {
                x = e.pageX;
                y = e.pageY;
        }
        else if (e.clientX || e.clientY)
        {
                x = e.clientX + document.body.scrollLeft;
                y = e.clientY + document.body.scrollTop;
        }
// alert("left before place: " + obj.style.left + "x = " + x);
		var popHeight = 250;
		var popWidth = 300;

  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

			if (myHeight < (y + popHeight))
			{
				y = (y - popHeight);
			}
			if (myWidth < (x + popWidth))
			{
				x = (x - popWidth);
			}

			obj.style.left = x + "px";
			obj.style.top  = y + "px";
// alert("left after place: " + obj.style.left + "x = " + x);


xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

// obj.id = obj.id.replace("rollpop", "opened");
obj.innerHTML = xmlhttp.responseText;

obj.style.display = "block";


}
}
xmlhttp.send(null);
}

function closeMe(e) {
var obje = document.getElementById('popdiv');
obje.style.display = "none";
}     

function stopProp(e) {
	if (!e) var e = window.event;
	if (!e.cancelBubble)
	{
		e.cancelBubble = true;
	}
	if (e.stopPropagation)
	{
		e.stopPropagation();
	}
	else
	{
		return false;
	}
}
// ----------------------------------------------
// Tools
// ----------------------------------------------




function showMoreEngines() {
	var radiodiv = document.getElementById('more-radios');
	var smore = document.getElementById('show-more');
	var sless = document.getElementById('show-less');
	if (radiodiv.style.display == 'none' || radiodiv.style.display == '')
	{
		radiodiv.style.display = 'block';
		smore.style.display = 'none';
		sless.style.display = 'block';
	}
	else
	{
		radiodiv.style.display = 'none';
		sless.style.display = 'none';
		smore.style.display = 'block';
	}
	
}

function updateDisplay(uid, box) {
        
        var kind = box.name.charAt(0);
        var sid = box.name.substring(1);
        var display = 'Y';
 
        var resp = '';
        if (box.checked == false)
        {
                display = 'N';
        }
 
        var updateFile = "updateOwnrolls.php";
        if (kind == "s")
        {
                updateFile = "updateFavorites.php";
        }
        else if (kind == "d")
        {
                updateFile = "updateDefaults.php";
        }
 
        updateFile = updateFile + "?uid=" + uid + "&sid=" + sid +"&display=" + display;
        xmlhttp.open("GET", updateFile);
        xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        resp = xmlhttp.responseText;
                }
        }
        xmlhttp.send(null);
        return resp;
}



