function getObj(n, d) { 
  var p,i,x;  
  if(!d) {
		d=document; 
	}
	if((p=n.indexOf("?")) > 0 && parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; 
    n=n.substring(0,p);
  }
  if(!(x=d[n]) && d.all) {
		x=d.all[n]; 
	}
	for (i=0; !x && i<d.forms.length; i++) {
		x=d.forms[i][n];
	}
  for(i=0; !x&&d.layers && i<d.layers.length; i++) {
		x=getObj(n,d.layers[i].document);
	}
  if(!x && d.getElementById) {
		x=d.getElementById(n); 
	}
	return x;
}

function getThingForPreview(thing,isrichtext)
{
	var parentPage = window.dialogArguments;
	if (isrichtext)
	{
		stuff = parentPage.mycontent_minirich.document.body.innerHTML;
	}
	else
	{
		stuff = eval('parentPage.document.theform.' + thing + '.value');
	}
	stuff = replace(stuff,'../media/',"media/");
	document.write(stuff);
}

function replace(argvalue, x, y) {
  if ((x == y) || (parseInt(y.indexOf(x)) > -1)) {
    errmessage = "replace function error: \n";
    errmessage += "Second argument and third argument could be the same ";
    errmessage += "or third argument contains second argument.\n";
    errmessage += "This will create an infinite loop as it's replaced globally.";
    alert(errmessage);
    return false;
  }
    
  while (argvalue.indexOf(x) != -1) {
    var leading = argvalue.substring(0, argvalue.indexOf(x));
    var trailing = argvalue.substring(argvalue.indexOf(x) + x.length, 
	argvalue.length);
    argvalue = leading + y + trailing;
  }

  return argvalue;
}


function emailCheck(emailStr) {
	var emailPat = /^([.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;
	var matchArray = emailStr.match(emailPat);
	if (matchArray == null) {
		return false;
	}

	var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
	if (IPArray != null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
      }
    }
	}
	return true;
}


function addtoorder(theid) {
	theQ = getObj('quantity|' + theid).value;
	getObj('addbasketid').value = theid;
	getObj('addbasketQuantity').value = theQ;
	getObj('action').value = 'addbasket';
	document.products.submit();
}

function InStr(Start, String1, String2, Compare) {
  if (Start < 1) {
    alert("Invalid Start argument\n\nInStr function (js2vb.js)"); return "";
  }
  if (Start > Len(String1)) return 0;
  if (Len(String2) == 0) return Start;
  if (Compare == 1) {String1 = LCase(String1); String2 = LCase(String2);}
  if (Start > 1) {
    var index = Right(String1, Len(String1) - Start + 1).indexOf(String2)
    if (index == -1) {return 0;} else {return index + Start;}
  } else {
    return String1.indexOf(String2) + 1
  }
}

function replace(argvalue, x, y) {
  if ((x == y) || (parseInt(y.indexOf(x)) > -1)) {
    errmessage = "replace function error: \n";
    errmessage += "Second argument and third argument could be the same ";
    errmessage += "or third argument contains second argument.\n";
    errmessage += "This will create an infinite loop as it's replaced globally.";
    alert(errmessage);
    return false;
  }
    
  while (argvalue.indexOf(x) != -1) {
    var leading = argvalue.substring(0, argvalue.indexOf(x));
    var trailing = argvalue.substring(argvalue.indexOf(x) + x.length, 
	argvalue.length);
    argvalue = leading + y + trailing;
  }

  return argvalue;
}

//-----------------------------------------------------------------------------
function UploadFile(mediaPath, previewFileName, HiddenFieldName, formName)
{
    var left = Math.floor((screen.width - 400) / 2);
    var top = Math.floor((screen.height - 200) / 2);
    mywindow = window.open('upload_file.asp', 'upload', 'top=' + top + ',left=' + left + ',status=yes,resizable=no,width=400,height=200');
    mywindow.location.href = 'customscripts/systemfunctions/upload_file.asp?thepath=' + mediaPath + '&previewFileName=' + previewFileName + '&HiddenFieldName=' + HiddenFieldName + '&formName=' + formName;
    if (mywindow.opener == null)
    {
        mywindow.opener = self;
    }
}

//-----------------------------------------------------------------------------
function DeleteCurricAtt(userID)
{
	var agree = confirm('Are you sure?');
	if (agree)
    {
		location.href='term-curriculum(delete)_' + userID + '.htm'
	}
}

//-----------------------------------------------------------------------------
function DeleteClassworkAtt(id, attID, userID)
{
	var agree = confirm('Are you sure?');
	if (agree)
    {
		location.href='classwork(action+deleteclasswork|id+' + id + '|attID+' + attID + ')_' + userID + '.htm'
	}
}

//-----------------------------------------------------------------------------
function DeleteClasswork(id, userID)
{
	var agree = confirm('Are you sure?');
	if (agree)
    {
		location.href='classwork(action+delete|id+' + id + ')_' + userID + '.htm'
	}
}

//-----------------------------------------------------------------------------
function DeleteHomeworkAtt(id, attID, userID)
{
	var agree = confirm('Are you sure?');
	if (agree)
    {
		location.href='homework(action+deletehomework|id+' + id + '|attID+' + attID + ')_' + userID + '.htm'
	}
}

//-----------------------------------------------------------------------------
function DeleteHomework(id, userID)
{
	var agree = confirm('Are you sure?');
	if (agree)
    {
		location.href='homework(action+delete|id+' + id + ')_' + userID + '.htm'
	}
}

