﻿function createjscssfile(filename, filetype) {
    if (filetype == "js") { //if filename is a external JavaScript file
        var fileref = document.createElement('script')
        fileref.setAttribute("type", "text/javascript")
        fileref.setAttribute("src", filename)
    }
    else if (filetype == "css") { //if filename is an external CSS file
        var fileref = document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    return fileref
}

function replacejscssfile(oldfilename, newfilename, filetype) {
    var targetelement = (filetype == "js") ? "script" : (filetype == "css") ? "link" : "none" //determine element type to create nodelist using
    var targetattr = (filetype == "js") ? "src" : (filetype == "css") ? "href" : "none" //determine corresponding attribute to test for
    var allsuspects = document.getElementsByTagName(targetelement)
    for (var i = allsuspects.length; i >= 0; i--) { //search backwards within nodelist for matching elements to remove
        if (allsuspects[i] && allsuspects[i].getAttribute(targetattr) != null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename) != -1) {
            var newelement = createjscssfile(newfilename, filetype)
            allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
        }
    }
}


/**
* Copyright 2009 Tim Down.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var Hashtable = (function() {
    function _1(_2) { return (typeof _2 === "undefined"); }; function _3(_4) { return (typeof _4 === "function"); }; function _5(_6) { return (typeof _6 === "string"); }; function _7(_8, _9) { return _3(_8[_9]); }; function _a(_b) { return _7(_b, "equals"); }; function _c(_d) { return _7(_d, "hashCode"); }; function _e(_f) {
        if (_5(_f)) { return _f; } else {
            if (_c(_f)) {
                var _10 = _f.hashCode(); if (!_5(_10)) { return _e(_10); }
                return _10;
            } else { if (_7(_f, "toString")) { return _f.toString(); } else { return String(_f); } } 
        } 
    }; function _11(_12, _13) { return _12.equals(_13); }; function _14(_15, _16) { if (_a(_16)) { return _16.equals(_15); } else { return _15 === _16; } }; function _17(o1, o2) { return o1 === o2; }; function _1a(arr, _1c, _1d, _1e, _1f) {
        var _20; for (var i = 0, len = arr.length; i < len; i++) { _20 = arr[i]; if (_1f(_1c, _1d(_20))) { return _1e ? [i, _20] : true; } }
        return false;
    }; function _23(arr, idx) { if (_7(arr, "splice")) { arr.splice(idx, 1); } else { if (idx === arr.length - 1) { arr.length = idx; } else { var _26 = arr.slice(idx + 1); arr.length = idx; for (var i = 0, len = _26.length; i < len; i++) { arr[idx + i] = _26[i]; } } } }; function _29(kv, _2b) { if (kv === null) { throw new Error("null is not a valid " + _2b); } else { if (_1(kv)) { throw new Error(_2b + " must not be undefined"); } } }; var _2c = "key", _2d = "value"; function _2e(key) { _29(key, _2c); }; function _30(_31) { _29(_31, _2d); }; function _32(_33, _34, _35) { this.entries = []; this.addEntry(_33, _34); if (_35 !== null) { this.getEqualityFunction = function() { return _35; }; } }; function _36(_37) { return _37[0]; }; function _38(_39) { return _39[1]; }; _32.prototype = { getEqualityFunction: function(_3a) { if (_a(_3a)) { return _11; } else { return _14; } }, searchForEntry: function(key) { return _1a(this.entries, key, _36, true, this.getEqualityFunction(key)); }, getEntryForKey: function(key) { return this.searchForEntry(key)[1]; }, getEntryIndexForKey: function(key) { return this.searchForEntry(key)[0]; }, removeEntryForKey: function(key) {
        var _3f = this.searchForEntry(key); if (_3f) { _23(this.entries, _3f[0]); return true; }
        return false;
    }, addEntry: function(key, _41) { this.entries[this.entries.length] = [key, _41]; }, size: function() { return this.entries.length; }, keys: function(_42) { var _43 = _42.length; for (var i = 0, len = this.entries.length; i < len; i++) { _42[_43 + i] = this.entries[i][0]; } }, values: function(_46) { var _47 = _46.length; for (var i = 0, len = this.entries.length; i < len; i++) { _46[_47 + i] = this.entries[i][1]; } }, containsKey: function(key) { return _1a(this.entries, key, _36, false, this.getEqualityFunction(key)); }, containsValue: function(_4b) { return _1a(this.entries, _4b, _38, false, _17); } 
    }; function _4c() { }; _4c.prototype = []; function _4d(_4e) { return _4e[0]; }; function _4f(_50, _51, _52) { return _1a(_50, _51, _4d, true, _52); }; function _53(_54, _55) {
        var _56 = _54[_55]; if (_56 && (_56 instanceof _4c)) { return _56[1]; }
        return null;
    }; function _57(_58, _59) {
        var _5a = []; var _5b = {}; _58 = _3(_58) ? _58 : _e; _59 = _3(_59) ? _59 : null; this.put = function(key, _5d) { _2e(key); _30(_5d); var _5e = _58(key); var _5f = _53(_5b, _5e); if (_5f) { var _60 = _5f.getEntryForKey(key); if (_60) { _60[1] = _5d; } else { _5f.addEntry(key, _5d); } } else { var _61 = new _4c(); _61[0] = _5e; _61[1] = new _32(key, _5d, _59); _5a[_5a.length] = _61; _5b[_5e] = _61; } }; this.get = function(key) {
            _2e(key); var _63 = _58(key); var _64 = _53(_5b, _63); if (_64) { var _65 = _64.getEntryForKey(key); if (_65) { return _65[1]; } }
            return null;
        }; this.containsKey = function(key) {
            _2e(key); var _67 = _58(key); var _68 = _53(_5b, _67); if (_68) { return _68.containsKey(key); }
            return false;
        }; this.containsValue = function(_69) {
            _30(_69); for (var i = 0, len = _5a.length; i < len; i++) { if (_5a[i][1].containsValue(_69)) { return true; } }
            return false;
        }; this.clear = function() { _5a.length = 0; _5b = {}; }; this.isEmpty = function() { return _5a.length === 0; }; this.keys = function() {
            var _6c = []; for (var i = 0, len = _5a.length; i < len; i++) { _5a[i][1].keys(_6c); }
            return _6c;
        }; this.values = function() {
            var _6f = []; for (var i = 0, len = _5a.length; i < len; i++) { _5a[i][1].values(_6f); }
            return _6f;
        }; this.remove = function(key) { _2e(key); var _73 = _58(key); var _74 = _53(_5b, _73); if (_74) { if (_74.removeEntryForKey(key)) { if (_74.size() === 0) { var _75 = _4f(_5a, _73, _74.getEqualityFunction(key)); _23(_5a, _75[0]); delete _5b[_73]; } } } }; this.size = function() {
            var _76 = 0; for (var i = 0, len = _5a.length; i < len; i++) { _76 += _5a[i][1].size(); }
            return _76;
        };
    }; return _57;
})();


// Replaces all instances of the given substring.
String.prototype.replaceAll = function( 
	strTarget, // The substring you want to replace
	strSubString // The string you want to replace in.
	){
	var strText = this;
	var intIndexOfMatch = strText.indexOf( strTarget );
	 
	// Keep looping while an instance of the target string
	// still exists in the string.
	while (intIndexOfMatch != -1){
		// Relace out the current instance.
		strText = strText.replace( strTarget, strSubString )
		 
		// Get the index of any next matching substring.
		intIndexOfMatch = strText.indexOf( strTarget );
	}
	 
	// Return the updated string with ALL the target strings
	// replaced out with the new substring.
	return( strText );
}

function ReplaceAll(Source,stringToFind,stringToReplace){

  var temp = Source;

    var index = temp.indexOf(stringToFind);

        while(index != -1){

            temp = temp.replace(stringToFind,stringToReplace);

            index = temp.indexOf(stringToFind);

        }

        return temp;

    }

function gotoPress() {
        
        var user = document.getElementById("txtUserName").value;
        var pwd = document.getElementById("txtPassword").value;

        
        var url = "ftp://" + user + ":" + pwd + "@www.nomenta.com/";

        //alert(url);

        window.location = url;
}

function InitializeSearchForm(sheetName) {
    var url2 = "/Home/InitializeSearchForm/" + sheetName;
    var wRequest = new Sys.Net.WebRequest();
    
    wRequest.set_url(url2);
    wRequest.set_httpVerb("GET");
    wRequest.set_userContext(sheetName);
    wRequest.add_completed(OnSearchSuccessCallBack);
    wRequest.invoke();

}

var JsonDataSet = null;
function OnSearchSuccessCallBack(executor2, e) {
    var webReq = executor2.get_webRequest();
    if (executor2.get_responseAvailable()) {
    
        var resData = executor2.get_responseData();
        //alert(resData);
        if (resData != "" && resData.length > 5) {
            var objBrand = document.getElementById("Brand");
            var s1 = Sys.Serialization.JavaScriptSerializer();
            JsonDataSet = Sys.Serialization.JavaScriptSerializer.deserialize(resData, true);
            objBrand.length = 0;
            //alert(JsonDataSet.Rows.length);
            for(var i=0; i < JsonDataSet.Rows.length; i++){
                objBrand.options[i] = new Option(JsonDataSet.Rows[i].Brand, JsonDataSet.Rows[i].Brand);
            }
            
            fillInModel(JsonDataSet.Rows[0].Brand);
        } else if (eval(resData) == 0) {
            alert("Can not find content of page in this language.");    
        }
        else {
            //do nothing   
        }
    } else {
        if (executor.get_timeOut()) {
            alert("timeout");

        } else if (executor.get_aborted()) {
            alert("aborted");
        }
    }
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function fillInModel(brandName) {
    var objModel = document.getElementById("Model");
    objModel.length = 0;
    for (var i = 0; i < JsonDataSet.Rows.length; i++) {
        if (brandName == JsonDataSet.Rows[i].Brand) {
            modelList = (new String(JsonDataSet.Rows[i].Model)).split("] [");

            modelList[0] = ReplaceAll(modelList[0], "[", "");
            modelList[modelList.length - 1] = ReplaceAll(modelList[modelList.length - 1], "]", "");

            for (var j = 0; j < modelList.length; j++) {
                modelList[j] = modelList[j].trim();
            }
            
            modelList.sort();
            for (var j = 0; j < modelList.length; j++) {
                objModel.options[j] = new Option(modelList[j], modelList[j]);
            }
        }
    }
}

function jumpTo() {
    var objBrand = document.getElementById("Brand");
    var objModel = document.getElementById("Model");

    var brandName = objBrand.options[objBrand.selectedIndex].value;
    var modelName = objModel.options[objModel.selectedIndex].value;
    //alert(brandName + "|" + modelName);
    
    for (var i = 0; i < JsonDataSet.Rows.length; i++) {
        if (brandName == JsonDataSet.Rows[i].Brand) {
            var originalModelName = "[" + modelName + "]";
            if (JsonDataSet.Rows[i].Model.indexOf(originalModelName) > -1) {
                //alert(JsonDataSet.Rows[i].JumpTarget);
                //window.open(JsonDataSet.Rows[i].JumpTarget, 'myTargetWindow', 'width=100%,height=100%')
                window.location.href = JsonDataSet.Rows[i].JumpTarget;
                return;
            }
        }
    }
}
  
function preloadImages() {
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}
preloadImages('../App_Themes/en_US/images/content-right-repeat.jpg');
