var validDoc;

function loadXMLDoc(url,firedEvent,username,password) {
	/* Arguments:
		url - File to receive, string.
		firedEvent - The event to be fired when data is collected, function.
	   Returns:
	   	boolean - Worked or not.
	*/
	req = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {
			req = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		req = false;
        	}
		}
    }
	if(req) {
		req.onreadystatechange = firedEvent;
		req.open("GET", url, true,username,password);
		req.send("");
	}
	return req;
}

function loadXMLValidDoc(url) {
	/* Arguments:
		url - File to receive, string.
		firedEvent - The event to be fired when data is collected, function.
	   Returns:
	   	boolean - Worked or not.
	*/
	validDoc = false;
    // branch for native XMLHttpRequest object
    if(window.XMLHttpRequest) {
    	try {
			validDoc = new XMLHttpRequest();
        } catch(e) {
			validDoc = false;
        }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       	try {
        	validDoc = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		validDoc = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {
          		validDoc = false;
        	}
		}
    }
	if(validDoc) {
		validDoc.onreadystatechange = show_validation;
		validDoc.open("GET", url, true,"","");
		validDoc.send("");
	}
	return validDoc;
}

function draw_search_button() {
	document.writeln("<a href=\"javascript:toggle_search();\">");
	document.writeln("<img src=\"/images/search_off.gif\" width=\"42\" height=\"16\" onmouseover=\"javascript:highlight_search();\" onmouseout=\"javascript:no_highlight_search();\" id=\"search_button\" style=\"border: none;\" />");
	document.writeln("</a>");
}

wins = 0;

function popUp(location,width,height,left,top) {
	if (width == null || width == 0) {
		width = screen.width / 2;
	}
	if (height == null || height == 0) {
		height = screen.height / 2;
	}
	if (left == null) {
		left = (screen.width / 2) - (width / 2);
	}
	if (top == null) {
		top = (screen.height / 2) - (height / 2);
	}
	wins += 1;
	var theWin = window.open(location,"window_" + (wins - 1),"width=" + width + ",height=" + height + ",left=" + left + ",top=" + top + ",location=no,directories=no,fullscreen=no,menubar=no,resizable=no,scrollbars=yes,status=no,titlebar=yes,toolbar=no");
	theWin.is_popUp = true;
	theWin.window_opener = window;
}

function toggle_search() {
	var sb = document.getElementById("search_field");
	if (sb.style.height == "0px") {
		showSearch();
		document.forms['searchForm'].elements['search'].focus();
	} else if (sb.style.height == "30px" || sb.style.height == "") {
		hideSearch();
	} else {
	//	alert(sb.style.height);
	}
}

function hideSearch() {
	hideSearchHelper(30,0);
	Set_Cookie("show_search","no",300,"/","","");
}

function hideSearchHelper(part,inc) {
	if (part >= 0) {
		var sb = document.getElementById("search_field");
		var isb = document.getElementById("inner_search_field");
		sb.style.height = part + "px";
		isb.style.top = (0 - (30 - part)) + "px";
		if (part > 15) {
			setTimeout("hideSearchHelper(" + (part - inc) + "," + (inc + 1) + ");",10);
		} else {
			setTimeout("hideSearchHelper(" + (part - inc) + "," + (inc - 1) + ");",10);
		}
	}
}

function showSearch() {
	showSearchHelper(0,0);
	Set_Cookie("show_search","yes",300,"/","","");
}

function showSearchHelper(part,inc) {
	if (part <= 30) {
		var sb = document.getElementById("search_field");
		var isb = document.getElementById("inner_search_field");
		sb.style.height = part + "px";
		isb.style.top = (0 - (30 - part)) + "px";
		if (part < 15) {
			setTimeout("showSearchHelper(" + (part + inc) + "," + (inc + 1) + ");",10);
		} else {
			setTimeout("showSearchHelper(" + (part + inc) + "," + (inc - 1) + ");",10);
		}
	}
}

function Set_Cookie(name, value, expires, path, domain, secure) {
	var today = new Date();
	today.setTime( today.getTime() );
	
	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime() + (expires));
	
	document.cookie = name + "=" + escape(value) +
	((expires) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	((path) ? ";path=" + path : "" ) + 
	((domain) ? ";domain=" + domain : "" ) +
	((secure) ? ";secure" : "" );
}

var pre = new Image();
pre.src = "/images/search_on.gif";

function highlight_search() {
	s = document.getElementById("search_button");
	s.src = "/images/search_on.gif";
}

function no_highlight_search() {
	s = document.getElementById("search_button");
	s.src = "/images/search_off.gif";
}


function start_validation(url) {
	var doc = loadXMLValidDoc("/valid_helper.php?url=" + escape(url));
	if (doc == false) {
		document.getElementById("valid_show").innerHTML = "Failed.";
	} else {
		document.getElementById("valid_show").innerHTML = "Validating...";
	}
}

function show_validation() {
	if (validDoc.readyState == 4) {
		if (validDoc.status == 200) {
			document.getElementById("valid_show").innerHTML = validDoc.responseText;
		}
	}
}

function init_search() {
	if (navigator.userAgent.toLowerCase().indexOf("safari") != -1) {
		var s = document.getElementById("search");
		s.type = "search";
		s.className = "";
	}
}

// XML Request Object - Much Better than function!

function RequestObject(url,event_handler) {
	this.req = false;
	
	if (window.XMLHttpRequest && !(window.ActiveXObject)) {
		try {
			this.req = new XMLHttpRequest();
		} catch(e) {
			this.req = false;
		}
	} else if (window.ActiveXObject) {
		try {
			this.req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				this.req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				this.req = false;
			}
		}
	}
	if (this.req) {
		this.req.open("GET", url, true);
		this.req.onreadystatechange = event_handler;
		this.req.send("");
	}
	return this;
}


// New Window Links (target_blank replacement!)

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"; 
 } 
} 
window.onload = externalLinks;