// global.js for eCommunities
// site-wide functionality
// code by Chris Nott (cnott@NOSPAM.blastradius.com)

/*
	MODIFICATIONS:
	====================================
	
	Date			Modifier			Comments
	=============	===============	===========================================
	Oct 20, 2003	C Bacinski		Modified validatePoll method to
								check that all single-response
								questions have been answered
	Oct 27, 2003	C Bacinski		Bug #1565: Rewrote validatePoll function so
								that ALL radio- and checkbox-type questions
								must be answered
*/

/**********************************************************************
   Redirecting
 *********************************************************************/

//checks to see if url param is not empty and tries to redirect to supplied
//url
function redirect(url){
	if(url != ''){
		location.href = url;
	}		
}

/**********************************************************************
   Popups and New Windows
 *********************************************************************/

// openExternalLink(url) - use to open all off-site links
// ie: <a href="http://url.here" onclick="openExternalLink(this.href); return false;">
// requires window.js
function openExternalLink(url) {
   return openSizedFullChromeWindow(url, 'external', getInnerWidth(window)-100, getInnerHeight(window)-100);
}

function open411Window() {
   return openCenteredOnOpenerWindow('', '411', 750, 400, true, true);
}

function openYellowPagesWindow() {
   return openCenteredOnOpenerWindow('', 'YellowPages', 750, 400, true, true);
}

function openCenterWindow(url, name, openerName, parms) {
   var height = getInnerHeight(window)-100;
   var width = getInnerWidth(window)-100;
   var left = Math.floor( (screen.width - width) / 2);
   var top = Math.floor( (screen.height - height) / 2);
   var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width + ",openerName" + openerName +",toolbar=yes"+",scrollbars=yes"+",status=yes"+",menubar=yes"+",location=yes";
   if (parms) { winParms += "," + parms; }
   var win = window.open(url, name, winParms);
   if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
   return win;
}


/**********************************************************************
   General Form Validation
 *********************************************************************/

function validateFormData(portletID) {
   var returnValue = true;
   var args = arguments;
	for (var inputIdIndex = 0; inputIdIndex < args.length; inputIdIndex++) {
      var inputId = args[inputIdIndex];
      var input = document.getElementById(inputId);
      // if field contains no data, display div containing error;
      if (((input.type == "text" || input.type == "textarea" || input.type == 'password') && input.value.length == 0) || 
         ((input.type == "select-one" || input.type == "select-multiple" || input.type == "select") && input.selectedIndex == -1)) {
         document.getElementById('e' + inputId).style.display = 'block';
         returnValue = false;
      }
      else {
         document.getElementById('e' + inputId).style.display = 'none';
      }
   }
   
   return returnValue;
}


function validate411FormData(portletID) {
   var objForm; 
   objForm = document.forms.per_input;
   var returnValue = true;
   var args = arguments;
	for (var inputIdIndex = 0; inputIdIndex < args.length; inputIdIndex++) {
      var inputId = args[inputIdIndex];
      var input = document.getElementById(inputId);
      // if field contains no data, display div containing error;
      if (((input.type == "text" || input.type == "textarea" || input.type == 'password') && input.value.length == 0) || 
         ((input.type == "select-one" || input.type == "select-multiple" || input.type == "select") && input.selectedIndex == -1)) {
         document.getElementById('e' + inputId).style.display = 'block';
         returnValue = false;
      }
      else {
         document.getElementById('e' + inputId).style.display = 'none';
         returnValue = false;
         //openCenteredOnOpenerWindow('', '411', 750, 500, true, true, 'toolbar=yes,menubar=yes,location=yes,resizable=yes');
         openCenterWindow('', '411','411','toolbar=yes,menubar=yes,location=yes,resizable=yes');
         objForm.submit();
      }
   }
   
   return returnValue;
}


function validateYPFormData1(portletID) {
   var objForm; 
   objForm = document.getElementById('YP1');
   var returnValue = true;
   var args = arguments;
	for (var inputIdIndex = 0; inputIdIndex < args.length; inputIdIndex++) {
      var inputId = args[inputIdIndex];
      var input = document.getElementById(inputId);
      // if field contains no data, display div containing error;
      if (((input.type == "text" || input.type == "textarea" || input.type == 'password') && input.value.length == 0) || 
         ((input.type == "select-one" || input.type == "select-multiple" || input.type == "select") && input.selectedIndex == -1)) {
         document.getElementById('e' + inputId).style.display = 'block';
         returnValue = false;
      }
      else {
         document.getElementById('e' + inputId).style.display = 'none';
         returnValue = false;
         //openCenteredOnOpenerWindow('', 'YellowPages', 750, 500, true, true,'toolbar=yes,menubar=yes,location=yes,resizable=yes');
         openCenterWindow('', 'YellowPages','YellowPages','toolbar=yes,menubar=yes,location=yes,resizable=yes');
         objForm.submit();
      }
   }
   return returnValue;
}


