// Disable the button in FireFox
function isFireFox()
{
var browser=navigator.appName;
var b_version=navigator.appVersion;
var version=parseFloat(b_version);
if ((browser=="Netscape") && (version>=4))
  	{return true;}
	else
	{return false;}	
}

/* Used so that for JS enabled browsers the seller-notes box doesn't appear */
document.write("<style type='text/css'>");

/* 16555 starts here */

document.write("a#col_calculate-total-link {display: inline;}");
document.write("a#col_outside-us-link {display: inline;}");
document.write("a#col_inside-us-link {display: inline;}");

document.write("a#exp_calculate-total-link {display: inline;}");
document.write("a#exp_outside-us-link {display: inline;}");
document.write("a#exp_inside-us-link {display: inline;}");
document.write("a#exp_zip-change-link {display: inline;}");

//document.write("#col_shipping-info-row {display: none;}");
document.write("div#col_inside-us {display:none;}");
document.write("div#col_outside-us {display:none;}");

document.write("div#exp_inside-us {display:none;}");
document.write("div#exp_outside-us {display:none;}");

document.write("span#col_zipcode-us{display:none;}");
document.write("span#exp_zipcode-us{display:none;}");

/* 16555 ends here */


document.write("table#cart td#notes div textarea { display: none;}");
document.write("</style>");

/* CONSTANTS */
/* variables are commented #PPSCR00629723 title are not getting localised
var TITLE_TAG_COLLAPSED = "Click to view purchase details";
var TITLE_TAG_EXPANDED = "Click to close purchase details"; */
/**
 * Original From: Dustin Diaz - Vitamin
 * Modified by: Emanuele Fabrizio
 */
YAHOO.widget.Effect = function(el) {
	this.oEl = YAHOO.util.Dom.get(el);
	this.height = parseInt(YAHOO.util.Dom.getStyle(this.oEl,'height'));
	this.width = parseInt(YAHOO.util.Dom.getStyle(this.oEl,'width'));
};

/**
 * Performs the closing of the widget towards the top. The iTimer takes
 * an integer in seconds. The onComplete callback function it's not used
 */
YAHOO.widget.Effect.prototype.BlindUp = function(iTimer, onComplete) {
	var timer = iTimer || 1;
	// Make sure TO NOT SPECIFY a unit of percentage or Firefox will be confused
	var blind = new YAHOO.util.Anim(this.oEl, { height: { to:0 } }, timer, YAHOO.util.Easing.easeOut);

	if ( onComplete ) {
		blind.onComplete.subscribe(onComplete);
	}
	blind.animate();
};

/**
 * Performs the opening of the widget towards the bottom. The iTimer takes
 * an integer in seconds. The onComplete callback function it's not used
 */
YAHOO.widget.Effect.prototype.BlindDown = function(iTimer, onComplete) {
	var timer = iTimer || 1;

	this.oEl.style.visibility = 'hidden';
	this.oEl.style.overflow = 'hidden';
	this.oEl.style.height = '';

	var finalHeight = parseInt(YAHOO.util.Dom.getStyle(this.oEl,'height'));

	this.oEl.style.height = '0';
	this.oEl.style.visibility = 'visible';

	//Make sure TO SPECIFY a unit of percentage or IE will not expand the item completely
	var blind = new YAHOO.util.Anim(this.oEl, { height: { to:finalHeight, from:0, unit: '%'} }, timer, YAHOO.util.Easing.easeOut);

	if ( onComplete ) {
		blind.onComplete.subscribe(onComplete);
	}
	blind.animate();
};

/**
 * This controller applies to the container with ID purchase-detail which in
 * turn will execute the animation on the container with ID of cover
 */
