// Code and display of Cat Breed Selector program, Copyright (c) 2006, Telemark Productions. 
//May not be duplicated in whole or in part. All rights reserved.
var browserOS;
var browserName;
var browserVersion;

function getBrowserID() {
    var appVersion = navigator.appVersion;
    if (appVersion.indexOf("Win") > 0) {
        browserOS = "WIN";
    } else if (appVersion.indexOf("Mac") > 0) {
        browserOS = "MAC";
    } else if (appVersion.indexOf("X11") > 0) {
        browserOS = "UNX";
    } else {
        browserOS = "UNK";
    }

    var browser = navigator.appName;
    if (browser.substring(0, 8) == "Netscape") {
        browserName = "NN";
    } else if (browser.substring(0, 9) == "Microsoft") {
        browserName = "IE";
    } else {
        browserName = browser;
    }

    var msidx = appVersion.indexOf("MSIE");
    if (msidx > 0) {
       browserVersion = parseFloat(appVersion.substring(msidx+5)) * 1000;
    } else {
       browserVersion = parseFloat(appVersion) * 1000;
    }
}

// --------------------------------------------
var debugWin;
function debugwrite(str) {
if (debugWin == null || debugWin.closed) {
    debugWin = window.open("", "debugWin", "width=450,height=600,scrollbars,resizable");
}
debugWin.document.write(str + "<BR>");
}

// --------------------------------------------
var userSizeM;
var userCoatM;
var userTypesM;
var userActiveM;
var userVocal = 0;
var userGrooming = 0;
var userAffection = 0;
var userDemanding = 0;
var userChildrenM;
var userChildren;
var userSeniors;
var userAdults;
var userIndependence = 1;

//var typeNames = "CBMSFV";

// ------------------------------
function setChar(str, idx, c) {
    return str.substring(0, idx) + c + str.substring(idx+1, str.length);
}

function unCheckAllSize() {
    if (document.catform.cbCatSize0.checked) {
        for (var idx = 1; idx <= 3; idx++) {
            document.catform.elements["cbCatSize" + idx].checked = false;
        }
    }
}

function unCheckSizePref() {
    for (var idx = 1; idx <= 3; idx++) {
        if (document.catform.elements["cbCatSize" + idx].checked) {
            document.catform.cbCatSize0.checked = false;
            break;
        }
    }
}

function unCheckAllCoat() {
    if (document.catform.cbCatCoat0.checked) {
        for (var idx = 1; idx <= 3; idx++) {
            document.catform.elements["cbCatCoat" + idx].checked = false;
        }
    }
}

function unCheckCoatPref() {
    for (var idx = 1; idx <= 3; idx++) {
        if (document.catform.elements["cbCatCoat" + idx].checked) {
            document.catform.cbCatCoat0.checked = false;
            break;
        }
    }
}

function unCheckAllLook() {
    if (document.catform.cbCatLook0.checked) {
        for (var idx = 1; idx <= 6; idx++) {
            document.catform.elements["cbCatLook" +idx].checked = false;
        }
    }
}

function unCheckLookPref() {
    for (var idx = 1; idx <= 5; idx++) {
        if (document.catform.elements["cbCatLook" +idx].checked) {
            document.catform.cbCatLook0.checked = false;
            break;
        }
    }
}

function unCheckAllActive() {
    if (document.catform.cbCatActive0.checked) {
        for (var idx = 1; idx <= 3; idx++) {
            document.catform.elements["cbCatActive" +idx].checked = false;
        }
    }
}

function unCheckActivePref() {
    for (var idx = 1; idx <= 3; idx++) {
        if (document.catform.elements["cbCatActive" +idx].checked) {
            document.catform.cbCatActive0.checked = false;
            break;
        }
    }
}

function unCheckAllAffection() {
    if (document.catform.affection0.checked) {
        for (var idx = 1; idx <= 3; idx++) {
            document.catform.elements["rbCatAffection" +idx].checked = false;
        }
    }
}

function unCheckAffectionPref() {
    for (var idx = 1; idx <= 3; idx++) {
        if (document.catform.elements["rbCatAffection" +idx].checked) {
            document.catform.affection0.checked = false;
            break;
        }
    }
}

