var host="http://www.poppydog.com";
//var host="http://localhost:9090";
var platformCheck = false;
var isOfficeLive = false;
var hasForm = false;

var survey_alreadyVoted='Sorry, only one vote allowed.';
var survey_chooseOne = 'Please Choose';
var survey_thankyou = 'Thank you for taking part in our survey';
var survey_viewRes = 'View Results';
var survey_vote = 'Submit';

function pd_setHasForm(val){
hasForm=val;
}

function pd_thanks(hid,tiid,viewRes){
document.write('<div id="survey"><div class="shade1">');
document.write('<br><br>'+survey_thankyou+'<br><br>');
if (viewRes)
document.write('<a href="'+host+'/survey.do?action=viewRes&tiid='+tiid+'&k='+hid+'" target="_blank">'+survey_viewRes+'</a><br>');
document.write('<br><hr><div><a href="'+host+'/prod/survey.jsp" target="_blank">Survey powered by poppydog.com</a></div></div></div>');
}

function pd_already(hid,tiid,viewRes){
document.write('<div id="survey"><div class="shade1">');
document.write('<br><br>'+survey_alreadyVoted+'<br><br>');
document.write('<br></div></div>');
}


function pd_t(hid,tiid,js,viewRes){
if (document.location.search.indexOf("pd_survey_msg_th_"+tiid+"=")>-1){
	pd_thanks(hid,tiid,viewRes);
	doFixIt("write");
	return true;
}
else if (document.location.search.indexOf("pd_survey_msg_al_"+tiid+"=")>-1){
	pd_already(hid,tiid,viewRes);
	doFixIt("write");
	return true;
}
if (!hasForm){
document.write('<form action="'+host+'/survey.do" method="post" onSubmit="return doValidate(this);">');
document.write('<input type="hidden" name="action" value="vote" />');
document.write('<input type="hidden" name="k" value="'+hid+'" />');
document.write('<input type="hidden" name="tiid" value="'+tiid+'" />');
if (js){
document.write('<input type="hidden" name="returnPath" value="'+document.location.href+'" />');
}
}
document.write('<div id="survey"><div class="shade1">');
return false;
}

function pd_b(){
document.write('<input type="submit" value="'+survey_vote+'" class="submit">');
document.write('<div><a href="'+host+'/prod/survey.jsp" target="_blank">Survey powered by poppydog.com</a></div>');
document.write('</div></div>');
if (!hasForm){
document.write('</form>');
}
doFixIt("write");
}


function pd_qt(num,qid,q,compulsary_type){
document.write('<div class="q">');

if (compulsary_type=='c' || compulsary_type=='s')
document.write('<input type="hidden" name="q_c_" value="'+qid+'"/>');

document.write('<b>'+num+'. ');
if (compulsary_type=='c' || compulsary_type=='s')
	document.write('<span id="e_'+qid+'">*</span>');
document.write(q);
document.write('</b><br />');
}

function pd_qb(){
document.write('</div><hr />');
}

function pd_l(name,rid,label){
document.write('<select name="'+name+'"><option value="">'+survey_chooseOne+'</option>');
for (i=0; i<rid.length; i++)
	document.write('<option value="'+rid[i]+'_-1">'+label[i]+'</option>');
document.write('</select>');
}

function pd_r(name,rid,label){
for (i=0; i<rid.length; i++)
	document.write('<input name="'+name+'" type="radio" value="'+rid[i]+'_-1">'+label[i]+'<br />');
}

function pd_c(name,rid,label){
for (i=0; i<rid.length; i++)
	document.write('<input name="'+name+'" type="checkbox" value="'+rid[i]+'_-1">'+label[i]+'<br />');
}

