/*=============================================================================
	toolbox.js -- A javascript resource of commonly used functions.
	
	All code unless otherwise noted written by Jack Jeskey	
	Copyright (c) 2003 Jack Jeskey. All yaya Reserved.
	
=============================================================================*/

/******************************************************************************
 Begin Cookie functions
******************************************************************************/
// Derived from the Bill Dortch code at http://www.hidaho.com/cookies/cookie.txt
var today = new Date();
var expiry = new Date(today.getTime() + 365 * 24 * 60 * 60 * 1000);

// Subroutine used by GetCookie() Not used for other functions
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) { endstr = document.cookie.length; }
	return unescape(document.cookie.substring(offset, endstr));
}

// Returns val of name. If name doesnt exist returns null
function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return getCookieVal (j);
			}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
		}
	return null;
}

// Deletes a cookie
function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
		}
}
// Sets the name value and other optional fields for a cookie
// Updated by Jack Jeskey
function SetCookie (name,value,expires,path,domain,secure) {
var cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
// If too big, truncate it!
if(cookie.length > 4000)
	cookie = cookie.substring(0, 4000);


document.cookie = cookie;
}

/******************************************************************************
 Checks to see if cookies are turned on, returns false if not. It sets a cookie
 then trys to retrive it. If it fails, then cookies are turned off.
******************************************************************************/
function CookieCheck() {
	SetCookie('cookiecheck', 'true');
	canusecookies = GetCookie('cookiecheck');
	if (canusecookies) {
		DeleteCookie('cookiecheck');
		return true;	
	}
	else {
		DeleteCookie('cookiecheck');
		return false;
	}
} // cookiecheck()

/******************************************************************************
 End Cookie functions
******************************************************************************/

/******************************************************************************
 This is the function that performs form verification. It will be invoked
 from the onSubmit() event handler. The handler should return whatever
 value this function returns. 
 uses the following functions:
 	isblank(s)
 	coppacheck(form)
 	passwordcheck(form)
******************************************************************************/

var optionalradio = ' ';

function verifyform(f) {
	//check coppa if it exists
	if (!coppacheck(f))
		return false;
	//check password and re-enter password elements if they exist
	if (!passwordcheck(f))
		return false;
    var msg;
    var empty_fields = "";
    var errors = "";


var ischecked = false;

//lame that new String() constructor will not create an empty string object
//as is done with new Array() 
var radiolist = ' ';

var checkedradiolist = ' ';

var radioarray = new Array();

var rarrayindex = 0;


    // Loop through the elements of the form, looking for all 
    // text and textarea elements that don't have an "optional" property
    // defined. Then, check for fields that are empty and make a list of them.
    // Also, if any of these elements have a "min" or a "max" property defined,
    // then verify that they are numbers and that they are in the right range.
    // Put together error messages for fields that are wrong.
    for(var i = 0; i < f.length; i++) {
        var e = f.elements[i];
        var errorfield = e.name;
		//asign errorfield a fullname value if one exists
        if (e.fullname) 
	        errorfield = e.fullname;
	   // alert(e.fullname);    
        // check that select boxes all have non "" values

        if(((e.type == "select-one") || (e.type == "select-multiple")) && (e.options.value == "") && !e.optional) 
        {
           errors += "- The field " + errorfield + " must have a selection.\n\n";      
		}
		//Since there is no way to indirectly refer to a group of radio buttons as a
		//seperate array that is traversable, we will have to create our own called
		//radio array. The array gets the value of errorfield added to it, which in 
		//this case is the radio buttons name="" value, or Full Name if one is defined. 
		if ((e.type == "radio") && (optionalradio.indexOf(e.name) == "-1")) {
			//add existance of all radio button groups to the arrays
			if (radiolist.indexOf(errorfield) == "-1") {
				//alert(errorfield);
				radiolist += errorfield;
				radioarray[rarrayindex] = errorfield;
				//alert(radioarray[rarrayindex]);
				rarrayindex++;
				
			}
			//add existance of a checked radio button	
			if (e.checked)
				checkedradiolist += errorfield;
		}
		
        if (((e.type == "text") || (e.type == "textarea") || (e.type == "password")) && !e.optional) {
            // first check if the field is empty
            if ((e.value == null) || (e.value == "") || isblank(e.value)) {
                empty_fields += "\n          " + errorfield;
                continue;
            }
            // Now check for fields that are supposed to be numeric.
            if (e.numeric || (e.min != null) || (e.max != null)) { 
                var v = parseFloat(e.value);
                if (isNaN(e.value) || 
                    ((e.min != null) && (v < e.min) && !isNaN(v)) || 
                    ((e.max != null) && (v > e.max) && !isNaN(v))) {
                    errors += "- The field " + errorfield + " must be a number";
                    if (e.min != null) 
                        errors += " that is greater than " + e.min;
                    if (e.max != null && e.min != null) 
                        errors += " and less than " + e.max;
                    else if (e.max != null)
                        errors += " that is less than " + e.max;
                    errors += ".\n";
                }
            }
			// Next check to see that the field is enough characters long
			if (e.minchars) {
				if ((e.value.length < e.minchars) && (!isNaN(e.minchars))) 
					errors += "- The field " + errorfield + " must be at least " + e.minchars + " characters long.\n\n";
			}	
			// If value is an email, check it to see if it is valid
        	if (e.email) {
        		if (e.value.indexOf("@") == "-1" ||
	    			e.value.indexOf(".") == "-1")
	   				errors += "- The field " + errorfield + " must be a valid email address.\n\n";
			}
			// If the value is a credit card number check to if it passes luhn algorithim and has a valid prefix
			if (e.creditcard) {
				// Check if it's a valid credit card
				if (isCreditCard(e.value) == "invalid")
					errors += "- The field " + errorfield + " must be a valid credit card number.\n\n";
				// Check if it's a accepted credit card
				if (e.creditcardsaccepted) {
					if (isCreditCard(e.value) != 'invalid' && e.creditcardsaccepted.indexOf(isCreditCard(e.value)) == -1)
						errors += "- The field " + errorfield + " must be one of the following credit cards: " + e.creditcardsaccepted + ".\n\n";
				}
			}
        } // if ()		
	} // for()



//Now we loop through the array of radio buttons we created to find ones that
//are not checked.
for (var y = 0; y < radioarray.length; y++) {
	if (checkedradiolist.indexOf(radioarray[y]) == -1) {
		errors += "- One of the radio buttons for " + radioarray[y] + " must be selected.\n\n"; 
	}
}


    // Now, if there were any errors, display the messages, and
    // return false to prevent the form from being submitted. 
    // Otherwise return true.
    if (!empty_fields && !errors) return true;

    msg  = "______________________________________________________\n\n"
    msg += "The form was not submitted because of the following\n";
    msg += "error(s). Please correct these error(s) and re-submit.\n";
    msg += "______________________________________________________\n\n"

    if (empty_fields) {
        msg += "- The following required field(s) are empty:" 
                + empty_fields + "\n";
        if (errors) msg += "\n";
    }
    msg += errors;
    alert(msg);
    return false;
} // verifyform(f)

