/*
 *
 * set_up_trade
 * General JavaScript File By: Paul Grimes
 *      Set Focus to main input
 *      Set Accordion Handler for side menu
 *      Set ajax handler for submission
 *      Set Drop Down Handler for top menu
 *      Set Modal class for submit, upload, etc...
 *      Set Delete function for table items
 *      Set Sum function for calculating totals
 *
 * set_up_thanks
 *      Set up Drop Down handler for Top Menu
 *      Set Validation Handler
 *
 */

 total = 0; // yay for globals
 currency_for_total = '$';

/*
 * set_up_trade
 *
 * JavaScript functions for calculator.php
 */
function set_up_trade() {
    window.addEvent('domready', function() {
        SetFocus();
        AccordionHandler();
        DropDownHandler();
        AjaxHandler();
        Modal();
        add_delete();
        calcSum();
        show_referral_other();

        //Remove the "new" class from all response elements.
        //These elements (if any) have all been added to the page
        //and are no longer "new" elements. The "new" class is
        //to denote rows that have just been added.
        $$(".response.new").each(
            function(element){
                element.removeClass("new");
            }
        );
    });
}

function set_up_view() {
  window.addEvent('domready', function() {
      DropDownHandler();
      view_modal();
      add_delete();
      calcSum();
    });
}

/*
 * set_up_thanks
 *
 * JavaScript functions for thank-you.php
 */
function set_up_thanks() {
    window.addEvent('domready', function() {
	    DropDownHandler();
	});
}

/*
 * AjaxHandler
 *
 * We need to add an event to utilize a
 * ajax feed to push/get results of the
 * calculator
 *
 */
function AjaxHandler() {
    validation = new FormCheck("abundaInput");
    enableSubmitHandling();
}

/*
 * Enable the submission handling for the "Add Item" submission
 */
function enableSubmitHandling(){
    $('abundaInput').removeEvent(
        'submit',
        killEvent
    );
    $('abundaInput').addEvent(
        'submit',
        handleAddedItem
    );
}

/*
 * Disable the submission handling for the "Add Item" submission
 */
function disableSubmitHandling(){
    $('abundaInput').removeEvent(
        'submit',
        handleAddedItem
    );
    $('abundaInput').addEvent(
        'submit',
        killEvent
    );
}

/*
 * An event handler that simply stops the event from being further processed
 */
function killEvent(e){
    e.stop();
}

/*
 * An event handler to add an item to the top of the list
 */
function handleAddedItem(e) {
    //While we are processing, we don't want to be handling competing
    //submissions (for instance, from the second click of a user's double click)
    disableSubmitHandling();
    e.stop(); // Stop posting to "process/request.php

    if (valid_input()) {
        addDuplicatesToQuantity();
        $('main_content').addClass('ajax-loading');
        var res = $('abundaCalcBody_request').empty();
        // Get the results back when completed
        this.set(
        'send',
        {   //Set on complete function
            onComplete : function(response) {
                var jsonobj = eval('(' + response + ')');

                window.total = jsonobj.total;
                window.currency_for_total = jsonobj.currency_for_total;

                //Remove any rows that are no longer needed
                //because they are expected to be updated
                //by the response
                disposeQueued();

                res.removeClass('ajax-loading');

                // Add the new response to the top of the existing table
                var itemTable = $("abundaCalcBody_process");
                itemTable.set("html", (jsonobj.row_html + itemTable.innerHTML));//itemTable.innerHTML;

                // Fade in each new element
                var newElements = $$("tr.response.new");
                newElements.each(function(element){fadeInNewElement(element)});

                add_delete();
                $('product_code').value = "";
                $('product_qty').value = "1";

                calcSum();
                SetFocus();
                enableSubmitHandling();
            }
        }
        );
        this.send();
    }
    else{
        validation.reinitialize();
        enableSubmitHandling();
    }
}

/*
 * Fade a new element in graphically, removing the "new" class as well
 */
function fadeInNewElement(element){
    // It isn't "new" anymore. We don't want it to get faded in
    // the next time an element is added...
    element.removeClass("new");

    var originalBGColor = element.getStyle("background-color");

    new Fx.Morph(element).set({
        'background-color': '#76B8FF'
    });

    new Fx.Morph(
    element,
    {
        duration: 800,
        transition: Fx.Transitions.Sine.easeOut
    }
    ).start({
        'background-color': originalBGColor
    });
}



