﻿// JScript File
// common JS function collections

// -- Get the Browser Name/Version/OS (START)
var BrowserDetect = {
    init: function () {
	    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
	    this.version = this.searchVersion(navigator.userAgent)
		    || this.searchVersion(navigator.appVersion)
		    || "an unknown version";
	    this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
	    for (var i=0;i<data.length;i++)	{
		    var dataString = data[i].string;
		    var dataProp = data[i].prop;
		    this.versionSearchString = data[i].versionSearch || data[i].identity;
		    if (dataString) {
			    if (dataString.indexOf(data[i].subString) != -1)
				    return data[i].identity;
		    }
		    else if (dataProp)
			    return data[i].identity;
	    }
    },
    searchVersion: function (dataString) {
	    var index = dataString.indexOf(this.versionSearchString);
	    if (index == -1) return;
	    return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
	    {
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
	    },
	    { 	string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
	    },
	    {
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
	    },
	    {
		    prop: window.opera,
		    identity: "Opera"
	    },
	    {
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
	    },
	    {
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
	    },
	    {
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
	    },
	    {
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
	    },
	    {		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
	    },
	    {
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
	    },
	    {
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
	    },
	    { 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
	    }
    ],
    dataOS : [
	    {
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
	    },
	    {
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
	    },
	    {
		       string: navigator.userAgent,
		       subString: "iPhone",
		       identity: "iPhone/iPod"
        },
	    {
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
	    }
    ]

};

BrowserDetect.init();
// -- Get Browser Name/Version/OS (END)

// -- GOOGLE MAP SECTION (start)
var geocoder;
var map;
function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(0, 0);
    var myOptions = {
      zoom: 17,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

function codeAddress(str_address) {
    var address = str_address;//document.getElementById("address").value;
    if (geocoder) {
        geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
          });
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
}
// -- GOOGLE MAP SECTION (END)

function ufSetTxt(aoTf,asMode,asTxt) {
    var lsValue="";
    
    lsValue = (aoTf.value).toLowerCase();
    
    if (asMode.toLowerCase()=='clear') {
        if (lsValue==asTxt.toLowerCase()) {
            aoTf.value = "";
        }
    } else if (asMode.toLowerCase()=='set') {
        if (lsValue=="") {
            aoTf.value = asTxt;
        }
    } else {
        // temporary use alert message.
        // for standard, use ERROR message container.
        alert("Validation failed. Contact VibrantStyle.");
    }
}

function ufLoadContent(asTopic) {
    var loForm = document.forms[0];
    
    loForm.Uc_header1_hfContent.value = asTopic
    loForm.action = "content_page.aspx";
    loForm.submit();
}

function ufLoadContentDirect(asMode, asSecNaviID, asSecSubOneID, asContenTitle) {
    var loForm = document.forms[0];
    
    loForm.Uc_header1_hfContentMode.value = asMode;
    loForm.Uc_header1_hfContent.value = asSecNaviID;
    loForm.Uc_header1_hfContentSubOneID.value = asSecSubOneID;
    loForm.Uc_header1_hfContentTitle.value = asContenTitle;
    loForm.action = "content_page.aspx";
    loForm.submit();
}
// [mk 20111101 : pass the "subTwoID" for loading the content , (used for loading "video" at home panel)]
function ufLoadContentDirectSubTwo(asMode, asSecNaviID, asSecSubOneID, asSecSubTwoID) {
    var loForm = document.forms[0];
    loForm.Uc_header1_hfContentMode.value = asMode;
    loForm.Uc_header1_hfContent.value = asSecNaviID;
    loForm.Uc_header1_hfContentSubOneID.value = asSecSubOneID;
    loForm.Uc_header1_hfContentSubTwoID.value = asSecSubTwoID;
    loForm.action = "content_page.aspx";    
    loForm.submit();
}

function ufPropertyBanner(asFolderName, asBannerImage) {
    if (document.getElementById("idContentMainBanner")) {
        document.getElementById("idContentMainBanner").src = asFolderName + "/" + asBannerImage;
    }
}

