
var original_page = true;

function pageLoad(hash,option) {
	// callback for handling ajax history/bookmarking if required
	if (hash.substring(0,1)=='/') {
		hash = hash.substring(1);
		if (option=='init') {
			var uri = parseUri(document.location.href);
			ajaxPage(uri.protocol+"://"+uri.host+uri.directory+hash,'',true);
		} else if (option=='check') {
			var uri = parseUri(document.location.href);
			ajaxPage(uri.protocol+"://"+uri.host+uri.directory+hash,'',true);
		}
	} else if (!original_page && document.location.href.indexOf('#') > -1 && hash == '') {
		var uri = parseUri(document.location.href);
		ajaxPage(uri.protocol+"://"+uri.host+uri.directory+uri.file+(uri.query!=''?'?'+uri.query:''),'',true);
	}
	original_page = false;
}

function showIndicator() {
	$('#indicator').defaultIndicator();
	$("#indicator").show();
}

function ajaxPage(href,reqtype,history,ni) {
	if (history==''||history==undefined) history=false;
	if (reqtype==''||reqtype==undefined) reqtype='default';
	if (ni==''||ni==undefined) ni=false;
	if (false && !history && (parseUri(href).protocol != parseUri(document.location.href).protocol)) {
		// switching protocol
		document.location.href=href;
	} else {
		if (ni!=true) showIndicator();
		$.ajax({
		    url: href,
		    type: 'POST',
			data: {ajax:1,ajaxtype:reqtype},
		    dataType: 'xml',
		    timeout: 10000,
		    error: function(){
				document.location.href=href;
		    },
		    success: function(xml){
				if (processResult(xml)) {
					if (history) {
						var uri = parseUri(href);
						var uri2 = parseUri(document.location.href);
						if (uri.file!=uri2.file || uri.query!=uri2.query) {
							$.historyLoad('/'+uri.file+(uri.query!=''?'?'+uri.query:''));
						} else {
							original_page = true;
							$.historyLoad('');
						}
					}
			    	$("#indicator").hide(200);
				}
		    }
		});
	}
}

function ajaxPageNI(href,reqtype,history) {
	if (history==''||history==undefined) history=false;
	if (reqtype==''||reqtype==undefined) reqtype='default';
	ajaxPage(href,reqtype,history,true);
}

function ajaxForm(oform,ni,wt,nhi) {
	if (wt==''||wt==undefined) {
		if (oform.id=='theLongForm') {
			// time out based on the form id
			wt=120000;
		} else if (document.forms[oform.id].timeout) {
			// use the specific time out setting
			wt=document.forms[oform.id].timeout.value;
		} else {
			// default time out setting
			wt=10000;
		}
	}
	if (ni==''||ni==undefined) ni=false;
	if (nhi==''||nhi==undefined) nhi=false;
	if (ni!=true) showIndicator();
	$('#'+oform.id).ajaxSubmit({
	    url: oform.action,
	    type: 'POST',
	    dataType: 'xml',
	    timeout: wt,
	    error: function(){
			document.forms[oform.id].ajax.value='x';
			$('#'+oform.id).submit();

	    },
	    success: function(xml){
			if (processResult(xml)) {
		    	if (nhi!=true) $("#indicator").hide(200);
			}
	    }
	});
	return false
}

function ajaxFormNI(oform) {
	ajaxForm(oform,true);
	return false
}

function ajaxReq(href,reqtype,ni,eh) {
	// no history required
	if (reqtype==''||reqtype==undefined) reqtype='default';
	if (ni==''||ni==undefined) ni=false;
	if (eh==''||eh==undefined) eh=false;
	if (false && parseUri(href).protocol != parseUri(document.location.href).protocol) {
		// switching protocol
		document.location.href=href;
	} else {
		if (ni!=true) showIndicator();
		$.ajax({
		    url: href,
		    type: 'POST',
			data: {ajax:1,ajaxtype:reqtype},
		    dataType: 'xml',
		    timeout: 10000,
		    error: function(){
				if(eh=='startNotifications') {
					// No Error Handler
					startNotifications();
				} else {
					document.location.href=href;
				}
		    },
		    success: function(xml){
				if (processResult(xml)) {
			    	if (ni!=true) $("#indicator").hide(200);
				}
		    }
		});
	}
	return false;
}