//FORUM FUNCTIONS
//----------------------------------------------------------------------
function chkmsg() {
	var bad='';
	if (getObj('name').value == '')
		bad += 'Name\n';

	if (getObj('email').value == '')
		bad += 'Email\n';

	if (getObj('msgtitle')) {
		if (getObj('msgtitle').value == '')
			bad += 'Title\n';
	}

	if (getObj('msg').value == '')
		bad += 'Message\n';
	
	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		document.postmsg.submit();
	}
}

//----------------------------------------------------------------------
function OpenImageWindow2(imgid)
{
	openCenteredWindow('customscripts/imagePreview.asp?imgid=' + imgid, 600, 600, null, 'status=yes, resizable=yes, scrollbars=yes');
}

//----------------------------------------------------------------------
function openCenteredWindow(url, height, width, name, parms) 
{
   var left = Math.floor( (screen.width - width) / 2);
   var top = Math.floor( (screen.height - height) / 2);
   var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
   if (parms) { winParms += "," + parms; }
   var win = window.open(url, name, winParms);
   if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

//-----------------------------------------------------------------------------
function gotochangepass() {
	javascript:getObj('changepass').value=1;
	document.user.submit();
}

//-----------------------------------------------------------------------------
function checknewpass() {
	var bad='';
	if (getObj('newpass1').value.length < 5) {
		bad += 'PassWord must be 5 characters in Length\n';
	}
	if (getObj('newpass2').value.length < 5) {
		bad += 'Confirm PassWord must be 5 characters in Length\n';
	}
	if (getObj('newpass1').value != getObj('newpass2').value) {
		bad += 'Passwords do not match';
	}
	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		document.changepass.submit();
	}
}


//-----------------------------------------------------------------------------
function checkUserDetails(oldPassword)
{
	if (!checkPassword(oldPassword))
	{
		return false;
	}

	// Validate input fields
    var strError = '';
	if (getObj('UserName').value == '')
	{
		strError += 'User Name / Email\n';
	}
	if (getObj('RealName').value == '')
	{
		strError += 'Real Name\n';
	}
	if (getObj('Password') == '') 
	{
		strError += 'Password\n';
	}

	if (strError != '') 
	{
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + strError);
	}
	else
	{
		document.user.submit();
	}
}

//-----------------------------------------------------------------------------
function displayDivForTime(theDiv, nMilliSecs)
{
	getObj(theDiv).style.display = 'block';
	setTimeout('getObj(\'' + theDiv + '\').style.display = \'none\';', nMilliSecs);
}

//-----------------------------------------------------------------------------
function IsNumeric(thefield) 
{
	sText = getObj(thefield).value;
	var ValidChars = "0123456789.";
	var IsNumber = true;
	var Char;
	
	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{
		Char = sText.charAt(i);
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}

	return IsNumber;
}

//-----------------------------------------------------------------------------
function SearchGo()
{
	if (getObj('keyword') != null)
    {
        if (getObj('keyword').value.length >= 3)
        {
	        document.search.submit();
        }
        else
        {
		    alert('Please enter a Search Keyword of at least 3 characters in length');
	    }
	}
}

//-----------------------------------------------------------------------------
function OpenImageWindow(imgid) 
{
	openCenteredWindow('customscripts/imagePreview.asp?imgid=' + imgid, '500', '500', null, 'status=yes, resizable=yes, scrollbars=yes');
}

//-----------------------------------------------------------------------------
function CheckStaffDetails()
{
    var bad = '';
	if (getObj('RealName').value == '')
    {
		bad += 'Real Name\n';
    }
	if (getObj('JobTitle').value == '')
    {
		bad += 'Job Title\n';
    }
    if (getObj('Portfolio').value == '')
    {
		bad += 'About You\n';
    }

    if (bad != '')
    {
		msg = 'The following fields must be complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	}
    else
    {
		document.staff_details.submit();
	}
}

//-----------------------------------------------------------------------------
function CheckClassDetails()
{
    var bad = '';
    if (getObj('ClassName').value == '')
    {
		bad += 'Class Name\n';
    }
	if (getObj('AgeGroup').value == '')
    {
		bad += 'Age Group\n';
    }
	if (getObj('ClassSize').value == '')
    {
		bad += 'Class Size\n';
    }

    if (bad != '')
    {
		msg = 'The following fields must be complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	}
    else
    {
		document.class_details.submit();
	}
}

//-----------------------------------------------------------------------------
function CheckCurriculumDetails()
{
    var bad = '';
    if (getObj('Title').value == '')
    {
		bad += 'Document Title\n';
    }
    if (getObj('Doc').value == '')
    {
		bad += 'Curriculum Document\n';
    }

    if (bad != '')
    {
		msg = 'The following fields must be complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	}
    else
    {
		document.curriculum.submit();
	}
}

//-----------------------------------------------------------------------------
function CheckWorkDetails()
{
    var bad = '';
    if (getObj('Title').value == '')
    {
		bad += 'Title\n';
    }
    if (getObj('Description').value == '')
    {
		bad += 'Description\n';
    }

    if (bad != '')
    {
		msg = 'The following fields must be complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	}
    else
    {
		document.classwork.submit();
	}
}

//-----------------------------------------------------------------------------
function CheckHomeworkDetails()
{
    var bad = '';
    if (getObj('Title').value == '')
    {
		bad += 'Title\n';
    }

    if (bad != '')
    {
		msg = 'The following fields must be complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	}
    else
    {
		document.homework.submit();
	}
}