var config = [
	{
		"name": "Sales",
		"serviceid": "83",
		"secondaryissues": [
			{"name": "Internet"},
			{"name": "Phone"},
			{"name": "Cable"},
			{"name": "Advertise on Cable"}
		]
	},
	{
		"name": "Billing",
		"serviceid":"84",
		"secondaryissues": [
			{
				"name": "Questions about my bill",
				"links": [
					"<a target=\"_blank=\" href=\"http://midcocomm.com/resourcecenter/index.cfm/105/Customer-Service--Understanding-Your-Bill/What-are-the-taxes-and-fees-on-my-telephone-bill-\">Phone Taxes on My Bill</a>",
					"<a target=\"_blank=\" href=\"http://midcocomm.com/resourcecenter/index.cfm/20/Customer-Service--Understanding-Your-Bill/Why-does-my-have-more-than-1-months-charges\">Why do I have more than 1 month of charges?</a>"
				]
			},
			{
				"name": "Online Bill payment (EZ-Pay)",
				"links": [
					"<a target=\"_blank=\" href=\"http://www.midcocomm.com/resourcecenter/index.cfm/11/Welcome--New-Charter-Customers/EZ-Pay-Overview\">How to use EZ-Pay</a>",
					"<a  target=\"_blank=\" href=\"http://www.midcocomm.com/resourcecenter/index.cfm/14/Customer-Service--Understanding-Your-Bill/EZ-Pay-FAQs\">EZ-Pay FAQ's</a>"
				]
			}
		]
	},
	{
		"name": "Contract Information",
		"serviceid":"84"
	},
	{
		"name": "Email Support",
		"serviceid":"84",
		"secondaryissues": [
			{
				"name": "Add/Delete Email Accounts" ,
				"note": "Please provide any available details about your operating system, the current program you've experienced issues with and any error messages you may be receiving"
			},
			{ 
				"name": "Cannot send or Receive",
				"note": "Please provide any available details about your operating system, the current program you've experienced issues with and any error messages you may be receiving"
			},
			{ 
				"name": "Settings and Configurations",
				"note": "Please provide any available details about your operating system, the current program you've experienced issues with and any error messages you may be receiving"
			},
			{ 
				"name": "Spam/Virus Concerns",
				"note": "Please provide any available details about your operating system, the current program you've experienced issues with and any error messages you may be receiving"
			},
			{ 
				"name": "Webmail Problems",
				"note": "Please provide any available details about your operating system, the current program you've experienced issues with and any error messages you may be receiving"
			}
		]
	},
	{
		"name": "Internet Support",
		"serviceid":"84",
		"secondaryissues": [
			{
				"name": "Service Issues",
				"note": "Please provide any available details about your operating system, the current program you've experienced issues with and any error messages you may be receiving"
			},
			{
				"name": "IP Addresses",
				"note": "Please provide any available details about your operating system, the current program you've experienced issues with and any error messages you may be receiving"
			},
			{
				"name": "Web Hosting",
				"note": "Please provide any available details about your operating system, the current program you've experienced issues with and any error messages you may be receiving"
			}
		]
	},
	{
		"name": "Phone Support",
		"serviceid":"84",
		"secondaryissues": [
			{ "name": "Service Issues" },
			{ "name": "Phone Feature Usage" },
			{ "name": "Long Distance" },
			{ "name": "Directory" }
		]
	},
	{
		"name": "General",
		"serviceid":"83",
		"secondaryissues": [
			{ "name": "Company Information" },
			{ "name": "General Comment/Question" },
			{ "name": "Vendor Inquiries" },
			{ "name": "Website Comments" }
		]
	}
];

var post_path = 'http://24.220.76.25/CCPROChat/CPChatMain.jsp?ChatSrvr=24.220.76.25&AllowPush=1&Orientation=-2147483647&SeperateWindow=-2147483647&Port=194&PrimaryIP=24.220.76.25&PrimaryPort=195&SecondaryIP=&SecondaryPort=';

function init() {
	document.getElementById('primaryissue').onchange = primaryChanged;
	document.getElementById('secondaryissue').onchange = secondaryChanged;
	document.getElementById('contactform').onsubmit = formsubmitted;
}

init();

function primaryChanged() {
	var me = document.getElementById('primaryissue');
	
	// Clear the "secondary issue" select.
	var subissueSelect = document.getElementById('secondaryissue');
	var options = subissueSelect.getElementsByTagName('option');
	for(var i = options.length - 1; i > 0; i--)
		subissueSelect.removeChild(options[i]);
	
	// Get the main issue.
	var mainIssue = findInArray(me.value, config, 'name');
	if(mainIssue == null) {
		// Not found?  Hide secondary issue, exit.
		document.getElementById('secondary').style.display = 'none';
		showHelp(null);
		return true;
	}
	
	if(mainIssue.secondaryissues && mainIssue.secondaryissues.length > 0) {
		// If there are secondary issues...
		document.getElementById('secondary').style.display = 'block';
		
		for(var x=0; x<mainIssue.secondaryissues.length; x++) {
			var option = subissueSelect.appendChild(document.createElement('option'));

			option.value = mainIssue.secondaryissues[x].name;
			option.appendChild(document.createTextNode(mainIssue.secondaryissues[x].name));
		}
	} else {
		// No secondary issues, hide secondary issues row.
		document.getElementById('secondary').style.display = 'none';
	}
	
	showHelp(mainIssue);
	
	return true;
}

