# Oracle stuff
#
#require 'oci8'
#used to handle javascript popups
require 'watir/contrib/enabled_popup'
require 'watir/WindowHelper'
#
# Method to handle javascript popups.
#
require 'thread'
#
require 'win32ole'
#
def startClicker( button , waitTime = 3)
  w = WinClicker.new
  longName = @@ie.dir.gsub("/" , "\\" )
  shortName = w.getShortFileName(longName)
  c = "start ruby #{shortName}\\watir\\clickJSDialog.rb #{button} #{waitTime} "   
  puts "Starting #{c}"
  w.winsystem(c)   
  w=nil
end
#
#
# DOSQL
#  
#def dosql(p_sql)
 # conn = OCI8.new(DBUSERNAME,DBPASSWORD,DATABASE)
  ##conn.exec('begin DELETE_TEST_DATA; end;')
  #puts p_sql
  #conn.exec(p_sql)
  #conn.exec('commit;')
#end

#
#LOGIN
#
def login(p_username, p_password)
    # 
    # Enter logon details
    #
    if @@ie.locate_tagged_element('fieldset', :id, 'P101_TC_ACCEPTED') then   
    #if @@ie.text_field( :id,'P101_TC_ACCEPTED').exists? then
      @@ie.radio( :id,'P101_TC_ACCEPTED_0').set #'I accept the Terms and Conditions of using this site'
    end
    @@ie.text_field( :id, 'P101_USERNAME' ).set( p_username )
    @@ie.text_field( :id, 'P101_PASSWORD' ).set( p_password )
    #
    @@ie.button(:id, "P101_LOGIN").click
    @@ie.wait
    #
    # TRUE if doesn't find
    #
    assert_nil(@@ie.contains_text('Invalid Login Credentials'),'Unable to login')
end

def menu (p_menuitem)
@@ie.link(:name, p_menuitem).click
@@ie.wait
end

def geterrormess(ie)
  if ie.div(:id, 'ErrorOutputDiv').exists? then
    return ie.div(:id, 'ErrorOutputDiv').text
  end
  return ie.div(:id,'colRight').to_s
end


#
# SETCONTRACTVALUE
#
def set_contract_value(p_index_start,p_values)
  #
  i = p_index_start
  @@ie.text_field( :index, i).set(p_values[0] )
  @@ie.text_field(:index, i+1).set(p_values[1])
  @@ie.text_field(:index, i+2).set(p_values[2])
  @@ie.text_field(:index, i+3).set(p_values[3])
  @@ie.text_field(:index, i+4).set(p_values[4])
  @@ie.text_field(:index, i+5).set(p_values[5])
  @@ie.text_field(:index, i+6).set(p_values[6])
  @@ie.text_field(:index, i+7).set(p_values[7])
  @@ie.text_field(:index, i+8).set(p_values[8])
  @@ie.text_field(:index, i+9).set(p_values[9])
  @@ie.text_field(:index, i+10).set(p_values[10])
  @@ie.text_field(:index, i+11).set(p_values[11])
  #@@ie.text_field(:index, i+12).set(p_values[12])
  #
