function showMe(mn) {
	getElement(mn).style.visibility = "visible";
}

function hideMe(mn) {
	getElement(mn).style.visibility = "hidden";
}

function highlightMe(me,bgcolor) {
	if (!getElement(me).style.disableHighlight) {
		getElement(me).style.backgroundColor = bgcolor;
	}
}

function unhighlightMe(me) {
	if (!getElement(me).style.disableHighlight) {
		getElement(me).style.backgroundColor = "";
	}
}

function highlightObj(me,bgcolor){
	me.style.backgroundColor = bgcolor;
}

function unhighlightObj(me,bgcolor){
	me.style.backgroundColor = "";
}

function submitForm(form){
	getElement(form).submit();
}

function confirmDelete(url){
	if(window.confirm("Are you sure you would like to remove this item?")){
		window.location = url;
	}
}

function cellChanged(cid) {
	if (getElement(cid)) {
		var val1 = getElement(cid).value;
		var val2 = getElement(cid).style.originalValue;
		if (val1 != val2) return true; return false;
	}
	else {
		return false;
	}
}

function highlightCell(cid) {
	if (getElement(cid)) {
		if (cellChanged(cid)) {
			getElement(cid).style.color = '#FF0000'; 
		}
		else {	
			getElement(cid).style.color = '';
		}
	}
}

function create_xmlHttp_Old () {
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request) {
    alert("Error initializing XMLHttpRequest!");
  }
  return request;
}

function create_xmlHttp() {
	var crossxhr = false;
	if(window.XMLHttpRequest) {
		crossxhr = new XMLHttpRequest();
		if(crossxhr.overrideMimeType) {
			crossxhr.overrideMimeType('text/xml');
		}
	} else if(window.ActiveXObject) {
		try {
			crossxhr = new ActiveXObject('Msxml2.XMLHTTP');
		} catch(e) {
			try {
				crossxhr = new ActiveXObject('Microsoft.XMLHTTP');
			} catch(e) {
				crossxhr = false;
			}
		}
	}
	if (!crossxhr) {
		alert("Error initializing XMLHttpRequest (2)!");
	}
	return crossxhr;
}

function getElement(obj_id) {
	if (document.all && !(document.getElementById)) {
		return(document.all(obj_id));
	} else if (document.layers) {
		return(document.layers(obj_id));
	} else if (document.getElementById) {
		return(document.getElementById(obj_id));
	} 
}

function waitMessage(cell) {
	getElement(cell).savedInnerHTML = getElement(cell).innerHTML;
	getElement(cell).innerHTML = "<img src='//" + cnstr_path + "/netcentive/flash/retrieving_data.gif' border='0'>";
}

function close_ajax(t) {
	t.parentNode.innerHTML = "";
}

function ajax_wrapper(url,dont_use_wrapper) {
	if (location.href.indexOf("egroupnet.com") <= 0) {
		if (!dont_use_wrapper) {
			url = "/netcentive/includes/ajax_wrapper.asp?url=" + escape(url);
		}
	}
	return url;
}

