// ActionScript file
import com.tp.helpers.Global;

import flash.events.Event;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.managers.PopUpManager;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;

public var cardId:int = new int();
private var cost:Number = 0; 
public var productName:String; 


private var httpService:HTTPService = new HTTPService();


private function init():void
{
	//PopUpManager.centerPopUp(this);
	costText.text = "CAD  $22.00";
	cost = 22;
	quantityComboBoxId.dataProvider = Global.quantityArray;
	paperStockComBoxId.dataProvider = Global.paperStockArray;
	ShippingComBoxId.dataProvider = Global.shippingOptionsArray;
	textStyleComBoxId.dataProvider = Global.textStyleArray;
}
private function closeYourSelf():void
{
	PopUpManager.removePopUp(this);
}

private function creationCompleteFunction():void
{
//	descriptionLabelId.text = (this.parentApplication as BussinessCard).step2.step2ProductComboBoxId.selectedItem.name.toString();
//	desImageId.source = (this.parentApplication as BussinessCard).step2.frontDesignImage.source;
	PopUpManager.centerPopUp(this);
}

private function quantityComboBoxChange(event:Event):void
{
	if(event.currentTarget.selectedItem.id == 5)
	{
		quantityTextInputId.visible = true;
	}
	else
	{
		quantityTextInputId.visible = false;
	}
	updateCost();
}

private function paperStockComboBoxChange(event:Event):void
{
	if(event.currentTarget.selectedItem.id == 6)
	{
		paperStockTextInputId.visible = true;
	}
	else
	{
		paperStockTextInputId.visible = false;
	}
	updateCost();
}

private function textStyleComboBoxChange(event:Event):void
{
	if(event.currentTarget.selectedItem.id == 3)
	{
		textStyleTextInputId.visible = true;
	}
	else
	{
		textStyleTextInputId.visible = false;
	}
	updateCost();
}

private function shippingComboBoxChange(event:Event):void
{
	if(event.currentTarget.selectedItem.id == 5)
	{
		shippingOptionTextInputId.visible = true;
	}
	else
	{
		shippingOptionTextInputId.visible = false;
	}
	updateCost();
}

private function SubmitDetails():void
{
	
	var quantitySelected:int = 0;
	var allClear:Boolean = true;
	if(quantityTextInputId.visible == true)
	{
		allClear = false;		
	}
	else
	{
		quantitySelected = quantityComboBoxId.selectedItem.value;
	}
	if(paperStockTextInputId.visible == true)
	{
		allClear = false;
	}
	if(textStyleTextInputId.visible == true)
	{
		allClear = false;
	}
	if(shippingOptionTextInputId.visible == true)
	{
		allClear = false;	
	}
	if(allClear == true)
	{
		var object:Object = new Object();
		object["productName"] = productName;		
		object["quantity"] = quantitySelected;	
		object["cardId"] = cardId;
		object["cost"] = cost;
		object["paperStock"] = paperStockComBoxId.selectedItem.value.toString();
		object["textStyle"] = textStyleComBoxId.selectedItem.styleName.toString();
		object["shippingDetail"] = ShippingComBoxId.selectedItem.name.toString(); 
//		httpService.url = Global.SetExpressCheckoutPath;
//		httpService.addEventListener(ResultEvent.RESULT,resultHandler);
//		httpService.addEventListener(FaultEvent.FAULT,faultHandler);
//		httpService.method = "GET";
//		httpService.send(object);
		PopUpManager.removePopUp(this);
		
		var url:URLRequest = new URLRequest(Global.SetExpressCheckoutPath);	
		var parameter:URLVariables = new URLVariables();
		parameter.productName = object.productName;
		parameter.quantity = object.quantity;
		parameter.cardId = object.cardId;
		parameter.cost = object.cost;
		parameter.paperStock = object.paperStock;
		parameter.textStyle = object.textStyle;
		parameter.shippingDetail = object.shippingDetail;
		url.data = parameter;
		url.method = URLRequestMethod.POST;
		navigateToURL(url,"_self");
		
	}
	else
	{
		Alert.show("To pay For a Custom Selected Value You have to Call Us .");
	}
}

private function updateCost():void
{
	cost = 0;
	costText.text = "";
//	for(var count:int = 0;count < Global.paperStockArray.length;count++)
//	{
	if(paperStockComBoxId.selectedIndex == 0)
	{
		calculateCostForPaperStock13pt();
	}	
	else if(paperStockComBoxId.selectedIndex == 1)
	{
		calculateCostForPaperStock14pt();	
	}
	else if(paperStockComBoxId.selectedIndex == 2)
	{
		calculateCostForPaperStock100lb();
	}
	else if(paperStockComBoxId.selectedIndex == 3)
	{
		calculateCostForPaperStock70lb();
	}
	else if(paperStockComBoxId.selectedIndex == 4)
	{
		calculateCostForPaperStock24lb();
	}
	else if(paperStockComBoxId.selectedIndex == 5)
	{
		setCostForCustomValueSelected();
	}
		
}

private function updateCostOld():void
{
	cost = 0;
	costText.text = "";
	if(paperStockComBoxId.selectedIndex == 0)
	{
		calculateCostForPaperStock13pt();
	}	
	else if(paperStockComBoxId.selectedIndex == 1)
	{
		calculateCostForPaperStock14pt();	
	}
	else if(paperStockComBoxId.selectedIndex == 2)
	{
		calculateCostForPaperStock100lb();
	}
	else if(paperStockComBoxId.selectedIndex == 3)
	{
		calculateCostForPaperStock70lb();
	}
	else if(paperStockComBoxId.selectedIndex == 4)
	{
		calculateCostForPaperStock24lb();
	}
	else if(paperStockComBoxId.selectedIndex == 5)
	{
		setCostForCustomValueSelected();
	}
}

