﻿// JavaScript File

// MainMaster
var text = 0;
var textCounter = 0;
var timer = 0;
var numText = 0;
var lineLength = 0;
var scrollWidth = 0;
var blockWidth = 0;
var position = 0;
var pause = 0;
var pauseCounter = 0;
var path = new Array(90);
var pIndex = 89;
var speed = 0;
var point = false;
  
// Bredde, pause, fart
function initScroller(w, p, s) {
width = w;
pause = p;
speed = s;
numText = text.length - 1;
textCounter = Math.round(numText*Math.random());
precalcPath();
changeText();
document.getElementById("pn_scrolltext").style.left = position + "px";        
scroll();
}

function precalcPath() {
var v = 0;
for (i = 0; i < 90; i++) {
    v = ((i + 180) / 180) * Math.PI;
    path[i] = Math.sin(v);
    }
}

function changeText() {

//Bytt tekst på tekstblokk 1 og øk tekstteller
document.getElementById("text1").innerHTML = text[textCounter];
textCounter++;        

//Sett linjelengde
blockWidth = document.getElementById("text1").offsetWidth;
if(blockWidth < width) {
  lineLength = width;
  document.getElementById("text1").style.marginRight = (width - blockWidth) + "px";
}
else {
  lineLength = blockWidth + width;
  document.getElementById("text1").style.marginRight = width + "px";
}

//Bytt tekst på tekstblokk2
document.getElementById("text11").innerHTML = text[textCounter];

if (textCounter >= numText) {
  textCounter = 1;
}

document.getElementById("pn_scrolltext").style.width = (lineLength + document.getElementById("text11").offsetWidth) + "px";
}

function scroll() {

if (point == false) {
  if (position < -lineLength) {
    clearTimeout(timer);
    delay();
    changeText();
    pIndex = 89;
    position = 0;
    document.getElementById("pn_scrolltext").style.left = position + "px";
  }
  else if (position < (-lineLength + 57)) {
    document.getElementById("pn_scrolltext").style.left = position + "px";
    position += path[pIndex];
    pIndex--;
    timer = setTimeout("scroll()", speed);
  }        
  else {
    document.getElementById("pn_scrolltext").style.left = position + "px";
    position--;
    timer = setTimeout("scroll()", speed);
  }
}
else {
  timer = setTimeout("scroll()", speed);        
}
}

function delay() {
pauseCounter ++;
if (pauseCounter == pause) {        
  clearTimeout(timer);
  pauseCounter = 0;
  scroll()
}
else {
  timer = setTimeout("delay()", pause);
}
}

function stop() {
clearTimeout(timer);
}

// GlobalSearch - min ett filtrerings alt avmerket
function MinimumCheck1(box)
{
    if(document.getElementById(box).type != "checkbox")
        return;
    if (!document.getElementById("ctl00_cphMain_chbPreferred").checked && !document.getElementById("ctl00_cphMain_chbSalesRep").checked && !document.getElementById("ctl00_cphMain_chbAgencies").checked)
    {   
        document.getElementById(box).checked = true;
        alert("At least one company type must be selected. Try again.");
    }
}

// GlobalSearch - min ett filtrerings alt avmerket
function MinimumCheck2(box)
{
    if(document.getElementById(box).type != "checkbox")
        return;
    if (!document.getElementById("ctl00_cphMain_chbShipping").checked && !document.getElementById("ctl00_cphMain_chbOffshore").checked && !document.getElementById("ctl00_cphMain_chbFishery").checked)
    {   
    document.getElementById(box).checked = true;
    alert("At least one market segment must be selected. Try again.");
    }
}

