

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}


var newtopicKey;

// called from www2b 'new discussion' page in iframe
function sayHello(theKey) {
	newtopicKey = theKey;
	document.getElementById("keyField").value = newtopicKey;
}

// talk to www2b iframe
document.domain = "abc.net.au";

function changeAction() {
	// avoids non-js form submission 
	if (document.m_frmMainForm) {
		document.m_frmMainForm.action = 'http://www2b.abc.net.au/tmb/View/NewMessage.aspx?b=253&amp;tn=questions&amp;dm=1&amp;tpa=&amp;r=&amp;layout=popup';
		}
}



addEvent(window, 'load', changeAction);


function formCheck(zeForm) {

	// pass key to form
	document.getElementById("keyField").value = newtopicKey;

	// check the form doesn't say "thanks"
	if ((zeForm.remark.value.indexOf("Thanks. Your question has been submitted.")>-1)||(zeForm.remark.value=="Your question has already been submitted.")) {
		zeForm.remark.value="Your question has already been submitted.";
		return false;
	} 

	// basic validation
	if (document.getElementById("yname").value.length < 1) {
		alert("Please enter your name...");
		document.getElementById("yname").style.background='#FFFDC8';
		return false;
	}	

	if (document.getElementById("yemailpage").value.length < 6) {
		alert("Please enter a valid email address.");
		document.getElementById("yemailpage").style.background='#FFFDC8';
		return false;
	}
	
	if (zeForm.score.options[0].selected && zeForm.score.options[0].value=="na") {
		alert("Please select your state or territory");
		document.getElementById("state").style.background='#FFFDC8';	
		return false;
	}	
	
	// check for http
	spamcheck=zeForm.m_tbxText.value;
	if (spamcheck.indexOf("http")!=-1) {
		document.m_frmMainForm.action = 'http://www.abc.net.au/tv/qanda/comment_blankb.htm';
	}	
	
	// check word count
	wct = cnt(zeForm.remark.value);
	if (wct > 80) {
		alert("Questions are limited to 80 words. You've written "+wct+" words!");
		document.getElementById("remark").style.background='#FFFDC8';
		return false;
	}
	
	
	// replace chars
	badChars = Array  ('…', '—', '–', '“', '”', 'ö', 'é', 'è', 'ñ', 'â', ' ', '’','‘','´');
	goodChars = Array ('...', '-', '-', '"', '"', 'o', 'e', 'e', 'n', 'a', '-', '\'','\'','\'');
	fld = document.getElementById("remark");
	for(i=0;i<badChars.length;i++){
		restring = new RegExp( badChars[i] , 'g');
		fld.value = fld.value.replace(restring,goodChars[i]);
	}
	fldn = document.getElementById("yname");
	for(j=0;j<badChars.length;j++){
		restringj = new RegExp( badChars[j] , 'g');
		fldn.value = fldn.value.replace(restringj,goodChars[j]);
	}	
	
	// remove spaces from phone number field
	var phoneField = document.getElementById("ytown").value;
	document.getElementById("ytown").value=removeSpaces(phoneField);

	
	// move email, phone and state to hidden email field
	document.getElementById("yemail").value+=document.getElementById("yemailpage").value+"_"+document.getElementById("ytown").value+"_"+zeForm.score.options[zeForm.score.selectedIndex].value;
	
	
	// display thankyou layer
	document.getElementById("favFormThx").style.display='block';
	//document.getElementById("favForm").style.display='none';
	//document.getElementById("wordCount").style.display='none';	
	
	// replace remark text, clear other fields
	var sto=setTimeout("cTime()",500);

	// submit form
	return true;
	
}

// remove spaces 
function removeSpaces(string) {
 return string.split(' ').join('');
}



function cTime() {
	document.getElementById("remark").value="Thanks. Your question has been submitted. \n\nPlease note Q&A receives hundreds of questions every week. All questions are read and most are published on this site. Some questions are selected and read out on air. Due to the large quantity, we cannot use all submissions. We appreciate your contribution, please keep them coming.";
	document.getElementById("yemailpage").value='';
	document.getElementById("yemail").value='';
	document.getElementById("yname").value='';
	document.getElementById("ytown").value='';
	// reset iframe loaction (to get new key)
	window.frames['2bframe'].location='http://www2b.abc.net.au/tmb/View/NewMessage.aspx?b=253&tn=questions&dm=1&tpa=&r=&layout=popup';
	
}

// word count function
function cnt(theform){
	var y=theform;
	var r = 0;
	a=y.replace('\n',' ');
	a=a.split(' ');
	for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
	return r;
}

function remainingWords(textAreaContent,textAreaContentValue, maxWords) 
{
	// Determine the number of words we have left
	var numberofwords = cnt(textAreaContentValue);
	wordsLeft = maxWords - numberofwords;
	if (wordsLeft < 1)
	{	// if 80 change color to red
		document.getElementById("wcNum").style.color='red'; 
		document.getElementById("wcNum").innerHTML='0';
	} else {
		document.getElementById("wcNum").style.color='white'; 
	}
	// update word count number
	if (wordsLeft > 0){
		document.getElementById("wcNum").innerHTML=80-(0+numberofwords);
	}
}

function launchFeed(){
	privacy_window = window.open('feed.htm', 'QA', 'height=400, width=450, toolbar=0, location=0, scrollbars=0, resizable=1');
}