end
#
# SETCONTRACT
#
def set_contract(p_name,p_customer,p_template,p_format,p_verify_data_entry,p_date_from=futuredate(0),p_date_to=futuredate(100),p_expect_success='y')
  @@ie.button(:value, 'Find').click
  @@ie.wait
  @@ie.button(:value, 'New Contract').click
  @@ie.wait
  #
  @@ie.text_field(:name,'p_contract_number').set(p_name)
  @@ie.select_list(:name, 'p_customer_id').select(p_customer)
  #
  #
  #get javascript popup, start thread before as popup locks ruby so can't click the button!
  #
  startClicker("OK" , 1)
  #sleep 1
  @@ie.select_list(:name,'p_template_id').select(p_template)
  #
  # Will need to click the cutton manually if can't find a way to interact with the Javascript box
  #
  @@ie.select_list(:name,'p_spte_id').select(p_format)
  @@ie.text_field(:name,'p_date_from').set(p_date_from)
  @@ie.text_field(:name,'p_date_to').set(p_date_to)
  @@ie.text_field(:name,'p_ops_contact').set('John')
  @@ie.text_field(:name,'p_bus_contact').set('Paul')
  @@ie.text_field(:name,'p_val_window').set('0')
  @@ie.text_field(:name,'p_days_before').set('14')
  @@ie.select_list(:name,'p_val_action').select('Look Back')
  @@ie.select_list(:name,'p_status').select('Open')
  @@ie.select_list(:name,'p_val_exception').select('Contracted Amount')
  #
  @@ie.select_list(:name,'add_nepo_id').select('EDEN HAG')
  @@ie.select_list(:name,'add_nepo_id').select('EDEN TSTV')
  @@ie.button(:value,'Add Network Points').click
  @@ie.wait
  @@ie.button('Insert').click
  #
  if p_expect_success=='y'
    assert(@@ie.contains_text('Success!'),'Create Contract '+p_name+' did not succeed: ' +geterrormess(@@ie)) 
  else
    assert(@@ie.contains_text('Insert failed'),'Create Contract '+p_name+' succeeded, should have given us an overlap error')
    assert(@@ie.contains_text('Open contracts exist for this date period'),'Expected error message: [Open contracts exist for this date period] but did not find it')
    #if we are testing it to fail there is no need to continue through rest of the test below
    return
  end  
  #
  # Create the contract rule values
  #
  @@ie.button('Set Values').click
  #
  #
  if p_verify_data_entry == 'Y' 
    #
    # Format of the numbers must be 99999.99
    #
    set_contract_value(3,['17,123.30','35,616.46','34,246.60','0.00','17123.3','0.00','0.00','17123.3','0.00','0.00','17123.3','0.00'])
    set_contract_value(15,['68493.20','142,465.86','136,986.40','0.00','68,493.20','0.00','0.00','68,493.20','0.00','0.00','68,493.20','0.00'])
    @@ie.button('Update').click
    @@ie.wait
    #
    assert(@@ie.contains_text('Data supplied with invalid number formats'),'Create Contract Values did not fail as expected'+geterrormess(@@ie))
    #
    # Contracted value is > maximum
    #
    set_contract_value(3,['17123.30','34246.60','35616.46','0.00','17123.3','0.00','0.00','17123.3','0.00','0.00','17123.3','0.00'])
    set_contract_value(15,['68493.20','136986.40','142465.86','0.00','68493.20','0.00','0.00','68493.20','0.00','0.00','68493.20','0.00'])
    @@ie.button('Update').click
    @@ie.wait
    #
    assert(@@ie.contains_text('Contracted greater than Maximum'),'Create Contract Values did not fail as expected'+geterrormess(@@ie))
    #
    #
    # Contracted value is < minimum
    #
    set_contract_value(3,['17123.30','35616.46','246.60','0.00','17123.3','0.00','0.00','17123.3','0.00','0.00','17123.3','0.00'])
    set_contract_value(15,['68493.20','142465.86','6986.40','0.00','68493.20','0.00','0.00','68493.20','0.00','0.00','68493.20','0.00'])
    @@ie.button('Update').click
    @@ie.wait
    #
    assert(@@ie.contains_text('Contracted less than Minimum'),'Create Contract Values did not fail as expected'+geterrormess(@@ie))
    #
  end
  #
  set_contract_value(3,['17123.30','35616.46','34246.60','0.00','17123.3','0.00','0.00','17123.3','0.00','0.00','17123.3','0.00'])
  set_contract_value(15,['68493.20','142465.86','136986.40','0.00','68493.20','0.00','0.00','68493.20','0.00','0.00','68493.20','0.00'])
  @@ie.button('Update').click
  @@ie.wait
  #
  assert(@@ie.contains_text('Success!'),'Create Contract Values for '+p_name+' did not succeed: ' +geterrormess(@@ie))
  #
end
#
# SETRULE
#
def set_rule(p_name,p_errmsg,p_LHS,p_RHS,p_operator)
  #
  @@ie.button(:value, 'New Rule').click
  @@ie.wait   
  #
  @@ie.text_field(:name,'p_rule_name').set(p_name)
  @@ie.text_field(:name,'p_err_message_hu').set(p_errmsg)
  @@ie.text_field(:name,'p_err_message_en').set(p_errmsg)
  #
  @@ie.text_field(:name,'p_left_side').set('$<'+p_LHS+'>')
  @@ie.text_field(:name,'p_right_side').set('$<'+p_RHS+'>')
  #
  @@ie.select_list(:name, 'p_rule_relation').select(p_operator)
  #
  @@ie.button( :value, 'Insert' ).click
  @@ie.wait   
  #
  assert(@@ie.contains_text('Success!'),'Create Rule '+p_name+' did not succeed: ' +geterrormess(@@ie))
  #
  @@ie.button( :value, 'Cancel' ).click
  @@ie.wait   
  #
end
#
# SETCATEGORIES
#
def set_categories(p_name,p_display_seq)
  #
  @@ie.button(:value,'New').click
  @@ie.wait
  #
  @@ie.text_field(:name,'P_CODE').set(p_name)
  @@ie.text_field(:name,'P_NAME').set(p_name)
  @@ie.text_field(:name,'P_UNITS').set('m3/day')
  @@ie.text_field(:name,'P_NUMBER_FORMAT').set('999999.99')
  @@ie.text_field(:name,'P_DISPLAY_SEQUENCE').set(p_display_seq)
  #
  @@ie.button( :value, 'Insert' ).click
  @@ie.wait
  #c