/******************************************************************************
 A utility function that returns true if a string contains only 
 whitespace characters.
******************************************************************************/
function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
} // isblank(s)


/******************************************************************************
 This function returns true or false if the value of any checkbox or radio
 button is checked and the value is 13 or under. Used by verfiyform()
******************************************************************************/
function coppacheck(form) {
	for(var i = 0; i < form.length; i++) {
		var elem = form.elements[i];

		if ((elem.type == "radio" || elem.type == "checkbox") && (elem.checked == true && elem.value == "13 or under")) {
		alert('We do not collect information from children under the age of 13. Therefore, you will not be able to receive this service, and your personal information will not be retained. Please contact us at the links on this page if you or your parent have any questions.')
		return false;
		}
	}
	return true;
} // coppacheck(form);

/******************************************************************************
 This function returns true or false if the value of any checkbox or radio
 button is checked and the value is 13 or under. Used by verfiyform()
******************************************************************************/
function passwordcheck(form) {
	if ((form.password) && (form.passwordcheck)) {
		if (form.password.value != form.passwordcheck.value) { // are password and passwordcheck the same?
			alert("Password and re-entered password are not the same. Please enter the same text for each.")
			form.passwordcheck.focus();
			return false;
		}
		if ((form.password.value.length < form.password.minchars)  && (!isNaN(form.password.minchars) && (form.password.value != ""))) {
			alert("Password must be at least " + form.password.minchars + " characters long.");
			return false;
		}
	}
return true;
} // passwordcheck(form)

/******************************************************************************
 This function converts form to a string, via the form elements[] array
 The returned string contains the name and values of text or text areas and the 
 name and values of checked radio or checkboxes. The format returned is 
 name=value. Each input element is seperated delimeter definded when the 
 function is called
******************************************************************************/
function formToString(form, delimeter) {
	//Have to make it equal "" or undefined becomes first item
	var formstring = "";
	
	for(var i = 0; i < form.length; i++) {
	var elem = form.elements[i];
		if ((elem.type == "text" || elem.type == "textarea" || elem.type == "password") && elem.value != null && elem.value != "" && !isblank(elem.value))
			formstring += elem.name + "=" + elem.value + delimeter;
		if ((elem.type == "radio" || elem.type == "checkbox") && (elem.checked == true))
			formstring += elem.name + "=" + elem.value + delimeter;
	}
	return formstring;
} // formToString(form)

