function DeleteQuestion(sid,qOrder,qid,pid){
	var bOk = confirm("Are you sure you want to delete this question.");
	if(bOk){
		window.location='summarySurvey.php?sid='+sid+'&qOrder='+qOrder+'&QAction=delete&qid='+qid+'&pid='+pid;
		}
		//DeleteQuestion('.$sid.','.$row3[qOrder].','.$qid.','.$pid.')
	//onclick="window.location=\'summarySurvey.php?sid='.$sid.'&qOrder='.$row3[qOrder].'&QAction=delete&qid='.$qid.'&pid='.$pid.'\'"	
	}
function DeletePage(sid,pOrder,pid){
	var bOk = confirm("Are you sure you want to delete this page.");
	if(bOk){
		window.location='summarySurvey.php?sid='+sid+'&pOrder='+pOrder+'&PAction=delete&pid='+pid;
		}
	//onClick="window.location='summarySurvey.php?sid=[?$sid?]&pOrder=[?$pOrder[page]?]&PAction=delete&pid=[?$pageIds[page]?]'"
	
	}
function do_deleteLogic_cb(val) {
document.getElementById("logicalQues").innerHTML = val;
}
function do_deleteLogic(skId,qid) {
document.getElementById("msg").innerHTML = '';
x_deleteLogic(skId,qid, do_deleteLogic_cb);
}
////////////////////////////////////
function do_showQues_cb(val) {

document.getElementById("logicalQues").innerHTML = val;
}
function do_showQues(qid) {
document.getElementById("msg").innerHTML = '';
x_showQues(qid, do_showQues_cb);
}
function submitIt(pageAction, terminate, requiredQues, requiredQuesTitles, optionNo, ctrlType, collsNo){
	
	var arrRequiredQues;
	var arrRequiredQuesTitles;
	var arrOptionNo;
	var arrColls;
	var arrCtrlType;
	var count;

	
	var ctrl, title;
	arrRequiredQues = requiredQues.split("|");
	arrRequiredQuesTitles = requiredQuesTitles.split("|");
	arrOptionNo = optionNo.split("|");
	arrColls = collsNo.split("|");
	arrCtrlType = ctrlType.split("|");
	
	count=(arrRequiredQues.length);
	
			if (count>0) 
			{ 
			
				for(i=0;i<count;i++) 
				{ 
					ctrl = arrRequiredQues[i];
					title = arrRequiredQuesTitles[i];
					optionLenth = arrOptionNo[i];
					collLenth = arrColls[i];
					ctrlTypeVal = arrCtrlType[i];
					
					switch (ctrlTypeVal){
						case "1": 
							if(!Validation1( ctrl, title, optionLenth ))
							return false;
						break;
						
						case "2": 
							if(!Validation1( ctrl, title, optionLenth ))
							return false;
						break;
						
						case "3": 
							if(!Validation3( ctrl, title, optionLenth ))
							return false;
						break;
						
						case "4": 
							if(!Validation4( ctrl, title, optionLenth ))
							return false;
						break;
						
						case "5": 
							if(!Validation4( ctrl, title, optionLenth ))
							return false;
						break;
						
						case "6": 
							if(!Validation6( ctrl, title, optionLenth, collLenth ))
							return false;
						break;
						
						case "7": 
							
							if(!Validation6( ctrl, title, optionLenth, collLenth ))
							return false;
						break;
						
						case "8": 
							if(!Validation8( ctrl, title, optionLenth, collLenth ))
							return false;
						break;
						
						case "9": 
							if(!Validation9( ctrl, title, optionLenth, collLenth ))
							return false;
						break;
						
						case "10": 
							
							if(!Validation10( ctrl, title, optionLenth ))
							return false;
						break;
						case "11": 
							if(!Validation10( ctrl, title, optionLenth ))
							return false;
						break;
						
						case "12": 
							if(!Validation10( ctrl, title, optionLenth ))
							return false;
						break;
						
						case "13": 
							if(!Validation10( ctrl, title, optionLenth ))
							return false;
							if(!ValidateSum( ctrl, title, optionLenth, collLenth ))
							return false;
							
						break;
						
						case "14": 
							if(!Validation14( ctrl, title, optionLenth, collLenth ))
							return false;
						break;
						}

					
				} 
			} 

	document.frm.terminate.value = terminate;
	document.frm.action = pageAction;
	document.frm.submit();
	//window.opener.history.go(0);
	}
	
