
	
var Delegate = {}
Delegate.create = function(obj, func)
{
	var f = function()
	{
		var target = arguments.callee.target;
		func = arguments.callee.func;

		return func.apply(target, arguments);
	};

	f.target = obj;
	f.func = func;

	return f;
}
				
var BrowserCheckSimple = {};

BrowserCheckSimple.isIE = function()
{
    return navigator.appName=="Microsoft Internet Explorer" ? true : false;
}

BrowserCheckSimple.ieVersion = function()
{
	if( ! BrowserCheckSimple.isIE() ) return null;
	var r = /MSIE (\d)\./;
	var v  = navigator.appVersion;
	return parseInt( r.exec(v)[1] );
}

BrowserCheckSimple.isFirefox = function()
{    
    return (navigator.userAgent.indexOf("Firefox") > 0) ? true : false;
}


function tamePngTag(el)
{
	var is;
	if(!el){
		el = document.body;
	}
	if(document.all && window.external && window.ActiveXObject) {
		// img tags 
		is = el.getElementsByTagName('IMG');
		for(x=0; x<is.length; x++) {
			if(is[x].src.indexOf('.png')!=-1 || is[x].src.indexOf('.res')!=-1) {
				is[x].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+is[x].src+"', sizingMethod='scale')";
				if (is[x].src.indexOf(document.domain) > -1) {
					is[x].src = is[x].src.substring(0, is[x].src.indexOf("images/")) + "images/blind.gif";
				}
			}
		}
	}
 }
 
 function tamePngStyle( el )
 {
	var img = el.getStyle( 'background-image' );
	if( typeof( img.match ) == 'function' ) {
		img = img.match( /url\(("|')([^'"]+)/ )[2];
		el.setStyle( 'filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+img+"', sizingMethod='image')" );
		el.setStyle( 'background-image', 'url("images/blind.gif")' );
	}
 }	
 
 

function initMain()
{
	
	$("cartAltLink").setStyle( "display", "none" );
	
	adjustContainerHeight();
	$('container').addEvent( 'enterFrame', adjustContainerHeight );
	setInterval( function() { $('container').fireEvent( 'enterFrame' ); }, 1000 );
	
	
	if(  BrowserCheckSimple.isIE() && BrowserCheckSimple.ieVersion() < 7 ) {
		tamePngStyle( $('leftMeta') );	
	}
	
	if( $('btnLogin') ) {
		$('btnLogin').onclick = showloginScreen;
	}else{
		if( $('btnLogout') ) {
			$('btnLogout').onclick = logout;	
		}
	}
	
	initControls();
	handleBacklinks();

}



function handleBacklinks()
{
	$$("div#footLinks a").each( function( e ) 
		{
			e.setStyles( {'font-size':'10px'} );	
		}
	);
}



function initControls()
{
	oldFocus = null;
	var fields = $$('input, textarea, select');
	
	fields.each( function( elem )
		{
			var type = elem.getProperty( 'type' );
			elem.setStyle('border', '1px solid');
			if( type != 'submit' && type != 'button' ) { 
				elem.addEvents (
					{
						'focus' : function() { loseFocus(); oldFocus = this; var ref = this; this.setStyles( {'border' : '1px solid #00CCFF', 'background-color' : '#FEFEFE' } )} 
					}
				);
			}
		}
	);
}

function loseFocus()
{
	if( oldFocus != null ) {
		oldFocus.setStyle('border', '1px solid');
		oldFocus.setStyle('background-color', '#F0F6F7');
	}
}



function adjustContainerHeight()
{
	var h = $('footer').getTop();
	$('container').setStyle( 'height', h );
	//alert( h );
}


function setCartStatus()
{
	var status = CART_FILLED;
	if ( status ) {
		$('cartIcon').addEvent( 'click', function() { window.location.href = xcart_web_dir + "/cart.php" } );
		$('cartIcon').addClass( 'cartIconFilled' );
		$('cartIcon').removeClass( 'cartIconEmpty' );
	}else{
		$('cartIcon').addClass( 'cartIconEmpty' );
		$('cartIcon').removeClass( 'cartIconFilled' );
	}
}

function number_format( n, delimDecimal, delimThousand, debug)
{
	if( delimDecimal == null ) delimDecimal = ',';
	if( delimThousand == null ) delimThousand = '.';
	n = Math.round( n * 100 ) / 100 ;
	
	// add thousandDelim
	var nStr = n.toString();
	if(nStr.indexOf('.') == -1) return nStr + delimDecimal + '00';
	var tokens = nStr.split('.');
	if(tokens[1].length < 2) tokens[1] += "0";
	nStr = tokens[0]+delimThousand+tokens[1];
	
	// add DecimalDelim
	x = nStr.split('.');
	x1 = x[0];
	
	x2 = x.length > 1 ? delimDecimal + x[1] : "";
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + delimThousand + '$2');
	}
	
	return x1 + x2;
}

function logout()
{
	
}

function login()
{
	
}

	function isValidEmail(email, required) {
	    if (required==undefined) {   // if not specified, assume it's required
	        required=true;
	    }
	    if (email==null) {
	        if (required) {
	            return false;
	        }
	        return true;
	    }
	    if (email.length==0) {  
	        if (required) {
	            return false;
	        }
	        return true;
	    }
	    if (! allValidChars(email)) {  // check to make sure all characters are valid
	        return false;
	    }
	    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
	        return false;
	    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
	        return false;
	    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
	        return false;
	    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
		return false;
	    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
		return false;
	    }
	    return true;
	}


	function allValidChars(email) {
	  var parsed = true;
	  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
	  for (var i=0; i < email.length; i++) {
	    var letter = email.charAt(i).toLowerCase();
	    if (validchars.indexOf(letter) != -1)
	      continue;
	    parsed = false;
	    break;
	  }
	  return parsed;
	}

	
	var Position = {
	  cumulativeOffset: function(element) {
		var valueT = 0, valueL = 0;
		do {
		  valueT += element.offsetTop  || 0;
		  valueL += element.offsetLeft || 0;
		  element = element.offsetParent;
		} while (element);
		return [valueL, valueT];
	  }
}