// SetFocus
function SetFocus()
{
    // mainMaster
    if (document.getElementById("ctl00_txtSearch") != null)
    document.getElementById("ctl00_txtSearch").focus();
    // Suppliers/SupplierList
    if (document.getElementById("ctl00_cphMain_ddlCountry") != null)
    document.getElementById("ctl00_cphMain_ddlCountry").focus();
    // GlobalSearch
    if (document.getElementById("ctl00_cphMain_txtGlobalSearch") != null)
    document.getElementById("ctl00_cphMain_txtGlobalSearch").focus();
    // Product/ProductList
    if (document.getElementById("ctl00_cphMain_txtProdName") != null)
    document.getElementById("ctl00_cphMain_txtProdName").focus();
    // Product/Suppliers
    if (document.getElementById("ctl00_ctl00_cphMain_cphMenu_txtProdDesc") != null)
    document.getElementById("ctl00_ctl00_cphMain_cphMenu_txtProdDesc").focus();
}			

// onkeydown - "text" id og "button" id
function txtOnKeyDown(evid, buttonId)
{
    preKeyDown();
    document.getElementById(buttonId).click();
}

// onkeydown
function preKeyDown()
{
    window.event.CancelBubble = true;
    window.event.returnValue = false;
}

// Print/SupplierWebCode.aspx:
	
function CheckJSVersion(scriptEngineVersion)
{
    var version;
    if("undefined" == typeof ScriptEngine) 
    {
	    version = 1;
    }	
    else 
    {
	    version = scriptEngineVersion;
    }
    return version;
}

function ChangeHREF(scriptEngineVers)
{
	var HTMLCode;
	if(CheckJSVersion(scriptEngineVers) >= 5)
	{
		HTMLCode=document.getElementsByName("txtTextBefore")[0].value	+" ";
		HTMLCode+="<A style='cursor: hand' target='_blank' HREF="+GetHREF()+">";
		HTMLCode+=document.getElementsByName("txtLinkText")[0].value+"</A>";
		HTMLCode+=" "+document.getElementsByName("txtTextAfter")[0].value;
		document.getElementById("htmlText").innerHTML=HTMLCode;
		document.getElementsByName("htmlRaw")[0].value=HTMLCode;
	}
	else
	{
	    try
	    {
		    HTMLCode=document.getElementsByName("txtTextBefore")[0].value	+" ";
		    HTMLCode+="<A style='cursor: hand' target='_blank' HREF="+GetHREF()+">";
		    HTMLCode+=document.getElementsByName("txtLinkText")[0].value+"</A>";
		    HTMLCode+=" "+document.getElementsByName("txtTextAfter")[0].value;
	    }
	    catch (e)
	    {
		    HTMLCode="Couldn't generate code due to error: "+e.message;
	    }
        finally
        {
		    try
		    {
			    document.getElementById("htmlText").innerHTML=HTMLCode;
			    document.getElementsByName("htmlRaw")[0].value=HTMLCode;
		    }
		    catch (e)
		    {
				alert("Couldn't write out code to screen due to error: "+e.message);
		    }
	    }
	}
}

function GetHREF()
{
	return document.getElementById("txtHref").value+"/Supplier/InfoSupp.aspx?Company="+document.getElementById("txtCompanyID").value;
}

function CopyLogoLink(scriptEngineVers)
{
	if(CheckJSVersion(scriptEngineVers) >= 5)
	{
        try
        {
	        window.clipboardData.setData("Text",document.getElementsByName("logoHTML")[0].value);
	        alert("Code copied to clipboard. Content: \n\n"+window.clipboardData.getData("Text"));
        }
        catch (e)
        {
	        alert("Couldn't copy to clipboard. Use manual cut and paste from the textbox.");
        }
    }
    else
    {
	    window.clipboardData.setData("Text",document.getElementsByName("logoHTML")[0].value);
	    alert("Code copied to clipboard. Content: \n\n"+window.clipboardData.getData("Text"));
	}
    
}

function CopyTextLink(scriptEngineVers)
{
	if(CheckJSVersion(scriptEngineVers) >= 5)
	{
	    try
	    {
		    window.clipboardData.setData("Text",document.getElementsByName("htmlRaw")[0].value);
		    alert("Code copied to clipboard. Content: \n\n"+window.clipboardData.getData("Text"));
	    }
	    catch (e)
	    {
		    alert("Couldn't copy to clipboard. Use manual cut and paste from the textbox.");
	    }
	}
	else
	{
		window.clipboardData.setData("Text",document.getElementsByName("htmlRaw")[0].value);
		alert("Code copied to clipboard. Content: \n\n"+window.clipboardData.getData("Text"));
	}
}