function	ValidateSum( ctrl, title, optionLenth, collLenth )
{
	var flag = false;
	var field;
	var fixedSum = parseFloat(collLenth);
	var counter = 0;
	if(optionLenth == 0){			
		field = eval( "document.frm."+ctrl );
		if(field){
			
			var v = parseFloat(field.value);
			
				if(v != fixedSum){
						field.focus();
						alert(title+ ' must be equal '+fixedSum);
						
						return false;
						}else{
							return true;
							}
					
				}
			}else if(optionLenth > 0){
				var i = 0;
				for(i = 0; i < optionLenth; i++){
					
						field = eval( "document.frm."+ctrl+"_"+i );
						if(field){
							var fieldVal = field.value;
							if(fieldVal != ''){
								var v = parseFloat(fieldVal);
								
								counter += v;
							}
							
						}
				}
				
				if(counter != fixedSum){
				field.focus();
					alert(title+ ' must be equal '+fixedSum);
					return false;
				}else{
					return true;
				}
				
			}
}

function	Validation14( ctrl, title, optionLenth, collLenth )
{
	var flag = false;
	var field;
	var arrDateTime;
	if(collLenth == 'date'){
		arrDateTime = new Array('_mon_', '_dd_', '_yy_');
										
	}else if(collLenth == 'time'){
		arrDateTime = new Array('_hh_', '_min_', '_ampm_');
		
			
			
	}else if(collLenth == 'both'){
				
		arrDateTime = new Array('_mon_', '_dd_', '_yy_', '_hh_', '_min_', '_ampm_');
		
	}
		
	if(optionLenth > 0){
				var i = 0;
				var m =0;
				
				//alert(arrDateTime[3]);
				for(i = 0; i < optionLenth; i++){
				var n =0;
						for(m =0; m < (arrDateTime.length); m++){
							
								field = eval( "document.frm."+ctrl+arrDateTime[m]+i );
								
								if(field){
									var str = field.value;
									if(collLenth == 'time'){
										if(str != ''){
											switch (arrDateTime[m]){
																			
												case "_hh_": 
													if(!checkNumber(field, 1, 2, 1, 24, 'You must enter the hours. (1-24)')){
													return false;
													}
												break;
												
												case "_min_": 
													if(!checkNumber(field, 1, 2, 1, 60, 'You must enter the minutes. (1-60)')){
													return false;
													}
												break;
												
											}
										}
									}
									if(collLenth == 'date'){
										if(str != ''){
											switch (arrDateTime[m]){
												case "_mon_": 
													if(!checkNumber(field, 1, 2, 1, 12, 'You must enter the month. (1-12)')){
													return false;
													}
												break;
												
												case "_dd_": 
													if(!checkNumber(field, 1, 2, 1, 31, 'You must enter the days. (1-31)')){
													return false;
													}
												break;
												
												case "_yy_": 
													if(!checkNumber(field, 1, 4, 1800, 2050, 'You must enter the year. (1800-2050)')){
													return false;
													}
												break;
																							
											}
										}
									}
									if(collLenth == 'both'){
										if(str != ''){
											switch (arrDateTime[m]){
												case "_mon_": 
													if(!checkNumber(field, 1, 2, 1, 12, 'You must enter the month. (1-12)')){
													return false;
													}
												break;
												
												case "_dd_": 
													if(!checkNumber(field, 1, 2, 1, 31, 'You must enter the days. (1-31)')){
													return false;
													}
												break;
												
												case "_yy_": 
													if(!checkNumber(field, 1, 4, 1800, 2050, 'You must enter the year. (1800-2050)')){
													return false;
													}
												break;
												
												case "_hh_": 
													if(!checkNumber(field, 1, 2, 1, 24, 'You must enter the hours. (1-24)')){
													return false;
													}
												break;
												
												case "_min_": 
													if(!checkNumber(field, 1, 2, 1, 60, 'You must enter the minutes. (1-60)')){
													return false;
													}
												break;
												
											}
										}
											//'_mon_', '_dd_', '_yy_', '_hh_', '_min_', '_ampm_'
										 	
										
									}
									if(str != ''){
										 n++;
									}	
								}
						}
						if(n == (arrDateTime.length)){
						flag = true;
						}
				}
				if(flag == false){
			
					alert(title+ ' is required.');
					return false;
				}else{
					return true;
				}
				
			}
}
function checkNumber(e, minLen, maxLen, minVal, maxVal, errorMsg) {
var v = parseFloat(e.value);
if (isNaN(v) || e.value.length < minLen || e.value.length > maxLen || v < minVal || v > maxVal) 
{
e.focus();
  alert(errorMsg); 
 return false;
} else { 
return true; 
}
}
function checkAMPM(e) {
if (e[e.selectedIndex].value == '') { e.focus(); secondaryAlert = true; secondaryAlertText = 'You must select AM or PM.'; return false; } else { return true; }
}
	
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

	
function	Validation10( ctrl, title, optionLenth )
{
	var flag = false;
	var field;
	if(optionLenth == 0){			
		field = eval( "document.frm."+ctrl );
		if(field){
			var str = field.value;
				if(str == ''){
						field.focus();
						alert(title+ ' is required.');
						
						return false;
						}else{
							return true;
							}
					
				}
			}else if(optionLenth > 0){
				var i = 0;
				for(i = 0; i < optionLenth; i++){
					
						field = eval( "document.frm."+ctrl+"_"+i );
						if(field){
							var str = field.value;
							if(str != ''){
								 flag = true;
								}
							
						}
				}
				if(flag == false){
				field.focus();
					alert(title+ ' is required.');
					return false;
				}else{
					return true;
				}
				
			}
}
	
