//if(window.width<1280||window.height<1024){
//alert("This web page is best viewed with a screen resolution of 1280 by 1024 or higher.  Your current resolution is "+screen.width+" by "+screen.height+".  If possible please change your resolution.")
//document.write("<style>.productList{border:1px solid blue;}</style>")
//}
//else{
//alert("Whoa, you have a high resolution.  Nice Job!" + window.width)

//document.write("<style>.productList{border:1px solid red;}</style>")

//}



function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
	{
		window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined')
	{
		document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined')
	{
		window.attachEvent('onload', fn);
	}
	else
	{
		return false;
	}
	
	return true;
};




function attachEventListener(target, eventType, functionRef, capture)
{
    if (typeof target.addEventListener != "undefined")
    {
        target.addEventListener(eventType, functionRef, capture);
    }
    else if (typeof target.attachEvent != "undefined")
    {
        target.attachEvent("on" + eventType, functionRef);
    }
    else
    {
        return false;
    }

    return true;
};



checkBrowserWidth();

attachEventListener(window, "resize", checkBrowserWidth, false);


function checkBrowserWidth()
{
	var theWidth = getBrowserWidth();
	
	if (theWidth == 0)
	{
		var resolutionCookie = document.cookie.match(/(^|;)tmib_res_layout[^;]*(;|$)/);

		if (resolutionCookie != null)
		{
			setStylesheet(unescape(resolutionCookie[0].split("=")[1]));
		}
		
		addLoadListener(checkBrowserWidth);
		
		return false;
	}

	if (theWidth > 1000)
	{
	    //alert('test');
		setStylesheet("general");
		//document.cookie = "tmib_res_layout=" + escape("1024 x 768");
	}
	else
	{
	    //alert('test2');
		setStylesheet("general2");
		//document.cookie = "tmib_res_layout=";
	}
	
	return true;
};




function getBrowserWidth()
{
	if (window.innerWidth)
	{
		return window.innerWidth;
	}
	else if (document.documentElement && document.documentElement.clientWidth != 0)
	{
		return document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		return document.body.clientWidth;
	}
	
	return 0;
};



function setStylesheet(styleTitle)
{
	var currTag;

	if (document.getElementsByTagName)
	{
		for (var i = 0; (currTag = document.getElementsByTagName("link")[i]); i++)
		{
			if (currTag.getAttribute("rel").indexOf("style") != -1 && currTag.getAttribute("title"))
			{
				currTag.disabled = true;

				if(currTag.getAttribute("title") == styleTitle)
				{
					currTag.disabled = false;
				}
			}
		}
	}
	
	return true;
};











function clickButton(e, buttonid){ 
  var evt = e ? e : window.event;
  var bt = document.getElementById(buttonid);

  if (bt){ 
      if (evt.keyCode == 13){ 
         bt.click(); 
        return false; 
      } 
  } 
}


function addToFavorites(urlName, pageName) { 
    window.external.AddFavorite(urlName,pageName);
    return false;
}


function ShowAlternativeImage(imageName, clientID)    {
    
    document.getElementById(clientID).src = '/images/ecommerce/standard/' + imageName;
    //document['mainImage'].src = '/images/ecommerce/mini/' + imageName;
}

function zoomProductImage(productImage) {
    
	if (productImage) {
		window.open('/Ecommerce/zoom.aspx?image=' + productImage,'ZOOM','width=700,height=700,scrollbars=no,resize=no');
    }
    
    //[RK] 21/12/06 - triggered from ImageButton so MUST return false
    return false;
}

function fitPic() {
	if (window.innerWidth){
		iWidth = window.innerWidth;
		iHeight = window.innerHeight;
	}else{
		iWidth = document.body.clientWidth;
		iHeight =document.body.clientHeight;
	}
	
	iWidth = document.images[0].width - iWidth;
	iHeight = document.images[0].height - iHeight;
	window.resizeBy(iWidth, iHeight);
}

function copyAddress()  {
    //[RK] 27/03/2007.
    //Uses hidden fields to allow us to access ASP.NET controls from Javascript.
    
    var prefix = document.forms['aspnetForm'].elements['jsPrefix'].value;
    var oForm = document.forms['aspnetForm']

    oForm.elements[prefix + '_DeliveryLine1TextBox'].value = oForm.elements[prefix + '_Line1TextBox'].value
    oForm.elements[prefix + '_DeliveryLine2TextBox'].value = oForm.elements[prefix + '_Line2TextBox'].value
    oForm.elements[prefix + '_DeliveryLine3TextBox'].value = oForm.elements[prefix + '_Line3TextBox'].value
    oForm.elements[prefix + '_DeliveryTownTextBox'].value = oForm.elements[prefix + '_TownTextBox'].value
    oForm.elements[prefix + '_DeliveryCountyTextBox'].value = oForm.elements[prefix + '_CountyTextBox'].value
    oForm.elements[prefix + '_DeliveryPostcodeTextBox'].value = oForm.elements[prefix + '_PostcodeTextBox'].value
    oForm.elements[prefix + '_DeliveryPhoneNumberTextBox'].value = oForm.elements[prefix + '_PhoneNumberTextBox'].value
    oForm.elements[prefix + '_DeliveryCountryDropDownList'].selectedIndex = oForm.elements[prefix + '_CountryDropDownList'].selectedIndex

}

function ConfirmOrder() {
    
    var prefix = document.forms['aspnetForm'].elements['jsPrefix'].value;
    var oForm = document.forms['aspnetForm']
    //alert('prefix=' + prefix)
    
    
    var confirmButton =  document.getElementById(prefix + '_OrderOptionsPanelNextButton');
    var cvnNumber = document.getElementById(prefix + '_CVNTextBox').value;
    
    
    //If the issue number is valid too, then disable the button so we dont get multiple submissions
    if (cvnNumber.length == 3)  {
        confirmButton.src = '/images/buttons/pleasewait.gif';
        confirmButton.style.cursor = 'wait';
        //confirmButton.onclick.value = '';
        return true;
        
        //oForm.submit();

        //confirmButton.disabled = true;
    }
}

function showSubCategoryFeed(categoryID)  {

    //alert('test' + categoryID);
}


function ViewProductSpecSheet(variantID, productID)    {
   // alert(productID);

	if (productID) {
		window.open('/Ecommerce/ProductSpecSheet.aspx?ID=' + variantID + '&productID=' + productID, 'SPECSHEET','width=450,height=700,scrollbars=yes,resize=no');
    }
}

function GlossaryTerm(term) {

	if (term) {
		window.open('/Glossary/Term.aspx?term=' + term, 'Glossary','width=450,height=350,scrollbars=yes,resize=no');
    }
}


function SubmitSecureIFrame() {
    document.forms['secureForm'].submit()
}

function test(productID, originalButton) {
    alert('productID=' + productID);
    //var prm = Sys.WebForms.PageRequestManager.getInstance(); 
    //prm._doPostBack(originalButton,'');

    var modalPopupBehavior = $find('programmaticModalPopupBehavior');            
    modalPopupBehavior.show(); //show the MPE first       
    $find("AEFadeIn").get_OnClickBehavior().play(); //play the animation      
    
      
}

function runAnimation() {
    alert('eunning');

    var modalPopupBehavior = $find('programmaticModalPopupBehavior');            
    modalPopupBehavior.show(); //show the MPE first       
    $find("AEFadeIn").get_OnClickBehavior().play(); //play the animation      
    
      
}   


//Populates a hidden field with all the selected orders from the admin salessummary page.
function populateHiddenField(orderID, clientID, checkboxClientID)   {
    document.getElementById(clientID).value += '|' + orderID;

    //alert(document.getElementById(clientID).value)
}

//used on checkout.aspx
function textCounter(field, countfield, maxlimit) {
    if (document.getElementById(field).value.length > maxlimit) // if too long...trim it!
        document.getElementById(field).value = document.getElementById(field).value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else 
        //document.getElementById(countfield).value = maxlimit - document.getElementById(field).value.length;
        document.getElementById(countfield).innerHTML = maxlimit - document.getElementById(field).value.length;
}

//home grid mouseover function. Uses JQuery on default.aspx
function SetHomePromo(areaID, price, packSize, textLinkID, URLLink) {
    document.getElementById('boxImageLink_' + areaID).href = URLLink;

    document.getElementById('boxPrice_' + areaID).innerHTML = price.toFixed(2);

    document.getElementById('boxPricePackMessage_' + areaID).innerHTML = 'for ' + packSize;

    document.getElementById(textLinkID).href = URLLink;
}

//sets up the first selected item for the given box grid area
function SetHomeFirstItem(areaID, price, packSize, textLinkID, URLLink, imageURL) {
    document.getElementById('boxImageLink_' + areaID).href = URLLink;

    document.getElementById('boxPrice_' + areaID).innerHTML = price.toFixed(2);

    document.getElementById('boxPricePackMessage_' + areaID).innerHTML = 'for ' + packSize;

    document.getElementById(textLinkID).href = URLLink;

    document.getElementById('boxImage_' + areaID).src = imageURL;
}

//Auto creates a page urlname from the incoming string. Used in admin (Page editing etc)
function createPageURLName(theString, clientID) {

    var pageURLField = document.getElementById(clientID);
    var outstring

    outstring = theString.split(' ').join('');
    outstring = outstring.split('&').join('&amp;');
    outstring = outstring.split('.').join('');
    outstring = outstring.split('?').join('');
    outstring = outstring.split('!').join('');
    outstring = outstring.split(',').join('');
    outstring = outstring.split('"').join('');
    outstring = outstring.split('/').join('');
    outstring = outstring.split('@').join('');
    outstring = outstring.split('*').join('');
    outstring = outstring.split('%').join('');

    pageURLField.value = outstring.toLowerCase();
}


//Allows pushing of into into a Ajax HTML editor
//See http://yasserzaid.wordpress.com/2009/12/18/ajax-html-editor-and-validation/ for example code.
function InsertPrintedItemsInfoTable(textToAdd, clientID) {
    //get the editor instance
    var a = $find(clientID);
    //alert(a);
    
    //get current value if you need to
    //var value = a.get_content();
    //alert(value);

    //set new value!
    a.set_content(textToAdd);
}

//Allows pushing of into into a Ajax HTML editor
//See http://yasserzaid.wordpress.com/2009/12/18/ajax-html-editor-and-validation/ for example code.
//Leaves [COLOURS] tag at the end so the next colour can append this etc.
function InsertPrintedItemsColour(colorToAdd, clientID) {
    //get the editor instance
    var a = $find(clientID);
    //alert(a);

    //get current value if you need to
    var value = a.get_content();
    //alert(value);

    //set new value!
    a.set_content(value.replace('[COLOURS]', '<img class="printedColourIcon" src="/images/printedProducts/colours/' + colorToAdd + '.gif"/> [COLOURS]'));
}

function ScrollToTop() {
    scroll(0, 0);
}

function PopupContentWindow(pageName) {
    if (pageName) {
        window.open('/ContentPopup.aspx?pageURLName=' + pageName, 'POPUP', 'width=450,height=350,scrollbars=yes,resize=no');
    }
}

//Adds/removes css class from video panel. Doesnt use display=none or display=block as this messes up flow player
function ShowProductVideo(imagePanelClientID, videoPanelClientID) {
    //if (document.getElementById(videoPanelClientID).style.visibility == "hidden") {
        //show videos

        document.getElementById(imagePanelClientID).style.visibility = "hidden";
        document.getElementById(imagePanelClientID).style.height = "0px";

        //enable video panel css.
        document.getElementById(videoPanelClientID).className = "videoHolder";
        document.getElementById(videoPanelClientID).style.visibility = "visible";
        document.getElementById(videoPanelClientID).style.height = "258px";
    //}
}

//Adds/removes css class from video panel. Doesnt use display=none or display=block as this messes up flow player
function ShowImages(imagePanelClientID, videoPanelClientID) {
    if (document.getElementById(imagePanelClientID).style.visibility == "hidden") {
        //show image

        document.getElementById(videoPanelClientID).className = "";
        document.getElementById(videoPanelClientID).style.visibility = "hidden";
        document.getElementById(videoPanelClientID).style.height = "0px";

        document.getElementById(imagePanelClientID).style.visibility = "visible";
        document.getElementById(imagePanelClientID).style.height = "300px";
    }
}

//mouseflow. included here so is included by default in every page.
document.write(unescape("%3Cscript src='" + (("https:" == document.location.protocol) ? "https" : "http") + "://a.mouseflow.com/projects/bccdb0f2-4d7c-4f27-af53-b39ed672ddca.js' type='text/javascript'%3E%3C/script%3E"));
