// Javascript file for the Historic VCU
// File: historic_vcu.js
// Author: Chia Chiu
// Created on: 07/17/2001
// Last Modified on: 07/17/2001

//Base URL for Historic VCU website

//================================================================================
//================================================================================
// Global Variables
var site_path = "http://www.pubinfo.vcu.edu/cumulus/frenchfilm";
var fcnWin;
var catalogName = "frenchfilm";

//================================================================================
//================================================================================
/*
categorySearch()

Description:
Submit a search query to the cumulus web publisher script (CUWP_CGI.EXE).  The search is limited to the category property of the cumulus catalog. The name of the category is pass into the function and a URL search string is constructed and submitted to the cumulus script.  The function also checks to see if users want to view the results in a different format and add the condition to the search string if one of the options was selected.  Otherwise, the results will be displayed in the current format.

Arguments:
0 - Name of the category

Constraints:
This version is used only in the result pages where an inital search has been done.  This function require the viewing options to be in a radio button format.

*/
function categorySearch() {
	//extracting category name and convert to URL format
	var category = escape(categorySearch.arguments[0]);
	//extracting current search value
	var searchArg = document.forms[0].directArg.value;
	var scriptloc = "http://www.pubinfo.vcu.edu/scripts/CUWP_CGI.EXE?";
	var catalog = catalogName;
	var replyPage = "vcu_fff";
	var qForm = "vcu_fff";
	var searchCond = "prop=" + escape("field[Categories]op[contains]");
	var sortingCond = "sorting=" + escape("field[Record Name]direction[Ascending]");
	var rFormat = "";	
	var rGrp = document.forms[0].resultFormat;
	for( i=0; i < rGrp.length; i++ ) {
		if( (i == 0 ) && rGrp[i].checked )
			rFormat = rGrp[i].value;
		else if( (i == 1  ) && rGrp[i].checked )
			rFormat = rGrp[i].value;
		else if( (i == 2 ) && rGrp[i].checked )
			rFormat = rGrp[i].value;
	}
	
	//Construction URL search string
	var searchURL = "" + scriptloc + "directArg=" + searchArg + "&catalogName=" + catalog + "&replyPage=" + replyPage + "&queryForm=" + qForm + "&resultFormat=" + rFormat + "&site_path=" + escape(site_path) + "&" + searchCond + "&" + sortingCond + "&qVal=" + category;
	document.location.href = searchURL;
	
} //categorySearch

//================================================================================
//================================================================================
/*
categorySearch2()

Description:
A second version of the category search intented for use on the starting search page.  Submit a search query to the cumulus web publisher script (CUWP_CGI.EXE).  The search is limited to the category property of the cumulus catalog. The name of the category is pass into the function and a URL search string is constructed and submitted to the cumulus script.  The function also checks to see if users want to view the results in a different format and add the condition to the search string if one of the options was selected.  Otherwise, the results will be displayed in the current format.

Arguments:
0 - Name of the category

Constraints:
This version is used in the starting search page.  The viewing options are in a pulldown menu format.

*/
function categorySearch2() {
	//extract the category name and convert to URL format
	var category = escape(categorySearch2.arguments[0]);
	var searchArg = document.forms[0].directArg.value;
	var scriptloc = "http://www.pubinfo.vcu.edu/scripts/CUWP_CGI.EXE?";
	var catalog = catalogName;
	var replyPage = document.forms[0].replyPage.value;
	var qForm = document.forms[0].queryForm.value;
	var rFormat = "";	
	var rGrp = document.forms[0].resultFormat;
	for( i=0; i < rGrp.length; i++ ) {
		if( (i == 0 ) && rGrp[i].checked )
			rFormat = rGrp[i].value;
		else if( (i == 1  ) && rGrp[i].checked )
			rFormat = rGrp[i].value;
		else if( (i == 2 ) && rGrp[i].checked )
			rFormat = rGrp[i].value;
	}
	var searchCond = "prop=" + escape("field[Categories]op[contains]");
	var sortingCond = "sorting=" + escape("field[Record Name]direction[Ascending]");

	//construct search string
	var searchURL = "" + scriptloc + "directArg=" + searchArg + "&catalogName=" + catalog + "&replyPage=" + replyPage + "&queryForm=" + qForm + "&resultFormat=" + rFormat + "&site_path=" + escape(site_path) + "&" + searchCond + "&" + sortingCond + "&qVal=" + category;
	document.location.href = searchURL;
	
} //categorySearch2

