
function openWindow(theURL, theName, width, height)
	{
		theWindow = window.open(theURL , ""+theName+"" ,"width=" + width + ",height="+ height + ",toolbar=no,directories=0,menubar=no,status=no,resizable=1,location=0,scrollbars=1,copyhistory=0,alwaysRaised=yes");
		theWindow.title = theName;
		theWindow.focus();
	}
	

var W3CDOM = (document.getElementsByTagName && document.createElement);

function validate()
{
	var DoValidate = true;
	if (DoValidate){
		validForm = true;
		firstError = null;
		errorstring = '';
		var x = document.forms[0].elements;
		
		// loop through everything and check it
		for (var i=0;i<x.length;i++)
		{
			// This specifically tests for the PN news fields which are input type=text
			// Must be a unique test!
			var reg = /fv_date|fv_title|fv_extract|fv_text/i;
			if (reg.test(x[i].name) && x[i].type == 'text')
				if (!x[i].value)
					if (x[i].name == "fv_date")
						writeError(x[i],'You need to enter the date in the format: yyyymmdd ');
					else
						//writeError(x[i],'This field is required: ' + x[i].name + ' \/ ' + x[i].type);
						writeError(x[i],'You need to enter something in this field. ');
		}
		
		// set more general tests
		if (!W3CDOM)
			alert(errorstring);
		if (firstError)
			firstError.focus();
		// finish
		if (validForm){
			//alert('Click OK to confirm changes ' );
			document.frmEdit.submit();
		}else{
			//alert('There are errors on the page ' );
			Return = false;
		}
	}else{
		//document.frmEdit.submit();
	}
}

function writeError(obj,message)
{
	validForm = false;
	if (obj.hasError) return;
	if (W3CDOM)
	{
		obj.className += ' error';
		obj.onchange = removeError;
		var sp = document.createElement('span');
		sp.className = 'error';
		sp.appendChild(document.createTextNode(message));
		obj.parentNode.appendChild(sp);
		obj.hasError = sp;
	}
	else
	{
		errorstring += obj.name + ': ' + message + '\n';
		obj.hasError = true;
	}
	if (!firstError)
		firstError = obj;
}

function removeError()
{
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.parentNode.removeChild(this.hasError);
	this.hasError = null;
	this.onchange = null;
}

