function $2(id, doc)
{
    if(doc == null){
        if(document.getElementById){ return document.getElementById(id); }
        else if(document.all){ return document.all(id); }
    } else {
        if(doc.getElementById){ return doc.getElementById(id); }
        else if(doc.all){ return doc.all(id); }
    }
}

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};


function AddOpt(ddl, t,v, len){
    var opt = document.createElement('option'); opt.text = t; opt.value = v;    
    try { ddl.options.add(opt, len); } catch(ex) { ddl.add(opt); }
}
function PopulateDDL(ddlId, vals){
    if(BrowserDetect.version == '6' && BrowserDetect.browser == 'Explorer')
    {
        var ddl = $2(ddlId);
        ddl.options.length = 0;
        var items = vals.split(',');
        for(i = 0; i < items.length; i++){
            var item = items[i].split('|');
            AddOpt(ddl, item[0], item[1], null);
        }
        ddl.selectedIndex = 0;
        ddl.disabled = false;    
    }
    else{
        var ddl = $2(ddlId);
        ddl.options.length = 0;
        var items = vals.split(',');
        for(i = items.length - 1; i >= 0; i--){
            var item = items[i].split('|');
            AddOpt(ddl, item[0], item[1], null);
        }
        ddl.selectedIndex = 0;
        ddl.disabled = false;    
    }       
}
function ClearAndDisableDDL(ddlId){
    var ddl = $2(ddlId);
    ddl.options.length = 0;
    ddl.disabled = true;
}
function GetAttribute(obj, attrib){        
    try{ if(obj != null && obj.attributes[attrib] != null && obj.attributes[attrib].value.length > 0) return obj.attributes[attrib].value; }
    catch(e){ return '';}
}
function ClearDropDown(id) { $2(id).options.length = 0; }



function toggleBlock(blockId) { if($2(blockId).style.display == "block") hideBlock(blockId); else showBlock(blockId); }
function showBlock(blockId)	{ $2(blockId).style.display = "block"; }
function show(blockId)	{ $2(blockId).style.display = ""; }
function hideBlock(blockId)	{ $2(blockId).style.display = "none"; }
function check(chkBoxId) { $2(chkBoxId).checked = true; }
function uncheck(chkBoxId) { $2(chkBoxId).checked = false; }
function disable(chkBoxId) { $2(chkBoxId).checked = false; $2(chkBoxId).disabled = true; }
function disableTxt(chkBoxId) { $2(chkBoxId).disabled = true; }
function enable(chkBoxId) { $2(chkBoxId).disabled = false; }
function deleteAlert(){ return confirm ("Are you sure you want to Delete this? Continue?"); }

function checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 0;
	for(z=0; z<theForm.length;z++) {
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall') {
		theForm[z].checked = theElement.checked;
		}
	}
}

function checkUncheckAll2(theElement, chked) {
	var div = $2(theElement);
	var fields = div.getElementsByTagName('input');	
	for(z=0; z<fields.length;z++) { fields[z].checked = chked; }	
}
	
function ValidateGroup(fields){
    var missing = false;
    for(i=0;i < fields.length;i++){
        var field = fields[i];        
        if(field.attributes != null && field.attributes['required'] != null && field.disabled == false){            
            if(field.value == null || field.value.length == 0)
            {             
                missing = true; 
                if(field.attributes['class']!=null &&field.attributes['class'].value.indexOf('required') == -1)
                    field.attributes['class'].value += 'required'; 
            }
            else if(field.attributes['class'] != null){ 
                var s = field.attributes['class'].value;
                field.attributes['class'].value = s.replace('required','');
            }
        } else if(field.disabled == true && field.attributes != null){
            var s = field.attributes['class'].value;
            field.attributes['class'].value = s.replace('required','');
        }
    }  
    return missing;
}


function AsyncSelect(url, params, callback, callbackargs)
{        
    var d = new Date();     
    $.ajax({
       type: "GET",
       url: url + '&timestamp='+d.getTime(),
       dataType: "html",
       data: params,       
       success: function(data, textStatus){ callback(data, callbackargs); }       
    });      
}

function SyncSelect(url)
{             
    var html = $.ajax({
       type: "GET",
       url: url,
       async: false,
       dataType: "html"       
    }).responseText;    
    
    return html;
}


function UsedInReportClicked(imageDownloadID, checked){
    AsyncSelect('../admin/AdminHandler.ashx?cmd=UsedInMag&imageDownloadID='+imageDownloadID+'&checked='+checked, null, UsedInReportChecked, null);
}
function UsedInReportChecked(data, callbackargs){}

