KAPELLE_STORE_EXPRESS_CHECKOUT="https://kapelle.maddancer.com/checkout/express_checkout_init.html";


function JSONHandler(object){
		
		//var err=$("ErrorCode")
		//if(err)
		//	err.innerHTML="";
		
		try{
			if(object!=""){
				//var JSON=eval('('+object+')');
				var JS=JSON.decode(object);
				
				if(JS==null)
					JS=object;
				
				if(typeof(JS)=="object"){
					for( var i in JS){
						
							//alert("I: "+i+"\n VALUES: "+JSON[i])
							if(arguments.length > 1 && arguments[1]=="inputValue")
								$(i).value=JS[i];
						
							else if(arguments.length>1 && arguments[1]=="process"){
								try{
									//alert("ELEMENT: "+JSON[i]);
									eval('('+JS[i]+')');
								}catch(e){
									
									try{
											eval('(['+JS[i]+'])');
									}catch(e){
										var z=e;
										alert("ERROR: "+i+"\n"+JS[i]+"\n"+e);
									}
								}
								//alert(JSON[i]);
							}
							else{
								if(i!=="undefined" && $(i)!==null){
									
									var el=$(i);
									
									if(el){
										
										var tag=el.get('tag');
										if(tag && tag=="input")
											el.value=JS[i];
										else
											el.innerHTML=JS[i];
									}
									else{
										//alert("Element i is not defined")
										alert("Error: Request Failed.  Please try again");
										window.location.reload(true);
									}
									
								}
								else{
									//alert("Element i is not defined")
									alert("Error: Request Failed.  Please try again");
									window.location.reload(true);
								}
							}
					}
				}
			}
		}catch(e){}				
		
}



function arraysEqOld(array1, array2) {
   var temp = new Array();
   if ( (!array1[0]) || (!array2[0]) ) { // If either is not an array
      return false;
   }
   if (array1.length != array2.length) {
      return false;
   }
   // Put all the elements from array1 into a "tagged" array
   for (var i=0; i<array1.length; i++) {
      key = (typeof array1[i]) + "~" + array1[i];
   // Use "typeof" so a number 1 isn't equal to a string "1".
      if (temp[key]) { temp[key]++; } else { temp[key] = 1; }
   // temp[key] = # of occurrences of the value (so an element could appear multiple times)
   }
   // Go through array2 - if same tag missing in "tagged" array, not equal
   for (var i=0; i<array2.length; i++) {
      key = (typeof array2[i]) + "~" + array2[i];
      if (temp[key]) {
         if (temp[key] == 0) { return false; } else { temp[key]--; }
      // Subtract to keep track of # of appearances in array2
      } else { // Key didn't appear in array1, arrays are not equal.
         return false;
      }
   }
   // If we get to this point, then every generated key in array1 showed up the exact same
   // number of times in array2, so the arrays are equal.
   return true;
}


function arraysEq(array1, array2) {
	
	
	var a1len=false;
	var a2len=false;
	
	try{
		a1len=array1.length;
		a2len=array2.length;
		
	}catch(e){
		return false;
	}
	
	if(a1len != a2len)
		return false;
		
	var a1Val=array1[0];
	var a2Val=array2[0];
	
	if(a1Val!=a2Val)
		return false;
	else{
		
		if(array1.length==1 & array2.length==1)
			return true;
		else{
			return arraysEq(array1.slice(1), array2.slice(1));
		}
		
		
	} 
	
	return true;
   
}


function getClient(){
	
	//var height = (document.body.height !== undefined) ? document.body.height : document.body.offsetHeight;
	//var width = (document.width !== undefined) ? document.width : document.body.offsetWidth;
	
	
	var x,y; 
	var test1 = document.body.scrollHeight; 
	var test2 = document.body.offsetHeight 
	if (test1 > test2) {
	// all but Explorer Mac 
		 x = document.body.scrollWidth; 
		 y = document.body.scrollHeight; 
	} else  {
		// Explorer Mac; //would also work in Explorer 6 Strict, Mozilla and Safari 
		x = document.body.offsetWidth; 
		y = document.body.offsetHeight; 
	}
	
	
	//alert("Height: "+y);
	
	var r=new Array(x, y);
	
	return r;
	
	
	
}



function getScrollXY(){


	 var scrOfX = 0, scrOfY = 0;
	 
	 if( typeof( window.pageYOffset ) == 'number' ) {
		 //Netscape compliant
		 scrOfY = window.pageYOffset;
		 scrOfX = window.pageXOffset;
	 } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		 //DOM compliant
		 scrOfY = document.body.scrollTop;
		 scrOfX = document.body.scrollLeft;
	 } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		 //IE6 standards compliant mode
		 scrOfY = document.documentElement.scrollTop;
		 scrOfX = document.documentElement.scrollLeft;
	 }
		 return [ scrOfX, scrOfY ];

}



