// JavaScript Document
function onFocusSearch(searchField)
{
	if(searchField.value == 'enter keyword')
	{
		searchField.value='';
	}
}

function onChangeSearch(searchField)
{
	if(searchField.value == '')
	{ 
		searchField.value = 'enter keyword';
	}
}

function NewWindow(mypage, myname, w, h, scroll, resizable, status) 
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable='+resizable+',status='+status+'';
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

// JavaScript Document
function makeInstance() {
	var http_request = false;
	
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	
	if (!http_request) {
		alert('Giving up sad.gif Cannot create an XMLHTTP instance');
		return false;
	}
	
	return http_request;
	
}

// validate form
function validate(objForm, file, submitOnSuccess)
{
	ajax = makeInstance();
	ajax.onreadystatechange = function()
	{
		if(ajax.readyState == 1) 
		{	
			if(submitOnSuccess == null)
			{
				document.getElementById('status').innerHTML = '<img src="../images/admin/loading.gif" /> <span class="gray">Validating Submission...</span>';		
			}
		}
		
		if((ajax.readyState == 4)&&(ajax.status == 200)) 
		{
			
			if(ajax.responseText == 0) 
			{		
					if(submitOnSuccess == null)
					{
						document.getElementById('status').innerHTML = '<span class="green">Submission Valid! Processing...</span><br/>';
						objForm.submit(); 
					}
					else
					{
						document.getElementById('status').innerHTML = '';
					}
			} 
			else 
			{
					document.getElementById('status').innerHTML = '<span class="red">' + ajax.responseText + '</span><br/>';
			}
		}				
	}
	ajax.open('POST',file,true);
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(getRequestBody(objForm));
}

function getRequestBody(fobj) { 
	 var str = ""; 
	 var ft = ""; 
	 var fv = ""; 
	 var fn = ""; 
	 var els = ""; 
	 for(var i = 0;i < fobj.elements.length;i++) { 
	  els = fobj.elements[i]; 
	  ft = els.title; 
	  fv = els.value; 
	  fn = els.name; 
	 switch(els.type) { 
	  case "text": 
	  case "hidden": 
	  case "password": 
	  case "textarea":
		case "file":

	  str += fn + "=" + encodeURI(fv) + "&"; 
	  break;  
	 
	  case "checkbox": 
	  case "radio": 
	   if(els.checked) str += fn + "=" + encodeURI(fv) + "&"; 
	  break;     
	 
	 	case "select-one":
	    str += fn + "=" + 
	    els.options[els.selectedIndex].value + "&"; 
		break;
		
	  case "select-multiple":
		
			for(j=0; j<els.length; j++)
			{
				if(els.options[j].selected)
				{
					str += fn + "=" + 
					els.options[j].value + "&";
				}
			}
			
	  break; 
	  } // switch 
	 } // for 
	 str = str.substr(0,(str.length - 1)); 
return str; 
}

// tell a friend
function emailThisPage(intSiteID)
{
	NewWindow('./emailThisPage.php', 'EmailThisPage', 600, 400, 1, 0);
	//alert(window.location);
}

//print this page
function printPage()
{
	var txtQueryString = window.location.search.substring(1);
	var txtPrint = (txtQueryString=="")?'?print=1':'&print=1';
	NewWindow(window.location + txtPrint, 'PrintPage', 790, 600, 1, 1, 0);
}

//preview page
function printIssue(txtPath, intIssueType, intIssueID)
{
//	var txtQueryString = window.location.search.substring(1);

	var asdf = "test"
	var txtQueryString = "?id=16&iid=" + intIssueID;

	if (intIssueType==1)
	{
		txtQueryString = "?id=15&iid=" + intIssueID;
	}

//	echo("host is " . $_SERVER['SERVER_NAME']);
	var txtPrint = (txtQueryString=="")?'?print=1':'&print=1';
//	NewWindow(window.location + txtPrint, 'PrintPage', 760, 600, 1, 1, 0);


	if (txtPath=="localhost") {
		txtPath = "localhost/pca";
	}

	NewWindow("http://" + txtPath + "/" + txtQueryString + txtPrint, 'PrintPage', 790, 600, 1, 1, 0);
// 	NewWindow("http://dictionary.reference.com/browse/" + path + "MMM" + txtQueryString + txtPrint, 'PrintPage', 790, 600, 1, 1, 0);
}