﻿// ExternalInterface.js

/***
 * Formerly inline JavaScript from index.html page
 */

var addthis_logo_color 		= 'ffffff';
var addthis_options         = 'favorites, email, digg, delicious, myspace, facebook, google, live, more';

var globalHomePageXML; // for home page xml
var globalPrimNavXML; // for prim nav xml

function addBookmark(pub,imagePath,options,title,url,w,h){
	var aturl  = 'http://www.addthis.com/bookmark.php';

 	aturl += '?v=10';
 	aturl += '&pub='+pub;
	aturl += '&url='+encodeURIComponent(url);
	aturl += '&title='+encodeURIComponent(title);
	aturl += '&logo='+encodeURIComponent(imagePath);
	aturl += '&logocolor='+addthis_logo_color;
	
	window.open(aturl,'addthis','scrollbars=yes,menubar=no,width='+w+',height='+h+',resizable=yes,toolbar=no,location=no,status=no,screenX=200,screenY=100,left=200,top=100');	
}
 
function getServerDomain() {
    return document.domain;
}

function updateDocumentTitle(title) {
    window.document.title=title;
}

function openBrowserPopup(page,name) {
    if (navigator.appName == 'Netscape') {
        popWindow = window.open(page,name);
        popWindow.focus();
    } 

    else if (navigator.appName == 'Microsoft Internet Explorer') {
        popWindow = window.open(page,name);
        popWindow.focus();
    }

    else {
        popWindow = window.open(page,name);
        popWindow.focus();
    }
}

function colorSmartPopup(page,name,w,h,scroll) {
    if (navigator.appName == 'Netscape') {
        popWindow = window.open(page,name,'width='+w+',height='+h+',resizable=no,menubar=no,status=no,scrollbars='+scroll+',toolbar=no,directories=no,location=no,screenX=0,screenY=0');
        popWindow.focus();
    } 

    else if (navigator.appName == 'Microsoft Internet Explorer') {
        popWindow = window.open(page,name,'width='+w+',height='+h+',resizable=no,menubar=no,status=no,scrollbars='+scroll+',toolbar=no,directories=no,location=no,top=0,left=0');
        popWindow.focus();
    }

    else {
        popWindow = window.open(page,name,'width='+w+',height='+h+',resizable=no,menubar=no,status=no,scrollbars='+scroll+',toolbar=no,directories=no,location=no,top=0,left=0');
        popWindow.focus();
    }
}

function getUserAgent(){
    return navigator.userAgent;
}

function getBrowser(){
    return navigator.appName;
}

function getBrowserVersion(){
    //return parseFloat(navigator.appVersion);
    var nVer = navigator.appVersion;
    var nAgt = navigator.userAgent;
    var browserName  = '';
    var fullVersion  = 0; 
    var majorVersion = 0;
	
    // In Internet Explorer, the true version is after "MSIE" in userAgent
    if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
        browserName  = "Microsoft Internet Explorer";
        fullVersion  = parseFloat(nAgt.substring(verOffset+5));
        majorVersion = parseInt(''+fullVersion);
    }
	
    // In Opera, the true version is after "Opera" 
    else if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
        browserName  = "Opera";
        fullVersion  = parseFloat(nAgt.substring(verOffset+6));
        majorVersion = parseInt(''+fullVersion);
    }
	
    // In Firefox, the true version is after "Firefox" 
    else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
        browserName  = "Firefox";
        fullVersion  = parseFloat(nAgt.substring(verOffset+8));
        majorVersion = parseInt(''+fullVersion);
    }
	
    // In most other browsers, "name/version" is at the end of userAgent 
    else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) ) 
    {
        browserName  = nAgt.substring(nameOffset,verOffset);
        fullVersion  = parseFloat(nAgt.substring(verOffset+1));
        if (!isNaN(fullVersion)) majorVersion = parseInt(''+fullVersion);
        else {fullVersion  = 0; majorVersion = 0;}
    }
	
    // Finally, if no name and/or no version detected from userAgent...
    if (browserName.toLowerCase() == browserName.toUpperCase()
        || fullVersion==0 || majorVersion == 0 )
    {
        browserName  = navigator.appName;
        fullVersion  = parseFloat(nVer);
        majorVersion = parseInt(nVer);
    }
    return fullVersion;
	
}