function centerElement(el){
	//alert('begin center');
	var winXY = window.getSize();
	var scrollXY = window.getScroll();
	var elXY=el.getSize();
	
	//var centerXY={'x':(winXY.x/2+scrollXY.x), 'y':(winXY.y/2+scrollXY.y)};
	//var scrollXY=getScrollXY();
	//var winXY=getWindowSize();
	//var divXY={'x':$(el).offsetWidth, 'y':$(el).offsetHeight};
	
	//var posX= centerXY['x']-parseInt(divXY['x']/2);
	//var posY= centerXY['y']-parseInt(divXY['y']/2);	
		
	//var posX= parseInt(winXY[0]/2) + parseInt(scrollXY[0])-parseInt(divXY[0]/2);
	//var posY= parseInt(winXY[1]/2) + parseInt(scrollXY[1])-parseInt(divXY[1]/2);
	
	var eH=el.offsetHeight;
	var eW=el.offsetWidth;
	
	var hpos=Math.round(scrollXY.x+((winXY.x-elXY.x)/2));
	var vpos=Math.round(scrollXY.y+((winXY.y-elXY.y)/2));
	hpos = (hpos < 0)?0:hpos;
	vpos = (vpos < 0)?0:vpos;
	
	
	el.setStyle('position',"absolute");
	el.setStyle("top", vpos+"px");
	el.setStyle("left", hpos+"px");
	
	//var c= "Scroll : "+scrollXY[0]+"   "+scrollXY[1]+"\n";
	//	c=c+"Window : "+winXY[0]+"   "+winXY[1]+"\n";
	//	c=c+" Div    : "+divXY[0]+"   "+divXY[1]+"\n";
	//	c=c+" Pos    : "+posX+"   "+posY+"\n";
	//alert(c);
	
	
		
	//$(el).setStyle('left', posX+"px");
	//$(el).setStyle('top',  posY+"px");
	
	//alert('end center');
}



function fade(type, obj){
	
	
	if(type=="in"){
		return new Fx.Tween(obj, 'opacity', {duration: 1000, wait: false}).start(0.8);
	}
	else if(type=="out"){
		return new Fx.Tween(obj, 'opacity', {duration: 1000, wait: false}).start(0);
	}
	else
		return false;
		
	
	
}



var bgFade={
	
	currentState: false,
	fadeIn: 0.5,
	fadeOut: 0,
	bg: false,
	
	show: function(){
		
		if(!this.currentState){
			$(document.body).setStyle('overflow', 'hidden');
			this.bg=new Element('div');
			this.bg.setProperty('id', 'bg');
			
			var scroll=window.getScroll();
			var win=window.getSize();
			
			
			this.bg.setStyles({
							'position': 'absolute',
							'background-color': 'Black',
							'top': scroll["y"],
							'left': scroll["x"],
							'width': win["x"],
							'height': win["y"],
							'opacity': 0,
							'z-index': 3	
												   
					   });
			
			this.bg.injectInside(document.body);
			if(arguments.length)
				this.bg.fade(arguments[0]);
			else
				this.bg.fade(this.fadeIn);
			this.currentState=true;
		}
		
		
	},
	
	hide: function(){
		
		if(this.currentState){
			this.bg.fade(this.fadeOut);
			this.bg.destroy();
			this.currentState=false;
			$(document.body).setStyle('overflow', 'auto');
		}		
		
	}
	
			
			
}



//type=hide || delete
function removeElement(type, id){
	
	//alert("1   "+type+"   "+id);
	
	try{
		
		var el=$(id);
		if(type=='hide'){
			el.style.display="none";
		}
		else if(type=='delete'){
			
			el.innerHTML="";
				
			var par=el.parentNode;
			par.removeChild(el);
			
		}
		
		return true;
			
		
	} catch (e){
		
		
		return false;
		
	}
	
}



function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  
  var v=new Array(myWidth, myHeight );
  return v;
}




function SelectMoveRows(SS1,SS2)
{
    var SelID='';
    var SelText='';
    // Move rows from SS1 to SS2 from bottom to top
    for (i=SS1.options.length - 1; i>=0; i--)
    {
		//alert(i+" "+SS1.options[i].value+"  "+SS1.options[i].value+". ");
		if (SS1.options[i].selected && SS1.options[i].value!=0){
            SelID=SS1.options[i].value;
            SelText=SS1.options[i].text;
            var newRow = new Option(SelText,SelID);
			insertRow(SS2, newRow);
			
            //SS2.options[SS2.length]=newRow;
            SS1.options[i]=null;
			
        }
    }
    //SelectSort(SS2);
	n1=	findSelNone(SS1);
	n2=findSelNone(SS2);
	
	if(SS1.options.length>1 && n1  )
		SS1.options[n1]=null;
	if(SS2.options.length>1 &&n2 )
		SS2.options[n2]=null;
	if(SS1.options.length==0)
		SS1.options[0]=new Option("None", "0");
	if(SS2.options.length==0)
		SS2.options[0]=new Option("None", "0");	
	
	
	
	
}