function ChangeLOGO(scriptEngineVers)
{
	var logoHTML;
	if(CheckJSVersion(scriptEngineVers) >= 5)
	{
	    try
	    {
		    var logoHTML="<IMG style='cursor: hand' height='"+document.getElementsByName("txtLogoSize")[0].value+"' ";
		    logoHTML+="onclick=\"javascript:window.open('"+GetHREF();
		    logoHTML+="', '_blank', 'height=600, width=800, resizable=yes, scrollbars=yes')\" ";
		    logoHTML+=" title='Click here to find "+document.getElementById("txtCompanyName").value+" at IMIOnline'";
		    logoHTML+=" src='"+document.getElementById("txtLogoFile").value+"'>";
			
	    }
	    catch (e) 
	    {
		    logoHTML="Couldn't generate code due to error: "+e.message;
	    }
	    finally
	    {
		    try
		    {
			    document.getElementById("logoView").innerHTML=logoHTML;
			    document.getElementsByName("logoHTML")[0].value=logoHTML;
		    }
		    catch (e)
		    {
			    alert("Couldn't write out code to screen due to error: "+e.message);
		    }
	    }
	}
	else
	{
		var logoHTML="<IMG style='cursor: hand' height='"+document.getElementsByName("txtLogoSize")[0].value+"' ";
		logoHTML+="onclick=\"javascript:window.open('"+GetHREF();
		logoHTML+="', '_blank', 'height=600, width=800, resizable=yes, scrollbars=yes')\" ";
		logoHTML+=" title='Click here to find "+document.getElementById("txtCompanyName").value+" at IMIOnline'";
		logoHTML+=" src='"+document.getElementById("txtLogoFile").value+"'>";
		document.getElementById("logoView").innerHTML=logoHTML;
		document.getElementsByName("logoHTML")[0].value=logoHTML;
	}
}

// Product/Suppliers.aspx:

function ProductSearch(searchString)
{
    var taTags = document.getElementsByTagName("textarea");
    var textRange;
    var count = 0;
    
    for(i = 0; i < taTags.length; i++)
    {
        textRange = taTags[i].createTextRange();

        if(taTags[i].id == "prodName")
        {       
            if(textRange.findText(searchString))
            {        
                count ++;
                textRange.parentElement().style.color = "red";
                textRange.scrollIntoView();
            }
            else
                textRange.parentElement().style.color = "#003399";
        }
    }
 
    alert(count + " product(s) found (highlighted in red).");
}

// Webshop/Shop.aspx
function UpdateValues(c, p)
{
i = 0
if (p == "LineItems")
    i = 1;
    quantity = c[i+2].getElementsByTagName('input')[0].value;
    unitprice = c[i+4].getElementsByTagName('input')[0].value;
    unitprice = unitprice.replace(",",".");
    quantity = quantity.replace(",",".");
    newTotal= quantity *  unitprice ;
	if(isNaN(newTotal))
		{
		alert("Not valid");
		quantity = "1";
		c[i+2].getElementsByTagName('input')[0].value = "1";
		newTotal= quantity *  unitprice ;
		}
	 c[i+5].getElementsByTagName('input')[0].value = newTotal;
}

//CheckBrowser version
function CheckBrowser()
{
    //CheckPopupSettings();
    //RedirectPage();
	var version;
	if("undefined" == typeof ScriptEngine) 
	{
		version = 1;
	}	
	else 
	{
		version = ScriptEngineMajorVersion();
	}
	if(version >= 5)
	{
		try
		{
			CheckBrowserSettings();
		}
		catch (e)
		{
			messageText = "Your browser settings couldn't be determined due to "+e.message+" It cannot be ";
			messageText += "verified that the browser is compatible with www.maritime-suppliers.com . Browser information: ";
			messageText += navigator.userAgent;
			alert(messageText)
		}
	}
	else
	{
		CheckBrowserSettings();
	}
}