function ufSubShowHide(asIDTr, asIDImgButton, asIDSPAN) {
    var lsImgSrc, lsGetSpanID, lsDisplayType;
    var liCntr;
    var larrObj = new Array();
    var lsBrowserType = BrowserDetect.browser;
    var lsBrowserVersion = BrowserDetect.version;
    
    lsGetSpanID = "";
    liCntr = 0;

    // -- set ROW display type based on browser type/version (START)
    if (lsBrowserType.toLowerCase() == "explorer") {
        if (lsBrowserVersion == "5.5" || lsBrowserVersion == "6") {
            lsDisplayType = "";
        } else if (lsBrowserVersion == "7") {
            lsDisplayType = "block";
        } else {
            lsDisplayType = "table-row";
        }
    } else if (lsBrowserType.toLowerCase() == "firefox") {
        lsDisplayType = "table-row";
    } else {
        lsDisplayType = "";
    }
    // -- set ROW display type based on browser type/version (END)

    lsImgSrc = document.getElementById(asIDImgButton).src;
    if (document.getElementById(asIDTr)) {
        if (lsImgSrc.indexOf("arrow_right.jpg") > 0) {
            document.getElementById(asIDImgButton).src = "images/button/arrow_down.jpg"
            document.getElementById(asIDTr).style.display = lsDisplayType;
        } else if (lsImgSrc.indexOf("arrow_down.jpg") > 0) {
            document.getElementById(asIDImgButton).src = "images/button/arrow_right.jpg"
            document.getElementById(asIDTr).style.display = "none";

            // close the sub menu
            ufCloseAllSub(asIDTr);
            
        } else {
            document.getElementById(asIDImgButton).src = "images/button/arrow_down.jpg"
            document.getElementById(asIDTr).style.display = lsDisplayType;
        }
    }

    document.getElementById(asIDSPAN).style.color = "#6d0e01"
    document.getElementById(asIDSPAN).style.fontWeight = "bold";
        
    // set the rest of the ITEM to TEXT color black   ;
    larrObj = document.getElementsByTagName("span");
    for(liCntr=0; liCntr < larrObj.length; liCntr++)
    {
        var lsGetSpanID = document.getElementsByTagName("span").item(liCntr).id
        if (lsGetSpanID.indexOf("idSPAN") >= 0) {
            if (lsGetSpanID != asIDSPAN) {
                document.getElementById(lsGetSpanID).style.color = "#333333";
                document.getElementById(lsGetSpanID).style.fontWeight = "normal";
            }
        }
    }
}

function ufOpenFirstSub(asIDTr) {
    var lsDisplayType;
    var liCntr;
    var lsBrowserType = BrowserDetect.browser;
    var lsBrowserVersion = BrowserDetect.version;

    liCntr = 0;

    // -- set ROW display type based on browser type/version (START)
    if (lsBrowserType.toLowerCase() == "explorer") {
        if (lsBrowserVersion == "5.5" || lsBrowserVersion == "6") {
            lsDisplayType = "";
        } else if (lsBrowserVersion == "7") {
            lsDisplayType = "block";
        } else {
            lsDisplayType = "table-row";
        }
    } else if (lsBrowserType.toLowerCase() == "firefox") {
        lsDisplayType = "table-row";
    } else {
        lsDisplayType = "";
    }
    // -- set ROW display type based on browser type/version (END)
    
    var lsSubOne = document.getElementById(asIDTr);

    // set the down arrow image
    var larrSubTwoImg = lsSubOne.childNodes[1].getElementsByTagName("img")
    for (liCntr = 0; liCntr < larrSubTwoImg.length; liCntr++) {
        var lsImgId = larrSubTwoImg.item(liCntr).id;
        if (lsImgId.indexOf("idNRRedTwo") >= 0) {

            var lsTrId = larrSubTwoImg.item(liCntr).parentNode.parentNode.nextSibling.nextSibling.id;
            if (lsTrId.indexOf("idSubTwo") >= 0) {
                // open tr
                document.getElementById(lsImgId).src = "images/button/arrow_down.jpg";
                document.getElementById(lsTrId).style.display = "";
            }
            break;
        }
    }

}
function ufCloseAllSub(asIDTr) {
    var liCntr;

    // close the tr
    var lsSubOne = document.getElementById(asIDTr);
    var larrSubTwo = lsSubOne.childNodes[1].getElementsByTagName("tr")
    for (liCntr = 0; liCntr < larrSubTwo.length; liCntr++) {
        var lsTrId = larrSubTwo.item(liCntr).id;
        if (lsTrId.indexOf("idSubTwo") >= 0) {
            document.getElementById(lsTrId).style.display = "none";
        }
    }

    // set the right arrow image
    var larrSubTwoImg = lsSubOne.childNodes[1].getElementsByTagName("img")
    for (liCntr = 0; liCntr < larrSubTwoImg.length; liCntr++) {
        var lsImgId = larrSubTwoImg.item(liCntr).id;
        if (lsImgId.indexOf("idNRRedTwo") >= 0) {
            document.getElementById(lsImgId).src = "images/button/arrow_right.jpg"
        }
    }
}

