/////////////////////////////////////////////////////		\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
//***************************************************		*******************************************************\\
//File Name : script.php
//Purpose : Collection of javascript funcitons.
//Copyright (c) 2008 Pomona College
//***************************************************		*******************************************************\\
/////////////////////////////////////////////////////		\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

// Common function for popup windows
function MM_openBrWindow(theURL,winName,features) 
{ 
  window.open(theURL,winName,features);
}

//  AJAX SCRIPTS - Starts 
function makeRequest(divid) {
	
	var http_request = false;
	var val=document.getElementById("main_categ").value;
	
	if (window.XMLHttpRequest) {
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			}
	} else if (window.ActiveXObject) { 
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			try {
					http_request = new
			ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
		
	http_request.onreadystatechange = function() {
	alertContents(http_request,divid); 
	};
		http_request.open('GET','dbprocess.php?'+'id='+val, true);						
		http_request.send(null);			
}

function subMakeRequest(divid) {
	
	var http_request = false;	
	var val=document.getElementById("sub_categ1").value;
	if (window.XMLHttpRequest) {
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			}
	} else if (window.ActiveXObject) { 
			try {
				http_request = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
			try {
					http_request = new
			ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
			
	}
if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
		
	http_request.onreadystatechange = function() {
	alertContents(http_request,divid); 
	};
		http_request.open('GET','dbprocess.php?'+'id1='+val, true);						
		http_request.send(null);	
		
}

function alertContents(http_request,divid) {
if (http_request.readyState == 4) {

		
		document.getElementById(divid).style.display="block";
		var C=document.getElementById(divid);
		var data=http_request.responseText;
		records='';
		C.options.length=0;
		var records=data.split(";");

		C.options[0]=new Option('--- Select Subcategory ---','');
		if(records.length>1)
		{
			document.getElementById(divid).style.display="block";
		}
		for(i=0;i<records.length-1;i++)
		{
			if(records[i]!='')
			{
				var single=records[i].split("^^^");
				C.options[i+1]=new Option(single[1],single[0]);
			}
		}
				
	}
	val = "";//need to del if not needed
}



/////////////////////////////////
var xmlHttp

function clickStar(str,id,mediatype)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	var url="getstars.php"
	url=url+"?star="+str+"&id="+id+"&media_type="+mediatype
	url=url+"&ippd="+Math.random()	
	xmlHttp.onreadystatechange=stateChanged 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
} 

function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{	
		if(xmlHttp.responseText != "You have already rated"){
			document.getElementById("ratingVal").innerHTML=xmlHttp.responseText
		}
		else
		{
			document.getElementById("error").innerHTML=xmlHttp.responseText
		}
	} 
} 

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
	objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
	objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 
//  AJAX SCRIPTS - Ends 

//Common function for Showstars  in ratings
function showStar(str)
{
	var stars = new Array("star1","star2","star3","star4","star5");
	for(i=0; i<5; i++)
	{	
		if(i<str)
			document.getElementById(stars[i]).src="images/star.gif";
		else
			document.getElementById(stars[i]).src="images/no_star.gif";
	}
}

//Common function for Hidestars in ratings
function hideStar(str)
{
	var stars = new Array("star1","star2","star3","star4","star5");
	for(i=0; i<str; i++)
	{
		document.getElementById(stars[i]).src="images/no_star.gif";
	}
}

//Common function for Subscribe
function subscribe(frm)
{
	if(frm.email_address.value == "") 
	{
		alert("Please Enter Your Email Address");
		frm.email_address.focus();
		return false;
	}
	else if(frm.email_address.value != "") 
	{
		
		var str = frm.email_address.value;
		var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
		var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
		if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
		 //  return true;
		}
		else
		{
			alert("Please Enter Your Valid Email Address");
			frm.email_address.select();
			return false;
		}
	}
	return true;
}

//Empty validation for Category search
function valid(frm)
{
	
	if(document.getElementById("main_categ").value == "")
	{	
		alert("Please select the category");
		document.getElementById("main_categ").focus();
		return false;	
	}	
	return true;
}
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
//Validation for Keyword search
function validate(frm)
{
	
	if(document.getElementById("keyword").value == "")
	{	
		alert("Please enter the Keyword");
		document.getElementById("keyword").focus();
		return false;	
	}
	if(trim(document.getElementById("keyword").value) == "")
	{	
		
		alert("Please enter the Keyword");
		document.getElementById("keyword").value = "";
		document.getElementById("keyword").focus();
		return false;	
	}
	else
	{
		document.getElementById("keyword").value = ltrim(document.getElementById("keyword").value);
		document.getElementById("keyword").value = rtrim(document.getElementById("keyword").value);
	}
		
	if(document.getElementById("searchall").checked ==false && document.getElementById("periodicals").checked == false && document.getElementById("websites").checked ==false && document.getElementById("books").checked ==false && document.getElementById("job").checked ==false && document.getElementById("alumni").checked ==false)
	{	
		alert("Please check atleast one Media Type");
		return false;	
	}
	return true;
}
//Function to make uncheck the resource types when "search all" checked
function callme(id)
{
	if(id.checked)
	{	
	document.getElementById("periodicals").checked = false;
	document.getElementById("websites").checked = false;
	document.getElementById("books").checked = false;
	document.getElementById("job").checked = false;
	document.getElementById("alumni").checked = false;
	}
}
//Function to make uncheck the "search all" when "resource types" checked
function call(id)
{
	if(id.checked)
	{	
	document.getElementById("searchall").checked = false;	
	}
}

//Common function for Resource validation
function resource_valid(frm)
{
	if(frm.titletxt.value == "" || frm.titletxt.value == "Enter book title/author, URL, etc.")
	{	
		alert("Please enter the resource");
		frm.titletxt.focus();
		return false;	
	}
	else if(trim(frm.titletxt.value) == "" || trim(frm.titletxt.value) == "Enter book title/author, URL, etc.")
	{
		alert("Please enter the resource");
		frm.titletxt.value = "";
		frm.titletxt.focus();
		return false;	
	}
	else
	{
		frm.titletxt.value = ltrim(frm.titletxt.value);
		frm.titletxt.value = rtrim(frm.titletxt.value);
	}
	
}

//Common function for Delay/Pause process
function pause(numberMillis) {
        var now = new Date();
        var exitTime = now.getTime() + numberMillis;
        while (true) {
            now = new Date();
            if (now.getTime() > exitTime)
                return;
        }
    }
function delay_process(){
for (var i=0;i<50000 ;i++ )
{
	
}
afterFiveSeconds();
}

function afterFiveSeconds(){		
	window.opener.location.reload();
	window.close();
}

//Common function to resizing the window based on the images
function fitPic() {
	if (window.innerWidth){
		iWidth = window.innerWidth;
		iHeight = window.innerHeight;
	}else{
		iWidth = document.body.clientWidth;
		iHeight =document.body.clientHeight;
	}
	iWidth = document.gallery.width - iWidth+40;
	iHeight = document.gallery.height - iHeight+50;
	window.resizeBy(iWidth, iHeight);
}
//Common function to navigate within pages
function navigate(linkVal,pageId)
{
	document.location.href=linkVal+"&page="+pageId;
}