/******************************************************************************
 This function populates the various form elements if they already exist in a 
 cookie. Because the cookie contains all the form
 elements in one string, It has to be converted to an array of strings. Two for
 loops are used, one to go through every element of the form via the elements[]
 aray, and then another to traverse the created array from the cookie. Used in
 conjuction with formToString().
******************************************************************************/
function populateformfromcookie(form, cookie, delimeter) {
	var cookieinfo = GetCookie(cookie);
	if (cookieinfo) {
	
	cookieinfo = cookieinfo.split(delimeter);
		for(var i = 0; i < form.length; i++) {
			var elem = form.elements[i];
				for(var u = 0; u < cookieinfo.length; u++) {
					var info = cookieinfo[u];
					if ((info.indexOf(elem.name) != -1) && (elem.type == "hidden" || elem.type == "text" || elem.type == "textarea")) {
						elem.value = info.slice((info.indexOf("=") + 1), info.length);
					}								
				}
		}	
	}
} // populateformfromcookie(form, cookie, delimeter)

/******************************************************************************
 Simalar to the above function, this instead populates the elements of a form
 based on individual cookies with the same name as those corresponding form
 elements.
 This function populates the various form elements if they already exist. It
 uses a basic for loop to go through each element via elements[] array. If a 
 cookie with the same element type or value exists, it is populated.
******************************************************************************/
function populateform(form) {
	for(var i = 0; i < form.length; i++) {

		var elem = form.elements[i];
		var val = GetCookie(elem.name)
		//alert(val);
		// check buttons if they should be
		if ((elem.type == "radio" || elem.type == "checkbox") && (elem.value == val)) {
			elem.checked = true;
		}
		// ad text from cookies to coresponding elements
		if (val && (elem.type == "text" || elem.type == "textarea")) {
			elem.value = val;
		}		
		// populate hidden fields too
		if (val && (elem.type == "hidden")) {
			elem.value = val;
		}		
		// select the appropiate selection based on the cookie
		if (val && (elem.type == "select-one")) { 
			for (u = 0; u < elem.options.length; u++) {
				if (elem.options[u].value == val)
					elem.selectedIndex = u;
			}
		}	
	}
} //  populateform(form)

/******************************************************************************
 This function saves all the form elements and values into seperate cookies 
 based on the elements name and value. Simalar to formtostring function.
******************************************************************************/
function formtocookies(form) {
	for(var i = 0; i < form.length; i++) {
		var elem = form.elements[i];
		
		if ((elem.type == "radio" || "checkbox") && (elem.checked == true))
			SetCookie(elem.name, elem.value);
		if (elem.type == "select-one")
			SetCookie(elem.name, elem.options[elem.options.selectedIndex].value);
		if ((elem.type != "radio") && 
			(elem.type != "reset") &&
			(elem.type != "submit") &&
			(elem.type != "checkbox") && 
			(elem.type != "select-one") &&
			(elem.value))
			SetCookie(elem.name, elem.value);
	}
} // function formtocookies(form)

/******************************************************************************
 THIS FUNCTION IS TAKEN DIRECTLY FROM NETSCAPE FROM:
 http://developer.netscape.com/library/examples/...
 .../javascript/formval/FormChek.js
 which is a bunch of functions to validate forms.
 This algorithim is used for creditcard numbers in addition to other numbers,
 such as canadian social security numbers and other numeric identification that
 requires validations. This works with the following credit cards: 
 Visa, MasterCard, American Express, Diner's Club, Carte Blanche, Discover, 
 en Route, JCB 

 INPUT:    st - a string representing a credit card number
 RETURNS:  true, if the number passes the Luhn Mod-10 test
 false, otherwise
******************************************************************************/
function isLuhnMod10(st) {
	// Encoding only works on numbers with less than 19 digits
	if (st.length > 19)
		return (false);

	sum = 0;
	mul = 1;
	l = st.length;
	for (i = 0; i < l; i++) {
		digit = st.substring(l-i-1,l-i);
		tproduct = parseInt(digit ,10)*mul;
		if (tproduct >= 10)
			sum += (tproduct % 10) + 1;
		else
			sum += tproduct;
		if (mul == 1)
			mul++;
		else
			mul--;
	}

	if ((sum % 10) == 0)
		return (true);
	else
		return (false);
} // isLuhnMod10(st)

/******************************************************************************
 This function checks to see if a string is a valid credit card number. The 
 input is a string, and the output is the type of credit card, amex visa etc. 
 or invalid if it is a invalid number. First it checks to see if the number 
 passes the luhn mod 10 algorithim. Then the type of card is determined by the
 prefix and string length. This functions uses isLuhnMod10().
******************************************************************************/
function isCreditCard(ccnum) {

var prefix = ccnum.substring(0,4);
var length = ccnum.length;

	if (!isLuhnMod10(ccnum))
		return "invalid";
	if ((prefix.charAt(0) == 4) && (length == 13 || length == 16))
		return "visa";
	if ((prefix.charAt(0) == 5) && (prefix.charAt(1) >= 1 && prefix.charAt(1) <= 5) && length == 16)
		return "mastercard";
	if ((prefix.charAt(0) == 3) && (prefix.charAt(1) == 4 || prefix.charAt(1) == 7) && length == 15)
		return "amex";
	if (prefix == 6011 && length == 16)
		return "discover";
	if ((prefix.charAt(0) == 3) && (prefix.charAt(1) == 0) && (prefix.charAt(2) >= 0 && prefix.charAt(1) <= 5) && length == 14)
		return "dinersclub";
	if ((prefix.charAt(0) == 3) && (prefix.charAt(1) == 6 || prefix.charAt(1) == 8) && length == 14)
		return "dinersclub";	
	if ((prefix == 2014 || prefix == 2149) && length == 15)
		return "enroute";
	if ((prefix == 2131 || prefix == 1800) && length == 15)
		return "jcb";
	if (prefix.charAt(0) == 3 && length == 16)
		return "jcb";
	else 
		return "invalid";
} // isCreditCard(ccnum)