function CheckBrowserSettings()
{
	var messageText;
	var agt=navigator.userAgent.toLowerCase();
	var isGecko = (agt.indexOf("gecko") != -1);

	var isMajorVer = parseInt(navigator.appVersion);
	var isIE = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var isIE5Up = (isIE && (CheckIEVersion() >= 5));
	var isOpera = (agt.indexOf("opera") != -1);
	var isOpera5Up = (isOpera && (CheckOperaVersion() >= 5));
	
	var isNavigator = (agt.indexOf("mozilla") != -1);
	var isNavigator4Up = (isNavigator && (isMajorVer >= 4));
		
	var browserOK = (isOpera5Up || isIE5Up || isGecko || isNavigator4Up);
	if(!browserOK)
	{
		messageText = "It may seem as your browser is not supported in www.maritime-suppliers.com. ";
		messageText += "Supported browsers include IE5 or above, Gecko, Mozilla 4 or above and Opera 5 and above. ";
		messageText += "You may continue but may encounter problems. Detected browser info: ";
		messageText += navigator.appName + ", version " + GetVersion(isGecko,isIE,isOpera,isNavigator);
		alert(messageText);
	}
}

function GetVersion(isGecko, isIE, isOpera, isNavigator)
{
if(isGecko)
	return navigator.appVersion;
else if(isIE)
	return CheckIEVersion();
else if(isOpera)
	return CheckOperaVersion();
else if(isNavigator)
	return navigator.appVersion;
}
function CheckIEVersion()
{
	var agt = navigator.userAgent;
    var isIE3 = ((agt.indexOf("MSIE 3") != -1) || (agt.indexOf("MSIE/3") != -1));
    var isIE4 = ((agt.indexOf("MSIE 4") != -1) || (agt.indexOf("MSIE/4") != -1));
    var isIE5 = ((agt.indexOf("MSIE 5") != -1) || (agt.indexOf("MSIE/5") != -1));
    if(isIE3)
		return 3;
	else if(isIE4)
		return 4;
	else if(isIE5)
		return 5;
	else
		return 6;
}
function CheckOperaVersion()
{
    var isOpera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
    var isOpera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
    var isOpera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
    if(isOpera2)
		return 2;
	else if(isOpera3)
		return 3;
	else if(isOpera4)
		return 4;
	else
		return 5;
}    	    	
function CheckPopupSettings()
{
var objChild;  // Window
    try 
    {
	    objChild = window.open('','child','width=0,height=0,status=no,resizable=yes'); 
	    objChild.close();
    }
    catch(e)
    {
           alert("You seem to have an active popup blocker. Disable this for www.maritime-suppliers.com to use all features. There are different ways of doing this depending on what kind of pop-up blocker you are using. Most browsers shows a message when a pop-up is detected and you may then allow it temporary or always. In general it is possible to add trusted sites to a browser, typically through Tools > Security > Trusted sites. Add 'http://www.maritime-suppliers.com' to this trusted sites list.");
    }
}

function btnCopyTextLink_click() {
    var scEngVer = ScriptEngineMajorVersion();
    CopyTextLink(scEngVer);
}
function btnCopyLogoLink_click() {
    var scEngVer = ScriptEngineMajorVersion();
    CopyLogoLink(scEngVer);
}
function txtLogoSize_keyup() {
    var scEngVer = ScriptEngineMajorVersion();
    ChangeLOGO(scEngVer);
}

function changeHref_keyup() {
    //if (event.srcElement.id == "txt_TextBefore" || event.srcElement.id == "txt_LinkText" || event.srcElement.id == "txt_TextAfter")
    var scEngVer = ScriptEngineMajorVersion();
    ChangeHREF(scEngVer);
}