//================================================================================
//================================================================================
// imgSearch( imgName )
/*
Description:

Arguments:

Constraints:

*/
function imgSearch( imgName ) {
	
	var scriptloc = "http://www.pubinfo.vcu.edu/scripts/CUWP_CGI.EXE?";
	var catalog = "historic_vcu";
	var rFormat = "historic_vcu_pi";
	var searchCond = "prop=" + escape("field[Record Name]op[contains]");
	var searchURL = "" + scriptloc + "directArg=Search&catalogName=" + catalog + "&replyPage=historic_vcu_p&queryForm=historic_vcu2&resultFormat=" + rFormat + "&" + searchCond + "&qVal=" + imgName;
	parent.fimg.document.location.href = searchURL;
	
} //categorySearch

//================================================================================
//================================================================================
// popWin()
/*
Description:
Open a popup window and load a page specified my the URL passed into the function.  Width and Height can also be customized.  The default values are height of 350 and width of 340.  The popup will have only the scrollbar to view the entire page.

Arguments:
0 - URL of webpage to be loaded into the popup window
1 - width of popup window
2 - height of popup window

Constraints:
The webpage url must be provided and in a valid format.  The script does not check if the information is available or valid.

*/

function popWin() {
	var webpage = popWin.arguments[0];
	var width = (popWin.arguments.length >= 2)? popWin.arguments[1]: 340;
	var height = (popWin.arguments.length >= 3)? popWin.arguments[2]: 350;
	var winoptions = "scrollbars,width=" + width + ",height=" + height;
	var historic_vcu_popWin = open( webpage, "historic_vcu_popWin", winoptions );
	historic_vcu_popWin.focus();
	

} //popWin

//================================================================================
//================================================================================
// viewPic()

function viewPic() {
	var pg = "<html><head><title></title></head>" +
			"<body bgcolor=\"#333333\">" +
			"<img src=\"http://www.pubinfo.vcu.edu" +  eval("\"" + viewPic.arguments[0] + "\"")  + "\">" +
			"</body>" + 
			"</html>";
			
	var picWin = open( "", "pic", "scrollbars,resizable,width=400,height=300" );
	picWin.document.write( pg );
	picWin.focus();
}//viewPic

//================================================================================
//================================================================================
// extraFcn( func )
/*
Description:
This function acts like a middle-man for the postcard and info functionality of the site.  The script changes the current form properties so that the information can be sent to the correct ASP script and loaded into a popup window.  Then the initial form values are restored so that the search functionality will work correctly.

Arguments:
func - name of the function to perform.
       'postcard' - send a postcard functionality
	   'info' - display print version functionality

Constraints:
It takes about 3 seconds before the initial form values are restored.  This was set to ensure that the form as be properly submitted to the respective ASP scripts.  This will cause some problem if users perform a search or next page functionality immediately after clicking on the "Send a Postcard" or "View Print Version" button.

*/     
function extraFcn( func ) {
	var tpage
	var winopt
	var winName
	var site_path = "http://www.pubinfo.vcu.edu/cumulus/frenchfilm";
	
	var tempAction = document.forms[0].action;
	var tempMethod = document.forms[0].method;
	var tempTarget = document.forms[0].target;
	
	if( func == "postcard" ) {
		tpage = "/sendpostcard.asp";
		winopt = "location=yes,width=300,height=375";
		aaa = open("", "aaa", winopt).focus();
		document.forms[0].action = site_path + tpage;
		document.forms[0].method = "post";
		document.forms[0].target = "aaa";
		document.forms[0].submit();
	} else if( func == "info" ) {
		tpage = "/printinfo.asp";
		winopt = "menubar=yes,scrollbars=yes,width=400,height=400";
		infoWin = open("", "infoWin", winopt).focus();
		document.forms[0].action = site_path + tpage;
		document.forms[0].method = "post";
		document.forms[0].target = "infoWin";
		document.forms[0].submit();
	}
	
	var timer1 = setTimeout( "resetForm(\'" + tempAction + "\', \'" + tempTarget + "\', \'" + tempMethod + "\')", 3000 );


}//extraFcn

//================================================================================
//================================================================================
// resetForm( ta, tt, tm )
/*
Description:
Set the form properties passed into the function.

Arguments:
ta - form action
tt - form target
tm - form method

Constraints:
none

*/

function resetForm( ta, tt, tm) {
	document.forms[0].action = ta;
	document.forms[0].target = tt;
	document.forms[0].method = tm;
}//resetForm

//================================================================================
//================================================================================
// psubmit()
/*
Description:
Change the form properties and submit form to retreive the postcard. This script is used to automatically submit the dynamic img source generated by cumulus web publisher to a script that retreives the rest of the postcard information.

Arguments:
none

Constraints:

*/