toggle = function() {
	var $D = YAHOO.util.Dom;
	var $E = YAHOO.util.Event;
	var $ = $D.get;
	var objExpando = null;
	var TITLE_TAG_COLLAPSED;
	var TITLE_TAG_EXPANDED;
	
	return {
		init : function() {
			objExpando = $('expander');
			if( !isOrderEditable() ){
				$D.addClass('purchase-detail','collapsed');
			}
			$E.on('expander','click',this.controller,$('cover'),true);
		},
		controller : function(e) {
			var z = new YAHOO.widget.Effect(this);
			if ( !$D.hasClass('purchase-detail','collapsed') ) {
				$D.addClass('purchase-detail','collapsed')
				z.BlindUp(2);
				if( objExpando )
				{
				TITLE_TAG_COLLAPSED =document.getElementById('expander') ? document.getElementById('expander').innerHTML: "";
					objExpando.title = TITLE_TAG_COLLAPSED
					document.getElementById("expander").style.position ="relative";
					document.getElementById("expander").style.top = "5px";
				}
			}
			else {
				$D.removeClass('purchase-detail','collapsed')
				z.BlindDown(2);
				if( objExpando )
				{
				TITLE_TAG_EXPANDED = document.getElementById('titleExpanded') ? document.getElementById('titleExpanded').innerHTML: "";
					objExpando.title = TITLE_TAG_EXPANDED; 
					document.getElementById("expander").style.position ="";
					document.getElementById("expander").style.top = 0;
				}
			}
			$E.stopEvent(e);
		}
	};
}();

/**
 * Method used to check whether there are any fields in the order details
 * component which are editable. If so the method returns true
 */
function isOrderEditable(){
	var result = false;
	var objValue = document.getElementById( 'editable-order' );
	if( objValue ){
		result = ( objValue.value == '1' ) ? true : false;
	}
	return result;
};

/**
 * Method invoked to display all textarea tags which are within the container
 * with ID of notes. It also toggles the controller link seller-notes-toggle
 * to add the appropriate CSS class
 */
function enableNotesTextArea(){
	var elements = null;
	if( document.getElementById('notes') ){
		elements = document.getElementById('notes').getElementsByTagName('textarea');
	}
	if( elements ){
		for( var i = 0; i < elements.length; i++ ){
			YAHOO.util.Dom.setStyle(elements[i],'display','block')
		}
		modifyLink( 'on' );
	}
};

/**
 * Used so that for JS enabled browsers the seller-notes-toggle link appears
 * with the proper visual design. If toggle has a value then the class 'opened'
 * is added, otherwise it gets removed
 */
function modifyLink(toggle){

	/* Spec 16555 starts */
	if (document.getElementById("item-quantity")) {
		var itemQty = document.getElementById("item-quantity").value;
 		
		if(itemQty>=10)	{
			document.getElementById("col_shipping-table").style.marginLeft="-10px";
			document.getElementById("col_shipping-table").style.width="100%";
		 }
	}
	/* Spec 16555 end */

	if( toggle == 'on' ){
		YAHOO.util.Dom.addClass(document.getElementById('seller-notes-toggle'), 'opened');
	} else {
		YAHOO.util.Dom.removeClass(document.getElementById("seller-notes-toggle"), 'opened')
	}
	if(document.getElementById("expander")){
	document.getElementById("expander").style.position ="relative";
	document.getElementById("expander").style.top = "5px";
	}
};


/* 16555 starts here */

function colShowInsideUS(){

		if (document.getElementById("col_calculate-shipping")) {
			var calculateShipping = document.getElementById("col_calculate-shipping");
			calculateShipping.style.display = "none"; 
		}
		
		if (document.getElementById("col_shipping-info-row")) {
			var shippingInfoRow = document.getElementById("col_shipping-info-row");
			shippingInfoRow.style.display = "block";
		}

		var insideUS = document.getElementById("col_inside-us");
		var outsideUS = document.getElementById("col_outside-us");
	
		insideUS.style.display = (isFireFox()) ? "block" : "inline";
		outsideUS.style.display = "none"; 

		if (document.getElementById("exp_calculate-shipping")) {
			var calculateShipping = document.getElementById("exp_calculate-shipping");
			calculateShipping.style.display = "none"; 
		}

		var insideUS = document.getElementById("exp_inside-us");
		var outsideUS = document.getElementById("exp_outside-us");
		
		insideUS.style.display = (isFireFox()) ? "block" : "inline";
		outsideUS.style.display = "none"; 

		if (document.getElementById("exp_shipping-and-handling")) {
			var shippingAndHandling = document.getElementById("exp_shipping-and-handling");
			shippingAndHandling.style.display = "none";
		}

		/*if (document.getElementById("exp_tax"))
		{
			var tax = document.getElementById("exp_tax");
			tax.style.display = "none"; 
		}

		if (document.getElementById("exp_total"))
		{
			var total = document.getElementById("exp_total");
			total.style.display = "none"; 
		}*/

		if (document.getElementById("col_shipping-and-handling")) {
			var shippingAndHandling = document.getElementById("col_shipping-and-handling");
			shippingAndHandling .style.display = "none";
		}

		/* if (document.getElementById("col_tax"))
		{
			var tax = document.getElementById("col_tax");
			tax.style.display = "none";
		}

		if (document.getElementById("col_total"))
		{
			var total = document.getElementById("col_total");
			total.style.display = "none";
		} */
};


