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) {
	var parentPage = window.dialogArguments;
	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 validateshoppage() {
	var bad='';
	if (getObj('address').value == '') {
		bad += 'Address\n';
	}
	/*if (getObj('postcode').value == '') {
		bad += 'Postcode\n';
	}*/
	if (getObj('tel').value == '') {
		bad += 'Telephone No\n';
	}
	if (getObj('mon_open').value == '') {
		bad += 'Monday Opening Time\n';
	}
	if (getObj('tue_open').value == '') {
		bad += 'Tuesday Opening Time\n';
	}
	if (getObj('wed_open').value == '') {
		bad += 'Wednesday Opening Time\n';
	}
	if (getObj('thu_open').value == '') {
		bad += 'Thursday Opening Time\n';
	}
	if (getObj('fri_open').value == '') {
		bad += 'Friday Opening Time\n';
	}
	if (getObj('sat_open').value == '') {
		bad += 'Saturday Opening Time\n';
	}
	if (getObj('sun_open').value == '') {
		bad += 'Sunday Opening Time\n';
	}
	if (getObj('mon_close').value == '') {
		bad += 'Monday Closing Time\n';
	}
	if (getObj('tue_close').value == '') {
		bad += 'Tuesday Closing Time\n';
	}
	if (getObj('wed_close').value == '') {
		bad += 'Wednesday Closing Time\n';
	}
	if (getObj('thu_close').value == '') {
		bad += 'Thursday Closing Time\n';
	}
	if (getObj('fri_close').value == '') {
		bad += 'Friday Closing Time\n';
	}
	if (getObj('sat_close').value == '') {
		bad += 'Saturday Closing Time\n';
	}
	if (getObj('sun_close').value == '') {
		bad += 'Sunday Closing Time\n';
	}


	if (bad != '') {
		msg = 'The following required fields are not complete.\n'
		msg += '--------------------------------------------------------\n'
		alert(msg + bad);
	} else {
		document.shopwebpage.submit();
	}
	
}


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;
}




function validatepack() {
	var bad='';
	if (getObj('name').value == '')
		bad += 'Name\n';

	if (getObj('address').value == '')
		bad += 'Address\n';

	/*if (getObj('postcode').value == '')
		bad += 'Postcode\n';*/

	if (emailCheck(getObj('email').value) == false) 
		bad += 'Email\n';

	if (getObj('telephone').value == '')
		bad += 'Telephone\n';

	if (getObj('howhear').selectedIndex == 0)
		bad += 'How did you Hear\n';
		
	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		document.downfp.submit();
	}
	
}

function validatepricecheck() {
	var bad='';
	if (getObj('name').value == '')
		bad += 'Name\n';

	if (getObj('make').value == '')
		bad += 'Make\n';

	if (getObj('model').value == '')
		bad += 'Model\n';

	if (emailCheck(getObj('email').value) == false) 
		bad += 'Email\n';

	if (getObj('telephone').value == '')
		bad += 'Telephone\n';

	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		document.pricecheck.submit();
	}
	
}

function validateemailorder() {
	var bad='';
	if (getObj('name').value == '')
		bad += 'Name\n';

	if (getObj('invoiceaddress').value == '')
		bad += 'Invoice Address\n';

	/*if (getObj('postcode').value == '')
		bad += 'Postcode\n';*/

	if (bad != '') {
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + bad);
	} else {
		document.emailform.submit();
	}
}

function checkadditionaladdress() {
	var bad='';
	if (emailCheck(getObj('emailaddress').value + '@12ink.com') == false) 
		bad += 'Email';

	if (bad != '') {
		msg = 'The email address you have specified is Invalid.\n'
		alert(msg);
	} else {
		document.chooseemail.submit();
	}
}

//FORUM FUNCTIONS
//----------------------------------------------------------------------
function chkmsg() {
	var bad='';
	if (getObj('name').value == '')
		bad += 'Name\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();
	}
}
//----------------------------------------------------------------------
//SUPPORT
//----------------------------------------------------------------------

function checkNewSupport()
{
	// Validate input fields
    var strError = '';
	if (getObj('s_title').value == '')
	{
		strError += 'Title\n';
	}

	if (getObj('s_description').value == '')
	{
		strError += 'Description\n';
	}

	if (strError != '') 
	{
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + strError);
	}
	else
	{
		document.addsupport.submit();
	}
}

function checkSupportPost(bAdmin)
{
	// Validate input fields
    var strError = '';
	if (getObj('s_description').value == '')
	{
		strError += 'Description\n';
	}

	if (bAdmin == 'True')
	{
		if (getObj('s_timespent').value == '')
		{
			strError += 'Time Taken\n';
		}
	}

	if (strError != '') 
	{
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + strError);
	}
	else
	{
		document.supportpost.submit();
	}
}

function displayReplyBox()
{
	// Hide the button
	getObj('replyboxbtn').style.display = 'none';

	// Display the form
	getObj('replybox').style.display = 'block';
}

//-----------------------------------------------------------------------------
function GetProductTypes(productType, product)
{
    // Run script to fill combo with product types
    RunScript('/customscripts/get_product_types.asp?pt=' + productType + '&p=' + product, 'product_type_div', 'manufacturers');
}

//-----------------------------------------------------------------------------
function GetProducts(manufacturer, product)
{
    // Run script to fill combo with product types
    RunScript('/customscripts/get_products.asp?m=' + manufacturer + '&p=' + product, 'products_div', 'product_type');
}