// --------------------------------------------
function setFormFromCookie() {
    var el;
    var allCookies = "" + document.cookie;
    var start = allCookies.indexOf("catSelectorData=");

    if (start == -1) {
        return;
    } else {
        start += "catSelectorData=".length;
        var end = allCookies.indexOf(':', start);
        if (end == -1) {
            end = allCookies.length;
        }

        var cookieString = unescape(allCookies.substring(start, end));

        var tokens = cookieString.split("&");
        userSizeM = tokens[0];
        userCoatM = tokens[1];
        userTypesM = tokens[2];
        userActiveM = tokens[3];
        userVocal = tokens[4];
        userGrooming = tokens[5];
        userAffection = tokens[6];
        userDemanding = tokens[7];
        userChildrenM = tokens[8];
        userSeniors = tokens[9];
        userAdults = tokens[10];
        userIndependence = tokens[11];

        // 1. size
        el = document.catform.elements["cbCatSize0"];
        el.checked = (userSizeM.length == 0);
        for (idx = 1; idx <= 3; idx++) {
            el = document.catform.elements["cbCatSize" + idx];
            el.checked = (userSizeM.indexOf(el.value) > -1);
        }

        // 2. coat
        el = document.catform.elements["cbCatCoat0"];
        el.checked = (userCoatM.length == 0);
        for (idx = 1; idx <= 3; idx++) {
            el = document.catform.elements["cbCatCoat" + idx];
            el.checked = (userCoatM.indexOf(el.value) > -1);
        }

        // 3. looks / body type
        el = document.catform.elements["cbCatLook0"];
        el.checked = (userTypesM.length == 0);
        for (idx = 1; idx <= 5; idx++) {
            el = document.catform.elements["cbCatLook" + idx];
            el.checked = (userTypesM.indexOf(el.value) > -1);
        }

        // 4. active
        el = document.catform.elements["cbCatActive0"];
        el.checked = (userActiveM.length == 0);
        for (idx = 1; idx <= 3; idx++) {
            el = document.catform.elements["cbCatActive" + idx];
            el.checked = (userActiveM.indexOf(el.value) > -1);
        }

        // 5. vocal
        el = document.catform.elements["rbCatVocal"];
        for (idx=0; idx<el.length; idx++) {
            el[idx].checked = (userVocal == el[idx].value);
        }

        // 6. grooming
        el = document.catform.elements["rbCatGroom"];
        for (idx=0; idx<el.length; idx++) {
            el[idx].checked = (userGrooming == el[idx].value);
        }

        // 7. affectionate
        el = document.catform.elements["rbCatAffection"];
        for (idx=0; idx<el.length; idx++) {
            el[idx].checked = (userAffection == el[idx].value);
        }

        // 8. demanding
        el = document.catform.elements["rbCatDemand"];
        for (idx=0; idx<el.length; idx++) {
            el[idx].checked = (userDemanding == el[idx].value);
        }

        // 9a. children
        for (idx = 1; idx <= 3; idx++) {
            el = document.catform.elements["cbCatPeople" + idx];
            el.checked = (userChildrenM.indexOf(el.value) > -1);
        }

        // 9b. seniors
        el = document.catform.elements["cbCatPeople4"];
        el.checked = (userSeniors == "Y");

        // 9c. Adults
        el = document.catform.elements["cbCatPeople0"];
        el.checked = (userAdults == "Y");

        // 10. independence
        el = document.catform.elements["rbCactIndependence"];
        for (idx=0; idx<el.length; idx++) {
            el[idx].checked = (userIndependence == el[idx].value);
        }

    }
}

// ------------------------------
function getRadioValue(buttonName) {
    var el = document.catform.elements[buttonName];

    for (idx=0; idx<el.length; idx++) {
        if (el[idx].checked) {
            return el[idx].value;
        }
    }
    return 0;
}

function setUserChoiceVars() {
	var idx;
    var el;
    var userCookieValue = "";

    // In most of these loops, we start the indexing at 1 because the first (zeroeth)
    // element is "no preference".

    // 1. size
    userSizeM = "";
    for (idx = 1; idx <= 3; idx++) {
        el = document.catform.elements["cbCatSize" + idx];
        if (el.checked) {
            userSizeM = userSizeM + el.value;
        }
    }
    userCookieValue += userSizeM;

    // 2. coat
    userCoatM = "";
    for (idx = 1; idx <= 3; idx++) {
        el = document.catform.elements["cbCatCoat" + idx];
        if (el.checked) {
            userCoatM = userCoatM + el.value;
           }
    }
    userCookieValue += "&" + userCoatM;

    // 3. looks / body type
    userTypesM = "";
    for (idx = 1; idx <= 5; idx++) {
        el = document.catform.elements["cbCatLook" +idx];
        if (el.checked) {
            userTypesM = userTypesM + el.value;
        }
    }
    userCookieValue += "&" + userTypesM;

    // 4. active
    userActiveM = "";
    for (idx = 1; idx <= 3; idx++) {
        el = document.catform.elements["cbCatActive" + idx];
        if (el.checked) {
            userActiveM = userActiveM + el.value;
        }
    }
    userCookieValue += "&" + userActiveM;

    // 5. vocal
    userVocal = getRadioValue("rbCatVocal");
    userCookieValue += "&" + userVocal;

    // 6. grooming
    userGrooming = getRadioValue("rbCatGroom");
    userCookieValue += "&" + userGrooming;

    // 7. affectionate
    userAffection = getRadioValue("rbCatAffection");
    userCookieValue += "&" + userAffection;

    // 8. demanding
    userDemanding = getRadioValue("rbCatDemand");
    userCookieValue += "&" + userDemanding;

    // 9a. children
    userChildrenM = "";
    for (idx = 1; idx <= 3; idx++) {
        el = document.catform.elements["cbCatPeople" + idx];
        if (el.checked) {
            userChildrenM = userChildrenM + el.value;
        }
    }
    userCookieValue += "&" + userChildrenM;

    // 9b. Seniors
    userSeniors = "N";
    if (document.catform.cbCatPeople4.checked) {
        userSeniors = "Y";
    }
    userCookieValue += "&" + userSeniors;

    // 9c. Adults
    userAdults = "N";
    if (document.catform.cbCatPeople0.checked) {
        userAdults = "Y";
    }
    userCookieValue += "&" + userAdults;

    // 10. independence

    userIndependence = getRadioValue("rbCactIndependence");
    userCookieValue += "&" + userIndependence;

    // Lastly, and very important, set the cookie

 var expires;
 var domain;
 var secure;

 Set_Cookie("catSelectorData",userCookieValue,expires,"/iams/",domain,secure);
   // document.cookie = "catSelectorData=" + escape(userCookieValue);

}


function Set_Cookie( name, value, expires, path, domain, secure ) 
{

// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
//if ( expires )
//{
expires = 1000 * 1000 * 60 * 60 * 24;
//} 
//alert('expires is '+expires);
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = eval('"'+name + "=" +escape( value ) + 
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" )+'"'
);

}