private function calculateCostForPaperStock13pt():void
{
	if(quantityComboBoxId.selectedIndex == 0)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(0).thirteenPoint.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 1)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(1).thirteenPoint.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 2)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(2).thirteenPoint.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 3)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(3).thirteenPoint.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 4)
	{
		cost = 0;
		setCostForCustomValueSelected();
	}
	
	if(quantityComboBoxId.selectedIndex != 4)
	{
		addShippingCost();		
	}
}

private function calculateCostForPaperStock14pt():void
{
	if(quantityComboBoxId.selectedIndex == 0)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(0).fourteenPoint.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 1)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(1).fourteenPoint.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 2)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(2).fourteenPoint.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 3)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(3).fourteenPoint.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 4)
	{
		cost = 0;
		setCostForCustomValueSelected();
	}
	
	if(quantityComboBoxId.selectedIndex != 4)
	{
		addShippingCost();		
	}
}

private function calculateCostForPaperStock100lb():void
{
	if(quantityComboBoxId.selectedIndex == 0)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(0).hunderedLb.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 1)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(1).hunderedLb.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 2)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(2).hunderedLb.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 3)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(3).hunderedLb.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 4)
	{
		cost = 0;
		setCostForCustomValueSelected();
	}
	
	if(quantityComboBoxId.selectedIndex != 4)
	{
		addShippingCost();		
	}
}

private function calculateCostForPaperStock70lb():void
{
	if(quantityComboBoxId.selectedIndex == 0)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(0).seventyLb.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 1)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(1).seventyLb.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 2)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(2).seventyLb.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 3)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(3).seventyLb.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 4)
	{
		cost = 0;
		setCostForCustomValueSelected();
	}
	
	if(quantityComboBoxId.selectedIndex != 4)
	{
		addShippingCost();		
	}
}

private function calculateCostForPaperStock24lb():void
{
	if(quantityComboBoxId.selectedIndex == 0)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(0).twentyFourLb.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 1)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(1).twentyFourLb.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 2)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(2).twentyFourLb.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 3)
	{
		cost = parseFloat(Global.quantityRate.getItemAt(3).twentyFourLb.toString());
	}
	else if(quantityComboBoxId.selectedIndex == 4)
	{
		cost = 0;
		setCostForCustomValueSelected();
	}
	
	if(quantityComboBoxId.selectedIndex != 4)
	{
		addShippingCost();		
	}
}

private function setCostForCustomValueSelected():void
{
	cost = 0;
	costText.text = "$0.00";
} 

private function addTextStyleCost():void
{
	var temp:Number = Global.textStyleRate;
	if(textStyleComBoxId.selectedIndex == 0)
	{
		updateCostText();
	}
	else if(textStyleComBoxId.selectedIndex == 1)
	{	
		cost += Global.textStyleRate;
		updateCostText();
	}
	else if(textStyleComBoxId.selectedIndex == 2)
	{
		setCostForCustomValueSelected();
	}
}
private function addShippingCost():void
{
	if(Global.shippingRate)
	{
		var tempArray:ArrayCollection = new ArrayCollection();
		tempArray = Global.shippingRate;
	}
	if(ShippingComBoxId.selectedIndex == 0)
	{
		cost += parseFloat(Global.shippingRate.getItemAt(0).pickUp.toString());
		addTextStyleCost();
	}
	if(ShippingComBoxId.selectedIndex == 1)
	{
//		cost += 15;
		cost += parseFloat(Global.shippingRate.getItemAt(0).oneDayFedEx.toString());
		addTextStyleCost();
	}
	else if(ShippingComBoxId.selectedIndex == 2)
	{		
//		cost += 8;
		cost += parseFloat(Global.shippingRate.getItemAt(0).threeDayFedEx.toString());
		addTextStyleCost();
	}
	else if(ShippingComBoxId.selectedIndex == 3)
	{
//		cost += 5;
		cost += parseFloat(Global.shippingRate.getItemAt(0).regularMail.toString());
		addTextStyleCost();
	}
	else if(ShippingComBoxId.selectedIndex == 4)
	{
		setCostForCustomValueSelected();
	}
}
private function updateCostText():void
{
	costText.text = "CAD  $" + cost.toString() + ".00";
}

private function back():void
{
	PopUpManager.removePopUp(this);
}

private function resultHandler(event:ResultEvent):void
{	
//	PopUpManager.removePopUp(this);
	
//	ExternalInterface.call('window.open','about:blank','payPalWindow','height=900,width=900,toolbar=no,scrollbars=yes');
//			
//	var url:URLRequest = new URLRequest(Global.SetExpressCheckoutPath);
//	
//	navigateToURL(url,"payPalWindow");

//	PopupWin.baseURL = Global.SetExpressCheckoutPath;
//	PopupWin.showHelp('');

//	URLUtils.openWindow(Global.SetExpressCheckoutPath);

//	var url:URLRequest = new URLRequest(Global.SetExpressCheckoutPath);	
//	navigateToURL(url,"_self");

}

private function faultHandler(event:FaultEvent):void
{
	Alert.show("Error in Paypal Handler" + event.message);
}