function ufLoadTopicContent(asMode, asMainID, asSubOneID, asSubTwoID, asSubThreeID) {
    if (asMode == "subone") {
        document.getElementById("ifrmContentPage").src = "content_topic.aspx?asSecMode=" + asMode + "&asSecMainID=" + asMainID + "&asSecSubOneID=" + asSubOneID;
    }

    if (asMode == "subtwo") {
        document.getElementById("ifrmContentPage").src = "content_topic.aspx?asSecMode=" + asMode + "&asSecSubOneID=" + asSubOneID + "&asSecSubTwoID=" + asSubTwoID;
    }
    
    if (asMode == "subthree") {
        document.getElementById("ifrmContentPage").src = "content_topic.aspx?asSecMode=" + asMode + "&asSecSubTwoID=" + asSubTwoID + "&asSecSubThreeID=" + asSubThreeID;
    }
}

function ufPropertyBannerParent(asFolderName, asBannerImage) {
    if (parent.document.getElementById("idContentMainBanner")) {
        parent.document.getElementById("idContentMainBanner").src = asFolderName + "/" + asBannerImage;
    }
}

function ufLoadTopicContentParent(asMode, asMainID, asSubOneID, asSubTwoID, asSubThreeID) {
    if (asMode == "subone") {
        window.parent.document.getElementById("ifrmContentPage").src = "content_topic.aspx?asSecMode=" + asMode + "&asSecMainID=" + asMainID + "&asSecSubOneID=" + asSubOneID;
    }

    if (asMode == "subtwo") {
        window.parent.document.getElementById("ifrmContentPage").src = "content_topic.aspx?asSecMode=" + asMode + "&asSecSubOneID=" + asSubOneID + "&asSecSubTwoID=" + asSubTwoID;
    }
    
    if (asMode == "subthree") {
        window.parent.document.getElementById("ifrmContentPage").src = "content_topic.aspx?asSecMode=" + asMode + "&asSecSubTwoID=" + asSubTwoID + "&asSecSubThreeID=" + asSubThreeID;
    }
}

function ufSubShowHideParent(asIDTr, asIDImgButton, asIDSPAN) {
    var lsImgSrc, lsGetSpanID;
    var liCntr;
    var larrObj = new Array();
    var lsBrowserType = BrowserDetect.browser;
    var lsBrowserVersion = BrowserDetect.version;
        
    lsGetSpanID = "";
    liCntr = 0;
    
    // -- set ROW display type based on browser type/version (START)
    if (lsBrowserType.toLowerCase() == "explorer") {
        if (lsBrowserVersion == "5.5" || lsBrowserVersion == "6") {
            lsDisplayType = "";
        } else if (lsBrowserVersion == "7") {
            lsDisplayType = "block";
        } else {
            lsDisplayType = "table-row";
        }
    } else if (lsBrowserType.toLowerCase() == "firefox") {
        lsDisplayType = "table-row";
    } else {
        lsDisplayType = "";
    }
    // -- set ROW display type based on browser type/version (END)

    lsImgSrc = window.parent.document.getElementById(asIDImgButton).src;
    if (window.parent.document.getElementById(asIDTr)) {
        if (lsImgSrc.indexOf("arrow_right.jpg") > 0) {
            window.parent.document.getElementById(asIDImgButton).src = "images/button/arrow_down.jpg"
            window.parent.document.getElementById(asIDTr).style.display = lsDisplayType;
        } else if (lsImgSrc.indexOf("arrow_down.jpg") > 0) {
            window.parent.document.getElementById(asIDImgButton).src = "images/button/arrow_right.jpg"
            window.parent.document.getElementById(asIDTr).style.display = "none";
        } else {
            window.parent.document.getElementById(asIDImgButton).src = "images/button/arrow_down.jpg"
            window.parent.document.getElementById(asIDTr).style.display = lsDisplayType;
        }
    }

    window.parent.document.getElementById(asIDSPAN).style.color = "#6d0e01"
    window.parent.document.getElementById(asIDSPAN).style.fontWeight = "bold";
        
    // set the rest of the ITEM to TEXT color black   ;
    larrObj = window.parent.document.getElementsByTagName("span");
    for(liCntr=0; liCntr < larrObj.length; liCntr++)
    {
        var lsGetSpanID = window.parent.document.getElementsByTagName("span").item(liCntr).id
        if (lsGetSpanID.indexOf("idSPAN") >= 0) {
            if (lsGetSpanID != asIDSPAN) {
                window.parent.document.getElementById(lsGetSpanID).style.color = "#333333";
                window.parent.document.getElementById(lsGetSpanID).style.fontWeight = "normal";
            }
        }
    }
}