function psubmit(imgW, imgH) {

	var tempAction = document.forms[0].action;
	var tempMethod = document.forms[0].method;
	var tempTarget = document.forms[0].target;
	
	var postcardwin = open( "", "postcardwin", "width=" + imgW + ",height=" + imgH );
	
	document.forms[0].action = "http://www.pubinfo.vcu.edu/cumulus/frenchfilm/pc_front.asp";
	document.forms[0].target = "postcardwin";
	document.forms[0].method = "post";
	document.forms[0].submit();
	
	var timer1 = setTimeout( "resetForm(\'" + tempAction + "\', \'" + tempTarget + "\', \'" + tempMethod + "\')", 2000 );
} //psubmit

//================================================================================
//================================================================================
// chk_feedback()
/*
Description:
Checks the feedback form for valid user data format.  Makes sure that sender's name consists of letters and that the email entered is in the correct format.

Arguments:
none

Constraints:

*/

function chk_feedback() {
	var errors = new Array();
	var objs = new Array();
	var name_exp = /^[\w-' ]+$/;
	var email_exp = /\b[\w\.-]+@[\w-]+\.[\w\.-]+\w\b/;
	
	
	//checking sender's name
	var result = document.form1.sender.value.search(name_exp);
	if( result == -1 ) {
		errors[ errors.length] = "Invalid sender name.";
		objs[ objs.length] = document.form1.sender;
	}
	
	//checking sender's email
	result = document.form1.frommail.value.search(email_exp);
	if( result == -1 ) {
		//alert( "Invalid sender email. Please try again.");
		//document.mes.fromField.focus();
		//document.mes.fromField.select();
		errors[errors.length] = "Invalid sender email."
		objs[objs.length] = document.form1.frommail;
	}
	
	//check recipient name
	if( document.form1.mess.value.length == 0 ) {
		errors[ errors.length ] = "The Comment/Suggestion field is empty.";
		objs[ objs.length ] = document.form1.mess;
	}

	
	if( errors.length > 0 ) {
	
		var err_message = "";
		for( i=0; i < errors.length; i++ )
			err_message += errors[i] + "\n\r";
		err_message += "Please try again.\n\r";
		alert( err_message );
		objs[0].focus();
		objs[0].select();
	} else {
		document.form1.submit();
	}

}//chk_feedback

//================================================================================
//================================================================================
// chk_postcard()
/*
Description:
Checks to make sure that all user inputted information are in the correct format.

Arguments:
none

Constraints:

*/

function chk_postcard() {
	var errors = new Array();
	var objs = new Array();
	var name_exp = /^[\w-' ]+$/;
	var email_exp = /\b[\w\.-]+@[\w-]+\.[\w\.-]+\w\b/;
	
	
	//checking sender's name
	var result = document.postcardform.sender.value.search(name_exp);
	if( result == -1 ) {
		errors[ errors.length] = "Invalid sender name.";
		objs[ objs.length] = document.postcardform.sender;
	}
	
	//checking sender's email
	result = document.postcardform.frommail.value.search(email_exp);
	if( result == -1 ) {
		//alert( "Invalid sender email. Please try again.");
		//document.mes.fromField.focus();
		//document.mes.fromField.select();
		errors[errors.length] = "Invalid sender email."
		objs[objs.length] = document.postcardform.frommail;
	}
	
	//checking recipient's name
	var result = document.postcardform.recipient.value.search(name_exp);
	if( result == -1 ) {
		errors[ errors.length] = "Invalid recipient name.";
		objs[ objs.length] = document.postcardform.recipient;
	}
	
	//checking recipient's email
	result = document.postcardform.tomail.value.search(email_exp);
	if( result == -1 ) {
		//alert( "Invalid sender email. Please try again.");
		//document.mes.fromField.focus();
		//document.mes.fromField.select();
		errors[errors.length] = "Invalid recipient email."
		objs[objs.length] = document.postcardform.tomail;
	}
	
	//check message
	if( document.postcardform.mess.value.length == 0 ) {
		errors[ errors.length ] = "The Comment/Suggestion field is empty.";
		objs[ objs.length ] = document.postcardform.mess;
	}

	
	if( errors.length > 0 ) {
	
		var err_message = "";
		for( i=0; i < errors.length; i++ )
			err_message += errors[i] + "\n\r";
		err_message += "Please try again.\n\r";
		alert( err_message );
		objs[0].focus();
		objs[0].select();
	} else {
		document.postcardform.action = "sendpostcard.asp";
		document.postcardform.method = "post";
		document.postcardform.target = "_self";
		document.postcardform.submit();
	}

}//checkfields

//================================================================================
//================================================================================
// delayClose(delaytime)
/*
Description:
Closes the current window after 'delaytime' milliseconds.

Arugments:
delaytime - number of millisecond to wait before closing the window

*/

function delayClose(delaytime) {
	var timer1 = setTimeout( "window.close()", delaytime );
}