//submit the search form on the header
function checkQueryForm(thisform){
 	var searchwords = trim(thisform.searchwords.value);
 	if(searchwords == null || searchwords == ""){
 		return false;
 	}
 	else{
 		var searchtype = thisform.searchtype.value;
		if(searchtype == "clubs"){
			thisform.action = "/club_search.php";
		}
		else if(searchtype == "ppl"){
			thisform.action = "/people_search.php";
		}
		else if(searchtype == "appl"){
			thisform.action = "/admin_people_search.php";
		}
		else if(searchtype == "book"){
			thisform.action = "/book_search.php";
		}
		else if(searchtype == "article"){
			thisform.action = "/article_search.php";
		}
		else if(searchtype == "author"){
			thisform.action = "/book_search.php";
		}
		else if(searchtype == "interests"){
			thisform.action = "/people_search.php";
		}
		thisform.submit();
	}
	return false;
}
function checkContentForm(thisform){
 	var query = trim(thisform.query.value);
 	if(query == null || query == ""){
 		return false;
 	}
 	else{
 		thisform.action = "/content_search.php";
		thisform.submit();
	}
	return false;
}

//check if the file name is in the extension array. if yes, return true, else return false.
function check_extension(file_name, extArray) {
    allowSubmit = false;
    if (!file_name) return true;
		
		//get file name
    while (file_name.indexOf("\\") != -1)
    	file_name = file_name.slice(file_name.indexOf("\\") + 1);
    
    //get file extension
    while (file_name.indexOf(".") != -1)	
    	file_name = file_name.slice(file_name.indexOf(".") + 1);
    
    file_ext = file_name.toLowerCase();
		if (!file_ext) return false;    
		
    for (var i = 0; i < extArray.length; i++) {
    	if (extArray[i] == file_ext) { allowSubmit = true; break; }
    }
    if (allowSubmit) 
    	return true;
    else
	    return false;
}


//trim a string	
function trim(inputString) {
	   // Removes leading and trailing spaces from the passed string. Also removes
	   // consecutive spaces and replaces it with one space. If something besides
	   // a string is passed in (null, custom object, etc.) then return the input.
	   if (typeof inputString != "string") { return inputString; }
	   return inputString.replace(/(^\s*)|(\s*$)/g, "");
	   
 
	} // Ends the "trim" function

//pop up new window.	
function launchPopupWindow(page, iname, iwidth, iheight) {
	if(iname==null){iname="_blank";}
	if(iwidth==null){iwidth=647;}
	if(iheight==null){iheight=440;}
	
	var ileft = (screen.width - iwidth)/2;
	var itop = 10;

	customise = "width="+iwidth+",height="+iheight+",left="+ileft+",top="+itop+",scrollbars=yes,resizable=yes,toolbar=no,menubar=no,location=no,status=no";
	myWindow = window.open(page,iname,customise);
	myWindow.focus();
	//return myWindow;
}

function launchFlipWindow(book_id, force_reload, check_str, root_path){
	//orgsize 800 * 432
	var isize = 1;
	var iwidth=860, iheight=500;
	if(screen.width>=1024){
		iwidth=1000, iheight=600;
		isize = 1.2;
	}
	if(screen.width>=1280){
		iwidth=1200, iheight=700;
		isize = 1.4;
	}

	if(root_path!=null)
		openUrl = root_path;
	else
		openUrl = "/";
	openUrl += "book_read_flip.php?style=popup&book_id="+book_id+"&size="+isize;
	if(force_reload!=null)
		openUrl += "&forceload=1";
	if(check_str!=null)
		openUrl += check_str;


	launchPopupWindow(openUrl, "book_read_flip", iwidth, iheight);
}
function launchArticleFlipWindow(article_id, force_reload){

	//orgsize 612 * 642
	isize = 0.75
	if(screen.height>=768){
		isize = 1;
	}
	if(screen.height>=1024){
		isize = 1.1;
	}
	var iheight = isize * 642 + 40;
	var iwidth = isize*612+20;

	var openUrl = "/article_read_flip.php?style=popup&article_id="+article_id+"&size="+isize;
	if(force_reload!=null)
		openUrl += "&forceload=1";	

	launchPopupWindow(openUrl, "article_read_flip", iwidth, iheight);
}

function getBrowserDimension(wh){
	  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(wh=='w')
  	return myWidth;
  else
  	return myHeight;
}
//object show hide.
function objShowHide(id, state) {
	if (document.getElementById) {
		document.getElementById(id).style.display = state;
	}
	else {
		if (document.layers) {
			document.id.visibility = state;
		}
		else { // IE 4
			document.all.id.style.display = state;
		}
	}
}

//pass the obj name for a set of objects.
function objShowHideByName(name, state) {
		if(document.getElementsByName){		
			var collObjects  = document.getElementsByName(name);
			for (i=0;i<collObjects.length;i++){
			 	collObjects[i].style.display = state;
			}
		}
}