function ufLoadPhotoBig(asPhotoBigID, asPhotoSmallID, asPhotoBig, asPhotoFolder) {
    document.getElementById(asPhotoBigID).src = asPhotoFolder + "/" + asPhotoBig;
}


// ------- auto resize iframe (START) ----------------------------------------------------
function getElement(aID) {
	return (document.getElementById) ?
	document.getElementById(aID) : document.all[aID];
}

function getIFrameDocument(aID) {
	var rv = null;
	var frame=getElement(aID);
	// if contentDocument exists, W3C compliant (e.g. Mozilla)
	if (frame.contentDocument)
		rv = frame.contentDocument;
	else // bad IE  ;)
		rv = document.frames[aID].document;
	return rv;
}

function adjustMyFrameHeight() {
	var frame = getElement("ifrmContentPage");
	var frameDoc = getIFrameDocument("ifrmContentPage");
	//frame.height = frameDoc.body.offsetHeight;
	frame.height = (frameDoc.getElementById("idContentTopic").offsetHeight) + 50;
}
// ------- auto resize iframe (END) ----------------------------------------------------

// ------- auto resize SITEMAP iframe (START) ----------------------------------------------------
function getSitemapElement(aID) {
	return (window.parent.document.getElementById) ?
	window.parent.document.getElementById(aID) : document.all[aID];
}

function getSitemapIFrameDocument(aID) {
	var rv = null;
	var frame=getSitemapElement(aID);
	// if contentDocument exists, W3C compliant (e.g. Mozilla)
	if (frame.contentDocument)
		rv = frame.contentDocument;
	else // bad IE  ;)
		rv = window.parent.document.frames[aID].document;
	return rv;
}

function adjustSitemapHeight() {
	var frame = getSitemapElement("ifrmContentPage");
	var frameDoc = getSitemapIFrameDocument("ifrmContentPage");
	//frame.height = frameDoc.body.offsetHeight;
	frame.height = (frameDoc.getElementById("idContentTopic").offsetHeight) + 50;
}
// ------- auto resize SITEMAP iframe (END) ----------------------------------------------------


// this function will get the keycode
function getKeyCode(e) {
	if (window.event) {
		return window.event.keyCode;
	} else if (e) {
		return e.which;
	} else {
		return null;
	}
}
// this function will either allow numeric or alphabet entry only.
function keyRestrict(e, validchars) {
	var key='', keychar='';
	key = getKeyCode(e);

	if (key == null) return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	validchars = validchars.toLowerCase();
	if (validchars.indexOf(keychar) != -1) {
		return true;
	}
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 ) {
		return true;
	}

	return false;
}

// this function will validate email entry.
function uf_IsEmail(input) {
	var inputStr=input.value
	if(window.RegExp && inputStr) {
		var regstr1 = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)"
		var regstr2 = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"
		var regExp1 = new RegExp(regstr1)
		var regExp2 = new RegExp(regstr2)
		var regExp3 = /[^a-zA-Z0-9\-_.@]/
		if(!(!regExp1.test(inputStr) && regExp2.test(inputStr) && !regExp3.test(inputStr)) || !(inputStr.indexOf("@")>=0)) {
			alert("Please enter a valid Email Address.");
			input.value=input.defaultValue;
			input.focus();
			return false;
		}
		return true;
	}
}