function Validation9( ctrl, title, optionLenth, collLenth )
{
var flag = false;
var field;
	if(optionLenth > 1 && collLenth > 1){
	
		var i = 0;
		for(i = 0; i < optionLenth; i++){	
			for(m = 0; m < collLenth; m++){	
					field = eval( "document.frm."+ctrl+"_"+i+"_"+m );
					if(field.value != ''){
							flag = true;
					}
			}						
		}
	}
	if(flag == false){
		//field.focus();
		alert(title+ ' is required.');
		return false;
	}else{
		return true;
	}
}
	
function Validation8( ctrl, title, optionLenth, collLenth )
{
var flag = false;
var field;
	if(optionLenth > 1 && collLenth > 1){
	
		var i = 0;
		for(i = 0; i < optionLenth; i++){	
			for(m = 0; m < collLenth; m++){	
					field = eval( "document.frm."+ctrl+"_"+i+"_"+m );
					if(field.checked == true){
							flag = true;
					}
			}						
		}
	}
	if(flag == false){
		//field.focus();
		alert(title+ ' is required.');
		return false;
	}else{
		return true;
	}
}

	
function Validation6( ctrl, title, optionLenth, collLenth )
{
	
var flag = false;
var field;
	if(optionLenth > 1 && collLenth > 1){
	
		var i = 0;
		for(i = 0; i < optionLenth; i++){	
			for(m = 0; m < collLenth; m++){	
					field = eval( "document.frm."+ctrl+"_"+i );
					
					if(field[m].checked == true){
							flag = true;
					}
					
			}						
		}
	}
	
	if(flag == false){
		field[0].focus();
		alert(title+ ' is required.');
		return false;
	}else{
		return true;
	}
}

	
function	Validation4( ctrl, title, optionLenth )
{
var flag = false;
var field;
	if(optionLenth == 0){
	}else if(optionLenth == 1){
		field = eval( "document.frm."+ctrl );
		if(field){
				if(field.checked == false){
					field.focus();
					alert(title+ ' is required.');
					return false;
				}else{
					return true;
				}
					
		}
	}else if(optionLenth > 1){
	
		var i = 0;
		for(i = 0; i < optionLenth; i++){	
		
				field = eval( "document.frm."+ctrl+"_"+i );
				
						if(field.checked == true){
							flag = true;
						}
				
								
		}
	}
	if(flag == false){
		field.focus();
		alert(title+ ' is required.');
		return false;
	}else{
		return true;
	}
}
function	Validation3( ctrl, title, optionLenth )
{
var flag = false;
var field;
	if(optionLenth == 0){
	}else if(optionLenth > 0){
		field = eval( "document.frm."+ctrl );
		if(field){
				if(field.value == ''){
					field.focus();
					alert(title+ ' is required.');
					return false;
				}else{
					return true;
				}
					
		}
	}
	
}

	
function	Validation1( ctrl, title, optionLenth )
{
var flag = false;
var field;
	if(optionLenth == 0){
	}else if(optionLenth == 1){
		field = eval( "document.frm."+ctrl );
		if(field){
				if(field.checked == false){
					field.focus();
					alert(title+ ' is required.');
					return false;
				}else{
					return true;
				}
					
		}
	}else if(optionLenth > 1){
	
		var i = 0;
		for(i = 0; i < optionLenth; i++){	
		
				field = eval( "document.frm."+ctrl );
				
						if(field[i].checked == true){
							flag = true;
						}
				
								
		}
	}
	if(flag == false){
		field[0].focus();
		alert(title+ ' is required.');
		return false;
	}else{
		return true;
	}
}


