/**
 *
 *  Javascript for LNG indices Input
 *
 * 	Author: Gunnar Skogen
 *	Copyright: Imarex ASA
 *
 */
// Using CalendarPopup.js :

var cal = new CalendarPopup();
 
 
 
 // Need these to handle input error + TAB
 var currentControl = null;
 var inputError = false;
 
 /**
  * 	Validates that the input is Numeric and with two decimal places
  *     and that the spread is within the limit
  */
 function validateInput(element) {
 
 	currentControl = element;
 	
 	if (! valNumeric(element)) {
		submitEnable(false);
        //element.focus();
        //element.select();
        inputError = true;
		return false;
 	}
 
 	// Spread
 	if ( ! enforceBidOfferSpread(element)) {
		submitEnable(false);
        //element.focus();
        //element.select();
        inputError = true;
 		return false;
 	}
 	 	
    inputError = false;
	submitEnable(true); 	
 	
 }



function validDate(element)  {
	//alert("validDate(" + element.value);
	valArr = element.value.split('/'); 
	//alert("validDate(" + valArr.length);
	if (valArr.length == 3 && valArr[2].length == 4) {
		element.value = valArr[2] + "-" + valArr[1] + "-" + valArr[0];
	}
	
}

 function allreadySubmitted(element) {
 	alert("A price for today has already been submitted here");
 }
 
 function submitEnable(enable) {
        var submitButton = document.getElementById('SUBMIT');
        submitButton.disabled =  ! enable; 	
 }
 
 function doFocusHere(element) {
 	if (! inputError) {
 		currentControl = element;
 	}
 	else if (currentControl != null) {
 		currentControl.focus();
 		currentControl.select();
 	}
 	
 }
 /**
  * 	Max allowed spread between a Bid and Offer price is $0 >= x <=$1
  * 	Rules:
  * 	1. When user enters either Bid or Offer, the corresponding value 
  *	   	   is given the same value provided it does not have a value
  *  	2. If both Bid and Offer has been given a value, the spread between 
  *	       these two values is $0 >= x <=$1
  *
  *     INVAR: value has been validated as numeric
  */
 function enforceBidOfferSpread(element) {
	correspondingElement = findCorrespondingValue(element); 	
	if (correspondingElement.value.length == 0) {
		correspondingElement.value = element.value;
		return true;
	}
	else if (Math.abs(element.value - correspondingElement.value) > 1) {
		if (confirm("Maximum allowed spread between BID and OFFER is $1. Click OK to adjust corresponding value to this new value")) {
			correspondingElement.value = element.value;
			return true;
		} else {		
        	return false;		
        }
	}	
	return true;
 }

 /**
  *
  */
  function findCorrespondingValue(element) {
  
  	var idArr = element.id.split('_');  // BID/OFFER_<Terminal id>_<30/60>
  	bidOffer = idArr[0] == "BID" ? "OFFER" : "BID";
  	
  	return document.getElementById((idArr[0] == "BID" ? "OFFER" : "BID") + '_' + idArr[1] + '_' + idArr[2] );
  	
  }
	
 
  /**
   * validation of input
   * only numeric input
   * remove blanksand replace comma with punctation and only at last
   *
   *    @param element                   the ID of the html object
   */
  function valNumeric(element)  {
  	if (! element ) {
  		return true;
  	}
    if (element.value.length === 0 ) {
        return true;
    }    
    
    if (element.tagName == "SELECT") {
		//prompt("valNumeric(" + element.name + ")", element.value);
    	return true; // not changeable
    }
    var i;
    var ok = true;

    var re = /,/g;
    var decimalSignOk = ".";
    element.value = element.value.replace(re, decimalSignOk);

    // To keep :
    var lastDecimalSign = element.value.lastIndexOf(".");
    // char by char :
    var newValue = "";
    for ( i = 0; i < element.value.length; i++ )  {
        if ((element.value.substr(i, 1) == '.') && (i < lastDecimalSign) ) {
            continue;
        }    
        if (element.value.substr(i, 1) == ' ')  {
            continue;
        }    
        if ( isNaN(parseInt(element.value.substr(i, 1))) && element.value.substr(i, 1) != '.' ) {
            ok = false;
        }
        newValue = newValue + element.value.substr(i, 1);
    }
    if (newValue.length > 14) {
        alert("To many digits in this value..");
        return false;
    }
    if ( ! ok ) {
        alert("Value here must be a number");
        return false;
    }
	element.value = round2dec(newValue);
    return true;
  }
 
 
 /**
  * Retrieves eventual prices for user on that given date 
  */
 function getMemberPrices(element) {
 	//alert("getMemberPrices: " + element.value );
 	
 	var getUsersPricesOnly = document.getElementById('GetUsersPricesOnly');
 	getUsersPricesOnly.value = "true";
	
	var submitButton = document.getElementById('SUBMIT');
	//alert(submit.value);
	submitButton.disabled = false;	
	submitButton.click();
	
 }