/*
 * add_delete
 *
 * We need a way to add the delete capabilities to table rows if applicable
 */
function add_delete() {
    $$('img.delete').each( function(el) {
	    el.addEvent('click', function() {
		    $upc = $(this).getParent().getParent().getElements('td.upc');
		    delete_item($upc[0].get('text'));
		    $(this).getParent().getParent().dispose();
		    calcSum();
		    SetFocus();
		});
	});
}

/*
 * Accordion Handler
 *
 * We need to add an event to handle
 * the side menu accordion.
 *
 */
function AccordionHandler() {
    var myAccordion = new Accordion($('accordion'), $$('h3.toggler'), $$('div.element'), {
	    display : 3,
	    opacity : true,
	    onActive : function(toggler, element) {
		toggler.removeClass('toggler');
		toggler.addClass('toggler_selected');
		SetFocus();
	    },
	    onBackground : function(toggler, element) {
		toggler.removeClass('toggler_selected');
		toggler.addClass('toggler');
	    }
	});
}

/*
 * DropDownHandler
 *
 * We need to set an event to handle the
 * main menu drop down effects.
 */
function DropDownHandler() {
    if ($('drop_down_menu')){
        $('drop_down_menu').getElements('li.menu').each( function(elem) {
		var list = elem.getElement('ul.links');
		var myFx = new Fx.Slide(list).hide();
		elem.addEvents( {
			'mouseenter' : function() {
			    myFx.cancel();
			    myFx.slideIn();
			},
			    'mouseleave' : function() {
				myFx.cancel();
				myFx.slideOut();
				elem.addClass('');
			    }
		    });
	    });

        $$('img.mo').each( function(el) {
		var src = el.getProperty('src');
		var extension = src.substring(src.lastIndexOf('.', src.length));
		el
                    .addEvent('mouseenter', function() {
			    el.setProperty('src', src
					   .replace(extension, '_hover' + extension));
			});
		el.addEvent('mouseout', function() {
			el.setProperty('src', src);
		    });
	    });
    }
}

/*
 * ValidationHandler
 *
 * We need to add an event to validate all form inputs.
 */
function ValidationHandler() {
    validation = new FormCheck("abundaInput", {
	    submitByAjax : false,
	    onAjaxRequest : AjaxHandler(),
	    display : {
		showErrors : 0,
		fixPngForIe : 1,
		scrollToFirst : false,
		checkValueIfEmpty : 0
	    }
	});
}

/*
 * Modal
 *
 * We need a way to show and hide the Modal form
 * from the calculator page.
 *
 */

