function AjaxRequest()
{
	this.mRequest = this.getHttpRequest();
	this.mHandlers = new Array();
	var self = this;
	
	this.mRequest.onreadystatechange = function()
	{
		if(	self.mHandlers[ self.mRequest.readyState ] != undefined )
		{
			for( i = 0 ; i < self.mHandlers[ self.mRequest.readyState ].length ; i++ )
			{
				self.mHandlers[ self.mRequest.readyState ][ i ]( self );				
			}
		}
	}
}

AjaxRequest.prototype.addEventListener = function( pEventType, pFunction )
{
	if(	this.mHandlers[ pEventType ] == undefined )
	{
		this.mHandlers[ pEventType ] = new Array();
	}
	
	this.mHandlers[ pEventType ].push( pFunction );
}

AjaxRequest.prototype.getHttpRequest = function()
{
	// List of Microsoft XMLHTTP versions - newest first

	var MSXML_XMLHTTP_PROGIDS = new Array
	(
		'MSXML2.XMLHTTP.5.0',
		'MSXML2.XMLHTTP.4.0',
		'MSXML2.XMLHTTP.3.0',
		'MSXML2.XMLHTTP',
		'Microsoft.XMLHTTP'
	);

	// Do we support the request natively (eg, Mozilla, Opera, Safari, Konqueror)

	if( window.XMLHttpRequest != null )
	{
		return new XMLHttpRequest();
	}
	else
	{
		// Look for a supported IE version

		for( i = 0 ; MSXML_XMLHTTP_PROGIDS.length > i ; i++ )
		{
			try
			{
				return new ActiveXObject( MSXML_XMLHTTP_PROGIDS[ i ] );
			}
			catch( e )
			{
			}
		}
	}
	
	return( null );
}

function PremiumPressDo( fileName, div )
{
	var Ajax = new AjaxRequest();

	if( Ajax.mRequest )
	{				
		Ajax.mFileName 	= fileName;		
		var obj = document.getElementById(div);				

		Ajax.mRequest.open( "GET", fileName);
		Ajax.mRequest.onreadystatechange = function() {
			if(Ajax.mRequest.readyState == 4 && Ajax.mRequest.status == 200){
				obj.innerHTML = Ajax.mRequest.responseText;
			}
		}		
	}
	Ajax.mRequest.send( null );
}



function add_image_next(current,path,ilink,type){
	 
var searchword = document.getElementById('searchBox1').value;
var a =ilink;
a=a.split("http://").join("");
PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=add_image_next&p='+current+'&path='+path+'&link='+a+'&type='+type+'&search='+searchword,'DisplayImages');
	
}

function PremiumPressChangeState(current){
 
PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=changestate&current='+current,'PremiumPressState');
	
}

function CatPriceBox(current){
 
PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=catprice&current='+current,'PPT-catpricebox');
	
}

function CatTextBox(current){
 
PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=cattext&current='+current,'PPT-cattext');
	
}

function UpdateSliderItem(){ 

	var eid = document.getElementById('ppsedit').value;
	var s1 = document.getElementById('pps1').value;
	var s2 = document.getElementById('pps2').value;
	var s3 = document.getElementById('pps3').value;
	var s4 = document.getElementById('pps4').value;
	var s5 = document.getElementById('pps5').value;
	var s6 = document.getElementById('pps6').value;
	
	PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=updateSlider&s1='+s1+'&s2='+s2+'&s3='+s3+'&s4='+s4+'&s5='+s5+'&s6='+s6+'&eid='+eid,'PPT-sliderbox');

}	

function DeleteSliderItem(thisone){
	 
	PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=deleteSlider&eid='+thisone,'PPT-sliderbox');
		
}

function EditsliderItem(thisone){
	
	PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=editSlider&eid='+thisone,'PPT-sliderbox');
	
	
}

function addiCodes(catid, title, desc, url, code, s5,s6,s7,status,img){


	PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=icodes&type='+s7+'&catid='+catid+'&title='+title+'&desc='+desc+'&url='+url+'&code='+code+'&exp='+s5+'&mer='+s6+'&st='+status+'&img='+img,'ShopperPressAlert');
}


