/*  Dotomator 
    
    Written by Jacqueline "Kira" Hamilton (kira@lightsphere.com)

    Copyright 2007. All rights reserved.
*/

partA = new Array();

partB = new Array();

names = new Array();
scratchboard = new Array();

function update_fields(field) {
	var mystr = new String(eval("document.fform." + field + ".value"));
	var tmpary = new Array;
	tmpary =  mystr.split("\n");
	
	for (var i = 0; i < tmpary.length; i++) {
		
	}
}

function combinem() {
    
    var strA = new String(document.fform.left.value);
    var strB = new String(document.fform.right.value);
    var err  = document.getElementById("combine_err");
    err.innerHTML = "";

    partA = strA.split("\n");
    partB = strB.split("\n");

    names.length = 0;
    var left_ct  = 0;
    

    for (var i = 0; i < partA.length; i++) {
		if (partA[i].match(/^\s*$/)) {   // skip blank lines.
			continue;
		}
		left_ct++;
		if (left_ct > 15) {
            break;
        }
        var right_ct = 0;
        var ptA = partA[i];

        for (var j = 0; j < partB.length; j++) {
            if (partB[j].match(/^\s*$/)) {
                continue;
            }
            right_ct++;
            if (right_ct > 15) {
                break;
            }
            var newname = new String();

 // if two words begin/end in the same letter, nix one of the duplicates.

            var ptB = partB[j];
            if ( ptA.charAt(ptA.length-1) == ptB.charAt(0) ) {
                newname = partA[i] + ptB.substr(1);
            } else {
                newname = partA[i] + partB[j];
            }
            newname = newname.replace(/\s/g, "");
            names.push(newname);
        }
    }
    if (left_ct == 0 || right_ct == 0) {
        err.innerHTML = "Please enter one or more names in the prefix and suffix columns. Then click \"Combine\" to view a list of combinations.";   
    }
    else {
        if (left_ct > 15 || right_ct > 15) {
            var errmsg = "You have more than 15 names in ";
            if (left_ct > 15) {
                if (right_ct > 15) {
                    errmsg += "both the prefix and suffix columns";
                } 
                else {
                    errmsg += "the prefix column";
                }
            }
            else if (right_ct > 15) {
                errmsg += "the suffix column";
            }
            errmsg += ". Only the first 15 have been used.";
            err.innerHTML = errmsg;
        }
        
        var results = new Array;
        
        for (var i = 0; i < names.length; i++) {
            results.push('<a href="javascript:saveit(\'' + names[i] + '\')">' + names[i] + '</a>');
        }
        
        var results_elt = document.getElementById("namebox");
        results_elt.innerHTML = results.join("<br>\n");
        
        // document.fform.results.value = results;
        document.rform.DomainName.value = names.join("\n");	// hidden
    
        document.fform.check_butn.disabled = false;
        var result_ct = document.getElementById("result_count");
        result_ct.innerHTML = String(results.length);
        var waitlen = document.getElementById("waitlen");
        waitlen.innerHTML = "Results open in a new window on Dotster. It may take several seconds to look up all the names.";
    }
}

// There is a max of 15 words at a time in each box

var leftnames = new Array();

leftnames["Colors"] = new Array("White", "Pink", "Red", "Orange", "Brown", "Yellow", "Green", "Blue", "Purple", "Gray", "Black");
leftnames["Colors2"] = new Array("Amber", "Aqua", "Azure", "Bronze", "Coral", "Copper", "Crimson", "Cyan", "Ginger", "Gold", "Hazel", "Indigo", "Jade", "Silver");

// partial colors dropped, most didnt work out very well.
leftnames["colorparts"] = new Array("Auri", "Azu", "Ceru", "Cerule", "Cinna", "Indi", "Magenta", "Saphi", "Tanger", "Viridi" );

leftnames["Games"] = new Array("Adventure", "Battle", "Casual", "Compete", "Console", "Fun", "Game", "Match", "MMO", "Play", "Sim", "Strategy", "Video");


leftnames["Tech"]   = new Array( "Bit", "Buzz", "Code", "Data", "Digi", "Giga", "Info", "Link", "Object", "Tech", "Tele", "Trade", "Venture", "Verti" );
leftnames["Tiny"] = new Array("Atom", "Atomi", "Bit", "Micro", "Mini", "Pint", "Pico", "Tiny");