function getPage(url, display_loc, plus_minus_loc, use_plus_minus, highlightRow1, highlightRow2, locking_var, dont_use_wrapper, callback_function, add_close_button) {
	var ok_to_do = true;
	if (locking_var != "" && locking_var != undefined) {
		if (document[locking_var] == undefined) {document[locking_var] = false;}
		if (document[locking_var]) {
			ok_to_do = false;
		} else {
			document[locking_var] = true;
		}
	}
	
	if (ok_to_do) {
		waitMessage(display_loc);
		if (plus_minus_loc != undefined && plus_minus_loc != '') {
			if (use_plus_minus) {
				getElement(plus_minus_loc).plusHTML = getElement(plus_minus_loc).innerHTML;
				getElement(plus_minus_loc).minusHTML = "<img class='bttn' src='//" + cnstr_path + "/netcentive/images/minus.gif' border='0' onmouseup='hidePage(" + unescape("%22") + display_loc + unescape("%22") + ", " + unescape("%22") + plus_minus_loc + unescape("%22") + ", " + unescape("%22") + locking_var + unescape("%22") + ");' />";
			} else {
				getElement(plus_minus_loc).plusHTML = getElement(plus_minus_loc).innerHTML;
				getElement(plus_minus_loc).minusHTML = "<a href='javascript:hidePage(" + unescape("%22") + display_loc + unescape("%22") + ", " + unescape("%22") + plus_minus_loc + unescape("%22") + ", " + unescape("%22") + locking_var + unescape("%22") + ");'>close</a>";
			}
			getElement(plus_minus_loc).innerHTML = "";
		}
	
		if (highlightRow1 != undefined && highlightRow1 != "") {
			highlightMe(highlightRow1,"#FFFFD1");
			getElement(display_loc).style.highlightRow1 = highlightRow1;
			getElement(highlightRow1).style.disableHighlight = true;
		}
	
		if (highlightRow2 != undefined && highlightRow2 != "") {
			highlightMe(highlightRow2,"#FFFFD1");
			getElement(display_loc).style.highlightRow2 = highlightRow2;
			getElement(highlightRow2).style.disableHighlight = true;
		}
	

		//alert(url);
		//alert(location.href);
	
		var use_wrapper = false;
		if (url.indexOf("ttp://www.egroupnet.com/netcentive") > 0) {
				// we can always get at this directory via a virtual from any of our domains
				dont_use_wrapper = true;
				url = url.replace("http://www.egroupnet.com","");
		}
		if (location.href.indexOf("egroupnet.com") <= 0) { use_wrapper = true; }
		if (location.href.indexOf("10.0.0.128:85") >= 1) { use_wrapper = false;}	//allow direct ip debugging
		if (url.substr(0,4) == "http") {
			var test_url = url.replace("http://","");
			var test_href = location.href.replace("http://","");
			if (test_url.substring(0,test_url.indexOf("/")) != test_href.substring(0,test_href.indexOf("/"))) { use_wrapper = true; }
		}
		if (use_wrapper) {
			if (!dont_use_wrapper) {
				url = "/netcentive/includes/ajax_wrapper.asp?url=" + escape(url);
			}
		}

/*
		if (location.href.indexOf("egroupnet.com") <= 0 || url.substr(0,4) == "http") {
			if (!dont_use_wrapper) {
				url = "/includes/ajax_wrapper.asp?url=" + escape(url);
			}
		}
*/	

		//alert(url);
		
		var xmlhttp = create_xmlHttp();
		xmlhttp.open("POST",url,true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				getElement(display_loc).innerHTML = xmlhttp.responseText;
				if (add_close_button) { getElement(display_loc).innerHTML = "<input type='button' class='bttn' value='CLOSE' onclick='hidePage(\"" + display_loc + "\", \"" + plus_minus_loc + "\", \"" + locking_var + "\");' />" + getElement(display_loc).innerHTML; }
				if (plus_minus_loc != undefined && plus_minus_loc != '') {
					getElement(plus_minus_loc).innerHTML = getElement(plus_minus_loc).minusHTML;
				}
				if (typeof(callback_function) == 'function') { callback_function(); }
			}
		};
		xmlhttp.send("");
	}
}


function getAjaxResponse(url, callback_function, dont_use_wrapper, dont_unescape) {
	// if callback function is specified, then issue this async. else wait for response and return it

	//alert(url);
	//alert(location.href);
	
	var use_wrapper = false;
	if (location.href.indexOf("egroupnet.com") <= 0) { use_wrapper = true; }
	if (url.substr(0,4) == "http") {
		var test_url = url.replace("http://","");
		var test_href = location.href.replace("http://","");
		if (test_url.substring(0,test_url.indexOf("/")) != test_href.substring(0,test_href.indexOf("/"))) { use_wrapper = true; }
	}
	if (use_wrapper) {
		if (!dont_use_wrapper) {
			url = "/netcentive/includes/ajax_wrapper.asp?url=" + escape(url);
		}
	}
	
	//alert(url);

	var async =  (typeof(callback_function) == "function") ? true : false;

	var xmlhttp = create_xmlHttp();
	xmlhttp.open("POST",url,async);
	if (async) {
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
				if (typeof(callback_function) == 'function') {
					var ret = xmlhttp.responseText;
					if (!dont_unescape) ret = unescapeHTML(ret);
					callback_function(ret); 
				}
			}
		};
	}
	xmlhttp.send("");
	
	
	var ret = ""
	if (!async) ret = xmlhttp.responseText;
	if (!dont_unescape) ret = unescapeHTML(ret);
	return ret;
}


function unescapeHTML (str) {
   var div = document.createElement('div');
   div.innerHTML = str;
   return div.innerHTML;
}