end
#
# SETNETWORKPOINT
#
def set_network_point(p_name)
  #
  @@ie.button(:value,'New').click
  @@ie.wait
  #
  @@ie.text_field( :name, 'P_CODE').set( p_name )
  @@ie.text_field( :name, 'P_NAME').set( p_name )
  @@ie.select_list( :name , "P_STATUS").select("Active")
  @@ie.select_list( :name , "P_SITE_TYPE").select("Entry")
  #
  @@ie.button( :value, 'Insert' ).click
  @@ie.wait
  #
end ;
#
# SETNOMINATION
#
#This method simply takes the date/time of the nomination , the nominated value and
#number of subsequent days and submits it into the current web form.  It assumes you have
#already loaded the web form in the @@ie class object variable
#Method designed for use iwth the EDEN1 contract (2 NP plus 4 categories)
#
def setnomination(p_contract,  p_quantity)
  #
  @@ie.select_list( :name , "p_contract").select(p_contract)
  @@ie.wait 
  # 
  #@@ie.text_field( :name, 'nomid' ).set( 'PUW_EDEN1_'+(Time.now).strftime("%d%m%y%H%M%S") )
  #
  i = 4
  #
  # Passing in an array of arrays the loop though, use the index i to deermine the text field for data entry.
  #
  p_quantity.each do |nomination|
    #
    @@ie.text_field( :index, i).set( nomination[0] )
    @@ie.text_field(:index, i+1).set(nomination[1])
    @@ie.text_field(:index, i+2).set(nomination[2])
    @@ie.text_field(:index, i+3).set(nomination[3])
    @@ie.text_field(:index, i+4).set(nomination[4])
    @@ie.text_field(:index, i+5).set(nomination[5])
    @@ie.text_field(:index, i+6).set(nomination[6])
    @@ie.text_field(:index, i+7).set(nomination[7])
    @@ie.text_field(:index, i+8).set(nomination[8])
    #
    i = i  + 8
    #
  end
  #
  @@ie.button( :value, 'Insert' ).click
  @@ie.wait
  #
end
#
# SETMULTINOM
#
#This method simply takes the date/time of the nimination , the nominated value and
#number of subsequent days and submits it into the current web form.  It assumes you have
#already loaded the web form in the @@ie class object variable
#Method designed for use iwth the EDEN1 contract (2 NP plus 4 categories)
#
def setmultinom(p_contract,  p_quantity)
  #
  @@ie.select_list( :name , "p_contract").select(p_contract)
  @@ie.wait 
  # 
  #@@ie.text_field( :name, 'nomid' ).set( 'PUW_EDEN2_'+(Time.now).strftime("%d%m%y%H%M%S") )
  #
  i = 5
  #
  # Passing in an array of arrays the loop though, use the index i to deermine the text field for data entry.
  #
  p_quantity.each do |nomination|
    #
    @@ie.text_field( :index, i).set( nomination[0] )
    @@ie.text_field(:index, i+1).set(nomination[1])
    @@ie.text_field(:index, i+2).set(nomination[2])
    @@ie.text_field(:index, i+3).set(nomination[3])
    @@ie.text_field(:index, i+4).set(nomination[4])
    i = i  + 5
    #
  end
  #
  @@ie.button( :value, 'Insert' ).click
  @@ie.wait
  #
end
#
# GETERRORMESS
#
#The geterrormess method extracts the error message from
#a designer built screen
def geterrormess (ie)
  #get the html for the current page
  html = ie.html 
  #Find the start of the error
  i= html.index('Error!')
  if i == nil 
  return ''
  end
  #get the html code from the error onwards
  htmlex = html.slice(i,html.length)
  #get the end of the error message
  x= htmlex.index('</B>')
  #extract just the error message (skip 'Error!</I></font><br>')
  return errcode = htmlex.slice(21,x-21)
end
#
# STARTDATE
#
#set up the startdate method (you could call it a function if you're not into Ruby)
#create time (year, month, day, hour, minutes, seconds)
#Hello testers be sure to set the start date for the test here
#Note that I've now set this in the controlling program so it's no longer
#required here
#STARTDATE = Time.gm(2007,'feb',13)

def futuredate (days=0)
#The startdate method takes a number of days + or - and then takes or
#adds them to the startdate specified in the local variable startdate and returns 
#the date in the format dd-mmm-yyyy

#add a number of days to the time and return it in the format dd/mm/yyyy eg. 10/02/2007
return ((Time.now)+(86400*days)).strftime("%d/%m/%Y").upcase
end
#
#
#
def purge_EDEN2_noms
  dosql('begin DELETE nom_net_point_cat_vals WHERE nomi_id IN (SELECT nomi_id FROM nominations WHERE cont_id = (SELECT cont_id FROM contracts WHERE contract_number = \'EDEN2\')); commit; end;')
  dosql('begin DELETE nominations WHERE cont_id = (SELECT cont_id FROM contracts WHERE contract_number = \'EDEN2\'); commit; end;')
end