leftnames["2words"] = new Array("Babble", "Blog", "Bright", "Bubble", "Dyna", "Flip", "Gab", "My", "Riff", "Snap");
leftnames["2parts"] = new Array("Cogi", "Fa", "Ka", "Ki", "My", "Qua", "Ska", "Twi", "Vi", "Ya", "Yo");

leftnames["V"] = new Array("Value", "Venue", "Veri", "Verti", "Via", "Vibe", "Vid", "Visi", "Voice", "Vox" );

var rightnames = new Array();

rightnames["Boats"]      = new Array("about", "boat", "craft", "cruiser", "cutter", "ferry", "frigate", "monitor", "rigger", "schooner", "skiff", "sloop", "trawler");
rightnames["Bugs"]      = new Array("ant", "beetle", "bug", "cada", "cricket", "fly", "hopper", "hornet", "mantis", "mite", "moth", "pillar", "roller", "wasp");

rightnames["Builder"]      = new Array("alizer", "builder", "itect", "ineer",  "inator", "maker", "mason", "mechanic", "miller", "smith", "wright");


rightnames["Cash"]     = new Array("note", "bucks", "cash", "coin", "ero", "inero", "funds", "loot", "pay", "ency", "ney");
rightnames["Charts"]    = new Array("chart", "graph", "line", "map", "print", "struct", "tect", "ture" );
rightnames["Circles"]  = new Array("circle", "cycle", "curve", "curl", "ical", "icus", "ique", "idian", "igee", "loop", "orbit", "quel", "sphere", "wheel" );
rightnames["Critters"]  = new Array("bat", "bird", "cat", "chilla", "dog", "fin", "fish", "fox", "gale", "gator", "hound", "lope", "shark", "wolf");

rightnames["Crops"]     = new Array("crop", "tory", "yard", "stead", "range", "land", "hold", "ery", "age", "ation", "scape");

rightnames["Experts"]   = new Array("ace", "mage", "mancer", "maven", "oso", "pro", "sage", "star", "wizard", "wiz");

rightnames["Friends"]   = new Array("amigo", "bud", "buddy", "fan", "friend", "kin", "mate", "pal", "partner");

rightnames["Groups"]     = new Array("band", "clan", "club", "crew", "gang", "group", "guild", "league", "lot", "mob", "order", "party", "squad", "team", "troop");

rightnames["Herds"]     = new Array("cloud", "colony", "flight", "flock", "gaggle", "herd", "horde", "pack", "pod", "pride", "shoal", "swarm", "tribe", "troop");


// 2words2 is filed under "Endings" in the form
rightnames["2words2"]   = new Array("ality", "ate", "ient", "ify", "ity", "ium", "ius", "ize", "izer", "nik", "opoly", "ster");
rightnames["Mecha"]     = new Array("ator", "craft", "iance", "ism", "iter", "izer", "get", "mator", "ment", "ton", "tor");

rightnames["Merchant"]   = new Array("keep", "keeper", "merchant", "baron", "ster", "mogul", "monger", "master", "keeper", "seller", "saler");


rightnames["Places"]    = new Array("cosm", "dom", "loft", "opolis", "plex", "shire", "sphere", "teria", "terium", "topia", "town", "urbia", "wick", "verse", "ville");

rightnames["Roads"]     = new Array("bridge", "crossing", "duit", "lane", "line", "link", "pass", "path", "road", "route", "span", "trail", "vard", "venue", "way");
rightnames["Shops"]     = new Array("eria", "exchange", "market", "mart", "mill", "outlet", "plaza", "sale", "shop", "teria", "tique", "xchange");

rightnames["Tech"]      = new Array("cast", "cube", "digita", "feed", "fire", "ology", "space", "span", "technica", "ware", "wire", "works");
rightnames["Tech2"]      = new Array("atar", "blog", "bot", "bug", "flash", "host", "icon", "source", "src", "space", "ware", "way", "work");
rightnames["Tech3"]      = new Array("base", "bit", "code", "load", "map", "module", "script", "serv", "server", "set", "state", "system", "token", "tag", "unit");

rightnames["Travel"]      = new Array("edition", "jaunt", "journey", "omotion", "pade", "quest", "range", "roam", "tour", "trek", "vant", "venture", "voyage");