function hidePage(display_loc,plus_minus_loc,locking_var) {
	if (locking_var != "" && locking_var != undefined) {
		document[locking_var] = false;
	}
	getElement(display_loc).innerHTML = "";
	if (getElement(plus_minus_loc) != undefined) {
		getElement(plus_minus_loc).innerHTML = getElement(plus_minus_loc).plusHTML;
	}
	
	var blah = getElement(display_loc).style.highlightRow1;
	if (blah != "" && blah != undefined) {
		getElement(blah).style.disableHighlight = false;
		unhighlightMe(blah);
	}
	
	var blah = getElement(display_loc).style.highlightRow2;
	if (blah != "" && blah != undefined) {
		getElement(blah).style.disableHighlight = false;
		unhighlightMe(blah);
	}
}

function savePageloadStats(sid,loadtime,runSQLs,downloadtime,custom_page) {
	try {
		var domain = location.href;
		domain = domain.substring(domain.indexOf("//")+ 2);
		domain = domain.substring(0,domain.indexOf("/"));
		var xmlhttp = create_xmlHttp();
		var pg = "//" + domain + "/netcentive/includes/helpers/savePageloadStats.asp";
		if (custom_page != "" && custom_page != undefined) {
			pg = custom_page;	
		}
		xmlhttp.open("POST",pg + "?sid=" + sid + "&loadtime=" + loadtime + "&runSQLs=" + runSQLs + "&downloadtime=" + downloadtime,true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState==4) {
			}
		};
		xmlhttp.send("");
	} catch(err) {
	}

}

function getElementsByClassName(oElm, strTagName, oClassNames){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    var arrRegExpClassNames = new Array();
    if(typeof oClassNames == "object"){
        for(var i=0; i<oClassNames.length; i++){
            arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
        }
    }
    else{
        arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
    }
    var oElement;
    var bMatchesAll;
    for(var j=0; j<arrElements.length; j++){
        oElement = arrElements[j];
        bMatchesAll = true;
        for(var k=0; k<arrRegExpClassNames.length; k++){
            if(!arrRegExpClassNames[k].test(oElement.className)){
                bMatchesAll = false;
                break;                      
            }
        }
        if(bMatchesAll){
            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements);
}

function hoverFix(element,tag,cls,includeIE7) {
	var agent = navigator.userAgent;
	if ((agent.indexOf("MSIE 7") <= 0 || includeIE7) && agent.indexOf("MSIE") > 0) {
		var elem = (element == "" || element == undefined ) ? document : getElement(element);
		if (cls == undefined || cls == "") {
			if (tag != "" && tag != undefined && elem) {
				var sfEls = elem.getElementsByTagName(tag);
			}
		} else {
			var sfEls = getElementsByClassName(document,tag,cls);
		}
		
		if (sfEls) {
			for (var i=0; i<sfEls.length; i++) {
				sfEls[i].onmouseover=function() {
					this.className+=" jshover";
				};
				sfEls[i].onmouseout=function() {
					this.className=this.className.replace(new RegExp(" jshover\\b"), "");
				};
			}
		}
	}
}

function autoReload(find_vars,doModal,curr_page,curr_querystring) {
	/*
	rebuilds the querystring and replaces values of find_vars with document elements
	
	*/
	var query = (curr_querystring != "" && curr_querystring != undefined) ? curr_querystring : window.location.search.substring(1);
	var vars = query.split("&");
	var finds = find_vars.split(";");
	var founds = new Array();
	for (var j = 0;j<finds.length;j++) {
		founds.push(false);
	}
	var qs = "";
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] != "") {
			for (var j = 0;j<finds.length;j++) {
				if (pair[0] == finds[j]) {
					pair[1] = getDocumentElementValue(pair[0]);
					founds[j] = true;
				}
			}
			if (pair[1] != "") {
				if (qs == "") {qs = "?";} else {qs += "&";}
				qs += pair[0] + "=" + pair[1];
			}
		}
	} 
	for (var j = 0;j< founds.length;j++) {
		if (!founds[j]) {
			if (getDocumentElementValue(finds[j]) != "") {
				if (qs == "") {qs = "?";} else {qs += "&";}
				qs += finds[j] + "=" + getDocumentElementValue(finds[j]);
			}
		}
	}
	var pg = (curr_page != "" && curr_page != undefined) ? curr_page : window.location.href;
	if (pg.indexOf("?") > 0) {
		pg = pg.substr(0,pg.indexOf("?"));
	}
	qs = pg + qs;
	if (doModal) {
		doModalPage(qs);
	} else {
		window.location.href = qs;
	}
}