function Modal() {
  $("submitList").addEvent("click", function() {
      valid_submit = new FormCheck('submit_valuation', {
	display  : {
	  fixPngForIe : 1
	}
        });
      if($('abundaCalcBody_process').getFirst()) {
	showModal("submitModal");
      } else {
	valid_submit.errors='Please enter a UPC or ISBN to continue.'; // find a way to pop up a message or tooltip.
	valid_submit.manageError($('product_code'), '');
      }
    });
  $("submitCancel").addEvent("click", function() {
      hideModal("submitModal");
    });
/*
  $("uploadList").addEvent("click", function() {
      valid_submit = new FormCheck('uploadForm', {
	display  : {
	  fixPngForIe : 1
	}
        });
	showModal("uploadModal");
	});
  */
  $("bulkCopy").addEvent("click", function() {
      valid_submit = new FormCheck('bulkPasteForm', {
	display  : {
	  fixPngForIe : 1
	}
        });

	showModal("bulkPasteContainer");
	});
	     $("bulkCopyCancel").addEvent("click", function() {
	  hideModal("bulkPasteContainer");
	});
      $("uploadCancel").addEvent("click", function() {
	  hideModal("uploadModal");
	});
    }

    function view_modal() {
      $("submitList").addEvent("click", function() {
	  valid_submit = new FormCheck('submit_valuation', {
            display  : {
	      fixPngForIe : 1
            }
	    });
	  if($('abundaCalcBody_process').getFirst()) {
            showModal("submitModal");
	  } else {
	    valid_submit.errors='Please enter a UPC or ISBN to continue.'; // find a way to pop up a message or tooltip.
	    valid_submit.manageError($('product_code'), '');
	  }
	});

      $("submitCancel").addEvent("click", function() {
	  hideModal("submitModal");
	});
    }

    /*
     * showModal
     *
     * We need a generic function to add the events
     * to show a modal box.
     */
    function showModal(value) {
      var effect = new Fx.Morph(value, {
	duration : 'long',
	transition : Fx.Transitions.Sine.easeOut
	});

      effect.start( {
	  'display' : 'block'
	    });
    }

    /*
     * hideModal
     *
     * We need a generic function to add the events
     * to hide a modal box
     */
    function hideModal(value) {
      var effect = new Fx.Morph(value, {
	duration : 'long',
	transition : Fx.Transitions.Sine.easeOut
	});

      effect.start( {
	  'display' : 'none'
	    });
    }

    /*
     * Sets focus on product_code
     *
     * You can change the element to focus on by passing the element id
     * --This is a string for that represents the element id
     * --Defalut is currently product_code, yes hard coded.
     *
     */
    function SetFocus(value) {
      value = (value == undefined) ? 'product_code' : value;
      if ($(value))
        $(value).focus();
    }

    /*
     * This function will find all duplicate UPC codes
     * and add their quantities to the current request
     * quantity. It will remove them from the table.
     * It will also remove them from the database.
     * 
     */

    function addDuplicatesToQuantity() {
        var upcs = $$('td.upc');
        var quantities = $$('td.quantity');
        var inputUPC = $('product_code').value;
        var inputQuantity = $('product_qty').value;
        var duplicateQuantity = 0;
  
        //Compare the new UPC to each UPC in the existing list
        for ( var idx = 0; idx < upcs.length; idx++) {
            //Check for existing UPCs matching the new UPC
            if (inputUPC == $(upcs[idx]).get('text')) {
                //Add the found UPC's quantity to the new UPC's
                //quantity
                duplicateQuantity += parseInt($(quantities[idx]).get('text'));
                queueForDisposal(upcs[idx].getParent());
            }
        }
        
        //If we found any duplicates, remove them from
        //the table and DB and add them to the current
        //request
        if(duplicateQuantity > 0){
            delete_item(inputUPC);
            $('product_qty').value = (parseInt(inputQuantity) + duplicateQuantity);
            //calcSum();
        }
    }
    
    //Rows waiting to be disposed
    var rowsToDispose = [];
    
    //Queue a row for disposal
    function queueForDisposal(element){
        rowsToDispose.push(element);
    }
    
    //Dispose of all queued rows
    function disposeQueued(){
        Array.forEach( rowsToDispose, function(element) {
            element.dispose();
        });
        rowsToDispose = [];
    }

    /*
     * calcSum
     *
     * Calculates the quantities and formats the grand total.
     *
     */
    function calcSum() {
      var lines = $$('td.line_number');
      var values = $$('td.values');
      var quantity = $$('td.quantity');
      var item = $$('div.item');
      var total_qty = 0;
      var idx;
      for (idx = 0; idx < values.length; idx++) {
        var qty = parseInt($(quantity[idx]).get('text'));
        total_qty += qty;
        $(lines[idx].set('text', idx + 1));
      }

        if (!window.total) {
            window.total = $('grand_total').innerHTML;
            if ($('total_prevaluation'))
                $('total_prevaluation').set('text', window.total);

        } else {
            var grandTotal = window.total;

            $('grand_total').set('text', window.currency_for_total + grandTotal);
            if ($('total_prevaluation'))
                $('total_prevaluation').set('text', window.currency_for_total + grandTotal);

        }


        if ($('total_item_count'))
            $('total_item_count').set('text', total_qty);
        $('item_count').set('text', total_qty);

    }

    /*
     * numberFormat
     *
     * Define number of decimals to format for
     * Define the style of the decimal point
     * Define the style of the seperator
     *syb
     * takes a number (980987.838)
     * turns the number into ($980,987.84)
     *
     */
    function numberFormat(decimals, dec_point, thousands_sep, total) {
      decimals = Math.abs(decimals) + 1 ? decimals : 2;
      dec_point = dec_point || '.';
      thousands_sep = thousands_sep || ',';

      var matches = /(-)?(\d+)(\.\d+)?/.exec(total + ''); // returns matches[1] as
      // sign, matches[2] as
      // numbers and
      // matches[2] as
      // decimals
      var remainder = matches[2].length > 3 ? matches[2].length % 3 : 0;
      return window.currency_for_total + (matches[1] ? matches[1] : '') + (remainder ? matches[2]
						     .substr(0, remainder) + thousands_sep : '') + matches[2]
	.substr(remainder).replace(/(\d{3})(?=\d)/g, "$1" + thousands_sep) + (decimals ? dec_point + (+matches[3] || 0)
									      .toFixed(decimals).substr(2) : '');
    }