/******************************************************************************
 This function creates a popup window that goes to the url stored in the 
 varible winurl and has the name that is stored in the varible winname
 with the parameters stored in the varible winparams. If the browser is IE it 
 will refresh the window because window.open does not jump to a # anchor if 
 one exists
******************************************************************************/
function popUpWin(winurl,winname,winparams) {
	//assign name if none is given 
	if (winname == null)
		winname = "popupwin";

	//open it up
	newwin = window.open(winurl,winname,winparams);

	//if ie 4+, reload again cause ie's dumb and wont goto #anchors on a window.open
	if (winurl.indexOf('#') & document.all) 
		newwin.window.location.href = newwin.document.URL;
} // popUpWin(winurl,winname,winparams)

/******************************************************************************
 This function creates a popup window that goes to the url stored in the 
 varible winurl and has the name that is stored in the varible winname.
 it uses the popupHeight and popupWidth varibles to asign the height and width,
 and determine the exact screen center from where to open the window to.
 If the browser is IE and the url contains a # anchor it will refresh the 
 window because window.open does not jump to a # anchor if one exists in IE
******************************************************************************/
function popUpWinCenter(winurl,winname,popupHeight,popupWidth) {

	//assign defaults if none is given
	if (popupHeight == null)
		popupHeight = 0;
	if (popupWidth == null)
		popupWidth = 0;
	if (winname == null)
		winname = "popupwin";

	//Set center for the params
	var screenCenterX = screen.height/2 - popupHeight/2;
	var screenCenterY = screen.width/2 - popupWidth/2;
	
	var popupParams = 'toolbar=0,location=0,directories=0,menubar=0,resizable=1,scrollbars=1';

	popupParams += ',height=' + popupHeight + ',width=' + popupWidth + ',top=' + screenCenterX + ',left=' + screenCenterY  + ',screenX=' + screenCenterX + ',screenY=' + screenCenterY;

	//open it up
	var newwin = window.open(winurl,winname,popupParams);

	//if ie 4+, reload again cause ie's dumb and wont goto #anchors on a window.open
	if (winurl.indexOf('#') & document.all) 
		newwin.window.location.href = newwin.document.URL;
} // popUpWinCenter(winurl,winname,popupHeight,popupWidth)

/******************************************************************************
	All page conditionals
******************************************************************************/

/******************************************************************************
 This creates two nifty arrays of name=value pairs if a query string exists
 in the location field. The two arrays are queryname and queryvalue. each
 element of the arrays correspond to each other based on their conescutive
 locations. A third array, queryarray contains name=value pairs as one string
 per array element. 
******************************************************************************/
if (location.search.indexOf("?") != -1) {
	//grab querystring
	var querystring = location.search.substring(location.search.indexOf("?") + 1, location.search.length);
	//convert to array
	var queryarray = querystring.split('&');
	//make the two arrays we will asign to
	var queryname = new Array();
	var queryvalue = new Array();

	for (var i = 0; i < queryarray.length; i++) {
		 var str = queryarray[i];
		 //Check to see if query string content has real name=value pair
		 if (str.indexOf("=") != -1) {
		 	queryname[i] = str.slice(0, str.indexOf("="));
		 	queryvalue[i] = str.slice(str.indexOf("=") + 1, str.length);
		 }	
	}
}



/******************************************************************************
    Brand asignment based on referrer or query string
******************************************************************************/

var notfromanywhere = false;

var fromwhere;
var referrer;
	
var fromcookie = GetCookie("fromwhere");
// Set value of referrer based on document.referrer
if ( (document.referrer.indexOf("detnews") != -1) || (document.URL.indexOf("detnews") != -1) )
	referrer = "detnews";
if ( (document.referrer.indexOf("freep") != -1) || (document.URL.indexOf("freep") != -1) )
	referrer = "freep";

// If specified cobranded, kill the cookie referrer and fromwhere
if (document.URL.indexOf("cobranded") != -1) {
	referrer = "";
	fromwhere = "";
	fromcookie = "";
	DeleteCookie("fromwhere");
}


if (!referrer && !fromcookie)
	notfromanywhere = true;