function getPlatform(){
    return navigator.platform;
}

function getBrowserLanguage(){
    return navigator.browserLanguage;
}

function getFlashVersion(){
    return GetSwfVer();
}

function closePopUp() {
    document.getElementById('flexObject').closePopUp();
}

var checkoutHostName = '';

function postXML(payload, hostname) {
	//alert(hostname);//http://devstore.behr.com/index.asp
	//document.getElementById('iframe_iFrame').contentWindow.postXML(payload);
	var checkoutForm = document.getElementsByTagName("form")[0];
	var checkoutField = document.getElementsByTagName("textarea")[0];
	var checkoutFrameField = document.getElementById('iFrameSrcId');
	var checkoutFrame = document.getElementById('iframe_iFrame');
	if (location.href.indexOf("swf") != -1) {
		var checkoutFrameFieldValue=location.href.substring(0,location.href.lastIndexOf("/")+1) + 'iframecheckout.html#';
	} else {
		var checkoutFrameFieldValue='http://' + document.domain + '/swf/' + 'iframecheckout.html#'; 
	}
	//checkoutForm.action = 'http://devstore.behr.com/cartServices.asp';
	checkoutHostName = hostname;
	checkoutForm.action = hostname; 
	checkoutField.value = '<?xml version="1.0" encoding="UTF-8" ?>' + payload;
	checkoutFrameField.value=checkoutFrameFieldValue;
	checkoutForm.submit();
}

document.write('<form method="post" style="display: none;" target="iframe_iFrame">');
document.write('<input type="hidden" name="iFrameSrc" id="iFrameSrcId" value="">');
document.write('<textarea name="XMLData"></textarea>');
document.write('<input type="text" name="service" value="checkout">');
document.write('</form>');

function postConfirmationStep(hostname) {
	//used for posting the last confirmation page
	if(hostname==undefined){
		//donothing
	}else{
		var checkoutForm = document.getElementsByTagName("form")[0];
		var checkoutFrameField = document.getElementById('iFrameSrcId');
		var checkoutFrame = document.getElementById('iframe_iFrame');
		if (location.href.indexOf("swf") != -1) {
			var checkoutFrameFieldValue=location.href.substring(0,location.href.lastIndexOf("/")+1) + 'iframecheckoutconfirmation.html#';
		} else {
			var checkoutFrameFieldValue='http://' + document.domain + '/swf/' + 'iframecheckoutconfirmation.html#'; 
		}
		//checkoutForm.action = 'http://devstore.behr.com/checkout5.asp';
		checkoutHostName = hostname;
		checkoutForm.action = hostname; 
		checkoutFrameField.value=checkoutFrameFieldValue;
		checkoutForm.submit();
	}
}

function bvLoginAuth(urlParam) {
	document.getElementById('flexObject').bvLoginAuth(urlParam);
}
function bvLoginAuthTest() {
	document.getElementById('flexObject').bvLoginAuthTest();
}

function bvClosePopUp() {
	document.getElementById('flexObject').bvClosePopUp();
}

function getCheckoutDomain() {
	return checkoutHostName.split('/')[2];
}

function getCheckoutURL() {
	return checkoutHostName;
}

function goHome() {
	document.getElementById('flexObject').goHome();
}

function openWorkbook(userId) {
    document.getElementById('flexObject').openWorkbook(userId);
}

function openProject(userId, projectId) {
    document.getElementById('flexObject').openProject(userId, projectId);
}

function loginUser(userId) {
    document.getElementById('flexObject').loginUser(userId);
}

function registerUser(userId) {
    document.getElementById('flexObject').registerUser(userId);
}

function registerPypUser(userId,main,accent1,accent2,white,paletteType) {
    document.getElementById('flexObject').registerPypUser(userId,main,accent1,accent2,white,paletteType);
}

function orderSamples(userId,projectId,main,accent1,accent2,white) {
    document.getElementById('flexObject').orderSamples(userId,projectId,main,accent1,accent2,white);
}