function validateYPFormData2(portletID) {
   var objForm; 
   objForm = document.getElementById('YP2');
   var returnValue = true;
   var args = arguments;
	for (var inputIdIndex = 0; inputIdIndex < args.length; inputIdIndex++) {
      var inputId = args[inputIdIndex];
      var input = document.getElementById(inputId);
      // if field contains no data, display div containing error;
      if (((input.type == "text" || input.type == "textarea" || input.type == 'password') && input.value.length == 0) || 
         ((input.type == "select-one" || input.type == "select-multiple" || input.type == "select") && input.selectedIndex == -1)) {
         document.getElementById('e' + inputId).style.display = 'block';
         returnValue = false;
      }
      else {
         document.getElementById('e' + inputId).style.display = 'none';
         returnValue = false;
         //openCenteredOnOpenerWindow('', 'YellowPages', 750, 500, true, true,'toolbar=yes,menubar=yes,location=yes,resizable=yes');
         openCenterWindow('', 'YellowPages','YellowPages','toolbar=yes,menubar=yes,location=yes,resizable=yes'); 
         objForm.submit();
      }
   }
   return returnValue;
}



/**********************************************************************
   Specific Form Validation
 *********************************************************************/

// Lost/Reset Password Form Validation
 function validateLoginForm(portletID) {
   var returnValue = true;
   var args = arguments;
   var inputId1 = args[0];
   var inputId2 = args[1];
   var input1 = document.getElementById(inputId1);
   var input2 = document.getElementById(inputId2);
   // if both fields contains no data, display div containing error;
   if (input1.value.length == 0 && input2.value.length == 0) {
      document.getElementById('emptyFields').style.display = 'block';
      returnValue = false;
   }
   else {
      document.getElementById('emptyFields').style.display = 'none';
   }

   return returnValue;
}

// Discussion Form Validation
function validateDiscussionForm(portletID) {
   var returnValue = true;
   var args = arguments;
	for (var inputIdIndex = 0; inputIdIndex < args.length; inputIdIndex++) {
      var inputId = args[inputIdIndex];
      var input = document.getElementById(inputId);
      // if field contains no data, display div containing error;
      if (input) {
         if (((input.type == "text" || input.type == "textarea" || input.type == 'password') && input.value.length == 0) || 
            ((input.type == "select-one" || input.type == "select-multiple" || input.type == "select") && input.selectedIndex == -1)) {
            document.getElementById('e' + inputId).style.display = 'block';
            returnValue = false;
         }
         else {
            document.getElementById('e' + inputId).style.display = 'none';
         }
      }
   }
   // validate max textarea size
   if (input.type == "textarea" && ((input.value.length) > 2000)) {
      document.getElementById('e' + inputId + 'b').style.display = 'block';
      returnValue = false;
      }
      else {
         document.getElementById('e' + inputId + 'b').style.display = 'none';
      }
   
   return returnValue;
}

// Feedback Form Validation (Contact Us)
function validateFeedbackForm(portletID) {
   var returnValue = true;
   var args = arguments;
	for (var inputIdIndex = 0; inputIdIndex < args.length; inputIdIndex++) {
      var inputId = args[inputIdIndex];
      var input = document.getElementById(inputId);
      // if field contains no data, display div containing error;
      if (((input.type == "text" || input.type == "textarea" || input.type == 'password') && input.value.length == 0) || 
         ((input.type == "select-one" || input.type == "select-multiple" || input.type == "select") && 
		 (input.selectedIndex == -1 || input.options[input.selectedIndex].text=="" ||
		 (input.options[input.selectedIndex].value=="(planbuildcentre)")))) {
         document.getElementById('e' + inputId).style.display = 'block';
		 //if(input.type=="select") alert(input.options[input.selectedIndex].value.charAt(0));
		 returnValue = false;
      }
      else {
         document.getElementById('e' + inputId).style.display = 'none';
      }
   }

   // display div containing introductory sentence for errors
   var errorIntro = 'e' + inputId;
   errorIntro = errorIntro.substring(0,(errorIntro.length -1));
   if (!returnValue) {
      document.getElementById(errorIntro).style.display = 'block';
   }
   else {
      document.getElementById(errorIntro).style.display = 'none';
   }

   return returnValue;
}