function SendSurvey(){
		if(document.sendSurvey.survey.value == ''){
			document.sendSurvey.survey.focus();
			alert("Please select a survey.");
			}else if(document.sendSurvey.subjectTag.value == ''){
			document.sendSurvey.subjectTag.focus();
			alert("Subject is required.");
			}else if(document.sendSurvey.messageBody.value == ''){
			document.sendSurvey.messageBody.focus();
			alert("Please enter message.");
			}else if(document.sendSurvey.replayAddress.value == ''){
			document.sendSurvey.replayAddress.focus();
			alert("Replay address is required.");
			}else{
				document.sendSurvey.pageAction.value = 'SendMail';
				document.sendSurvey.submit();
				}
		}
	function sendSurveyLink(sid,emailListId){
		
		document.sendSurvey.pageAction.value = 'fillForm';
		document.sendSurvey.sid.value = sid;
		document.sendSurvey.emailListId.value = emailListId;
		document.sendSurvey.action = 'sendsurvey2.php';
		document.sendSurvey.submit();
		}
	function toggleBox(szDivID, iState) // 1 visible, 0 hidden
{

    if(document.layers)	   //NN4+
    {
       document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById)	  //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = iState ? "visible" : "hidden";
    }
    else if(document.all)	// IE 4
    {
        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
}
// -->
////////////////////////////////////////////////////


// DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)


function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail address")
		    return false
		 }

 		 return true					
	}

function ValidateEmail(txtEmail){
	//var emailID=document.emailList.txtEmail
	var emailID = eval( "document.emailList."+txtEmail );
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }

/////////////////////////////////////////////////////
	function updateEmlList(emId){
		
		if(document.emailList.title.value ==''){
			document.emailList.title.focus();
			alert("Please enter a title.");
		}else if(document.emailList.fromTag.value ==''){
			document.emailList.fromTag.focus();
			alert("From is required.");
		}else if(document.emailList.subjectTag.value ==''){
			document.emailList.subjectTag.focus();
			alert("Subject is required.");
		}else if(document.emailList.messageBody.value ==''){
			document.emailList.messageBody.focus();
			alert("Message is required.");
		}else if(document.emailList.replayAddress.value ==''){
			document.emailList.replayAddress.focus();
			alert("Replay Email Address is required.");
		}else if(ValidateEmail('fromTag') == false){
		}else if(ValidateEmail('replayAddress') == false){
		}else{
		document.emailList.pageAction.value = 'update';
		document.emailList.action = 'emailLIst.php?emId='+emId;
		document.emailList.submit();
		}
		}
	function DeleteEmail(eid){
		document.emails.pageAction.value = 'deleteEmail';
		document.emails.action = 'emailSettings.php?eid='+eid;
		document.emails.submit();
		}
	function submitEditEmails(eid){
		document.emails.pageAction.value = 'update';
		document.emails.action = 'emailSettings.php?eid='+eid;
		document.emails.submit();
		}
	function doEdit(eid){
		document.emails.pageAction.value = '';
		document.emails.action = 'emailSettings.php?eid='+eid+'&action=doEdit'
		document.emails.submit();
		}
	function changeContents(){
	document.emails.pageAction.value = '';
	document.emails.submit();
	}

	function checkResponseNo(){
		if(document.form1.responseNo.value ==''){
			
			alert("Please enter a number");
		}else{
		document.form1.submit();
		}
	}
	function submitEmails(){
		if(document.emails.FirstName.value ==''){
			document.emails.FirstName.focus();
			alert("First name is required.");
		}else if(document.emails.emailAddress.value ==''){
			document.emails.emailAddress.focus();
			alert("Email address is required.");
		}else if(document.emails.emlid.value ==''){
			document.emails.emlid.focus();
			alert("Email list is required.");
		}else if(document.emails.detail.value ==''){
			document.emails.detail.focus();
			alert("Detail is required.");
		}else{
		document.emails.pageAction.value = 'Add';
		document.emails.submit();
		}
	}
	function submitEmlList(){
		
		if(document.emailList.title.value ==''){
			document.emailList.title.focus();
			alert("Please enter a title.");
		}else if(document.emailList.fromTag.value ==''){
			document.emailList.fromTag.focus();
			alert("From is required.");
		}else if(document.emailList.subjectTag.value ==''){
			document.emailList.subjectTag.focus();
			alert("Subject is required.");
		}else if(document.emailList.messageBody.value ==''){
			document.emailList.messageBody.focus();
			alert("Message is required.");
		}else if(document.emailList.replayAddress.value ==''){
			document.emailList.replayAddress.focus();
			alert("Replay Email Address is required.");
		}else if(ValidateEmail('fromTag') == false){
		}else if(ValidateEmail('replayAddress') == false){
		}else{
		document.emailList.pageAction.value = 'Add';
		document.emailList.submit();
		}
	}
function submitSurveySummary(sid2){
	//alert("Done!");
	document.frm.theme.value = 1;
	document.frm.action = 'summarySurvey.php?sid='+sid2;
	document.frm.submit();
	}
	
	function	RemoveSpecialChar( ctrl )
		{
			var field;
			
			field = eval( "document.form1."+ctrl );
			var str = field.value;
			s = str.replace( /\|+|\>+|\<+|\"+|\'+|\/+/gi, "" );
			//s = str.replace( /\|+|\?+|\>+|\<+|\;+|\"+|\'+|\,+|\/+/gi, "" );
			//s = str.replace( /\|+|\'+|\;/, "" );
			
			//s = s.replace( /^_|_$/gi, "" );
			//s = str.replace( /\.+|\?+|\:+|\>+|\<+|\;+|\"+|\'+|\,+|\/+/gi, " " );
			//return s.substr( 0, 60 );
			//return s;
			field.value = s;
		}
		
	function	RemoveSpecialChar2( ctrl )
		{
			// this function is commnted due to, when a respondent enter some text and then try to edit a word from that text by selecting it, then all the text is removed suddenly.
			
			/*
			var field;
			
			field = eval( "document.frm."+ctrl );
			var str = field.value;
			s = str.replace( /\|/, "" );
			field.value = s;
			*/
		}