



function setChatVar(list, varName, fieldNames) {
	for (var i = 0, form; form = document.forms[i]; i++) {
		for (var j = 0, fieldName; fieldName = fieldNames[j]; j++) {
			var field = form[fieldName];
			if (field && field.value) list[varName] = field.value;
		}
	}
}

function popUpChatWindow(title, subject, pr, ct, omniture, formType) { // pop up the chat request window
	var leftPos = (screen.width / 2) - 325;
	var topPos = (screen.height / 2) - 265;
	var chatVars = {title: title, subject: subject, p_prods: pr, p_cats: ct, omniture: omniture, type: formType};
	setChatVar(chatVars, 'c_fname', ['firstName', 'opt_details_first']);
	setChatVar(chatVars, 'c_lname', ['lastName', 'opt_details_sur']);
	setChatVar(chatVars, 'c_area', ['lineNumberPrefix']);
	setChatVar(chatVars, 'c_phone', ['lineNumberMain', 'numberForService']);
	setChatVar(chatVars, 'c_email', ['emailAddress', 'opt_details_email', 'email']);
	if (chatVars.c_area) {
		chatVars.c_phone = chatVars.c_area + (chatVars.c_phone || '');
		delete chatVars.c_area;
	}
	
	var args = '';
	for (var varName in chatVars) {
		args += (args.length ? '&' : '') + varName + '=' + encodeURIComponent(chatVars[varName]);
	}
	chatWindow = window.open('/help/webchat/form?' + args, 'chatWindow', 'status=0,toolbar=0,menubar=0,location=0,resizable=1,height=530,width=640,left=' + leftPos + ',top=' + topPos);
}

function gradschatpopup(poptype) {return}



function getFieldValue(name) {
	var field = document.frm_chat_data[name];
	return (field && field.type == "text") ? field.value : null;
}

function submitChatRequest(obj) { // form validation and submit
  var emailFilter = /^.+@.+$/
  var emailIn = getFieldValue('c_email')
  var question = getFieldValue('_icf_227')
  if (getFieldValue('c_fname') == '') {
    document.getElementById('webchat_notification').innerHTML = 'Please fill in your first name.'
    document.frm_chat_data.c_fname.focus()
  } else if (getFieldValue('c_lname') == '') {
    document.getElementById('webchat_notification').innerHTML = 'Please fill in your last name.'
    document.frm_chat_data.c_lname.focus()
  } else if (emailIn != null && !(emailFilter.test(emailIn))) {
    document.getElementById('webchat_notification').innerHTML = 'Please enter a valid email address.'
    document.frm_chat_data.c_email.focus()
  } else if (question == '') {
    document.getElementById('webchat_notification').innerHTML = 'Please enter your question.'
    document.frm_chat_data._icf_227.focus()
  } else if (question && question.length >= 500) {
    document.getElementById('webchat_notification').innerHTML = 'Your question is too long.'
    document.frm_chat_data._icf_227.focus()
  } else if (getFieldValue('c_phone') == '') {
    document.getElementById('webchat_notification').innerHTML = 'Please fill in your phone number.'
    document.frm_chat_data.c_phone.focus()
  } else if (getFieldValue('c_dob') == '') {
    document.getElementById('webchat_notification').innerHTML = 'Please fill in your date of birth.'
    document.frm_chat_data.c_dob.focus()
  } else {
    _gaq.push(['_trackPageview', '/form/webchat/submit'])
    var s = s_gi('telecomnz-prd')
    s.linkTrackVars = 'events'
    s.linkTrackEvents = 'event12'
    s.events = 'event12'
    s.prop1 = 'Submit'
    s.tl(obj, 'o', 'Start Chat')
    
    var chatVars = {first_name: 'c_fname', last_name: 'c_lname', email: 'c_email', prod: 'topic_id', cat: 'subtopic_id',
      _icf_227: '_icf_227', _icf_233: 'c_phone', _icf_236: 'c_dob', _icf_239: 'chat_url' }
    
    var chatUrl = 'http://telecom.custhelp.com/app/chat/chat_landing/'
    for (var varName in chatVars) {
      var fieldName = chatVars[varName]
      var field = document.frm_chat_data[fieldName]
      if (field && field.value) chatUrl += varName + '/' + encodeURIComponent(field.value) + '/'
    }
    document.frm_chat_data.chat_submit.disabled = true
    document.location.href = chatUrl
  }
}

function setupHours(linkId, copyId) {
  var link = document.getElementById(linkId)
  var copy = document.getElementById(copyId)
  link.onclick = function() {
    this.style.display = "none"
    copy.style.display = "block"
    return false
  }
  copy.style.display = "none"
}