if (notfromanywhere == false) {
	// if has a valid document.referrer and cookie, check to see that
	// Both match. If not set the cookie using referrer
	if (referrer && fromcookie) {
		if (referrer == fromcookie) {
			fromwhere = fromcookie;
		}
		if (referrer != fromcookie) {
			fromwhere = referrer;
			SetCookie("fromwhere", referrer);
		}
	}	
	if (!referrer && fromcookie) {
		fromwhere = fromcookie;
	}
	if (referrer && !fromcookie) {
		fromwhere = referrer;
		SetCookie("fromwhere", referrer);
	}
}


/******************************************************************************
    Old Marketplace Branding
******************************************************************************/
//Branded links arrays
var marketbrand = new Array();
var detnewsbrand = new Array();
var freepbrand = new Array();

marketbrand = [
'http://www.newhomenetwork.com/marketplacedetroit/',
'http://www.apartments.com/search/oasis.dll?p=detroit&Region.x=61&Region.y=9&page=SubArea&state=MI&rgn1=2&partner=detroit&prvpg=3',
'http://www.cars.com/carsapp/detroit/?szc=48124&srv=parser&act=display&tf=/index-default.tmpl',
'https://secure.detroitnewspapers.com/circ/marketplace/index.html',
'http://www.michiganjobhunter.com/',
'http://www.michiganjobhunter.com/html/postaresume.html',
'http://www.marketplacedetroit.com/index.shtml?right',
'http://www.marketplacedetroit.com/index.shtml?middle',
'http://www.marketplacedetroit.com/index.shtml?left'
];

detnewsbrand = [
'http://www.newhomenetwork.com/detnews/',
'http://www.apartments.com/search/oasis.dll?p=detroit&Region.x=61&Region.y=9&page=SubArea&state=MI&rgn1=2&partner=detnews&prvpg=3',
'http://www.cars.com/carsapp/detnews/?szc=48124&srv=parser&act=display&tf=/index-default.tmpl',
'https://secure.detroitnewspapers.com/circ/marketplace/index.html?detnews',
'http://www.michiganjobhunter.com/index.shtml?detnews',
'http://www.michiganjobhunter.com/html/postaresume.html?detnews',
'http://www.detnews.com/',
'http://www.detnews.com/',
'http://www.detnews.com/'
];

freepbrand = [
'http://www.newhomenetwork.com/freep/',
'http://www.apartments.com/search/oasis.dll?p=detroit&Region.x=61&Region.y=9&page=SubArea&state=MI&rgn1=2&partner=freep&prvpg=3',
'http://www.cars.com/carsapp/freep/?szc=48124&srv=parser&act=display&tf=/index-default.tmpl',
'https://secure.detroitnewspapers.com/circ/marketplace/index.html?freep',
'http://www.michiganjobhunter.com/index.shtml?freep',
'http://www.michiganjobhunter.com/html/postaresume.html?detnews',
'http://www.freep.com/',
'http://www.freep.com/',
'http://www.freep.com/'
];


cobrandlogo = [
'http://www.marketplacedetroit.com/index.shtml?right',
'http://www.marketplacedetroit.com/index.shtml?middle',
'http://www.marketplacedetroit.com/index.shtml?left'
];

logo = [
'http://www.detnews.com/',
'http://www.freep.com/',
'http://www.freep.com/'
]


if (!pagetype) {
	//default pagetype
	var pagetype = "normal";	
}

function swapbrands() {
//swap all the dv links
    if (fromwhere == "detnews") {
	//swap images
	document.toplogo.src = 		'http://marketplacedetroit.com/assets/images/branding/detnews_top_logo.gif';
	
	if (pagetype == "shopping") {	
		document.masthead.src = 	'/assets/images/branding/detnews_masthead_shop.gif';
		document.instructions.src = '/assets/images/branding/instructions_shop.gif';
	}
	else if (pagetype == "contest") {	
	    document.masthead.src = 	'/assets/images/branding/detnews_masthead_contest.gif';
		document.instructions.src = '/assets/images/branding/instructions_contest.gif';
	}
	else if (pagetype == "destinations") {	
	    document.masthead.src = 	'/assets/images/branding/detnews_masthead_classified.gif';
		document.instructions.src = '/assets/images/branding/instructions_destinations.gif';
	}
	else if (pagetype == "personals") {	
	    document.masthead.src = 	'/assets/images/branding/detnews_masthead_personals.gif';
		document.instructions.src = '/assets/images/branding/instructions_personals.gif';
	}
	else {
	    document.masthead.src = 	'/assets/images/branding/detnews_masthead_classified.gif';
	    document.instructions.src =	'/assets/images/branding/detnews_instructions_class.gif';
	}
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < marketbrand.length; u++) {
			if (document.links[i].href == marketbrand[u]) {
			    document.links[i].href = detnewsbrand[u];
			}
	    }
	}
    }
    else if (fromwhere == "freep") {
	//swap images
	document.toplogo.src = 		'http://marketplacedetroit.com/assets/images/branding/freep_top_logo.gif';
	
	if (pagetype == "shopping") {	
	    document.masthead.src = 	'/assets/images/branding/masthead_shop.gif';
		document.instructions.src =	'/assets/images/branding/instructions_shop.gif';
	}
	else if (pagetype == "contest") {	
	    document.masthead.src = 	'/assets/images/branding/masthead_contest.gif';
		document.instructions.src = '/assets/images/branding/instructions_contest.gif';
	}
	else if (pagetype == "destinations") {	
	    document.masthead.src = 	'/assets/images/branding/masthead_classified.gif';
		document.instructions.src = '/assets/images/branding/instructions_destinations.gif';
	}
	else if (pagetype == "personals") {	
	    document.masthead.src = 	'/assets/images/branding/masthead_personals.gif';
		document.instructions.src = '/assets/images/branding/instructions_personals.gif';
	}
	else {
	    document.masthead.src = 	'/assets/images/branding/masthead_classified.gif';
		document.instructions.src =	'/assets/images/branding/freep_instructions_class.gif';
	}
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < marketbrand.length; u++) {
		if (document.links[i].href == marketbrand[u]) {
		    document.links[i].href = freepbrand[u];
		}
	    }
	}
    }
    else {
	document.toplogo.src = 		'http://marketplacedetroit.com/assets/images/branding/cobranded_top_logo.gif';
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < marketbrand.length; u++) {
		if (document.links[i].href == cobrandlogo[u]) {
		    document.links[i].href = logo[u];
		}
	    }
	}
    }
} // swapbrands()