function icodesList(a1){

	PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=icodesList&type='+a1,'icodesList');
}

function changeProperty(a1,div){

	PremiumPressDo('wp-content/themes/directorypress/PPT/ajax/actions.php?action=changeProperty&d='+a1,div);
}

function changeProperty1(a1,div){

	PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=changeProperty&d='+a1,div);
}


function UserActionForm(val,val1,act,div){

	PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=UserActionForm&act='+act+'&data='+val+'&data1='+val1,div);
}











// SHOPPERPRESS CART OPTIONS

  function addProduct(SessionID, siteurl, ui, clicked, clickparts, ajaxURL, error_text, ok_text) {


            // ASSIGN DEFAULTS
            productextra = 0;
            productprice = 0;
            productqty   = 1;
            productshipping =0;

			// this is for wordpress featured page buy links on a 2 tier dynamic url /88/88/
			if(ajaxURL ==1){	mydomain ="../../";		}
            
            //calculate & update new cart total
			varcarttotal = $("#carttotal").text();
  	 		
			prodparts = clickparts.split("|");			
			
			productid = prodparts[0];
			productname = prodparts[1];
			productprice = prodparts[2]; 
		  
            
            // CUSTOM FIELDS
            CustomField1 = $("#CustomField_1").text();
            CustomField2 = $("#CustomField_2").text();
            CustomField3 = $("#CustomField_3").text();
            CustomField4 = $("#CustomField_4").text();
            CustomField5 = $("#CustomField_5").text();
            CustomField6 = $("#CustomField_6").text();
            CustomField7 = $("#CustomField_7").text();
           
            CustomField1_Required = $("#CustomField_1_required").text();
            CustomField2_Required = $("#CustomField_2_required").text();
            CustomField3_Required = $("#CustomField_3_required").text();
            CustomField4_Required = $("#CustomField_4_required").text();
            CustomField5_Required = $("#CustomField_5_required").text();
            CustomField6_Required = $("#CustomField_6_required").text();
            CustomField7_Required = $("#CustomField_6_required").text();
             
            if(CustomField1_Required ==1 && CustomField1 == ""){ alert(error_text); return false; }
            if(CustomField2_Required ==1 && CustomField2 == ""){ alert(error_text); return false; }
            if(CustomField3_Required ==1 && CustomField3 == ""){ alert(error_text); return false; }
            if(CustomField4_Required ==1 && CustomField4 == ""){ alert(error_text); return false; }
            if(CustomField5_Required ==1 && CustomField5 == ""){ alert(error_text); return false; }
            if(CustomField6_Required ==1 && CustomField6 == ""){ alert(error_text); return false; }
            if(CustomField7_Required ==1 && CustomField6 == ""){ alert(error_text); return false; }
            
            if( CustomField3 == ""){  CustomField3=0; }
            if( CustomField6 == ""){  CustomField6=0; }
            
            // custom qty			
			FindQty = $("#CustomQty").text();				
			if(FindQty != ""){ productqty = FindQty; }
            
            // custom shipping	
            FindShip = $("#CustomShipping").text();	
            if(FindShip != ""){ productshipping = FindShip;} 	
            
            // custom extra	 price options,
            // custom fields 3 and 6 can adjust the item price				 	
			if(CustomField3 != ""){productextra = productextra + parseFloat(CustomField3); }  
			if(CustomField6 != ""){productextra = productextra + parseFloat(CustomField6); }
                
			//remove any commas
			varcarttotal = varcarttotal.replace(',', '');
			newtotal = parseFloat(varcarttotal)+ ( parseFloat(productprice) * parseFloat(productqty) )+parseFloat(productextra);
			newtotal = Math.round(newtotal*100)/100;
			newtotal = newtotal.toFixed(2);
			
			$("#carttotal").text(newtotal);
			$.get(siteurl+"PPT/ajax/updatecart.php", { sid: SessionID, newprice: newtotal } );

			//calculate and update product qty
			varcartqty = $("#cartqty").text();           
             
			if(varcartqty ==""){ varcartqty=0; }
 
			newqty = parseFloat(varcartqty)+parseFloat(productqty);
			$("#cartqty").text(newqty);
			$.get(siteurl+"PPT/ajax/updatecart.php", { sid: SessionID, action: "increaseqty" } );
			
			if($("#ddcproduct"+productid).text()!="") {

				//already in list, just increase qty
				newprodqty = parseFloat($("#ddcproduct"+productid).attr("qty"))+1;
				$("#ddcproduct"+productid).attr("qty", newprodqty);
				$("#ddcproduct"+productid+" > #cell2").html(newprodqty);

			}
								 
			$.get(siteurl+"PPT/ajax/updatecart.php", { sid: SessionID, action: "addproduct", id: productid, qty: productqty, ship:productshipping, c1:CustomField1, c2:CustomField2, c3:CustomField3,  c4:CustomField4, c5:CustomField5, c6:CustomField6,c7:CustomField7 } ); //, size: productsize, color: productcolor,   extra:productextra
             
	$('#stockdisplay').hide();			 
	$('#BUYBUTTON').addClass("added");
	$('#BUYBUTTON').text(ok_text);
            
  
  }
  
  
  
  
  
  
  
  
  
  
  
    function removeProduct(SessionID,siteurl,productid,shippingCost) {

  	//if qty = 1, fade out item
 
	if(parseFloat($("#ddcproduct"+productid).attr("qty"))==1) {

		//fade out & remove item entirely
		$("#ddcproductCheckoutt"+productid).fadeOut(500, function() {
			$("#ddcproduct"+productid).remove();
		});

	} else {
		//just decrease qty
		productname = $("#ddcproduct"+productid).attr("productname");
		newprodqty = parseFloat($("#ddcproduct"+productid).attr("qty"))-1;
		$("#ddcproduct"+productid).attr("qty", newprodqty);
		$("#CheckoutQty"+productid).html(newprodqty);
	}
	
	//calculate and update product qty
	varcartqty = $("#cartqty").text();
	newqty = parseFloat(varcartqty)-1;
	
	//protect against going below zero.
	if(newqty < 0) newqty = 0;
	
	$("#cartqty").text(newqty);
	
	//update session item list 
	$.get(siteurl+"PPT/ajax/updatecart.php", { sid: SessionID, action: "removeproduct", id: productid } );
	
	//update price
	var ShoppingCartTotal = $("#CheckoutSubTotal").text();    
    var RemovingProductPrice = $("#ddcproduct"+productid).attr("price");
   
	newtotal =  parseFloat(ShoppingCartTotal) - parseFloat(RemovingProductPrice);
  
	//newtotal = Math.round(newtotal*100)/100;
	//newtotal = parseFloat(ShoppingCartTotal) - newtotal.toFixed(2);
     
	//protect against going below zero.
	if(parseFloat(newtotal) < 0) newtotal = 0;
 
 
	$("#carttotal").text(newtotal);
	//$("#CheckoutSubTotal").text(newtotal);

	//withShipping = parseFloat(newtotal) + parseFloat(shippingCost);
 	//$("#CheckoutTotal").text(withShipping.toFixed(2));
	$.get(siteurl+"PPT/ajax/updatecart.php", { sid: SessionID, newprice: newtotal } );	
	
  }
  
  
  
  
  
  
  
  
  
  function ChangeCustomQty(val){

  document.getElementById('CustomQty').innerHTML = val;

}

function ChangeCustomShip(val){

 document.getElementById('CustomShipping').innerHTML = val;
}

 

function ChangeCustomFieldValue(val,divName){
 
 document.getElementById(divName).innerHTML = val;

}

function CheckoutAlert(text, CheckoutLink){

var answer = confirm (text)
if (answer)
window.location= CheckoutLink;

}





function ShopperPress_ChangeState(current){
	
	PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=changestate&current='+current,'ShopperPressState');
	
}

function ShopperPress_ChangeState_Value(current){
	
	PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=changestatevalue&current='+current,'ShopperPressState_amount');
	
}

function ShopperPress_ChangeState1(current){
	
	PremiumPressDo('../wp-content/themes/directorypress/PPT/ajax/actions.php?action=changestate&current='+current,'ShopperPressState');
	
}