// this function will validate email entry.
function uf_IsEmailNoFocus(input) {
	var inputStr=input.value
	if(window.RegExp && inputStr) {
		var regstr1 = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)"
		var regstr2 = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"
		var regExp1 = new RegExp(regstr1)
		var regExp2 = new RegExp(regstr2)
		var regExp3 = /[^a-zA-Z0-9\-_.@]/
		if(!(!regExp1.test(inputStr) && regExp2.test(inputStr) && !regExp3.test(inputStr)) || !(inputStr.indexOf("@")>=0)) {
			alert("Please enter a valid Email Address.");
			input.value=input.defaultValue;
			return false;
		}
		return true;
	}
}

// -- signup alert
function ufSignUp() {
    var loForm = document.forms[0];
    var lsEmail = "";
    var lbProceed = false;
    lsEmail = loForm.Uc_header1$txtSignUp.value;
    if (lsEmail=="") {
        alert("Email address is required.");
    } else if ((lsEmail.toLowerCase() == "email address") || (lsEmail.toLowerCase() == "emailaddress")) {
        alert("Please enter your email address.");
    } else {
        lbProceed = uf_IsEmailNoFocus(loForm.Uc_header1$txtSignUp)
        if (lbProceed) {
            loForm.Uc_header1_hfEmailSignup.value = lsEmail;
            loForm.action = "email_signup.aspx";
            loForm.submit();
        }
    }
    
}

// -- search site
function ufSearchSite() {
    var loForm = document.forms[0];
    var lsSearchText = "";

    lsSearchText = loForm.Uc_header1$txtSearch.value;
    lsSearchText = uf_Trim(lsSearchText);
    if ((lsSearchText=="") || (lsSearchText.toLowerCase() == "search site") || (lsSearchText.toLowerCase() == "searchsite")) {
        alert("Please enter the word to search.");
    } else {
        if (lsSearchText.length < 3) {
            alert("The minimum character allowed per word is 3.");
        } else {
            loForm.Uc_header1_hfSearchText.value = lsSearchText;
            loForm.action = "search_site.aspx";
            loForm.submit();
        }
    }
}

// -- remove spaces
// remove multiple, leading or trailing spaces
function uf_Trim(asText) {
	asText = asText.replace(/(^\s*)|(\s*$)/gi,"");
	asText = asText.replace(/[ ]{2,}/gi," ");
	asText = asText.replace(/\n /,"\n");
	return asText;
}

// -- function to load content from search site 
function uf_ReadMore(asMode, asSecNaviID, asSecSubOneID, asSecSubTwoID, asSecSubThreeID, asContenTitle, asContentTitleTwo, asContentTitleThree) {
    var loForm = window.parent.document.forms[0];
    loForm.Uc_header1_hfContentMode.value = asMode;
    loForm.Uc_header1_hfContent.value = asSecNaviID;
    loForm.Uc_header1_hfContentSubOneID.value = asSecSubOneID;
    loForm.Uc_header1_hfContentSubTwoID.value = asSecSubTwoID;
    loForm.Uc_header1_hfContentSubThreeID.value = asSecSubThreeID;
    loForm.Uc_header1_hfContentTitle.value = asContenTitle;
    loForm.Uc_header1_hfContentTitleTwo.value = asContentTitleTwo;
    loForm.Uc_header1_hfContentTitleThree.value = asContentTitleThree;
    loForm.action = "content_page.aspx";
    loForm.submit();
}

// -- printer friendly version
function uf_PrinterFriendly() {
    var loForm = document.forms[0];
    var lsContent = "";
    var loFrame = getElement("ifrmContentPage");
	var loFrameDoc = getIFrameDocument("ifrmContentPage");
	lsContent = loFrameDoc.getElementById("idContentTopic").innerHTML;
	
	loForm.Uc_header1_hfContentPrint.value = lsContent;
	loForm.action = "printer_friendly.aspx";
	loForm.target = "_blank";
    loForm.submit();
}

function uf_PrivacyExit(asType) {
    if (asType=="pdf") {
        window.opener='as';
        window.close();
    } else {
        window.parent.location.replace("index.aspx");
    }
}

function uf_PrivacyProcess() {
    var loForm = document.frmCountry;

    loForm.action = "content_privacy_process.aspx";
    loForm.submit();

}

function uf_PrivacyNotice() {
    var loForm = document.frmNotice;
    
    loForm.action = "content_privacy_allowed.aspx";
    loForm.submit();    
}
