if (!window.VTM) ( function() {
    var initialised = false;	
    VTM = { 
        globals : { 
            parentId: Math.round(Math.random() * 1000000000), 
            scriptID:    1,
            timerlen: 3,
            slideAniLen: 300,
            timerID: null,
            startTime: null,
            obj: null,
            moving: null,
            dir: null,
            startWidth: null,
            endWidth: null,
            endPos: null,
            templateId: 0,
            currentWidth: 220,
            callback: null,
            processingPayment: false
        },
   		
        init : function (){
            if (!initialised) {
            	initialised = true; 
   	 		    VTM.globals.timerID = new Array();
                VTM.globals.startTime = new Array();
                VTM.globals.obj = new Array();
                VTM.globals.moving = new Array();
                VTM.globals.dir = new Array();
                VTM.globals.startWidth = new Array();
                VTM.globals.endWidth = new Array();
                VTM.globals.endPos = new Array();
            }		
        },
        
        expand: function (objname, location, origWidth, expWidth, callback){ // tId) {  
            if(VTM.globals.moving['divBuyotronModule'])
				return;

            VTM.globals.moving['divBuyotronModule'] = true;
            VTM.globals.dir['divBuyotronModule'] = location;                             
            VTM.globals.obj['divBuyotronModule'] = document.getElementById('divBuyotronModule');
            VTM.globals.obj['divBuyotronBuyvertising'] = document.getElementById('divBuyotronBuyvertising');
            VTM.globals.obj['divBTExTop'] = document.getElementById('divBTExTop');
            VTM.globals.obj['divBTExBot'] = document.getElementById('divBTExBot');
            VTM.globals.startTime['divBuyotronModule'] = (new Date()).getTime();
            //VTM.globals.templateId = tId;
            VTM.globals.callback = callback;
            
            VTM.globals.startWidth['divBuyotronModule'] = origWidth;
		    VTM.globals.endWidth['divBuyotronModule'] = expWidth;
            /// 24 is the width of both borders combined
			VTM.globals.startWidth['divBuyotronBuyvertising'] = origWidth - 24; 
			VTM.globals.endWidth['divBuyotronBuyvertising'] =  expWidth - 24;
            
            if(VTM.globals.dir['divBuyotronModule'] == "right"){
                if (VTM.globals.endWidth['divBuyotronModule']  <= 220){
                    VTM.globals.endPos['divBuyotronModule'] = 0;        
				}
				else {
                    // The divBuyotronModule is centred between the parent div, so get parent div width and 
                    // consider this in working out the final left pos of divBuyotronModule
					var parentWidth = document.getElementById("divBuyotronModule").parentNode.offsetWidth;
					var margin = (parentWidth - VTM.globals.startWidth['divBuyotronModule']) / 2;					
                    VTM.globals.endPos['divBuyotronModule'] = ((VTM.globals.endWidth['divBuyotronModule'] - VTM.globals.startWidth['divBuyotronModule'])  * -1)  + margin;
				}
            }
            var methodSig = "VTM.slidetick(\"divBuyotronModule\");";
            VTM.globals.timerID['divBuyotronModule'] = setInterval(methodSig,VTM.globals.timerlen);
        },

        slidetick: function (objname){
            var elapsed = (new Date()).getTime() - VTM.globals.startTime[objname];

            if (elapsed > VTM.globals.slideAniLen){
				VTM.endSlide(objname);
            }
            else {			 
				if (VTM.globals.dir[objname] == "right"){
					if (VTM.globals.endWidth[objname] > VTM.globals.startWidth[objname]){
                        var d = Math.round(elapsed / VTM.globals.slideAniLen * (VTM.globals.endWidth[objname] - VTM.globals.startWidth[objname]) );
						var pos = VTM.globals.endPos[objname] + ((VTM.globals.endWidth[objname] - VTM.globals.startWidth[objname]) - d);

                        VTM.globals.obj["divBuyotronModule"].style.width = (VTM.globals.startWidth[objname] + d ) + "px";
						VTM.globals.obj["divBTExTop"].style.width = 1 + d + "px";
						VTM.globals.obj["divBTExBot"].style.width = 1 + d + "px";
                        VTM.globals.obj["divBuyotronBuyvertising"].style.width = (VTM.globals.startWidth['divBuyotronBuyvertising'] + d) + "px";
						if (pos <= 0){
	                        VTM.globals.obj["divBuyotronModule"].style.left =   pos + "px"; 
	                    }
                    }
                    else{
                        var d = Math.round(elapsed / VTM.globals.slideAniLen * ( VTM.globals.startWidth[objname] - VTM.globals.endWidth[objname]) );
						var parentWidth = document.getElementById("divBuyotronModule").parentNode.offsetWidth;
					    var margin = (parentWidth - VTM.globals.endWidth['divBuyotronModule']) / 2;					
						var pos = VTM.globals.endPos[objname] - ((VTM.globals.startWidth[objname] - VTM.globals.endWidth[objname]) - d) + margin;

                        VTM.globals.obj["divBuyotronBuyvertising"].style.width = (VTM.globals.startWidth['divBuyotronBuyvertising'] - d) + "px";
						VTM.globals.obj["divBTExTop"].style.width = 1 + (VTM.globals.startWidth[objname] - VTM.globals.endWidth[objname] - d) + "px";
						VTM.globals.obj["divBTExBot"].style.width = 1 + (VTM.globals.startWidth[objname] - VTM.globals.endWidth[objname] - d) + "px";
                        VTM.globals.obj["divBuyotronModule"].style.width = (VTM.globals.startWidth[objname] - d ) + "px";
						if (pos <=0){	
                            VTM.globals.obj["divBuyotronModule"].style.left =   pos + "px"; 
						}                        
                    }
                }
            }
            return;
        },

        endSlide: function (objname){
            clearInterval(VTM.globals.timerID[objname]);
            
            if (VTM.globals.endWidth['divBuyotronModule'] <=220){
                VTM.globals.obj["divBTExTop"].style.width =  "1px";
                VTM.globals.obj["divBuyotronModule"].style.width = VTM.globals.endWidth[objname] + "px";
                VTM.globals.obj["divBuyotronBuyvertising"].style.width = VTM.globals.endWidth['divBuyotronBuyvertising'] + "px";
                VTM.globals.obj["divBTExBot"].style.width = "1px";
                VTM.globals.obj["divBuyotronModule"].style.left = "0px";
            }
            else {
                var finalExpand = (1 + VTM.globals.endWidth[objname] - VTM.globals.startWidth[objname]);
                VTM.globals.obj["divBTExTop"].style.width = finalExpand + "px";
                VTM.globals.obj["divBuyotronModule"].style.width = VTM.globals.endWidth[objname] + "px";
                VTM.globals.obj["divBuyotronBuyvertising"].style.width = VTM.globals.endWidth['divBuyotronBuyvertising'] + "px";
                VTM.globals.obj["divBTExBot"].style.width = finalExpand + "px";
                VTM.globals.obj["divBuyotronModule"].style.left =  VTM.globals.endPos[objname] +  "px"; 
            }
            
            VTM.globals.currentWidth = VTM.globals.endWidth[objname];

            delete(VTM.globals.moving[objname]);
            delete(VTM.globals.timerID[objname]);
            delete(VTM.globals.startTime[objname]);
            delete(VTM.globals.obj[objname]);
            delete(VTM.globals.dir[objname]);
            delete(VTM.globals.startWidth[objname]);
            delete(VTM.globals.endWidth[objname]);
            delete(VTM.globals.endPos[objname]);

			VTM.globals.callback();
            return;
        },

        // Alternates a frame between baseframe and blingframe
        toggleFrame: function (baseframe, baseframewidth, blingframe, blingframewidth){
        	var frame, width;
        	if (VTM.globals.templateId == blingframe){
        		frame = baseframe;
        		width = baseframewidth;
        	}
            else {
            	frame = blingframe;
            	width = blingframewidth
        	}
        	
        	if (VTM.globals.templateId != frame){
	        	if (VTM.globals.currentWidth!=width){
					VTM.removeAllChildren(document.getElementById('divBuyotronBuyvertising'));           
        			VTM.expand("buyotron", "right", VTM.globals.currentWidth, width, 
        		       function(){
        		          VTM.globals.templateId = frame;
  				          VTM.pullTemplate(VTM.globals.templateId);
  			       	    } );   
	 			}
        		else {
	        		VTM.globals.templateId = frame;
  					VTM.pullTemplate(VTM.globals.templateId);      	
       			}
       		}
        },
         
        // Loads this frame
        loadFrame: function (frame, width){
        	if (VTM.globals.templateId != frame){
	        	if (VTM.globals.currentWidth!=width){
					VTM.removeAllChildren(document.getElementById('divBuyotronBuyvertising'));           
        			VTM.expand("buyotron", "right", VTM.globals.currentWidth, width, 
	        			function(){
    	    		          VTM.globals.templateId = frame;
  					          VTM.pullTemplate(VTM.globals.templateId);
  			       	    } );  
	 			}
        		else {
	        		VTM.globals.templateId = frame;
  					VTM.pullTemplate(VTM.globals.templateId);      	
        		}
        	}
        },
        
        loadPreviousFrame: function (){
        	var prev = document.getElementById("_VTMPrevTemSub").value;
        	VTM.loadFrame(prev, 220);
        },
        
        // Pulls the template of 'frame' with the current brand, platform, locale settings
        pullTemplate: function (frame){
            var loc = document.getElementById("_VTMLoc").value;
            var brand = document.getElementById("_VTMBrnd").value;
            var plat = document.getElementById("_VTMPlat").value;
            var curr = document.getElementById("_VTMTemSub").value;
            VTM.load("http://www.vtmvision.net/vtm/serve?_VTMLoc=" + loc + "&_VTMBrnd=" + brand + "&_VTMPlat=" + plat  + "&_VTMTemSub=" + frame + "&_VTMPrevTemSub=" + curr);    
        },
        
        showInfo: function(){
			var _b20= _bD.createElement("div");_b20.style.width="311px";_b20.style.height="270px";_b20.style.padding="10px 10px 10px 10px";_b20.style.margin="0 0 0 0";
			_b20.style.font="12px Arial";_b20.style.fontWeight="bold";_b20.style.backgroundColor="white";_b20.style.cssFloat="left";_b20.style.styleFloat="left";
			divBuyotronBuyvertising.appendChild (_b20);var _b21= _bD.createTextNode("VTM is a Virtual Teller Machine that allows you to buy goods or services in a convenient and safe way without ever leaving the site you are on. Security of data is extremely important to us and any credit card information is encrypted using the latest encryption algorithms.");
			_b20.appendChild (_b21);_b20.appendChild (_bD.createElement("br"));_b20.appendChild (_bD.createElement("br"));
			/*var _b22= _bD.createTextNode("If you would like a chance to sell goods or services using VTM on your site please visit contact us:");_b20.appendChild (_b22);_b20.appendChild (_bD.createElement("br"));
			_b20.appendChild (_bD.createElement("br"));var _b23= _bD.createTextNode("Phone: + 353 1 8709097");_b20.appendChild (_b23);*/
			_b20.appendChild (_bD.createElement("br"));_b20.appendChild (_bD.createElement("br"));var _b22 = _bD.createElement("center");_b20.appendChild (_b22);var _b23= _bD.createTextNode("Is this an authorized VTM?");_b22.appendChild (_b23);_b22.appendChild (_bD.createElement("br"));_b22.appendChild (_bD.createElement("br"));
			var _b24 = _bD.createTextNode("We will be opening a browser window with the url beginning with \"https://vtmvision.net/authorized/\".");_b20.appendChild (_b24);_b20.appendChild (_bD.createElement("br"));var _b25 = _bD.createTextNode("Please check this is in the url bar above the window.");_b20.appendChild (_b25);
			_b20.appendChild (_bD.createElement("br")); _b20.appendChild (_bD.createElement("br"));var _b26 = _bD.createElement("center");_b20.appendChild (_b26);var _b27 = _bD.createElement("input");_b27.type="button";_b27.value="Verify this VTM";_b27.onclick=function(){var url = "https://vtmvision.net/authorized?vtmID=" + (document.getElementById("_VTMId")!=null?document.getElementById("_VTMId").value:""); window.open(url);};_b26.appendChild (_b27);_bD.getElementById("divBuyotronSecl").style.background="";
        },
        
        showInfoFrame: function (){
			if (VTM.globals.templateId != -1){
				VTM.removeAllChildren(document.getElementById('divBuyotronBuyvertising'));           
	        	if (VTM.globals.currentWidth!=355){
        			VTM.expand("buyotron", "right", VTM.globals.currentWidth, 355, 
	        			function(){
    	    		          VTM.globals.templateId = -1;
				        	  VTM.showInfo();
  			       	    } );  
	 			}
        		else {
	        		VTM.globals.templateId = -1;
	        		VTM.showInfo();
        		}
        	}
        },

		presetOption: function (listId, entry){
			var list = document.getElementById(listId);
			if (list!=null){
				for (var i = 0; i < list.options.length; ++i) {
  					if (list.options[i].value == entry) {
  						list.selectedIndex = i;
  					}
  			    }
  			}
		},
		
        showPaymentFrame: function (){
			VTM.removeAllChildren(document.getElementById('divBuyotronBuyvertising'));           
	        if (VTM.globals.currentWidth!=355){
        		VTM.expand("buyotron", "right", VTM.globals.currentWidth, 355, 
	        		function(){
    	    	          VTM.globals.templateId = 10;
					 	  var url = "http://www.vtmvision.net/vtm/serve?";
			        	  url += "_VTMLoc=" + document.getElementById("_VTMLoc").value;
			        	  url += "&_VTMBrnd=" + document.getElementById("_VTMBrnd").value;
                          url += "&_VTMPlat=" + document.getElementById("_VTMPlat").value;
                          url += "&_VTMCurTemPrc=" + document.getElementById("_VTMCurTemPrc").value;
                          url += "&_VTMCurTemCur=" + document.getElementById("_VTMCurTemCur").value;
                          url += "&_VTMCurTemDesc=" + document.getElementById("_VTMCurTemDesc").value;
                          url += "&_VTMTemSub=" + VTM.globals.templateId;
                          url += "&_VTMPrevTemSub=" + document.getElementById("_VTMTemSub").value;
            			  VTM.load(url);    
  			    	} );  
	 		}
        	else {
	        	VTM.globals.templateId = 10;
			 	var url = "http://www.vtmvision.net/vtm/serve?";
			    url += "_VTMLoc=" + document.getElementById("_VTMLoc").value;
			    url += "&_VTMBrnd=" + document.getElementById("_VTMBrnd").value;
                url += "&_VTMPlat=" + document.getElementById("_VTMPlat").value;
                url += "&_VTMCurTemPrc=" + document.getElementById("_VTMCurTemPrc").value;
                url += "&_VTMCurTemCur=" + document.getElementById("_VTMCurTemCur").value;
                url += "&_VTMCurTemDesc=" + document.getElementById("_VTMCurTemDesc").value;
                url += "&_VTMTemSub=" + VTM.globals.templateId;
                VTM.load(url);    
        	}
        },

        load : function(libPath) {
            // Cleanup previously added script element - if there was one.
            if (VTM.globals.scriptID >1){            
                var pScript = document.getElementById("VTMScript_" + (VTM.globals.scriptID-1));
            	if (pScript != null) {
                    pScript.parentNode.removeChild(pScript);
                    delete pScript;
            	}
            }

            var parentId = "_VTMParentDiv_" + VTM.globals.parentId;
            var scriptId = "VTMScript_" + VTM.globals.scriptID++;        	
            var vtmId = (document.getElementById("_VTMId")!=null?document.getElementById("_VTMId").value:"");
            var head = document.getElementsByTagName("head")[0]; 
            script = document.createElement("script"); 
            script.id =  scriptId;
            script.type = "text/javascript"; 
            script.charset = "utf-8"; // Needed so IE will handle international characters correctly :-)
            script.src = encodeURI(libPath + "&_VTMParentDivId=" + parentId + "&_VTMScriptId=" + scriptId + "&_VTMId=" + vtmId);
            head.appendChild(script); 
        },

        removeAllChildren : function(parentNode,loop) {
            if(!loop)
                loop=0;
            if(loop<5){
                try{
                    if(parentNode.childNodes){
                        while(parentNode.childNodes.length>0){
                            var toRemove=parentNode.childNodes[parentNode.childNodes.length-1];
                            VTM.removeAllChildren(toRemove);
                            parentNode.removeChild(toRemove,loop++);
                        }
                    }
                }catch(e){
                    //VTM.logMessage(e);
                }
            }
        },

        validP : function(num) {
	            var filter= /(^[1-9]\d{0,4}$)|(^[1-9]\d{0,4}\.\d{1,2}$)/ 
    	        if (filter.test(num)==false){
    	        	return false;
    	        }
    	        else {
					var num = parseFloat (num);
					if (num>=3.00){
						return true;
					}
					else {
					    return false;
					}
    	        }
        },

        validS : function (type, num){
            if ((type==0 || type==1) && (num.length!=3))
                return false;
            else if (type==2 && num.length!=4)
                return false;							
            for (var i=0; i<num.length; i++){ if (!(num[i]>='0' && num[i]<='9')) return false; }
            return true;
        },
        
        validN : function (num){
            if (num!="" && num.length>=12 && num.length<=16 ){
                for (var i=0; i<num.length; i++) {	if (!(num.charAt(i)>='0' && num.charAt(i)<='9')) { return false; } } 
                var i, sum=0, weight; 
                for (i = 0; i < num.length - 1; i++) { 
                    weight = num.substr(num.length - (i + 2), 1) * (2 - (i % 2)); 
                    sum += ((weight < 10) ? weight : (weight - 9)); 
                } 
                if (parseInt(num.substr(num.length-1)) == ((10 - sum % 10) % 10)) { return true; } else { return false; }			
            }
            return false;
        },

        payment : function (){
        	if (VTM.globals.processingPayment){
        		return;
        	}
            VTM.globals.processingPayment = true;
            var err = document.getElementById("_VTMError");
            err.innerHTML = "";
            
			document.getElementById("_VTMbtnPayNow").style.background="url(http://www.vtmvision.net/images/btnPayNowClicked.gif)";
			
            var am = document.getElementById("_VTMAmt").value;
            if (!VTM.validP(am)){err.innerHTML = ("Please enter an amount greater than 3 " + document.getElementById("_VTMCurr").options[document.getElementById("_VTMCurr").selectedIndex].text);document.getElementById("_VTMbtnPayNow").style.background="url(http://www.vtmvision.net/images/btnPayNow.gif)";VTM.globals.processingPayment=false;return;}
            var cu = document.getElementById("_VTMCurr").value;
            var ct = document.getElementById("_VTMType").value; 
            if (ct == "none"){err.innerHTML = "Please select your Card Type";document.getElementById("_VTMbtnPayNow").style.background="url(http://www.vtmvision.net/images/btnPayNow.gif)";VTM.globals.processingPayment=false;return;}
            var nu = document.getElementById("_VTMNum").value.replace(/^\s+|\s+$/g,"");
            if (!VTM.validN (nu) ){err.innerHTML = "Please enter a valid card number";document.getElementById("_VTMbtnPayNow").style.background="url(http://www.vtmvision.net/images/btnPayNow.gif)";VTM.globals.processingPayment=false;return;}
            var cs = document.getElementById("_VTMSec").value.replace(/^\s+|\s+$/g,"");
            if (!VTM.validS(ct, cs)){err.innerHTML = "Please enter a valid security number";document.getElementById("_VTMbtnPayNow").style.background="url(http://www.vtmvision.net/images/btnPayNow.gif)";VTM.globals.processingPayment=false;return;}
            var xm = document.getElementById("_VTMExpM").value;
            if (xm == "--"){err.innerHTML = "Please select an Expiry Month";document.getElementById("_VTMbtnPayNow").style.background="url(http://www.vtmvision.net/images/btnPayNow.gif)";VTM.globals.processingPayment=false;return;}
            var xy = document.getElementById("_VTMExpY").value;
            if (xy == "--"){err.innerHTML = "Please select an Expiry Year";document.getElementById("_VTMbtnPayNow").style.background="url(http://www.vtmvision.net/images/btnPayNow.gif)";VTM.globals.processingPayment=false;return;}
            var nm = document.getElementById("_VTMName").value.replace(/^\s+|\s+$/g,"");
            if (nm == "" || nm.length>50 || nm.indexOf(" ")==-1){err.innerHTML = "Please enter the card holders name";document.getElementById("_VTMbtnPayNow").style.background="url(http://www.vtmvision.net/images/btnPayNow.gif)";VTM.globals.processingPayment=false;return;}
            var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
            var em = document.getElementById("_VTMEmail").value.replace(/^\s+|\s+$/g,"");
            if (!filter.test(em)){err.innerHTML = "Please enter a valid email address";document.getElementById("_VTMbtnPayNow").style.background="url(http://www.vtmvision.net/images/btnPayNow.gif)";VTM.globals.processingPayment=false;return;}
  				
            var url = "https://vtmvision.net/vtm/purchase?";
			url += "_VTMLoc=" + document.getElementById("_VTMLoc").value + "&_VTMBrnd=" + document.getElementById("_VTMBrnd").value;
			url += "&_VTMPlat=" + document.getElementById("_VTMPlat").value;
            url += "&cn=" + nu + "&ex=" +  xm + xy + "&cc=" + cs + "&ct=" + ct + "&em=" + em + "&cu=" + cu + "&am=" + am;
            url += "&fn=" + nm.substr(0, nm.indexOf(" ")) + "&ln=" + nm.substr(nm.indexOf(" ")+1); 
            url += "&in=" + document.getElementById("_VTMInvNum").value + "&tt=0&md=CC";
            VTM.load(url); 
        },		
        
        getWidget : function (){
            var err = document.getElementById("_VTMError");
            err.innerHTML = "";
            var nm = document.getElementById("_VTMContactName").value.replace(/^\s+|\s+$/g,"");
            if (nm == "" || nm.length>50 || nm.indexOf(" ")==-1){err.innerHTML = "Please enter your Name";return;}
            var emlfilter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
            var em = document.getElementById("_VTMContactEmail").value.replace(/^\s+|\s+$/g,"");
            if (!emlfilter.test(em)){err.innerHTML = "Please enter your Email address";return;}          
            var wsfilter=/^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$/
            var ws = document.getElementById("_VTMContactWebsite").value.replace(/^\s+|\s+$/g,"");
            if (!wsfilter.test(ws)){err.innerHTML = "Please enter your Website Url";return;}

            var url = "http://www.vtmvision.net/vtm/getwidget?";
			url += "_VTMLoc=" + document.getElementById("_VTMLoc").value + "&_VTMBrnd=" + document.getElementById("_VTMBrnd").value;
			url += "&_VTMPlat=" + document.getElementById("_VTMPlat").value + "&_VTMContactName=" + nm;
			url += "&_VTMContactEmail=" + em + "&_VTMContactWebsite=" + ws;
          	VTM.load(url); 
        }
    };

    var self = VTM; 
    self.init();
}
)();


VTM.prepareWidget = function(loc, brand, plat, tempS) {
    var parentId = "_VTMParentDiv_" + VTM.globals.parentId;
    document.write("<div id=\"" + parentId + "\" ></div>");
    if (VTM){
        VTM.load("http://www.vtmvision.net/vtm/serve?_VTMLoc=" + loc + "&_VTMBrnd=" + brand + "&_VTMPlat=" + plat +  "&_VTMTemSub=" + tempS);
    }
}