//check if checkall button status.
function CheckCheckAll(ele, checkAllEle)
{
 if(ele.options && ele.options.length){ //selections
		 	
		 for (var i=0;i<ele.options.length;i++)
		 {
		   var e = ele.options[i];
		   if (e.selected==false)
		   {
		    checkAllEle.checked = false;
		    return;
		   }
		 }
		 checkAllEle.checked = true;
		 return;
	}

 if(ele.length){ //checkbox
		 	
		 for (var i=0;i<ele.length;i++)
		 {
		   var e = ele[i];
		   if (e.checked==false)
		   {
		    checkAllEle.checked = false;
		    return;
		   }
		 }
		 checkAllEle.checked = true;
	}	
}

//check all selections buttons, or checkbox 
function CheckAll(ele)
{
 	if(ele.options){ //selections
	 for (var i=0;i<ele.options.length;i++)
	 {
	 	 var e = ele.options[i];
   	 e.selected = true;
	 }
	 return;
	}

	//single checkbox
	if (ele.checked == false)
  {
      ele.checked = true;
      return;
  }
  
 	if(ele.length){ //multiple checkbox
	 for (var i=0;i<ele.length;i++)
	 {
	 	 var e = ele[i];
   	 e.checked = true;
	 }
	 return;
	}
	
}
//clear all selections buttons, or checkbox 
function ClearAll(ele)
{
	
 	if(ele.options){ //selections
	 for (var i=0;i<ele.options.length;i++)
	 {
	 	 var e = ele.options[i];
   	 e.selected = false;
	 }
	 return;
	}

	//single checkbox
	if (ele.checked == true)
  {
      ele.checked = false;
      return;
  }
  
 	if(ele.length){ //multiple checkbox
	 for (var i=0;i<ele.length;i++)
	 {
	 	 var e = ele[i];
   	 e.checked = false;
	 }
	 return;
	}
	
}
//show text in source element to target element
function showText(sourceName, targetName) {
		targetDiv = document.getElementById(targetName);
		sourceDiv = document.getElementById(sourceName);
		if(targetDiv && sourceDiv){
			targetDiv.innerHTML = sourceDiv.innerHTML;
		}
}

//check the object length, used for textarea.
function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}

//ajax calls
//initialize an ajxt request.
function initRequest() {
		var request=null;
	  if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return request;
}
//get the target URL content to the target htmle element.
function getUrlContent(strUrl, targetName, httpMethod, params) {
		if(!httpMethod){
			httpMethod = "GET";
		}
		
    var request = initRequest();
    if(request){
    	if(httpMethod=="GET"){
	    	request.open("GET", strUrl, true);
	    }
	    else{
	    	request.open("POST", strUrl, true);
  			request.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
	  	}
	    request.onreadystatechange = function()
	    {
	    		if(targetName){
			    	 	var targetEle = window.document.getElementById(targetName);
			    	 	
					    if (request.readyState == 4) {
					        if (request.status == 200) {
					        		var responseText = request.responseText;
					        		targetEle.innerHTML = responseText;
					        }
					    }
					    else{
					    	targetEle.innerHTML = "loading...";
					  	}
					 }
	  	}
	    request.send(params);
	  }
}

//request the book attributes from ecommerce system
function requestContent(strUrl,bookidarr,callarr) {  
	var http_request = initRequest();
  http_request.onreadystatechange = function() {
      handlejson(http_request,bookidarr,callarr)
    };
  http_request.open("GET", strUrl, true);
  http_request.send(null);
}
//handle the json cal results from ecommerce system
 function handlejson(http_request,bookidarr,callarr) {
 if (http_request.readyState == 4) {
 		if(http_request.responseText == null || http_request.responseText == "")
 			return;
  	var content = eval("(" + http_request.responseText + ")");
  	for(i = 0;i < bookidarr.length;i++){
			for(j = 0;j < callarr.length;j++){
				if(content['attr'] && content['attr']['book' + bookidarr[i]][callarr[j]] && document.getElementById(callarr[j] + bookidarr[i])){
					var calldata = content['attr']['book' + bookidarr[i]][callarr[j]];
					document.getElementById(callarr[j] + bookidarr[i]).innerHTML = calldata;
				}
			}
		}
 }
}
  
//Functions to submit a form, create the post string prepared for ajax post call.
function getformvalues (fobj){
		
		var str = "";
		//Run through a list of all objects contained within the form.
		for(var i = 0; i < fobj.elements.length; i++){
			var eobj = fobj.elements[i];
			if(eobj.options && eobj.options.length){
				 for (var j=0;j<eobj.options.length;j++)
				 {
				   var opt = eobj.options[j];
				   if (opt.selected || opt.checked)
				   {
				   	str += eobj.name + "=" + opt.value + "&";
				   }
				 }
 
			}
			else if(eobj.type == "checkbox" || eobj.type == "radio"){
				if(eobj.checked){
				   	str += eobj.name + "=" + eobj.value + "&";
				 }  	
			}			
			else {
				str += eobj.name + "=" + escape(eobj.value) + "&";
			}
		}
		//Then return the string values.
		return str;
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	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" : "" );
}

function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function Check_Cookie() {
		document.cookie="testcookie"
		cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
		document.cookie="" //erase dummy value
		return cookieEnabled;
}

function openURL( url, target ) //called in flash
{
	try
	{
		var popup = window.open( url, target );
		if ( popup == null )
			return false;

		if ( window.opera )
			if (!popup.opera)
				return false;
	}
	catch(err)
	{
		return false;
	}
	return true;
}
