//------------------------------------------------------------------------------------------
//advanced controls for adding competition questions

function competitionTools(){
	if(document.getElementById('competitionControls') && document.getElementById('columna')){
		var toolbar=document.getElementById('competitionControls');
		toolbar.innerHTML='<div class="competionAddButton"><a onclick="javascript:void(competitionAddQuestion())">add question</a></div>';		
		var total=0;
		var formArea=document.getElementById('competitionAdd');
		var val='';
		var html='<div id="competitionAddInner">'
		while(document.getElementById('compquestion'+total)){
			val=document.getElementById('compquestion'+total).innerHTML;
			html+='<div id="quesinput'+total+'"><a onclick="compQuestionRemove(\'quesinput'+total+'\')">x</a><input onkeyup="competitionCreateForm()" type="text"  value="'+val+'" /></div>';
			total++;
		}
		html+="</div>";
		formArea.innerHTML=html;
		var competitionArea=document.getElementById('columnd');
		competitionArea.setAttribute('contentEditable',false);
		document.getElementById('compQuestions').style.display="none";
	}
}

function competitionAddQuestion(){
	var formArea=document.getElementById('competitionAddInner');
	var inputs=formArea.getElementsByTagName('INPUT');
	var total=inputs.length+1;
	formArea.innerHTML+='<div id="quesinput'+total+'"><a onclick="compQuestionRemove(\'quesinput'+total+'\')">x</a><input onkeyup="competitionCreateForm()" type="text" value="'+total+'. your question here" /></div>';
	/*
	
	var formArea=document.getElementById('columna');
	//count inputs
	var inputs=formArea.getElementsByTagName('INPUT');
	var total=inputs.length+1;	
	formArea.innerHTML+='<div>'+total+'. your question here</div><input class="fullWidth" name="answer'+total+'" />';
	*/
}

function compQuestionRemove(id){
	//alert(id);return;
	document.getElementById(id).outerHTML='';
	competitionCreateForm();
}

function competitionCreateForm(){
	var formArea=document.getElementById('competitionAdd');
	var competitionArea=document.getElementById('columng');
	var inputs=formArea.getElementsByTagName('INPUT');
	var html='';
	for(var i=0;i<inputs.length;i++){
		html+='<div><label id="compquestion'+i+'">'+inputs[i].value+'</label><input class="fullWidth" name="answer'+i+'" /></div>';		
	}
	competitionArea.innerHTML=html;
}

//apply for competition
function competitionValidateAndSend(){
	
	//check user info form - the question answers are optional
	var form=document.getElementById('competform'); 	
	var field='';
	var errorcount=0;
	var question='';
	var formFields=form.getElementsByTagName('INPUT');
	for(var i=0;i<formFields.length;i++){
		field=formFields[i];
		if(field.value.length<1 && field.name!="telephone" && field.name!="country"){
			field.style.border="1px solid red";
			errorcount++;
		}else{
			field.style.border="1px solid #cccccc";
		}
	}	
	if(errorcount>0){
		alert('You must complete the form fields marked in red. \nPlease complete these and try again.');	
	}else{
		//submit all form data
		formFields=form.elements;
		var savingData='';
		for(var i=0;i<formFields.length;i++){
			field=formFields[i];
			if(field.name){
				savingData+=field.name+"="+field.value+"&";
			}
		}
		//add q & a's
		formFields=document.getElementsByTagName('input');		
		for(var i=0;i<formFields.length;i++){
			field=formFields[i];
			if(field.name.substr(0,6)=="answer"){
				question=field.parentNode.previousSibling.previousSibling.innerHTML.replace('\n','');
				if(whichBrs()=="ie"){
					question=field.parentNode.previousSibling.innerHTML.replace(/[\n\r\t]*/,'');
				}else{
					question=field.parentNode.previousSibling.previousSibling.innerHTML.replace(/[\n\r\t]*/,'');	
				}
				savingData+=question+"="+field.value+"&";
			}
		}
		var myConn = new XHConn();
		myConn.connect("/competition.php", "POST", savingData, competitionRedirect);	
	}
}


var validcaptcha = false;