/******************************************************************************
    New MarketplaceDetroit Branding
******************************************************************************/

var marketplace_blank = new Array();
var marketplace_detnews = new Array();
var marketplace_freep = new Array();
var marketplace_cobranded = new Array();

marketplace_blank = [
'http://www.marketplacedetroit.com/index.html?left',
'http://www.marketplacedetroit.com/index.html?right'
];

marketplace_detnews = [
'http://www.detnews.com',
'http://www.detnews.com'
];

marketplace_freep = [
'http://www.freep.com',
'http://www.freep.com'
];

marketplace_cobranded = [
'http://www.detnews.com',
'http://www.freep.com'
];

function swapbrands_marketplacedetroit () {
    if (fromwhere == "detnews") {
	document.toplogo.src = 'http://www.marketplacedetroit.com/assets/images/marketplace/detnews_top_logo.gif';
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < marketplace_blank.length; u++) {
		if (document.links[i].href == marketplace_blank[u]) {
		    document.links[i].href = marketplace_detnews[u];
		}
	    }
	}
    }
    else if (fromwhere == "freep") {
	document.toplogo.src = 'http://www.marketplacedetroit.com/assets/images/marketplace/freep_top_logo.gif';    
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < marketplace_blank.length; u++) {
		if (document.links[i].href == marketplace_blank[u]) {
		    document.links[i].href = marketplace_freep[u];
		}
	    }
	}
    }
    else {
    	document.toplogo.src = 'http://www.marketplacedetroit.com/assets/images/marketplace/cobranded_top_logo.gif';	
	//swap links	
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < marketplace_blank.length; u++) {
		if (document.links[i].href == marketplace_blank[u]) {
		    document.links[i].href = marketplace_cobranded[u];
		}
	    }
	}
    }
}

function swapbrands_marketplacedetroit_secure () {
    if (fromwhere == "detnews") {
	document.toplogo.src = '/assets/images/marketplace/detnews_top_logo.gif';
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < marketplace_blank.length; u++) {
		if (document.links[i].href == marketplace_blank[u]) {
		    document.links[i].href = marketplace_detnews[u];
		}
	    }
	}
    }
    else if (fromwhere == "freep") {
	document.toplogo.src = '/assets/images/marketplace/freep_top_logo.gif';    
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < marketplace_blank.length; u++) {
		if (document.links[i].href == marketplace_blank[u]) {
		    document.links[i].href = marketplace_freep[u];
		}
	    }
	}
    }
    else {
    	document.toplogo.src = '/assets/images/marketplace/cobranded_top_logo.gif';	
	//swap links	
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < marketplace_blank.length; u++) {
		if (document.links[i].href == marketplace_blank[u]) {
		    document.links[i].href = marketplace_cobranded[u];
		}
	    }
	}
    }
}
/******************************************************************************
    miHomeHunt Branding
******************************************************************************/

var homes_blank = new Array();
var homes_detnews = new Array();
var homes_freep = new Array();
var homes_cobranded = new Array();

homes_blank = [
'http://www.mihomehunt.com/index.html?right',
'http://www.mihomehunt.com/index.html?left',
'http://www.homefinder.com/detroit/index_quicksrch.jhtml',
'http://www.homefinder.com/detroitv/index_map.jhtml'
];

homes_detnews = [
'http://www.detnews.com',
'http://www.detnews.com',
'http://www.homefinder.com/detroit/index_quicksrch.jhtml?detnews',
'http://www.homefinder.com/detroitv/index_map.jhtml?detnews'
];

homes_freep = [
'http://www.freep.com',
'http://www.freep.com',
'http://www.homefinder.com/detroit/index_quicksrch.jhtml?freep',
'http://www.homefinder.com/detroitv/index_map.jhtml?freep'
];