function colShowOutsideUS(){

		if (document.getElementById("col_calculate-shipping")) {
			var calculateShipping = document.getElementById("col_calculate-shipping");
			calculateShipping.style.display = "none"; 
		}

		if (document.getElementById("col_shipping-info-row")) {
			var shippingInfoRow = document.getElementById("col_shipping-info-row");
			shippingInfoRow.style.display = "block";
		}

		var insideUS = document.getElementById("col_inside-us");
		var outsideUS = document.getElementById("col_outside-us");
	
		
		outsideUS.style.display = (isFireFox()) ? "block" : "inline";
		insideUS.style.display = "none";
		
		if (document.getElementById("exp_calculate-shipping")) {
			var calculateShipping = document.getElementById("exp_calculate-shipping");
			calculateShipping.style.display = "none"; 
		}

		var insideUS = document.getElementById("exp_inside-us");
		var outsideUS = document.getElementById("exp_outside-us");
	
		outsideUS.style.display = (isFireFox()) ? "block" : "inline";
		insideUS.style.display = "none";

		if (document.getElementById("exp_shipping-and-handling")) {
			var shippingAndHandling = document.getElementById("exp_shipping-and-handling");
			shippingAndHandling.style.display = "none";
		}

	/*	if (document.getElementById("exp_tax")) {
			var tax = document.getElementById("exp_tax");
			tax.style.display = "none";
		}

		if (document.getElementById("exp_total")) {
			var total = document.getElementById("exp_total");
			total.style.display = "none"; 
		} */

		if (document.getElementById("col_shipping-and-handling")) {
			var shippingAndHandling = document.getElementById("col_shipping-and-handling");
			shippingAndHandling .style.display = "none";

		}
	/*	if (document.getElementById("col_tax")) {
			var tax = document.getElementById("col_tax");
			tax.style.display = "none";
		}

		if (document.getElementById("col_total")) {
			var total = document.getElementById("col_total");
			total.style.display = "none";
		}  */
		
};

function colShowUSZipCodeField(){
	var zipCodeUS = document.getElementById("col_zipcode-us");
	var countryCode = document.getElementById("col_ship_to_country");

	var selIndex = countryCode.selectedIndex;
	zipCodeUS.style.display = (countryCode.options[selIndex].value == "US") ? "inline" : "none";

	var zipCodeUS = document.getElementById("exp_zipcode-us");
	var countryCode = document.getElementById("exp_ship_to_country");

	var selIndex = countryCode.selectedIndex;
	zipCodeUS.style.display = (countryCode.options[selIndex].value == "US") ? "inline" : "none";
};


function expShowUSZipCodeField(){
	var zipCodeUS = document.getElementById("exp_zipcode-us");
	var countryCode = document.getElementById("exp_ship_to_country");
	var selIndex = countryCode.selectedIndex;

	if (countryCode.options[selIndex].value=="US") {
		zipCodeUS.style.display = "inline"; 
	} else {
		zipCodeUS.style.display = "none"; 
	}
};


function submitTheForm() {
	var reviewForm = document.getElementById("reviewForm");
	reviewForm.submit();
};