function ajaxReqNI(href,reqtype) {
	if (reqtype==''||reqtype==undefined) reqtype='default';
	ajaxReq(href,reqtype,true);
}

function processResult(xml) {
	var redirect = '';var aj_redirect = '';
	$(xml).find('item').each(function(){
		var elem_id = $(this).find('name').text();
		var item_opt = $(this).find('option').text();
		var item_text = $(this).find('data').text();
		if (elem_id=='#redirect') {
			redirect = item_text;
		}else if (elem_id=='#aj_redirect') {
			aj_redirect = item_text;
		} else if (elem_id.substring(0,4)=='#js_') {
			$.globalEval(item_text);
		} else {
			if (item_opt!='general') {
				eval('\$(elem_id).'+item_opt+'(item_text);');
			} else {
				$(elem_id).html(item_text);
				loadAutoClass(elem_id);
			}
		}
    });
	if (redirect) {
		document.location.href=redirect;
		return false;
	}else if (aj_redirect) {
		ajaxReq(aj_redirect);
		return false;
	} else {
		return true;
	}
}

function preProcess(history) {
	if($.browser.safari) {
		// not very reliable, so don't use it
	} else {
		if (history) $.historyInit(pageLoad);
	}
	loadAutoClass('');
	browserFix();
	preLoadImg();
}

function loadAutoClass(id) {
	$(id + (id!=''?' ':'') + 'a.ajax').bind('click', function() {
		ajaxPage(this.href,'',($.browser.safari?false:true));
		return false;
	});
	$(id + (id!=''?' ':'') + 'a.ajax2').bind('click', function() {
		ajaxReq(this.href);
		return false;
	});
	$(id + (id!=''?' ':'') + 'a.ipopup').bind('click', function() {
		popup2(this.href);
		return false;
	});
	$(id + (id!=''?' ':'') + 'form.ajaxform').submit(function() {
		$('.DisableOnSubmit').attr("disabled","disabled");
		$('.DisableOnSubmit').val("Please Wait...");
		if (this.ajax.value == 'x') {
			// previous attempt failed lets not use ajax this time around
			this.ajax.value=0;
			return true;
		} else {
			this.ajax.value=1;
			return(ajaxForm(this))
		}
	});
	$(id + (id!=''?' ':'') + 'input.img-over').bind('mouseover', function() {
		this.src = this.src.replace('.gif','Ovr.gif');
		return false;
	});
	$(id + (id!=''?' ':'') + 'input.img-over').bind('mouseout', function() {
		this.src = this.src.replace('Ovr.gif','.gif');
		return false;
	});

	loadFlashFonts(id)
}

function browserFix() {
}

function loadFlashFonts(id) {
	if (false) {
		if(typeof sIFR == "function") {
			sIFR.replaceElement(id + (id!=''?' ':'') + "h1", named({sFlashSrc: "/fonts/minionPro.swf", sColor: "#304D65", sWmode:"transparent"}));
			sIFR.replaceElement(id + (id!=''?' ':'') + "h2", named({sFlashSrc: "/fonts/minionPro.swf", sColor: "#304D65", sWmode:"transparent"}));
			sIFR.replaceElement(id + (id!=''?' ':'') + "h3", named({sFlashSrc: "/fonts/minionPro.swf", sColor: "#304D65", sWmode:"transparent"}));
			sIFR.replaceElement(id + (id!=''?' ':'') + "h4", named({sFlashSrc: "/fonts/minionPro.swf", sColor: "#304D65", sWmode:"transparent"}));
			sIFR.replaceElement(id + (id!=''?' ':'') + "h5", named({sFlashSrc: "/fonts/minionPro.swf", sColor: "#304D65", sWmode:"transparent"}));
		};
	}
}

function goTo(href) {
	document.location.href=href;
	return false;
}

function sendForm(mvar,oform,ret,mval) {
	document.forms[oform].iaction.name='iaction';
	document.forms[oform].iaction.value='';
	if (mval!=undefined) document.forms[oform].iaction.value=mval;
	if (mvar!='') document.forms[oform].iaction.name=mvar;
	$('#'+oform).submit();
	return (ret!='' && ret!=undefined ? ret : false);
}