function forgotPassword() {
    document.getElementById('flexObject').forgotPassword();
}

function navigateToShoppingCart() {
	document.getElementById('flexObject').navigateToShoppingCart();
}

function openShoppingCart(userId) {
    document.getElementById('flexObject').openShoppingCart(userId);
}

function startPypFromCheckout() {
	document.getElementById('flexObject').startPypFromCheckout();
}

function openPrivacyPolicy() {
	document.getElementById('flexObject').openPrivacyPolicy();
}

function openOrderFaqs() {
	document.getElementById('flexObject').openOrderFaqs();
}

function createOrder(orderId) {
	document.getElementById('flexObject').createOrder(orderId);
}

function getCookie(name) {
    var cookieContent = new String();
	
    if(document.cookie.length > 0) {
        var cookieName = name + '=';
        var cookieStart = document.cookie.indexOf(cookieName);
        var cookieEnd = 0;
		
        if (cookieStart > -1) {
            cookieStart += cookieName.length;
            cookieEnd = document.cookie.indexOf(";",cookieStart);
		
            if (cookieEnd < cookieStart) { 
                cookieEnd = document.cookie.length; 
            }
		
            cookieContent = document.cookie.substring(cookieStart,cookieEnd);
        }
    }
    return unescape(cookieContent);
}

function buildDimmerDiv()
{
    document.write('<div class="wrapper2">');    
    document.write('<div class="wrapper">');
    document.write('<h2><img src="/cma/System_Content/Site_Graphics/htmlView/homepage_flash.jpg" alt="Welcome to Behr" usemap="#HTMLlink"/></h2>');  
    document.write('</div>');    
    document.write('</div>');
    document.write('<map name="HTMLlink" id="HTMLlink">');
    document.write('<area shape="rect" coords="732,286,887,323" href="http://www.adobe.com/go/getflashplayer" target="_blank" alt="Get ADOBE Flash Player" />');
    document.write('<area shape="rect" coords="472,339,833,367" href="/Behr/home" alt="HTML Site" />');
    document.write('</map>');
    document.write('');     
    document.write(''); 
    document.write(''); 
    document.write(''); 
    //setTimeout("document.getElementById('content').style.display='none'", 5000);
    //setTimeout("document.getElementById('header').style.display='none'", 5000);
    //setTimeout("document.getElementById('footer').style.display='none'", 5000);
    
}

// creating XMLHttpRequest instance depending upon browser type
function createXMLHttpRequest() {
   try { return new XMLHttpRequest(); } catch(e) {}   
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   alert("This browser does not support XMLHttpRequest");
   return null;
 }

 // Home XML response
 function onResponseHome() {
   if (xhReqHome.readyState != 4)  { return; }
   var serverResponseHome = xhReqHome.responseXML;
   var Xml2StringHome;                                                                                             // Convert the xml to string just to display it 
   if (serverResponseHome.xml) {
        Xml2StringHome=serverResponseHome.xml                                                                            // Converts the xml object to string  (  For IE)
   }else{
       Xml2StringHome= new XMLSerializer().serializeToString(serverResponseHome);                        // Converts the xml object to string (For rest browsers, mozilla, etc)
   }
   globalHomePageXML = Xml2StringHome;
   var flexHomeOb = document.getElementById('flexObject');
   if(flexHomeOb != null){
      try{
         flexHomeOb.callHome(globalHomePageXML);
      }catch(e) {}
   }
 }

 // prim nav response
 function onResponsePrimNav() {
   if (xhReqPrimNav.readyState != 4)  { return; }
   var serverResponsePriNav = xhReqPrimNav.responseXML;
   var Xml2StringPriNav;                                                                                        
   if (serverResponsePriNav.xml) {
       Xml2StringPriNav=serverResponsePriNav.xml                                                                           
   }else{
       Xml2StringPriNav= new XMLSerializer().serializeToString(serverResponsePriNav); 
   }
    globalPrimNavXML = Xml2StringPriNav;   
    var flexPrimOb = document.getElementById('flexObject');
    if(flexPrimOb != null){
         try{
          flexPrimOb.callPrimNav(globalPrimNavXML);
         }catch(e) {}
    }
 }

