var jsdebug = "NO";
var saveBB = "NO";
var saveWebCT = "NO"

function hexnib(decimal)
{
	if(decimal<10)
	{
		return decimal;
	}
	else
	{
		return String.fromCharCode(65+decimal-10);
	}
}

function hexcode(data)
{
	var result="";
	for(var i=0;i<data.length;i++)
	{
		var cc=data.charCodeAt(i);
		var hex=hexnib((cc&240)>>4)+""+hexnib(cc&15);
		result+="%"+hex;
	}
	return result;
}

function SaveCurrSearchToBB()
{
	HandleFt();

	var AnyColl = AreAnyCollectionsChecked();

	if(AnyColl)
	{
	   saveBB = "YES";
		validate_form_browse();
	}
}

function SaveCurrSearchToWebCT()
{
	HandleFt();

	var AnyColl = AreAnyCollectionsChecked();

	if(AnyColl)
	{
	   saveWebCT = "YES";
		validate_form_browse();
	}
}

function validate_form_browse()
{

	if ( document.querybox.pm_SA.value == "" && saveBB != "YES" && saveWebCT != "YES")
	{
		alert ( "Please enter a term to search on." );
		return false;
	}
	else if (document.querybox.pm_SC.value == "FT" || document.querybox.pm_SC.value == "AFT")
	{
		xloc = document.ft.action;

		if (saveBB == "YES")
		{
			xloc += "SaveXML=search&inxsl=savexml.xsl&undoxsl=cannedsearch.xsl&undooutxsl=simsearch.xsl";
			xloc += "&OUTPUTXSL=CoursewareContentProp.xsl";
		}
		else if (saveWebCT == "YES")
		{
			xloc += "SaveXML=search&inxsl=savexml.xsl&undoxsl=cannedsearch.xsl&undooutxsl=simsearch.xsl";
			xloc += "&OUTPUTXSL=webctstatus.xsl";
		}
		else
		{
			xloc += "OUTPUTXSL=" + document.ft.OUTPUTXSL.value;
		}

		if(document.querybox.pm_SC.value == "FT")
		{
			xloc += "&pm_FT=" + hexcode(document.querybox.pm_SA.value);
		}
		else if (document.querybox.pm_SC.value == "AFT")
		{
			xloc += "&pm_AFT=" + encodeURIComponent(document.querybox.pm_SA.value);
		}

		xloc += "&Submit=Go";
		xloc += "&pm_MH=" + document.querybox.pm_MH.value;
		xloc += "&pm_USI=" +  document.querybox.pm_USI.value;
	
		xloc += "&api_2=SIMULFIND" ;
		
		// Dedupe Flag ?
		if(document.querybox.DDF.value == 'Y')
		{
			xloc += "&ds_svGeneric_DDF=Y";
		}
		else
		{
			xloc += "&ds_svGeneric_DDF=N";
		}
		
		// Include Local Catalog ?
		if(document.querybox.pm_OPC.checked)
		{
			xloc += "&pm_OPC=Y";
		}
		else
		{
			xloc += "&pm_OPC=N";
		}
		if ( document.querybox.pm_CL.length)
		{
			for(i = 0; i < document.querybox.pm_CL.length; i++)
			{
				if(document.querybox.pm_CL[i].checked)
				{
					xloc += "&pm_CL=" + document.querybox.pm_CL[i].value;
				}
			}
		}

		else
		{
        	xloc += "&pm_CL=" + document.querybox.pm_CL.value;
		}
		
		xloc += "&api_1=COLLECTION_REPOLOGIN";
		xloc += "&DIRECTIVE=createSrHist&ds_svGeneric_MH=" + document.querybox.pm_MH.value;

		if (jsdebug == "YES")
		{
			alert(xloc);
		}
		if (saveBB == "YES")
		{
			cwLoc = xloc;
			cwLoc += '&amp;ds_svGeneric_CWType=Search with User Term&amp;ds_svGeneric_CWTypeParm=Search';
			
			var newWin = open(cwLoc,'CourseWare','resizable,dependent,status,width=800,height=500');
			saveBB = "false";
		}
		else if (saveWebCT == "YES")
		{
			cwLoc = xloc;
			//alert (xloc);
			var newWin = open(cwLoc,'WebCT','resizable,dependent,status,width=800,height=500');
			saveWebCT = "false";
		}
		else
		{
			location.href = xloc;
		}

		return false;
    }

	 else
	 {
	 	document.querybox.ds_svGeneric_MH.value = document.querybox.pm_MH.value;
		
		// Dedupe Flag ?
		
		if(document.querybox.DDF.value == 'Y')
		{
			document.querybox.ds_svGeneric_DDF.value = "Y";
		}
		else
		{
			document.querybox.ds_svGeneric_DDF.value = "N";
		}
		document.querybox.submit();
		return false;
	 }
    return true;

}

function validate_form_suggest ( )
{
    valid = true;
    document.querybox.ds_svGeneric_MH.value = document.querybox.pm_MH.value;
	 
	 if (typeof(document.querybox.pm_FT) != "undefined")
    {
	 	document.querybox.ds_svGeneric_LDS.value = document.querybox.pm_FT.value;
	 }
	 
	 if (typeof(document.querybox.pm_AFT) != "undefined")
    {
	 	document.querybox.ds_svGeneric_LDS.value = document.querybox.pm_AFT.value;
	 }
	 
    if ( document.querybox.ds_svGeneric_LDS.value == "" )
    {
        alert ( "Please enter a term to search on." );
        valid = false;
    }

    return valid;
}
function validate_form_discovery ( )
{
    valid = true;

    document.querybox.ds_svGeneric_MH.value = document.querybox.pm_MH.value;
    if ( document.querybox.pm_SA.value == "" )
    {
        alert ( "Please enter a term to search on." );
        valid = false;
    }

    return valid;
}

function RegistrationRequestValidate()
{
	var emailFilter=/^.+@.+\..{2,3}$/;
	var illegalChars = /[\W_]/; // allow only letters and numbers
	
	//alert('validating...');
	
	//Validating username
	if ((illegalChars.test(document.getElementById('pref-user-name').value)))
	{
		alert('Username must not contain spaces or special characters');
		document.getElementById('pref-user-name').focus();
		return false;
	}
	else if ((document.getElementById('pref-user-name').value.length < 6)) 
    {
		alert("Username must be at least 6 characters.\n");
		document.getElementById('pref-user-name').focus();
		return false;
    }
    else if ((document.getElementById('pref-user-pass').value.length < 6)) 
    {
		alert("Password must be at least 6 characters.\n");
		document.getElementById('pref-user-pass').focus();
		document.getElementById('pref-user-pass').value='';
		return false;
    }
    else if (illegalChars.test(document.getElementById('pref-user-pass').value)) 
    {
		alert("Password ust not contain spaces or special characters");
		document.getElementById('pref-user-pass').focus();
		document.getElementById('pref-user-pass').value='';
		return false;
    }
	else if (!(emailFilter.test(document.forms['registration'].email.value)))
	{
		alert('Please enter a valid email address');
		document.getElementById('email').focus();
		document.getElementById('email-confirm').value = '';
		return false;
	}
	else if (document.getElementById('email').value != document.getElementById('email-confirm').value)
	{
		alert('The email and confimation email fields do not match');
		document.getElementById('email-confirm').focus();
		return false;
	}
	else
	{
		return true;
	}

	
}