function secondaryChanged() {
	var me = document.getElementById('secondaryissue');
	
	var mainIssue = findInArray(
		document.getElementById('primaryissue').value, 
		config,
		'name'
	);
	
	if(mainIssue == null)
		return;
	
	var secondaryIssue = findInArray(
		me.value, 
		mainIssue.secondaryissues,
		'name'
	);
	
	if(secondaryIssue == null)
		return true;
	
	if(!mainIssue.links || mainIssue.links.length == 0)
	showHelp(secondaryIssue);
	
	return true;
}



function showHelp(issue) {
	var hideHelp = true;
	
	
	document.getElementById('help').style.display = 'none';
	document.getElementById('helplinks').innerHTML = '';
	document.getElementById('note').innerHTML = '';
	
	if(issue && issue.links && issue.links.length > 0) {
		var html = "";
		for(var x = 0; x < issue.links.length; x++) {
			html += '<div class="link">' + issue.links[x] + '</div>';
		}
		document.getElementById('helplinks').innerHTML = html;
		document.getElementById('help').style.display = 'block';
	}
	
	if(issue && issue.note) {	
		document.getElementById('note').innerHTML = issue.note;
		document.getElementById('help').style.display = 'block';
	}
}




function findInArray(key, arr, searchKey) {
	var x = arr.length - 1;
	while (x >= 0) {
		if(arr[x][searchKey] == key)
			return arr[x];
		x--;
	}
	return null;
}


function formsubmitted() {
	// Validate form.
	var company = document.getElementById('company');
	var fname = document.getElementById('firstname');
	var lname = document.getElementById('lastname');
	var email = document.getElementById('email');
	var phone = document.getElementById('phone');
	var address = document.getElementById('address');
	var city = document.getElementById('city');
	var state = document.getElementById('state');
	var zip = document.getElementById('zip');
	var account = document.getElementById('acctnum');
	var primaryissue = document.getElementById('primaryissue');
	var secondaryissue = document.getElementById('secondaryissue');
	// Contact method removed from form.
	// var method = document.getElementById('contactmethod');
	var comments = document.getElementById('comments');
	
	company.className = (company.value == '') ? "error" : "ok";
	fname.className = (fname.value == '') ? "error" : "ok";
	lname.className = (lname.value == '') ? "error" : "ok";
	email.className = (email.value == '') ? "error" : "ok";
	address.className = (address.value == '') ? "error" : "ok";
	city.className = (city.value == '') ? "error" : "ok";
	state.className = (state.value == '') ? "error" : "ok";
	// zip.className = (zip.value == '') ? "error" : "ok";
	primaryissue.className = (primaryissue.value == 'Did not select') ? "error" : "ok";
	if(primaryissue.className == 'ok') {
		var mainIssue = findInArray(primaryissue.value, config, 'name');
		if(mainIssue.secondaryissues) {
			secondaryissue.className = (secondaryissue.value == 'Did not select') ? "error" : "ok";
		} else {
			secondaryissue.className = 'ok';
		}
	}
	
	// Phone is only required when it exsits, and users
	// have "phone" selected as the contact method.
	/* - Contact method removed from form
	phone.className = 'ok';
	if(method != null) {
		if(method.value == 'Phone') {
			phone.className = (phone.value == '') ? "error" : "ok";
		}
	}
	*/
	
	var wasError = company.className == 'error' ||
		fname.className == 'error' ||
		lname.className == 'error' ||
		email.className == 'error' ||
		phone.className == 'error' ||
		address.className == 'error' ||
		city.className == 'error' ||
		state.className == 'error' ||
		zip.className == 'error' ||
		account.className == 'error' ||
		primaryissue.className == 'error' ||
		secondaryissue.className == 'error';
	
	if (wasError) {
		alert("We're sorry, we could not process this request.  Please fill in the all the required fields (marked with asterisks).");
		return false;
	}
	
	// If this is a chat launch
	if(comments == null) {
		var mainIssue = findInArray(primaryissue.value, config, 'name');
		if(mainIssue == null)
			mainIssue = config[config.length - 1];
		var serviceID = mainIssue.serviceid;
		
		
		var custname = fname.value + ' ' + lname.value;
		
		/*
		var url = post_path +
			'&CustName=' + escape(custname) +
			"&ServiceID=" + escape(serviceID) +
			'&firstname=' + escape(fname.value) +
			'&lastname=' + escape(lname.value) +
			'&email=' + escape(email.value) +
			'&phone=' + escape(phone.value) +
			'&address=' + escape(address.value) +
			'&city=' + escape(city.value) +
			'&state=' + escape(state.value) +
			'&zip=' + escape(zip.value) +
			'&account=' + escape(account.value) +
			'&primary=' + escape(primaryissue.value) +
			'&secondary=' + escape(secondaryissue.value);
		*/
		
		var url = post_path +
			'&CustName=' + escape(custname) +
			'&ServiceID=' + mainIssue.serviceid +
			'&UserDefined1=' + escape(primaryissue.value) +
			'&UserDefined2=' + escape(secondaryissue.value) +
			'&UserDefined3=' + escape(custname) +
			'&UserDefined4=' + '' + // Company
			'&UserDefined5=' + escape(address.value) +
			'&UserDefined6=' + escape(city.value) +
			'&UserDefined7=' + escape(state.value) +
			'&UserDefined8=' + escape(zip.value) +
			'&UserDefined9=' + escape(phone.value) +
			'&UserDefined10=' + escape(account.value);
		
		//Open the window
		var ChatWindow = window.open('', 'ChatWindow', 'width=500,height=700');
		try {
			if (ChatWindow.location.href == 'about:blank') {
				ChatWindow.location.href = url;
			}
		}
		catch(err) {}
		ChatWindow.focus()
		return false;
	}
	
	return true;
}