function svalidate() {
	var x = document.forms[0].elements;
	var myHash = new Array()
	// loop through everything and load it
	for (var i=0;i<x.length;i++){
		var key = x[i].name;
		myHash[key] = x[i].value;
	}
	// loop through again and check it
	for (var i=0;i<x.length;i++){
		switch(x[i].name){
		case "fv_sort_date":
			if(myHash["fv_uk_pubdate"]){
				var dates = "Zero,JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC"
				var dateArray = new Array();
				dateArray = dates.split(",");
				var dateHash = new Array();
				for (var n=0; n<dateArray.length; n++) {
  					dateHash[dateArray[n]] = PadDigits(n, 2);
				}
				var parts = myHash["fv_uk_pubdate"].toUpperCase();
				parts = parts.split(" ");
				x[i].value = parts[2]+dateHash[parts[1]]+parts[0];
			}else{
				x[i].value = '';
			}
		break;
		case "fv_pub_year":
			if(myHash["fv_uk_pubdate"]){
				var parts = myHash["fv_uk_pubdate"].split(" ");
				x[i].value = parts[2];
			}else{
				x[i].value = '';
			}
		break;
		case "fv_sort_title":
			var n = myHash["fv_title"].toUpperCase();
			var reg = /\s+|^the |^a |^an |\W /ig;
			x[i].value = n.replace(reg, '');
		break;
		case "fv_sort_series":
			var n = myHash["fv_series"].toUpperCase();
			var reg = /\s+|^the |^a |^an /ig;
			x[i].value = n.replace(reg, '');
		break;
		case "fv_sort_name":
			var n = ''; var nn = '';
			if (myHash["fv_author"]){n = myHash["fv_author"]}
			if (n == ''){n = myHash["fv_editor"]}
			if (n == ''){n = myHash["fv_corp_author"]}
			if (n){
				parts = n.split(" ");
				for (var p=parts.length-1;p>=0;p--){
					nn += parts[p];
				}
				nn = nn.toUpperCase();
				if (nn == ''){nn = "ZZZZZZZZ"}
				x[i].value = nn;
			}
		break;
		case "fv_title_index":
			var n ='';
			if(myHash["fv_title"]){n += myHash["fv_title"]}
			if(myHash["fv_sub_title"]){n += " "+myHash["fv_sub_title"]}
			if(myHash["fv_series"]){n += " "+myHash["fv_series"]}
			x[i].value = n;
		break;
		case "fv_uk_vat_price":
			var reg = /\£/g;
			x[i].value = x[i].value.replace(reg, '');
		break;
		case "fv_identifier":
			var n ='';
			// if(myHash["fv_ref_no"]){n += myHash["fv_ref_no"]}
			if(myHash["fv_barcode"]){n += ""+myHash["fv_barcode"]}
			if(myHash["fv_isbn"]){n += "|"+myHash["fv_isbn"]}
			x[i].value = n;
		break;
		case "fv_vx_isbn":
			var n = myHash["fv_isbn"].toUpperCase();
			var reg = /\-|\s+/ig;
			x[i].value = n.replace(reg, '');
		break;
		case "fv_contributor":
			var n ='';
			if(myHash["fv_author"]){n += myHash["fv_author"]}
			if(myHash["fv_artist"]){n += "|"+myHash["fv_artist"]}
			if(myHash["fv_corp_author"]){n += "|"+myHash["fv_corp_author"]}
			if(myHash["fv_editor"]){n += "|"+myHash["fv_editor"]}
			if(myHash["fv_series_editor"]){n += "|"+myHash["fv_series_editor"]}
			var reg = /^\||\|$/ig;
			n = n.replace(reg, '');
			x[i].value = n;
		break;
		case "fv_subj_code":
			var reg = /\s+/g;
			x[i].value = x[i].value.replace(reg, '|');
			var reg = /\|\|/g;
			x[i].value = x[i].value.replace(reg, '|');
		break;
		}
	}
	//
	document.frmEdit.submit();
}

function addOption(){
	// allows arguments to be string or select box
	//second argument if exists contains existing string for appending
	var strList = "";
	if (arguments[1])
		strList = arguments[1] + ' ';	
	if (arguments[0].options) {
		for (i=0;i<arguments[0].options.length;i++){
			if (arguments[0].options[i].selected){
				if (!String(strList).match(arguments[0] + ' '))
				strList = arguments[0].options[i].value + ' '
				}
		}
	}	
	else{
		if (!String(strList).match(arguments[0] + ' '))
		strList = arguments[0] + ' ';
	}
	var intLen = String(strList).length
	if (String(strList).substring(intLen,intLen-1)==' ')
		strList = String(strList).substring(0,intLen-1);
	return strList;
}

function PadDigits(n, totalDigits) 
{ 
    n = n.toString(); 
    var pd = ''; 
    if (totalDigits > n.length) 
    { 
        for (i=0; i < (totalDigits-n.length); i++) 
        { 
            pd += '0'; 
        } 
    } 
    return pd + n.toString(); 
} 

function CleanISBNs() {

	var x = document.forms[0].elements;
	var blob = '';
	// loop through everything and check it
	for (var i=0;i<x.length;i++)
	{
		var sreg = /identifier|isbn/ig;
		if (sreg.test(x[i].value)){
			var reg = /\-/g;
			x[i+1].value = x[i+1].value.replace(reg, '');
		}
		//blob += x[i].name + ' ' + x[i].value + "\n";
	}
	//alert(blob);	
}