function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function openBrWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}

function NewWindow(mypage,myname,w,h,scroll,resize) {
	var win = null;
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable='+resize;
	win = window.open(mypage,myname,settings);
	return false;
}

function generalPopup(mypage,myname,width,height,scroll,resize) {
	if (myname == undefined) myname = 'defpopup';
	if (width == undefined) width = 800;
	if (height == undefined) height = 530;
	if (scroll == undefined) scroll = 1;
	if (resize == undefined) resize = 1;
	return(NewWindow(mypage,myname,width,height,scroll,resize));
}

function getStamp() {
	var jar=new Date();
	var flashc=jar.getTime();
	return flashc;
}

function clkAddToBasket(id) {
	var dpi = document.forms['listform']['dpi'+id].value;
	var qty = document.forms['listform']['qty'+id].value;
	document.forms['addform']['dpi1'].value = dpi;
	document.forms['addform']['qty1'].value = qty;
	$('#addform').submit();
	return false;
}

function clkSwapColour(id,dpi,img,totCols,col,maxQty) {
	document.forms['listform']['dpi'+id].value = dpi;
	var imgID = document.getElementById('img'+id);
	if (imgID) {
		imgID.src = img;
	}
	for(var x=1;x<=totCols;x++) {
		swapClass('li'+id+'-'+x,'');
	}
	swapClass('li'+id+'-'+col,'selected');
	updateQty(id,maxQty);
	return false;
}

function updateQty(id,maxqty) {
	if (maxqty > 20) {
		maxqty = 20;
	}
	var xOldVal = document.forms['listform']['qty'+id].value;
	xOldVal = (xOldVal==0?1:xOldVal);
	with (document.forms['listform']['qty'+id]) {
		for (var i = options.length; 0 <= i; i--) {
			options[i] = null;
		}
		var xOld = 1;
		for (var i = 0; i < maxqty; i++) {
			options[i] = new Option((i+1), (i+1));
			if ((i+1) == xOldVal) {
				xOld = i;
			} else if ((i+1) < xOldVal) {
				xOld = i;
			}
		}
		if (options[xOld]) {
			options[xOld].selected = true;
		}
	}
}

function checkSearch(e) {
	if (e.value=='keyword, product name, code') {
		e.value = '';
	} else {
		e.select()
	}
}

function videoPlayer(id,cb1,video,width,height,img) {
	var s1 = new SWFObject('/flash/flvplayer.swf?cb1='+cb1, 'showplayer1', width, height, '1', '#000000');
	s1.addParam('allowfullscreen','true');
	s1.addVariable('file',video);
	s1.addVariable('image',img);
	s1.addVariable('backcolor','0x837870');
	s1.addVariable('frontcolor','0xFFFFFF');
	s1.addVariable('lightcolor','0xEEE605');
	s1.addVariable('autostart','false');
	s1.write(id);
}

function preLoadImg() {
}

function searchTrack(href,prodid,consid,consolid) {
	document.searchclick.linkto.value = href;
	document.searchclick.prodid.value = prodid;
	document.searchclick.consid.value = consid;
	document.searchclick.consolid.value = consolid;
	$('#searchclick').submit();
	return false;
}

function submitForm(formName) {
	$('#'+formName).submit()
	return false;
}

function relatedTrack(mType,mCode) {
	document.cookie="posttrk="+escape(mType)+";path=/";
	document.cookie="postalt="+escape(mCode)+";path=/";
	return true;
}


function startNotifications() {
	setTimeout("ajaxReq('/tools/notifications.php','notifications',true,'startNotifications')",30000);
}

function testAlert(opt) {
	alert(opt);
}

function checkEnter(e,func) {
	if (!e) e = window.event;
	var keyCode = e.keyCode ? e.keyCode : e.which ? e.which : e.charCode;
	if (keyCode == 13 ) {
    	if (func!=undefined && func!='') {
			eval(func);
			return false;
		} else {
			e.cancelBubble = true;
			e.returnValue = false;
			if (e.stopPropagation) {
				e.stopPropagation();
				e.preventDefault();
			}
		}
	}
}