function competitionValidateAndSendTest(){
	
	//check user info form - the question answers are optional
	var form=document.getElementById('competform'); 	
	var field='';
	var errorcount=0;
	var question='';

	var qVal
	var answerInputs = getElementsByClassName(document,'fullWidth');
	for(var x=0;x<answerInputs.length;x++){
		qVal = answerInputs[x].value;
		if(qVal.length < 1){
			answerInputs[x].style.border = "1px solid #f00";
			errorcount++;
		} else {
			answerInputs[x].style.border = "1px solid #cccccc";
		}
	}

	var formFields=form.getElementsByTagName('INPUT');
	var formTextArea = document.getElementById('comp_address');
	for(var i=0;i<formFields.length;i++){
		field=formFields[i];
		if(field.value.length<1){
			field.style.border="1px solid red";
			errorcount++;
		}else{
			field.style.border="1px solid #cccccc";
		}
	}
	if(formTextArea.value.length<1) {
		formTextArea.style.border = "1px solid #f00";
		errorcount++;
	} else {
		formTextArea.style.border = "1px solid #cccccc";
	}
	if(errorcount>0){
		alert('You must complete the form fields marked in red. \nPlease complete these and try again.');	
	}else{
		
		if(!validcaptcha){
			competitionCaptcha();
		} else {
			//submit all form data
			formFields=form.elements;
			var savingData='';
			for(var i=0;i<formFields.length;i++){
				field=formFields[i];
				if(field.name){
					savingData+=field.name+"="+field.value+"&";
				}
			}
			//add q & a's
			formFields=document.getElementsByTagName('input');		
			for(var i=0;i<formFields.length;i++){
				field=formFields[i];
				if(field.name.substr(0,6)=="answer"){
					
					if(document.getElementById(('compquestion'+field.name.replace('answer','')))){
						question=document.getElementById(('compquestion'+field.name.replace('answer',''))).innerHTML.replace(/[\n\r\t]*/,'');
					}else{
						question=field.parentNode.previousSibling.previousSibling.innerHTML.replace('\n','');
						if(whichBrs()=="ie"){
							question=field.parentNode.previousSibling.innerHTML.replace(/[\n\r\t]*/,'');
						}else{
							question=field.parentNode.previousSibling.previousSibling.innerHTML.replace(/[\n\r\t]*/,'');	
						}
					}
					savingData+=question+"="+field.value+"&";
				}
			}
			
			var myConn = new XHConn();
			myConn.connect("/competition.php", "POST", savingData, competitionRedirect);	
		}
//		alert(savingData);

	}
}

function competitionCaptcha(){
	var savingData = "captcha_code="+document.getElementById('captcha_code').value;
	var myConn = new XHConn();
	myConn.connect("/secureimage/captchacheck.php","POST",savingData,captchaDone);	
}

function captchaDone(data){
	if(data.responseText == "1"){
		validcaptcha = true;
		competitionValidateAndSendTest();
	} else {
		document.getElementById('captchaFeedback').innerHTML = "Captcha code incorrect. Please try again.";
	}
}




function competitionValidateAndSendTestNew(){
	
	var doctitle = document.title;
	if(doctitle.match("news")){
		var isnewsletter = doctitle.match("news");
	}
	
	//check user info form - the question answers are optional
	var form=document.getElementById('competform'); 	
	var field='';
	var errorcount=0;
	var question='';

	var qVal
	var answerInputs = getElementsByClassName(document,'fullWidth');
	for(var x=0;x<answerInputs.length;x++){
		qVal = answerInputs[x].value;
		if(qVal.length < 1){
			answerInputs[x].style.border = "1px solid #f00";
			errorcount++;
		} else {
			answerInputs[x].style.border = "1px solid #cccccc";
		}
	}

	var formFields=form.getElementsByTagName('INPUT');
	var formTextArea = document.getElementById('comp_address');
	for(var i=0;i<formFields.length;i++){
		field=formFields[i];
		if(field.value.length<1){
			field.style.border="1px solid red";
			errorcount++;
		}else{
			field.style.border="1px solid #cccccc";
		}
	}
	if(formTextArea.value.length<1) {
		formTextArea.style.border = "1px solid #f00";
		errorcount++;
	} else {
		formTextArea.style.border = "1px solid #cccccc";
	}
	if(errorcount>0){
		alert('You must complete the form fields marked in red. \nPlease complete these and try again.');	
	}else{
		
		if(!validcaptcha){
			competitionCaptchaTestNew();
		} else {
			//submit all form data
			formFields=form.elements;
			if (isnewsletter) {
				var savingData = 'func=newsletter&';
			}
			else {
				var savingData = '';
			}
			var theval='';
			for(var i=0;i<formFields.length;i++){
				field=formFields[i];
				if(field.name){
					if(field.name.substr(0,3) == "GP_"){
						if(field.checked == 1){
							theval = "yes";
						} else {
							theval = field.value;
						}
					} else {
						theval = field.value;
					}
						savingData+=field.name+"="+theval+"&";
					
				}
			}
			//add q & a's
			formFields=document.getElementsByTagName('input');		
			for(var i=0;i<formFields.length;i++){
				field=formFields[i];
				if(field.name.substr(0,6)=="answer"){
					question=field.parentNode.previousSibling.previousSibling.innerHTML.replace('\n','');
					if(whichBrs()=="ie"){
						question=field.parentNode.previousSibling.innerHTML.replace(/[\n\r\t]*/,'');
					}else{
						question=field.parentNode.previousSibling.previousSibling.innerHTML.replace(/[\n\r\t]*/,'');	
					}
					savingData+=question+"="+field.value+"&";
				}
			}
			
			var myConn = new XHConn();
			myConn.connect("/comp_test.php", "POST", savingData, competitionRedirect);	
		}
//		alert(savingData);

	}
}

function competitionCaptchaTestNew(){
	var savingData = "captcha_code="+document.getElementById('captcha_code').value;
	var myConn = new XHConn();
	myConn.connect("/secureimage/captchacheck.php","POST",savingData,captchaDoneTestNew);	
}

function captchaDoneTestNew(data){
	if(data.responseText == "1"){
		validcaptcha = true;
		competitionValidateAndSendTestNew();
	} else {
		document.getElementById('captchaFeedback').innerHTML = "Captcha code incorrect. Please try again.";
	}
}





function competitionRedirect(data){
	window.location.href=data.responseText;	
}

