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 uploadimage(thepath) {
    mywindow=open('upload.asp','upload','status=yes,resizable=no,width=400,height=200');
    mywindow.location.href = 'customscripts/upload.asp?thepath=' + thepath;
    if (mywindow.opener == null) mywindow.opener = self;
}

//FORUM FUNCTIONS
//----------------------------------------------------------------------
function chkmsg() {
	var strError='';
	if (getObj('name').value == '')
		strError += 'Name\n';

	if (getObj('email').value == '')
		strError += 'Email\n';

	if (getObj('msgtitle')) {
		if (getObj('msgtitle').value == '')
			strError += 'Title\n';
	}

	if (getObj('msg').value == '')
		strError += 'Message\n';
	
	if (strError != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + strError);
	} else {
		document.postmsg.submit();
	}
}

//----------------------------------------------------------------------
function OpenImageWindow(strImage, strDesc) 
{
	openCenteredWindow('customscripts/imagePreview.asp?img=' + strImage + '&desc=' + strDesc, '500', '500', 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 DisplayDivForTime(theDiv, nMilliSecs)
{
	getObj(theDiv).style.display = 'block';
	setTimeout('getObj(\'' + theDiv + '\').style.display = \'none\';', nMilliSecs);
}

//-----------------------------------------------------------------------------
function CheckPassword()
{
	var strError = '';
	if (getObj('newpass1').value != getObj('newpass2').value)
	{
		strError += 'Passwords do not match';
	}

	var varGo = getObj('go');
	if (strError != '')
	{
		strErrorMsg = 'Your password could not be changed. See the details below:\n'
		strErrorMsg += '-------------------------------------------------------------------\n'
		strErrorMsg += strError;
		alert(strErrorMsg);

		varGo.value = '';
	}
	else
	{
		varGo.value = '1';
		document.changepass.submit();
	}
}

//-----------------------------------------------------------------------------
function ShowInfo(divID)
{
	var theDiv = getObj(divID)
	theDiv.style.display = 'block';
}

//-----------------------------------------------------------------------------
function HideInfo(divID)
{
	var theDiv = getObj(divID)
	theDiv.style.display = 'none';
}


// Opens Help window
//-----------------------------------------------------------------------------
function openhelp(id)
{
	var winl = (screen.width - 800) / 2;
	var wint = (screen.height - 550) / 2; 
    mywindow = open('help.asp','Help','channelmode=no,scrollbars=yes,status=yes,resizable=yes,width=800,height=550,top=' + wint + ',left=' + winl);
	if (mywindow != null)
	{
		mywindow.location.href = 'help_' + id + '.htm';
		mywindow.focus();
	}
}

// Opens Help window from body onhelp
//-----------------------------------------------------------------------------
function openHelpWindow(id)
{
	openhelp(id);
}

//Open Modal Window
//------------------------------------------------------------------------------
function openModalWindow(url,width,height)
{
	var paramspassed = new Array();
	if(arguments.length>3)
		for(i=3;i<arguments.length;i++)
		paramspassed[paramspassed.length] = arguments[i];

	if(window.showModalDialog) 
	{
    	var return_value = window.showModalDialog(url,paramspassed,"dialogWidth:"+width+"px;dialogHeight:"+height+"px;resizable=yes;status:no;help:no;");
		return return_value;
	}
	else
	{
		var modalWin = window.open(url,"","width="+width+"px,height="+height+"px;");
		window.onfocus = function() {
			if(modalWin && !modalWin.closed)
				modalWin.focus();
		}
		window.paramspassed = paramspassed;
	}
}

//Login and Change Password Focus
//------------------------------------------------------------------------------
function LoadPage()
{
	// Set the focus to the username input area
	objLogin = document.forms('login');
	if (objLogin != null)
	{
	    objUser = objLogin.item('csc_user');
	    objUser.focus;
	    objUser.setActive();
	}

    objChangePassword = document.forms('changepass');
    if (objChangePassword != null)
    {
        objCurrent = objChangePassword.item('newpass1');
        objCurrent.setActive();
	    objCurrent.focus;
    }
    
	return false;
}

function TrapTab(nIndex)
{
    // Only do this for tab's
    if (event.keyCode != 9)
    {
        return true;
    }
    else
    {
        // Set the focus to the next control in the tab order
        switch(nIndex)
        {
            case 2:
                document.all("csc_login").focus();
                break;
            case 3:
                document.all("csc_user").focus();
                break;
            default:
                document.all("csc_pass").focus();
                break;
        }
        
        // cancel the event since we handled it
        event.cancelBubble = true;
        return false;
    }
}

function TrapTabChange(nIndex)
{
    // Only do this for tab's
    if (event.keyCode != 9)
    {
        return true;
    }
    else
    {
        // Set the focus to the next control in the tab order
        switch(nIndex)
        {
            case 1:
                document.all("newpass1").focus();
                break;
            case 2:
                document.all("newpass2").focus();
                break;
            case 3:
                document.all("continue").focus();
                break;
            case 4:
                document.all("current").focus();
                break;
            default:
                document.all("newpass1").focus();
                break;
        }
        
        // cancel the event since we handled it
        event.cancelBubble = true;
        return false;
    }
}

function setshowimage(action)
{
	if (action == 'hide')
	{
		getObj('show').src = 'customscripts/systemfunctions/showhidemenu.asp?showhide=hide';
	}
	else
	{
		getObj('hide').src = 'customscripts/systemfunctions/showhidemenu.asp?showhide=show';
	}
}

function showhidemenu(action, seltheme) 
{
    if (action == 'hide') {
        getObj('menuopen').style.display = 'none';
        getObj('body').style.backgroundImage = 'url(themes/' + seltheme + '/bg2.gif)';
        getObj('menutd').style.width = '17px';
        getObj('menuclose').style.display = 'block';
    }
    else {
        getObj('menuopen').style.display = 'block';
        getObj('body').style.backgroundImage = 'url(themes/' + seltheme + '/bg.gif)';
        getObj('menutd').style.width = '215px';
        getObj('menuclose').style.display = 'none';
    }
}

function CheckEnter(event,frm)
{
    var code = 0;
    
    if (event.keyCode) code = event.keyCode;
	else if (event.which) code = event.which;
        
    if (code==13)
    { 
        frm.submit();
        event.returnValue = false;
    }
}