function showShippingFields() {
	var shippingCalculatedBy = document.getElementById("shipping-calculated-by");
	if (shippingCalculatedBy.value == 'country') {
		colShowOutsideUS();
	} else {
		colShowInsideUS();
	}
};

function updateBothZipField() {
	var expShipToZip = document.getElementById("exp_ship_to_zip");
	var colShipToZip = document.getElementById("col_ship_to_zip");
	expShipToZip.value = colShipToZip.value;
};

function updateExpZipField() {
	if (document.getElementById("exp_ship_to_zip")) {
		var colShipToZip = document.getElementById("col_ship_to_zip");
		var expShipToZip = document.getElementById("exp_ship_to_zip");
		expShipToZip.value = colShipToZip.value;
	}
};

function updateColZipField() {
	if (document.getElementById("col_ship_to_zip")) {
		var colShipToZip = document.getElementById("col_ship_to_zip");
		var expShipToZip = document.getElementById("exp_ship_to_zip");
		colShipToZip.value = expShipToZip.value;
	}
};

/* 16555 ends here */


YAHOO.util.Event.addListener( window, 'load', modifyLink );
YAHOO.util.Event.addListener('seller-notes-toggle', 'click', enableNotesTextArea);
YAHOO.util.Event.onAvailable('purchase-detail', toggle.init, toggle, true);


/* 16555 starts here */

YAHOO.util.Event.addListener('exp_calculate-total-link', 'click', colShowInsideUS);
YAHOO.util.Event.addListener('exp_outside-us-link', 'click', colShowOutsideUS);
YAHOO.util.Event.addListener('exp_inside-us-link', 'click', colShowInsideUS);

YAHOO.util.Event.addListener('col_calculate-total-link', 'click', colShowInsideUS);
YAHOO.util.Event.addListener('col_outside-us-link', 'click', colShowOutsideUS);
YAHOO.util.Event.addListener('col_inside-us-link', 'click', colShowInsideUS);

YAHOO.util.Event.addListener('col_ship_to_country', 'change', colShowUSZipCodeField);
YAHOO.util.Event.addListener('exp_ship_to_country', 'change', colShowUSZipCodeField);

YAHOO.util.Event.addListener('col_zip-change-link', 'click', showShippingFields);
YAHOO.util.Event.addListener('exp_zip-change-link', 'click', showShippingFields);

YAHOO.util.Event.addListener('shipping_method', 'change', submitTheForm);

YAHOO.util.Event.addListener('col_ship_to_zip', 'keyup', updateExpZipField);
YAHOO.util.Event.addListener('exp_ship_to_zip', 'keyup', updateColZipField);


/* Widget Code added by Parthiban */

function getKeyCodeWidget(e) {
	var key = e ? e.which : window.event.keyCode;	
	return key;
};

function getFieldWidget(e) {
	var elem;
	
	if (!e) { 
		var e = window.event;		
	}
	if (e.target) { // the w3c way
		elem = e.target;
	}
	else if (e.srcElement) { // the IE way
		 elem = e.srcElement;
	} else {
		elem = null;
	}
	if (elem && elem.nodeType == 3) { // defeat Safari bug
		elem = elem.parentNode;
	}

	return elem;
};

function dfltSubmitWidget(e) {
	var key = getKeyCodeWidget(e);
	var field = "";		
	
	// if <Enter> key was used
	if (key == 13) {
		field = getFieldWidget(e);		

		if (field && document.getElementById("dfltButton")) 
		{
			if (field.id == "login_email" || field.id == "login_password" || field.id == "login.x") {
				document.getElementById("dfltButton").name = "login";
				document.getElementById("dfltButton").value = "Log In";
			} 
			else if (field.id == "col_ship_to_zip" || field.id == "exp_ship_to_zip") {
				document.getElementById("dfltButton").name = "inside_update";
				document.getElementById("dfltButton").value = "Update";
			} else {
				document.getElementById("dfltButton").focus();
				document.getElementById("dfltButton").name = "continue";	
				document.getElementById("dfltButton").value = "Continue";
			}
		}
	}
};

document.onkeypress = dfltSubmitWidget;

/* 16555 ends here */