//-----------------------------------------------------------------------------
function LoadProductTypes(manufacturer, productType, product)
{
    if (manufacturer != '')
    {
        GetProductTypes(productType, product);
    }
}

//-----------------------------------------------------------------------------
function LoadProducts(manufacturer, productType, product)
{
    if (productType != '')
    {
        GetProducts(manufacturer, product);
    }
}

//-----------------------------------------------------------------------------
function Search()
{
    var errors = '';
    
    var manufacturer = getObj('manufacturers').value;
    if (manufacturer == '')
    {
        //errors += '- Manufacturer\n';
    }
    
    var product_type_hid = getObj('product_type_hid');
    product_type_hid.value = getObj('product_type').value;
    if (product_type_hid.value == '')
    {
        //errors += '- Printer\n';
    }
    
    var product_name_hid = getObj('product_name_hid');
    product_name_hid.value = getObj('product_name').value;
    if (product_name_hid.value == '')
    {
        //errors += '- Model No.\n';
    }
    
    if (errors == '')
    {
        document.forms.search.submit();
    }
    else
    {
        errors = 'Just a sec! Please select the following:\n' + errors;
        
        alert(errors);
    }
}

//-----------------------------------------------------------------------------
function AddToBasket(productID)
{
    var addProduct = getObj('add_product_' + productID);
    addProduct.value = '1';
    
    document.forms.basket.action = document.forms.basket.action + '#' + productID
    
    document.forms.basket.submit();
}

//-----------------------------------------------------------------------------
function UpdateQuantities()
{
    getObj('F_action').value = 'UpdateBasket';
    document.order.submit();
}

//-----------------------------------------------------------------------------
function RemoveItems()
{
    getObj('F_action').value = 'RemoveItems';
    document.order.submit();
}

//-----------------------------------------------------------------------------
function ConfirmOrder()
{
    var strError = '';
	if (getObj('FirstName').value == '')
	{
		strError += '- First Name\n';
	}
	if (getObj('Surname').value == '')
	{
		strError += '- Surname\n';
	}
	if (getObj('Email').value == '' || emailCheck(getObj('Email').value) == false) 
	{
		strError += '- Email Address - missing or invalid\n';
	}
    if (getObj('Tel').value == '')
	{
		strError += '- Phone Number\n';
	}
	if (getObj('Street1').value == '')
	{
		strError += '- Invoice Street Address\n';
	}
	if (getObj('City').value == '')
	{
		strError += '- Invoice City\n';
	}
	if (getObj('Postcode').value == '')
    {
	    strError += '- You must enter your Invoice Post Code.\n';
    }

    if (getObj('delivery_diff').style.display == 'none')
    {
        if (getObj('DelStreet1').value == '')
	    {
		    strError += '- Delivery Street1\n';
	    }
	    if (getObj('DelCity').value == '')
	    {
		    strError += '- Delivery City\n';
	    }
	    if (getObj('DelPostcode').value == '')
        {
	        strError += '- You must enter your Delivery Post Code.\n';
        }
    }

	if (strError != '') 
	{
		msg = 'The following fields are not complete.\n'
		msg += '----------------------------------------------\n'
		alert(msg + strError);
	}
	else
	{
		getObj('F_action').value = 'finishorder';
        document.order.submit();
	}
}

//-----------------------------------------------------------------------------
function DisplayDeliveryAddress()
{
    getObj('delivery_diff').style.display = 'none';
    getObj('delivery_address').style.display = 'block';
    getObj('del_address_displayed').value = '1';
}

//----------------------------------------------------------------------
function OpenImageWindow(img, desc) 
{
	OpenCenteredWindow('/customscripts/imagePreview.asp?img=' + img + '&desc=' + desc, '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();
    }
}

//-----------------------------------------------------------------------------
// Ajax functions
//-----------------------------------------------------------------------------
function RunScript(strScript, strDivName, strFieldName)
{
    // strDivName is the div to fill with what is response.written in the script
    // strFieldName is the field to get info from to put into the q=
    var xmlHttpReq = false;
    var self = this;
    if (window.XMLHttpRequest)
    {   // Mozilla / Safari / IE7
        self.xmlHttpReq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {   // IE
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strScript, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function()
    {
        if (self.xmlHttpReq.readyState == 4)
        {
            UpdatePage(self.xmlHttpReq.responseText, strDivName);
        }
    }
    self.xmlHttpReq.send(GetQueryString(strFieldName));
}

//-----------------------------------------------------------------------------
function UpdatePage(strInnerHTML, strDivName)
{
    var result = getObj(strDivName);
    if (result != null)
    {
        result.innerHTML = strInnerHTML;
        if (strInnerHTML == '')
        {
            result.style.display = 'none';
        }
        else
        {
            result.style.display = 'block';
        }
    }
}

//-----------------------------------------------------------------------------
function GetQueryString(strFieldName)
{
    var strQuery = '';
    var field = getObj(strFieldName);
    if (field != null)
    {
        var strValue = field.value;
        strValue = Replace(strValue, '+', 'PLUS_SIGN');
        strQuery = 'q=' + escape(strValue) + '';  // NOTE: no '?' before query string
    }
    return strQuery;
}

//-----------------------------------------------------------------------------
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;
}