rightnames["Hardware"]      = new Array("array", "bridge", "bus", "cable", "card", "core", "disk", "drive", "fiber", "optic", "sink", "slot");
rightnames["Network"]      = new Array("band", "circuit", "cluster", "frame", "line", "link", "net", "port", "route", "station", "switch", "torrent");

rightnames["Messages"]      = new Array("cable", "flash", "gram", "graph", "ique", "line", "memo", "squawk", "tweet", "wire");


rightnames["2words"]    = new Array("blab", "buzz", "club", "cube", "mix", "nic", "opia", "pad", "path", "point", "space", "sphere", "vine", "zone", "zoom");
rightnames["2parts"]    = new Array("ble", "ba", "deo", "do", "jo", "mbo", "ndo", "nix", "tz", "vee", "yo", "zio", "zu", "zzle");

rightnames["Watery"]    = new Array("bay", "cove", "creek", "harbor", "port", "rain", "river", "sea", "stream", "tide",  "wave" );
rightnames["Works"]     = new Array("shop", "foundry", "forge", "form", "mill", "works", "mill", "tory", "tary", "ary", "ery", "al" );

// train, cate, rello, nium

function set_left() {
    var field = document.fform.leftnames;
    document.fform.left.value = "";
    if (field.selectedIndex > 0) {
        var lvalue = field.options[field.selectedIndex].value;
        if (leftnames[lvalue].length) {
            var names = leftnames[lvalue].join("\n");
            document.fform.left.value = names;
        }
    }
}

function set_right() {
    var field = document.fform.rightnames;
    document.fform.right.value = "";
    if (field.selectedIndex > 0) {
        var lvalue = field.options[field.selectedIndex].value;
        if (rightnames[lvalue].length) {
            var names = rightnames[lvalue].join("\n");
            document.fform.right.value = names;
        }
    }
}

function saveit(name) {     // save to the scratchbox
// revised 11/26/07 to not overwrite any customizations the user made
// to the scratchbox.
//
    var scratch = document.fform.scratchbox.value;
    var scratch_array = scratch.split("\n");
    var newary = new Array();

    for (var i=0; i < scratch_array.length; i++) {
        var namestr = String( scratch_array[i] );
        if (namestr.length > 0) {
            newary.push(scratch_array[i]);
        }
        if (scratch_array[i] == name) {
            return;
        }
    }
    newary.push(name);
    document.fform.scratchbox.value = newary.join("\n");
    document.fform.check_butn2.disabled = false;
}

function clear_scratch() {  // erase the scratchbox
    scratchboard.length = 0;
    document.fform.scratchbox.value = "";
}

function submitform() {	    // for the "check all" form
    var tmpAry = new Array();
    var namestr = new String();
    namestr = document.rform.DomainName.value;
    tmpAry = namestr.split("\n");
    if (tmpAry.length == 1) {
        var name = tmpAry.shift();
        submit_single(name);
    }
    else {
        document.rform.submit();
    }
}

function submitform2() {	// for the scratchbox form
    var newary = new Array();
    var maxlen = 225;
    var scratch = document.fform.scratchbox.value;
    var scratch_array = scratch.split("\n");

    // revised 11/26/07 to check what's actually in the FORM, not
    // what's in our saved scratchbox array.
    //
    for (var i=0; i < scratch_array.length; i++) {
        if (i >= 225) {
            var err  = document.getElementById("scratch_err");
            err.innerHTML = "Too many names in the scratchboard; only the first 225 will be checked.";
            break;
        }
        var namestr = String( scratch_array[i] );
        if (namestr.length > 0) {
            newary.push(scratch_array[i]);
        }
    }

    if (newary.length == 1) {
        var name = newary.shift();
        submit_single(name);
    }
    else {
        document.rform2.DomainName.value = newary.join("\n");
        document.rform2.submit();
    }
}

function submit_single(name) {  // single names have to be sent separately
    document.sform.DomainName.value = name;
    document.sform.submit();
}

function init_fields() {
    update_sb();
}


function update_sb() {
    var newary  = document.fform.scratchbox.value.split("\n");
    if (scratchboard.length != newary.length) {
        scratchboard.length = 0;        
        for (var i=0; i < newary.length; i++) {
            if (newary[i].match(/^\w+$/)) {
                scratchboard.push(newary[i]);
            }
        }
        if (scratchboard.length > 0) {
            document.fform.check_butn2.disabled = false;
        }
    }
}