homes_cobranded = [
'http://www.detnews.com',
'http://www.freep.com',
'http://www.homefinder.com/detroit/index_quicksrch.jhtml?cobranded',
'http://www.homefinder.com/detroitv/index_map.jhtml?cobranded'
];

function swapbrands_homehunt () {
    if (fromwhere == "detnews") {
	document.toplogo.src = 'http://www.mihomehunt.com/assets/images/mihomehunt/detnews_top_logo.gif';
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < homes_blank.length; u++) {
		if (document.links[i].href == homes_blank[u]) {
		    document.links[i].href = homes_detnews[u];
		}
	    }
	}
    }
    else if (fromwhere == "freep") {
	document.toplogo.src = 'http://www.mihomehunt.com/assets/images/mihomehunt/freep_top_logo.gif';    
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < homes_blank.length; u++) {
		if (document.links[i].href == homes_blank[u]) {
		    document.links[i].href = homes_freep[u];
		}
	    }
	}
    }
    else {
    	document.toplogo.src = 'http://www.mihomehunt.com/assets/images/mihomehunt/cobranded_top_logo.gif';	
	//swap links	
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < homes_blank.length; u++) {
		if (document.links[i].href == homes_blank[u]) {
		    document.links[i].href = homes_cobranded[u];
		}
	    }
	}
    }
}

/******************************************************************************
    miWheelsHunt Branding
******************************************************************************/

var wheels_blank = new Array();
var wheels_detnews = new Array();
var wheels_freep = new Array();
var wheels_cobranded = new Array();

wheels_blank = [
'http://www.miwheelshunt.com/index.html?right',
'http://www.miwheelshunt.com/index.html?left'
];

wheels_detnews = [
'http://www.detnews.com',
'http://www.detnews.com'
];

wheels_freep = [
'http://www.freep.com',
'http://www.freep.com'
];

wheels_cobranded = [
'http://www.detnews.com',
'http://www.freep.com'
];

function swapbrands_wheelshunt () {
    if (fromwhere == "detnews") {
	document.toplogo.src = '/assets/images/miwheelshunt/detnews_top_logo.gif';
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < wheels_blank.length; u++) {
		if (document.links[i].href == wheels_blank[u]) {
		    document.links[i].href = wheels_detnews[u];
		}
	    }
	}
    }
    else if (fromwhere == "freep") {
	document.toplogo.src = '/assets/images/miwheelshunt/freep_top_logo.gif';    
	//swap links
	
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < wheels_blank.length; u++) {
		if (document.links[i].href == wheels_blank[u]) {
			document.links[i].href = wheels_freep[u];
		}
	    }
	}
    }
    else {
    	document.toplogo.src = '/assets/images/miwheelshunt/cobranded_top_logo.gif';	
	//swap links	
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < wheels_blank.length; u++) {
		if (document.links[i].href == wheels_blank[u]) {
		    document.links[i].href = wheels_cobranded[u];
		}
	    }
	}
    }
}


/******************************************************************************
    miStuffHunt Branding
******************************************************************************/

var stuff_blank = new Array();
var stuff_detnews = new Array();
var stuff_freep = new Array();
var stuff_cobranded = new Array();

stuff_blank = [
'http://www.mistuffhunt.com/index.html?right',
'http://www.mistuffhunt.com/index.html?left'
];

stuff_detnews = [
'http://www.detnews.com',
'http://www.detnews.com'
];

stuff_freep = [
'http://www.freep.com',
'http://www.freep.com'
];

stuff_cobranded = [
'http://www.detnews.com',
'http://www.freep.com'
];

function swapbrands_stuffhunt () {
    if (fromwhere == "detnews") {
	document.toplogo.src = '/assets/images/mistuffhunt/detnews_top_logo.gif';
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < stuff_blank.length; u++) {
		if (document.links[i].href == stuff_blank[u]) {
		    document.links[i].href = stuff_detnews[u];
		}
	    }
	}
    }
    else if (fromwhere == "freep") {
	document.toplogo.src = '/assets/images/mistuffhunt/freep_top_logo.gif';    
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < stuff_blank.length; u++) {
		if (document.links[i].href == stuff_blank[u]) {
			document.links[i].href = stuff_freep[u];
		}
	    }
	}
    }
    else {
    	document.toplogo.src = '/assets/images/mistuffhunt/cobranded_top_logo.gif';	
	//swap links	
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < stuff_blank.length; u++) {
		if (document.links[i].href == stuff_blank[u]) {
		    document.links[i].href = stuff_cobranded[u];
		}
	    }
	}
    }
}


/******************************************************************************
    Applause Branding
******************************************************************************/

var applause_blank = new Array();
var applause_detnews = new Array();
var applause_freep = new Array();
var applause_cobranded = new Array();

applause_blank = [
'http://www.detroitnewspapers.com/applause/index.cfm?right',
'http://www.detroitnewspapers.com/applause/index.cfm?left'
];