function validatePoll(form, choiceControlId) {
	// every radio-style and checkbox-style survey question must be answered
	
	// key: question; values: false (question not answered: default),
	// true (question answered)
	var questionAnswers=new Object();
	
	// loop through all form elements (mainly answer controls)
	for(var i=0;i<form.length;i++) {
		
		// only interested in "choice" controls with the given id name
		if((form[i].type=="radio" || form[i].type=="checkbox") &&
			form[i].id==choiceControlId) {
				
			// determine question this answer goes with
			// if radio button, control name is of form "questionQ1";
			// if checkbox, question name is a hidden form field with dynamic name
			// and form "questionQ1"
			if(form[i].type=="radio") var thisQuestion=form[i].name;
			else if(form[i].type=="checkbox") {
				var thisQuestionBox=eval("form.answerQuestion"+form[i].name);
				if(thisQuestionBox!=null) var thisQuestion=thisQuestionBox.value;
				else var thisQuestion=form[i].name;
			}
				
			// initialize response to this question
			if(questionAnswers[thisQuestion]==null)
				questionAnswers[thisQuestion]=false;
			// if the form control has been checked, set the response to true
			if(form[i].checked) questionAnswers[thisQuestion]=true;
		}
	}
	
	// loop through all questions to determine whether all have been answered
	for(var i in questionAnswers) {
		// here's one that hasn't been answered; show error message and halt
		// form submission
		if(questionAnswers[i]==false) {
			document.getElementById("e"+choiceControlId).style.display="block";	
			return false;
		}
	}
	
	// hide error messages and allow form submission
	document.getElementById("e"+choiceControlId).style.display="none";	
}


/**********************************************************************
  Plan & Build
 *********************************************************************/
// format Committee of Adjustment Decision Text

function formatPre(data) {
   var br = "\n";
   var brCode = br.charCodeAt(0);
   var space = " ";
   var spaceCode = space.charCodeAt(0);
   var tab = "	";
   var tabCode = tab.charCodeAt(0);
   var tabHTML = "  ";
   var rawData = data.value;
   var rawLen = rawData.length;
   var formatData = "";
   var currPos = 0;
   var colSize = 75;
   var newString = "";

   for (i=0;i<rawLen;i++){
   
      // 
      if(parseInt(i-currPos) < parseInt(currPos+colSize)){
         if(rawData.charAt(i).charCodeAt(0) == brCode){
            //formatData += rawData.charAt(i);
            currPos = i;
            formatData += br;
         }
         else {
            formatData += rawData.charAt(i);
         }
      }
      if(parseInt(i - currPos) >= colSize-1){
         after = "";
         for(j=i;j>=currPos;j--){
            if(rawData.charAt(j).charCodeAt(0) == spaceCode){
               formatData = formatData.substring(0,j) + br + after;
               currPos=j;
               break;
            }
            else{
               after = rawData.charAt(j) + after;
            }
         }
      }
   }

   //convert tabs to spaces
  for (x=0;x<formatData.length;x++){
      if(formatData.charAt(x).charCodeAt(0) == tabCode)
         newString += tabHTML;
      else
         newString += formatData.charAt(x);
   }

document.write("<pre>" + newString + "</pre>");

}

function removeSpaces(inputString) {
	var strNoSpaces="";
	for(var i=0;i<inputString.length;i++) {
		if(inputString.charAt(i)!=' ') {
			strNoSpaces+=inputString.charAt(i);
		}
	}
	return strNoSpaces;
}

function trimEdges(inputString) {
	var firstNonSpaceIdx=-1;
	var lastNonSpaceIdx=-1;
	for(var i=0;i<inputString.length;i++) {
		if(inputString.charAt(i)!=' ') {
			firstNonSpaceIdx=i; 
			break;
		}	
	}
	if(firstNonSpaceIdx!=-1) {
		for(var i=inputString.length-1;i<inputString.length;i--) {
			if(inputString.charAt(i)!=' ') {
				lastNonSpaceIdx=i;
				break;
			}
		}
		if(lastNonSpaceIdx==inputString.length-1) {
			trimmedString=inputString.substr(firstNonSpaceIdx);
		}
		else	trimmedString=inputString.substring(firstNonSpaceIdx,lastNonSpaceIdx+1);
		return trimmedString;
	}
	else return "";
}










/**********************************************************************
  1k DHTML API - standards version
 *********************************************************************/
d=document;l=d.layers;op=navigator.userAgent.indexOf('Opera')!=-1;px='px';
function gE(e,f){if(l){f=(f)?f:self;var V=f.document.layers;if(V[e])return V[e];for(var W=0;W<V.length;)t=gE(e,V[W++]);return t;}if(d.all)return d.all[e];return d.getElementById(e);}
function sE(e){l?e.visibility='show':e.style.visibility='visible';}
function hE(e){l?e.visibility='hide':e.style.visibility='hidden';}
function sZ(e,z){l?e.zIndex=z:e.style.zIndex=z;}
function sX(e,x){l?e.left=x:op?e.style.pixelLeft=x:e.style.left=x+px;}
function sY(e,y){l?e.top=y:op?e.style.pixelTop=y:e.style.top=y+px;}
function sW(e,w){l?e.clip.width=w:op?e.style.pixelWidth=w:e.style.width=w+px;}
function sH(e,h){l?e.clip.height=h:op?e.style.pixelHeight=h:e.style.height=h+px;}
function sC(e,t,r,b,x){l?(X=e.clip,X.top=t,X.right=r,X.bottom=b,X.left=x):e.style.clip='rect('+t+px+' '+r+px+' '+b+px+' '+x+px+')';}
function wH(e,h){if(l){Y=e.document;Y.open();Y.write(h);Y.close();}if(e.innerHTML)e.innerHTML=h;}