function getDocumentElementValue(id) {
	/* only works with text and selects right now */
	if (getElement(id) != undefined) {
		switch (getElement(id).type) {
			case "select-one" :
				return getElement(id).options[getElement(id).selectedIndex].value;
				break;
			case "text" :
				return getElement(id).value;
				break;
			case "hidden" :
				return getElement(id).value;
				break;
			case "textarea" :
				return getElement(id).value;
				break;
			case "checkbox", "radio" :
				var ret = "";
				var chks = document.getElementsByName(id);
				for (var i = 0; i < chks.length; i++) {
					if (chks[i].checked) {
						if (ret != "") ret += ",";
						ret += chks[i].value.replace(",","");
					}
				}
				return ret;
				break;
			default :
			//alert(getElement(id).type);
				return false;
		}
	} else {
		return "";
	}
	
}

function addEvent(elm, evType, fn, useCapture) {
  if (elm.addEventListener) {
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent) {
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } 
} 

function swapProductImage(file) {
//	if (getElement("product_image_300").src.indexOf("media.egroupnet") > 0) {
		var lg_img_loc = getElement("product_image_300").src;
		getElement("product_image_300").src = lg_img_loc.substr(0,lg_img_loc.lastIndexOf("/")+1) + file;

		var lnk = getElement("zoom_link").href;
		lnk = lnk.substr(0,lnk.lastIndexOf("/")+1);
		getElement("zoom_link").href = lnk + file;
		getElement("zoom_link2").href = lnk + file;
//	} else {
//		getElement("product_image_300").src = "http://" + cnstr_path + "/netcentive/images/dynamic/getProductImage.aspx?file=" + file + "&size=300";
//		var lnk = getElement("zoom_link").href;
//		lnk = lnk.substr(0,lnk.indexOf('?')) + "?file=" + file + "&size=1000";
//		getElement("zoom_link").href = lnk;
//	}
}

function trim(sString) {
	while (sString.substring(0,1) == ' ') {
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ') {
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}




//////////

var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}


function table2excel(tbl_id,filename) {
	var frm = document.createElement("form");
	frm.setAttribute("target","_blank");
	frm.setAttribute("action","http://" + cnstr_path + "/netcentive/includes/js_table2excel.asp");
	frm.setAttribute("method","POST");
	//frm.setAttribute("enctype","multipart/form-data");
	var tbl = document.createElement("input");
	tbl.setAttribute("type","hidden");
	//tbl.setAttribute("value",Base64.encode("<table border='1'>" + getElement(tbl_id).innerHTML + "</table>"));
	tbl.setAttribute("value","<table border='1'>" + getElement(tbl_id).innerHTML + "</table>");
	tbl.setAttribute("name","tbl");
	tbl.setAttribute("id","tbl");
	frm.appendChild(tbl);

	if (filename != undefined) {
		var fl = document.createElement("input");
		fl.setAttribute("type","hidden");
		fl.setAttribute("value",filename);
		fl.setAttribute("name","fl");
		fl.setAttribute("id","fl");
		frm.appendChild(fl);
	}

	document.body.appendChild(frm);
	frm.submit();
}

function div2word (div_id,filename) {
	var frm = document.createElement("form");
	frm.setAttribute("target","_blank");
	frm.setAttribute("action","/netcentive/includes/js_text2word.asp");
	frm.setAttribute("method","POST");
	//frm.setAttribute("enctype","multipart/form-data");
	var tbl = document.createElement("input");
	tbl.setAttribute("type","hidden");
	//tbl.setAttribute("value",Base64.encode("<table border='1'>" + getElement(tbl_id).innerHTML + "</table>"));
	tbl.setAttribute("value",getElement(div_id).outerHTML);
	tbl.setAttribute("name","txt");
	tbl.setAttribute("id","txt");
	frm.appendChild(tbl);

	if (filename != undefined) {
		var fl = document.createElement("input");
		fl.setAttribute("type","hidden");
		fl.setAttribute("value",filename);
		fl.setAttribute("name","fl");
		fl.setAttribute("id","fl");
		frm.appendChild(fl);
	}

	document.body.appendChild(frm);
	frm.submit();
}

function disableSubmitButtons(frm) {
	// add this function to a form's onsubmit event: <form onsubmit="disableSubmitButtons(this);">

	var inputs = frm.getElementsByTagName('input');

	for (var i = 0; i < inputs.length; i++) {
		if (inputs[i].type.toLowerCase() == 'submit') {
			inputs[i].disabled = true;
		}
	}
}
function CheckUserName(fld,promo){
	var UName = getElement(fld).value;
	if (UName.length > 0){
		getPage('http://' + cnstr_path + '/netcentive/includes/UserCheck.asp?UName=' + UName + '&Promo=' + promo,'check_user','','','','','',true,'',false);
	}
}