function pd_gr(qid,rid,label,top_id,top_label,compulsary_type){
var star = (compulsary_type!='n')?'* ':''; 
document.write('<table cellspacing="0" cellpadding="0" border="0">');
document.write('<tr><td>&nbsp;</td>');
	for (j=0; j<top_label.length; j++)
		document.write('<td>'+top_label[j]+'</td>');
document.write('</tr>');
for (i=0; i<rid.length; i++){
	document.write('<tr><th>'+star+label[i]+'</th>');
	for (j=0; j<top_label.length; j++) 
		document.write('<td><input type="radio" name="v_mr_'+qid+'_'+rid[i]+'" value="'+rid[i]+'_'+top_id[j]+'"></td>');
	document.write('</tr>');
}
document.write('</table>');
}

function pd_gc(qid,rid,label,top_id,top_label,compulsary_type){
var star = (compulsary_type!='n')?'* ':''; 
document.write('<table cellspacing="0" cellpadding="0" border="0">');
document.write('<tr><td>&nbsp;</td>');
	for (j=0; j<top_label.length; j++)
		document.write('<td>'+top_label[j]+'</td>');
document.write('</tr>');
for (i=0; i<rid.length; i++){
	document.write('<tr><th>'+star+label[i]+'</th>');
	for (j=0; j<top_label.length; j++) 
		document.write('<td><input type="checkbox" name="v_mr_'+qid+'_'+rid[i]+'" value="'+rid[i]+'_'+top_id[j]+'"></td>');
	document.write('</tr>');
}
document.write('</table>');
}


function pd_gl(qid,rid,label,top_id,top_label,compulsary_type){
var star = (compulsary_type!='n')?'* ':''; 
document.write('<table cellspacing="0" cellpadding="0" border="0">');
for (i=0; i<rid.length; i++){
	document.write('<tr><th>'+star+label[i]+'</th>');
	document.write('<td><select name="v_mr_'+qid+'_'+rid[i]+'"><option value="">'+survey_chooseOne+'</option>');
	for (j=0; j<top_id.length; j++) 
		document.write('<option value="'+rid[i]+'_'+top_id[j]+'">'+top_label[j]+'</option>');
	document.write('</select></td></tr>');
	}
document.write('</table>');
}



function doFixIt(action){
	if (!platformCheck)
		doPlatformCheck();
	if (isOfficeLive){
			top.resizeBy(10,10);
			top.resizeBy(-10,-10);
	}
}


function doPlatformCheck(){
	platformCheck=true;
try{
			var iframes = top.document.getElementsByTagName("iframe");
			for (var i=0; i< iframes.length; i++){
				if (iframes.item(i).id.indexOf("CPH_Content_HtmlControl")>-1){
					isOfficeLive = true;
				}
			}
			}
			catch(e){isOfficeLive=false;}

}


function doValidate(f){
var isError=false;
var valid=true;
var mrList;
for (var i=0; i<f.elements.length; i++){
		if (f.elements[i].name.indexOf("q_c_")>-1){
			mrList="";
			isError=false;
			qid=f.elements[i].value;
			document.getElementById("e_"+qid).style.visibility="hidden";
									
			for (var j=0; j<f.elements.length; j++){
				if (f.elements[j].name.indexOf("v_"+qid)>-1){
					isError= !isChecked(f.elements[j],f);
					break
				}
				else if (f.elements[j].name.indexOf("v_mr_"+qid)>-1&&mrList.indexOf(f.elements[j].name)<0){
					mrList=mrList+f.elements[j].name+",";
					isError= (!isError)?!isChecked(f.elements[j],f):isError;					
				}
			}
			
			if (isError){
					document.getElementById("e_"+qid).style.backgroundColor = "#FF0000";
					document.getElementById("e_"+qid).style.color = "#FFFFFF";
					document.getElementById("e_"+qid).style.visibility="visible";
					valid=false;
			}
			
			
		}
			
	}
	if (!valid)
		alert("You haven't answered all the required fields");
	return valid;
}

function isChecked(c,f){
	var checked=false;
	if (c.type.indexOf("select")>-1){
		checked = c.selectedIndex>0;
	}
	else{
	
	for (var j=0; j<f.elements.length; j++){
	if (f.elements[j].name==c.name)
			if (f.elements[j].name==c.name && f.elements[j].checked){
				checked=true;			
				break;
			}
	}
	}
	return checked;
}