// ***************** LIGHT BOX ********************
function AddToLB(imgId, eventId, addedId){        
    AsyncSelect('../site/Cart.ashx?cmd=AddToLightBox&imgId='+imgId+'&eventId='+eventId, null, AddedToLightBox, addedId);
}
function RemoveFromLB(imgId, eventId, addedId){    
    AsyncSelect('../site/Cart.ashx?cmd=RemoveFromLightBox&imgId='+imgId+'&eventId='+eventId, null, RemovedFromLightBox, addedId);    
}
function AddedToLightBox(data, callbackargs){
    //alert(data);
    if(callbackargs != null && callbackargs.length > 0) $2(callbackargs).style.display = '';
}
function RemovedFromLightBox(data, callbackargs){
    //alert(data); 
    var args = callbackargs;    
    if(args != null){
        if(args.length > 0 && args.indexOf('.aspx') == -1)
            $2(args).style.display = 'none';
        else if(args.length > 0 && args.indexOf('.aspx') != -1)
            window.location = args;
    }
}
function RemoveAllFromLB(){ AsyncSelect('../site/Cart.ashx?cmd=RemoveAllFromLightBox', null, AllRemoved, null); }
function RemoveSelectedFromLB(ids){ AsyncSelect('../site/Cart.ashx?cmd=RemoveSelectedFromLightBox&ids=' + ids, null, AllRemoved2, null); }
function AllRemoved2(data, callbackargs){ window.location = 'lightbox.aspx'; }
function AllRemoved(data, callbackargs){ alert(data); window.location = 'lightbox.aspx'; }
// ***************** LIGHT BOX ********************


// ***************** CART IMAGES ********************
function AddToCart(imgId, eventId, addedId){ 
    AsyncSelect('../site/Cart.ashx?cmd=AddToCart&imgId='+imgId+'&eventId='+eventId, null, AddedToCart, addedId);    
}
function RemoveFromCart(imgId, eventId, addedId){    
    AsyncSelect('../site/Cart.ashx?cmd=RemoveFromCart&imgId='+imgId+'&eventId='+eventId, null, RemovedFromCart, addedId);
}
function RemoveItemFromCart(cartItemID){    
    if(deleteAlert())
        AsyncSelect('../site/Cart.ashx?cmd=RemoveItemFromCart&cartItemID='+cartItemID, null, RemovedFromCart, 'cart.aspx');
}
function AddedToCart(data, callbackargs){
    //alert(data);
    var args = callbackargs;    
    if(args != null){
        if(args.length > 0 && args.indexOf('.aspx') == -1) 
            $2(args).style.display = '';
        else if(args.length > 0 && args.indexOf('.aspx') != -1) 
            window.location = args;
    }
}
function RemovedFromCart(data, callbackargs){
    //alert(data); 
    var args = callbackargs;    
    if(args != null){
        if(args.length > 0 && args.indexOf('.aspx') == -1) 
            $2(args).style.display = 'none';
        else if(args.length > 0 && args.indexOf('.aspx') != -1) 
            window.location = args;
    }
}
// ***************** CART IMAGES ********************



// ***************** BOOKS ********************
function AddBookToCart(bookId, ltdEd,args){
    AsyncSelect('../site/Cart.ashx?cmd=AddBookToCart&bookId='+bookId+'&ltdEd='+ltdEd, null, BookAddedToCart, args);
}
function RemoveBookFromCart(bookId, args){     
    AsyncSelect('../site/Cart.ashx?cmd=RemoveBookFromCart&bookId='+bookId, null, BookAddedToCart, args);
}
function RemoveBookFromCart2(cartItemID){     
    AsyncSelect('../site/Cart.ashx?cmd=RemoveBookFromCart2&cartItemID='+cartItemID, null, BookAddedToCart, 'cart.aspx');
}
function RemoveBookCopiesFromCart(bookId, args){     
    AsyncSelect('../site/Cart.ashx?cmd=RemoveBookCopiesFromCart&bookId='+bookId, null, BookAddedToCart, args);
}
function BookAddedToCart(data, callbackargs){
    try{
    var args = callbackargs;
    if(args != null && args.length > 0 && args.indexOf('.aspx') != -1){ window.location = args; }
    else { window.location = '../products/index.aspx'; }
    } catch(e){ window.location.reload(); return; }
}

// ***************** BOOKS ********************