var xhReqHome = createXMLHttpRequest();
var xhReqPrimNav = createXMLHttpRequest();

function getGlobalHomePageXML() {
	return globalHomePageXML;
}
function getGlobalPrimNavXML() {
	return globalPrimNavXML;
}

// Version check for the Flash Player that has the ability to start Player Product Install (6.0r65)
var hasProductInstall = DetectFlashVer(6, 0, 65);

// Version check based upon the values defined in globals
var hasRequestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion,requiredRevision);

if ( hasProductInstall && !hasRequestedVersion ) {
    // DO NOT MODIFY THE FOLLOWING FOUR LINES
    // Location visited after installation is complete if installation is required
	//alert("install flash");
    var MMPlayerType = (isIE == true) ? "ActiveX" : "PlugIn";
    var MMredirectURL = window.location;
    document.title = document.title.slice(0, 47) + " - Flash Player Installation";
    var MMdoctitle = document.title;

    AC_FL_RunContent(
    "src", "/swf/playerProductInstall",
    "FlashVars", "MMredirectURL="+MMredirectURL+'&MMplayerType='+MMPlayerType+'&MMdoctitle='+MMdoctitle+"",
    "width", "100%",
    "height", "100%",
    "align", "middle",
    "id", "index",
    "quality", "high",
    "bgcolor", "#dddcd6",
    "name", "index",
    "allowScriptAccess","sameDomain",
    "type", "application/x-shockwave-flash",
	"wmode","opaque",
    "pluginspage", "http://www.adobe.com/go/getflashplayer"
);
} else if (hasRequestedVersion) {
	//alert("loading flex");
    // if we've detected an acceptable version
    // embed the Flash Content SWF when all tests are passed

	xhReqHome.open("GET", "http://"+getServerDomain()+"/Behr/home?siteView=Flex&version="+swfVersion, true);
	xhReqHome.onreadystatechange = onResponseHome;
	xhReqHome.send(null);	 
		
	xhReqPrimNav.open("GET","http://"+getServerDomain()+"/Behr/site_menu?siteView=Flex&version="+swfVersion, true);
	xhReqPrimNav.onreadystatechange = onResponsePrimNav;
	xhReqPrimNav.send(null);

    AC_FL_RunContent(
    "src", "/swf/index_"+swfVersion,
    "FlashVars", "vgnextoid="+vgnextoid+'&view='+view+'&channel='+channel+"",
    "width", "100%",
    "height", "100%",
    "align", "middle",
    "id", "flexObject",
    "quality", "high",
    "bgcolor", "#dddcd6",
    "name", "flexObject",
    "allowScriptAccess","sameDomain",
    "type", "application/x-shockwave-flash",
	"wmode","opaque",
    "pluginspage", "http://www.adobe.com/go/getflashplayer"
);
    //document.write('<style>body { margin: 0px; overflow:hidden }</style>'); 
    //setTimeout("alert (document.getElementById('htmlwrapper'))", 5000);
	//setTimeout("document.getElementById('htmlwrapper').style.display='none'", 3000);
	// this line causes issues with IE6/7, we assume this can't be done while elements of the div are still loading.
	//setTimeout('document.body.removeChild(document.getElementById("htmlwrapper"))', 5000);
    var turnOffHTMLDisplay=true;
	document.write('<style>* {margin: 0px; overflow:hidden;} body {margin: 0px; overflow:hidden; width:100%; height:100%}</style>'); 
    //setTimeout("document.getElementById('htmlcrawablewrapper').style.display='none'", 5000);
} else {
    var isViewableTemplate = isViewableTemplate;
    var isMgmtStage = isMgmtStage;
    if (isViewableTemplate!=null) { 
        //Track Webtrends HTML user
        setTimeout("document.getElementById('flexwrapper').style.display='none'", 1000);
	    dcsMultiTrack('DCS.dcsuri', '/html_user.html', 'WT.ti', 'HTML Site User', 'DCSext.type','HTML');
    } else {
        if (isMgmtStage) {
            //do nothing if on mgmt stage
        } else {
            //buildDimmerDiv();
            window.location = noFlashErrorPageURI;
        } 
    }
}