function findSelNone(Sel){
	
	for(i=0; i<Sel.options.length; i++){
			if(Sel.options[i].value=="0")
				return i;
		}
		
		return false;
	
}


function insertRow(sList,OP){
	
	
	var index=getInsertionIndex(sList.options, OP.text, 0, sList.options.length-1);
	
	for(var i=index; i<sList.options.length; i++){
		
		var tOpt=sList.options[i];
		sList.options[i]=OP;
		OP=tOpt;
		
	}
	
	sList.options[i]=OP;
		
}

function getInsertionIndex(sList, selText, low, high){	
	
	if(high<low)
		return false;
	
	if(selText < sList[low].text)
		return low;
	else if(selText > sList[high].text)
		return high+1;
		
	  
	var diff=high-low;  
	
	
	var half=low+( diff % 2==0?diff/2:(diff-1)/2);
	
	
	var halfText=sList[half].text
	
	if(selText>halfText){
		if(diff==1)
			return high;
		else
			return getInsertionIndex(sList, selText, half, high);
	}
	else if(selText<halfText){
		if(diff==1)
			return low;
		else
			return getInsertionIndex(sList, selText, low, half);
	}
	else
		return half;
	
	
	
	
	
}


function isEmail(value){	
	
	return value.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
	
	/*				   /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/	
	)
	*/
}


function selectAll(sel){
	
	for(var i=0; i<sel.length; i++){
		
		sel.options[i].selected=true;
		
	}
	
	
}


function isCurrency(str) {
	isPrice = /^(\+|-)?\d*(\.\d\d)?$/;  
	return (str==0 || isPrice.test( str ) );
}

function isFloat(str) {
	var Float = /0||(^((\d*(\.\d*)?)|((\d*\.)?\d+))$)/;
	return Float.test( str );
}

function isInteger(str){
	
	var Int=/^\d+$/;
	return Int.test(str);
	
}



var SM=new Class({
						   
	el: false,
	response: false, 
	
	initialize: function(r){
		this.response=r;
	},
	
	show: function(r){
		this.response=r;
		//bgFade.show(0.8);
		
		this.el=new Element('div',{
										
										'styles':{
												
													'position': 'absolute',
													'top': -1000,
													'left': -1000,
													'opacity': 0,
													'z-index': 5
												
												}
								}).injectInside(document.body);
					
					
		//if(this.buildInnerCallback)
		//	this.el.innerHTML=this.buildInnerCallback()
		//else if(response)
		this.el.innerHTML=this.buildMessage();
			
		centerElement(this.el);
		this.el.fade(1);
			
		
	},
	
	buildMessage: function(){
		
		var output="<table width='500px' bgcolor=\"#FFFFFF\">\n";
			output=output+"<tbody>	\n";
				output=output+"<tr>\n";
					output=output+"<td>\n";
						output=output+"<center> \n";
							output=output+"<table margin=\"0\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">  \n";
								output=output+"<tbody> \n";
									output=output+"<tr> \n";
										output=output+"<td align=\"left\" valign=\"top\"><img src=\"http://admin.maddancer.com/kapelle/images/alert_top.gif\" style=\"display: block;\" height=\"5\" width=\"100%\"></td>  \n";
									output=output+"</tr>  \n";
									output=output+"<tr>  \n";
										output=output+"<td style=\"background-color: rgb(191, 30, 30);\" align=\"center\" valign=\"top\">  \n"+this.response+"</td>  \n";
									output=output+"</tr>  \n";
									output=output+"<tr>  \n";
										output=output+"<td align=\"left\" valign=\"top\"><img src=\"http://admin.maddancer.com/kapelle/images/alert_bottom.gif\" style=\"display: block;\" border=\"0\" height=\"5\" width=\"100%\"></td>  \n";
									output=output+"</tr>  \n";
								output=output+"</tbody>  \n";
							output=output+"</table>  \n";
						output=output+"</center>";
					output=output+"</td>  \n";
				output=output+"</tr> \n";													
			output=output+"</tbody> \n";
		output=output+"</table>";
		
		return output;
		
		
		
	},
	
	hide: function(){
		
		this.el.fade(0);
		this.el.destroy();
		
	}				   
						   
						   

});

var storeMessage=new SM('');

function writeAlert(msg){
	
	var output="<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\" width=\"100%\">  \n";
			output=output+"<tbody>  \n";
				output=output+"<tr>  \n";
					output=output+"<td align=\"center\" valign=\"middle\" width=\"10%\"><img src=\"http://admin.maddancer.com/kapelle/images/alert_point.gif\" border=\"0\"></td>  \n";
					output=output+"<td align=\"left\" valign=\"middle\" width=\"90%\"><font class=\"AlertText\">Alert:</font> <font class=\"Alert2Text\">"+msg+"</font></td>  \n";
				output=output+"</tr>  \n";
			output=output+"</tbody>  \n";
		output=output+"</table>  \n";
		
	return output;
	
}