// ******************** MERCHANDISE ***************************
function AddMerchandiseToCart(productItemID, sizeID, colorID, args){
    AsyncSelect('../site/Cart.ashx?cmd=AddMerchandiseToCart&productItemID='+productItemID+'&sizeID='+sizeID+'&colorID='+colorID, null, Merchandise, args);
}
function RemoveMerchandiseFromCart(productItemID, args){
    AsyncSelect('../site/Cart.ashx?cmd=RemoveMerchandiseFromCart&productItemID='+productItemID, null, Merchandise, args);
}
function RemoveMerchandiseCopiesFromCart(productItemID, args){     
    AsyncSelect('../site/Cart.ashx?cmd=RemoveMerchandiseCopiesFromCart&productItemID='+productItemID, null, Merchandise, args);
}
function Merchandise(data, callbackargs){    
    var args = callbackargs;
    if(args != null && args.length > 0 && args.indexOf('.aspx') != -1){ window.location = args; }
    else { window.location = '../products/merchandise.aspx'; }
}
function FillDropDown(ddl, type, targetDDL){         
    AsyncSelect('../site/Cart.ashx?cmd='+type+'&id='+ddl.value, null, FillDropDownReturned, targetDDL);        
}
function FillDropDownReturned(data, callbackargs)
{       
    ClearDropDown(callbackargs);
    var ddl = $2(callbackargs); var resp = data.split(','); var len = resp.length;
    for(i = 0; i < len; i++){ 
        AddOpt(ddl, resp[i].split(":")[0], resp[i].split(":")[1], len);        
    }   
}

function AddMerchandiseToCart2(productItemID, sizeID, colorID, args){
    AsyncSelect('../site/Cart.ashx?cmd=AddMerchandiseToCart&productItemID='+productItemID+'&sizeID='+sizeID+'&colorID='+colorID, null, Merchandise2, args);
}
function Merchandise2(data, callbackargs){    
    var args = callbackargs;  
    var cartURL = "https://" + document.location.hostname + "/users/cart.aspx";      
    try{
    $2(args).innerHTML = '<p>Item Added to Cart <span class=\"note\">(Change Quantity or Remove in <a href=\"'+cartURL+'\">My Cart</a>)</span></p><p><a class=\"button\" href=\"'+cartURL+'\">Checkout</a></p>';
    } catch(e){ window.location.reload(); return; }
    if(args != null && args.length > 0 && args.indexOf('.aspx') != -1){ window.location = args; }    
}
// ******************** MERCHANDISE ***************************



function validateCart()
{
    var error = '';
    var fieldId = '';
    
    var fields = document.getElementsByTagName('input');
    for(i=0; i<fields.length; i++){
        if(fields[i].id.indexOf('_quantity') != -1){
            var val = fields[i].value;
            if(val == null || val.length == 0){ fieldId = fields[i].id; error = 'Fill in the quantity.'; break; }
            if(isNaN(val)){ fieldId = fields[i].id; error = 'Quantity must be a number.'; break; }
        }
    }
    
    fields = document.getElementsByTagName('select');
    for(i=0; i<fields.length; i++){
        if(fields[i].id.indexOf('_type') != -1){
            if(fields[i].value == null || fields[i].value.length == 0){ fieldId = fields[i].id; error = 'Select a Type.'; break; }
        } else if(fields[i].id.indexOf('_size') != -1){
            if((fields[i].value == null || fields[i].value.length == 0) && fields[i].disabled == false && fields[i].options.length > 1){ 
                fieldId = fields[i].id; error = 'Select a Size.'; break; 
            }
        } else if(fields[i].id.indexOf('_border') != -1){
            if((fields[i].value == null || fields[i].value.length == 0) && fields[i].disabled == false && fields[i].options.length > 1){ 
                fieldId = fields[i].id; error = 'Select a Border.'; break;         
            }
        } else if(fields[i].id.indexOf('_paper') != -1){
            if((fields[i].value == null || fields[i].value.length == 0)  && fields[i].disabled == false){ 
                fieldId = fields[i].id;
                var opttext = fields[i].options[0].text;
                if(opttext == 'Color:') error = 'Select a Color.'; else error = 'Select a Paper Type.'; 
                break; 
            }
        } else if(fields[i].id.indexOf('_color') != -1){
            if((fields[i].value == null || fields[i].value.length == 0) && fields[i].disabled == false && fields[i].options.length > 1){ 
                fieldId = fields[i].id; error = 'Select a Color.'; break; 
            }
        }
    }
    
    if(error.length > 0){
        alert(error);
        $2(fieldId).focus();
        return false;
    }
    return true;
}


var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
