Replace broken images

//Dev image replacement to fix broken images on dev site
cfReplaceDevImages('devdomain.us','http://livedomain.com');

function cfReplaceDevImages(devDomain,liveDomainUrl){
if(window.location.host.indexOf(devDomain) == -1) return false;
jQuery(window).bind('load', function(){
jQuery('img').each(function(){
if((typeof this.naturalWidth != "undefined" && this.naturalWidth == 0) || this.readyState == 'uninitialized'){
var oldUrl = jQuery(this).attr('src').replace(/^.*//[^/]+/, '');
var newUrl = liveDomainUrl + oldUrl;
jQuery(this).attr('src',newUrl);
}
});
})
}

Format Number With Commas

revised code
function formatNumberWithCommas(number) {
var sum = number;

var tens=0;
var sumPlace = sum;

while ( sumPlace > 1 ) {
sumPlace = sumPlace / 10;
tens++;
}

// now we have the number of digits left of the decimal
var commaCount=0;
if ( tens > 1 ) {
tens = tens/3;
commaCount++;

}

// now we have the number of commas
var sumString = sum + "";
for (var i = 0; i < commaCount; i++) formattedSum = sumString.substr(0, sumString.length-3) + "," + sumString.substr(sumString.length-3); return formattedSum; }

Disable form submit on enter

<form action="sent.asp" method="post" onkeypress="return disableEnterKey(event);">

<script language="JavaScript">
function disableEnterKey(e)
{
var key;

if(window.event)
key = window.event.keyCode; //IE
else
key = e.which; //firefox

if(key == 13)
return false;
else
return true;
}
</script>

Document Onload

if (window.attachEvent) {window.attachEvent('onload', your_function);}
else if (window.addEventListener) {window.addEventListener('load', your_function, false);}
else {document.addEventListener('load', your_function, false);}

Js Cookie

function ddtSetCookie(c_name,value,exdays){
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays==null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}

function ddtGetCookie(c_name){
var i,x,y,ARRcookies=document.cookie.split(";");
for(i = 0; i<ARRcookies.length; i++){
x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^s+|s+$/g,"");
if(x == c_name) return unescape(y);
}
}

Remember scroll position

////////////////////////////////
// fixscroll.js:
// call loadP and unloadP when body loads/unloads and scroll position will not move
function getScrollXY() {
var x = 0, y = 0;
if( typeof( window.pageYOffset ) == 'number' ) {
// Netscape
x = window.pageXOffset;
y = window.pageYOffset;
} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
// DOM
x = document.body.scrollLeft;
y = document.body.scrollTop;
} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
// IE6 standards compliant mode
x = document.documentElement.scrollLeft;
y = document.documentElement.scrollTop;
}
return [x, y];
}

function setScrollXY(x, y) {
window.scrollTo(x, y);
}
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function loadP(pageref){
x=readCookie(pageref+'x');
y=readCookie(pageref+'y');
setScrollXY(x,y)
}
function unloadP(pageref){
s=getScrollXY()
createCookie(pageref+'x',s[0],0.1);
createCookie(pageref+'y',s[1],0.1);
}