/*
 * Delete Item
 *
 * We need a method to delete items from the database.
 */
function delete_item(code) {
    var session = /info_id=(\w+)/.exec(window.location.href);

    var req = new Request( {
	    method : 'get',
	    url : 'process/deleteItem.php',
	    async : false,
	    onRequest : function() {
            },
	    onComplete : function(response) {

                var jsonobj = eval('(' + response + ')');

                window.total = jsonobj.total;
                window.currency_for_total = jsonobj.currency_for_total;

            }
	});

    if (session != null && session.length == 2)
        req.send('product=' + code + '&session_id=' + session[1]);
    else
        req.send('product=' + code);
}

/*
 * Update Item Quantity
 *
 * We need a way to update the quantity in the database
 */
function update_item_qty(qty, product) {
    var req = new Request( {
	    method : 'get',
	    url : 'process/updateItem.php',
	    async : false
	});
      req.send('product=' + product + '&quantity=' + qty);
    }

    function valid_input() {

      var alphanum = new RegExp("^\\w{3,25}$");
      var digit = new RegExp("^[0-9]*$");
      var product = $('product_code').get('value');

      product = product.replace(" ", "");
      product = product.replace("-", "");

      if (alphanum.test(product) == false) {
        return false;
      }

      if (digit.test($('product_qty').get('value')) == false)
        return false;

      var num = $('product_qty').get('value');
      if (parseInt(num) == 0 || isNaN(parseInt(num)))
        return false;

      if (num < 0 && num > 25)
        return false;

      return true;
    }

    /*
     * show_referral_other
     *
     * We need a way to show the other box and field
     * when someone selects a referral option that
     * has a value of 1.
     *
     */
    function show_referral_other() {

      $('txtOther').setStyle('display', 'none');
      $('lblOther').setStyle('display', 'none');

      $("ddlReferrals").addEvent('change', function() {
          $('hvReferral').set('value', $('ddlReferrals').getSelected().get('text'));

	  if ($('ddlReferrals').value == 1) {
	    $('txtOther').setStyle('display', 'block');
	    $('lblOther').setStyle('display', 'block');
	    $('modal').setStyle('height','490px');
	  } else {
	    $('txtOther').setStyle('display', 'none');
	    $('lblOther').setStyle('display', 'none');
	    $('modal').setStyle('height','430px');
	  }

	});

    }

    function get_ie_version(){
      var version = -1;
      if (navigator.appName == 'Microsoft Internet Explorer') {
        var agent = navigator.userAgent;
        var regex = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (regex.exec(agent) != null)
	  version = parseFloat(RegExp.$1);
      }

      return version;

    }

    function valid_browser(){
      var valid = true;
      var version = get_ie_version();
      if (version >= 8.0)
        valid = true;
      else
        valid = false;

      return valid;
    }

function clean_product_code(element) {
    element.value = element.value.replace(/\W+/g, "");
}

function validate_email(form_id,email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])/;
   var address = document.forms[form_id].elements[email].value;
   if(reg.test(address) == false) {
      alert('Valid Email Address Required. ');
      return false
   }
}
function validate_email_name(form_id, name){

   var user  = document.forms[form_id].elements[name].value.length;
    if(user < 2){
     alert('Name must be more than 2 Characters');
     return false;
   }else{
     return true;
   }
}
function countLines(area)  {
   var theLines = area.value.replace((new RegExp(".{"+area.cols+"}","g")),"\n").split("\n");
  if(theLines[theLines.length-1]=="") theLines.length--;
  return theLines.length;
}

function checkBulk(form){
   if(countLines(form.bulkinput)>250){
     var email_test = validate_email_name('bulkPasteForm','bulk_name');
     var name_test = validate_email('bulkPasteForm','bulk_email');
     if(email_test != false && name_test !=false){
        return true;
     }else{
        return false;
     }
   }else{
      return true;
   }
}

function submit_validate(form_id,email){

	validate_email(form_id,email);

	if (document.forms[form_id].elements['ddlReferrals'].value == -1)
	{
		alert('Please let us know how you heard about us.');
		return false;
	}



}