applause_detnews = [
'http://www.detnews.com',
'http://www.detnews.com'
];

applause_freep = [
'http://www.freep.com',
'http://www.freep.com'
];

applause_cobranded = [
'http://www.detnews.com',
'http://www.freep.com'
];

function swapbrands_applause () {
    if (fromwhere == "detnews") {
	document.toplogo.src = '/assets/images/applause/detnews_top_logo.gif';
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < applause_blank.length; u++) {
		if (document.links[i].href == applause_blank[u]) {
		    document.links[i].href = applause_detnews[u];
		}
	    }
	}
    }
    else if (fromwhere == "freep") {
	document.toplogo.src = '/assets/images/applause/freep_top_logo.gif';    
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < applause_blank.length; u++) {
		if (document.links[i].href == applause_blank[u]) {
			document.links[i].href = applause_freep[u];
		}
	    }
	}
    }
    else {
    	document.toplogo.src = '/assets/images/applause/cobranded_top_logo.gif';	
	//swap links	
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < applause_blank.length; u++) {
		if (document.links[i].href == applause_blank[u]) {
		    document.links[i].href = applause_cobranded[u];
		}
	    }
	}
    }
}


/******************************************************************************
    Death Notices Branding
******************************************************************************/

var death_blank = new Array();
var death_detnews = new Array();
var death_freep = new Array();
var death_cobranded = new Array();

death_blank = [
'http://www.detroitnewspapers.com/deathnotices/index.html?right',
'http://www.detroitnewspapers.com/deathnotices/index.html?left'
];

death_detnews = [
'http://www.detnews.com',
'http://www.detnews.com'
];

death_freep = [
'http://www.freep.com',
'http://www.freep.com'
];

death_cobranded = [
'http://www.detnews.com',
'http://www.freep.com'
];

function swapbrands_deathnotices () {
    if (fromwhere == "detnews") {
	document.toplogo.src = '/deathnotices/images/detnews_top_logo.gif';
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < death_blank.length; u++) {
		if (document.links[i].href == death_blank[u]) {
		    document.links[i].href = death_detnews[u];
		}
	    }
	}
    }
    else if (fromwhere == "freep") {
	document.toplogo.src = '/deathnotices/images/freep_top_logo.gif';    
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < death_blank.length; u++) {
		if (document.links[i].href == death_blank[u]) {
			document.links[i].href = death_freep[u];
		}
	    }
	}
    }
    else {
    	document.toplogo.src = '/deathnotices/images/cobranded_top_logo.gif';	
	//swap links	
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < death_blank.length; u++) {
		if (document.links[i].href == death_blank[u]) {
		    document.links[i].href = death_cobranded[u];
		}
	    }
	}
    }
}

function swapbrands () {

return 1;

}

/******************************************************************************
    Tee-It-Up Branding
******************************************************************************/

var teeitup_blank = new Array();
var teeitup_detnews = new Array();
var teeitup_freep = new Array();
var teeitup_cobranded = new Array();

teeitup_blank = [
'http://www.teeitupmichigan.com/index.html?right',
'http://www.teeitupmichigan.com/index.html?left'
];

teeitup_detnews = [
'http://www.detnews.com',
'http://www.detnews.com'
];

teeitup_freep = [
'http://www.freep.com',
'http://www.freep.com'
];

teeitup_cobranded = [
'http://www.detnews.com',
'http://www.freep.com'
];

function swapbrands_teeitup () {
    if (fromwhere == "detnews") {
	document.toplogo.src = 'http://www.teeitupmichigan.com/assets/images/teeitup/detnews_top_logo.gif';
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < teeitup_blank.length; u++) {
		if (document.links[i].href == teeitup_blank[u]) {
		    document.links[i].href = teeitup_detnews[u];
		}
	    }
	}
    }
    else if (fromwhere == "freep") {
	document.toplogo.src = 'http://www.teeitupmichigan.com/assets/images/teeitup/freep_top_logo.gif';    
	//swap links
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < teeitup_blank.length; u++) {
		if (document.links[i].href == teeitup_blank[u]) {
			document.links[i].href = teeitup_freep[u];
		}
	    }
	}
    }
    else {
    	document.toplogo.src = 'http://www.teeitupmichigan.com/assets/images/teeitup/cobranded_top_logo.gif';	
	//swap links	
	for (var i = 0; i < document.links.length; i++) {
	    for (var u = 0; u < teeitup_blank.length; u++) {
		if (document.links[i].href == teeitup_blank[u]) {
		    document.links[i].href = teeitup_cobranded[u];
		}
	    }
	}
    }
}

function swapbrands () {

return 1;

}


/******************************************************************************
    Site Catalyst
******************************************************************************/

var gpaper = 'gpaper205';

if (fromwhere == "detnews") {
    gpaper = 'gpaper123';
}
else if (fromwhere == "freep") {
    gpaper = 'gpaper204';
}
else {
    gpaper = 'gpaper205';
}

//For popup search window to target original window


//EOF

