/*
 * Created on May 19, 2007 by nhorvath
 *
 * NXWEB Technologies
 * All Rights Reserved - Unauthorized Use Prohibited
 * For information contact licensing@nxwebtechnologies.com
 * 
 */ 

var fieldRegexes = new Array();
var fieldErrorMessages = new Array();
var requiredFields = new Array();
var conditionalFields = new Array();
var declaredConditionalFields = new Array();

function verifyForm(obj) {

   theForm = obj;
   var errorMessage = '';
   var errors = false;

   // add to required fields if values are in conditional set
   // build a list of error messages that result from a conditional value in the form and the form 
   // elements it affects
   for(i=0; i<theForm.elements.length; i++){
      if(theForm.elements[i].type == "text" || theForm.elements[i].type == "hidden" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button"){
   	       if (conditionalFields[theForm.elements[i].name+"-"+theForm.elements[i].value] != null) {
   	       		for(keyVar in conditionalFields[theForm.elements[i].name+"-"+theForm.elements[i].value]) {
     	       		declaredConditionalFields[keyVar] = conditionalFields[theForm.elements[i].name+"-"+theForm.elements[i].value][keyVar];
  	       		}
   	       }

      }
      else if(theForm.elements[i].type == "password"){
   	       if (conditionalFields[theForm.elements[i].name+"-"+theForm.elements[i].name+"Reenter"] != null) {
   	       		for(keyVar in conditionalFields[theForm.elements[i].name+"-"+theForm.elements[i].name+"Reenter"]) {
	   	       		if (theForm.elements[i].value != document.getElementById(theForm.elements[i].name+"Reenter").value) {
   	       				document.getElementById(theForm.elements[i].name+"Reenter").value = '';
	   	       			declaredConditionalFields[keyVar] = conditionalFields[theForm.elements[i].name+"-"+theForm.elements[i].name+"Reenter"][keyVar];
	   	       		}
   	       		}
   	       }
 
      }
      else if(theForm.elements[i].type == "checkbox"){
 
      }
      else if(theForm.elements[i].type == "radio"){
      	  if (theForm.elements[i].checked) {
      	       if (conditionalFields[theForm.elements[i].name+"-"+theForm.elements[i].value] != null) {
      	       		for(keyVar in conditionalFields[theForm.elements[i].name+"-"+theForm.elements[i].value]) {
      	       			declaredConditionalFields[keyVar] = conditionalFields[theForm.elements[i].name+"-"+theForm.elements[i].value][keyVar];
      	       		}
      	       }
      	  }
      }
      else if(theForm.elements[i].type == "select-one"){
   	       if (conditionalFields[theForm.elements[i].name+"-"+theForm.elements[i].options[theForm.elements[i].selectedIndex].value] != null) {
   	       		for(keyVar in conditionalFields[theForm.elements[i].name+"-"+theForm.elements[i].options[theForm.elements[i].selectedIndex].value]) {
   	       			declaredConditionalFields[keyVar] = conditionalFields[theForm.elements[i].name+"-"+theForm.elements[i].value][keyVar];
   	       		}
   	       }
      }
   }


   for(i=0; i<theForm.elements.length; i++){
      if(theForm.elements[i].type == "text" || theForm.elements[i].type == "hidden" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "password"){
 	     errorMessage += validateField(theForm.elements[i].name, theForm.elements[i].value, fieldRegexes[theForm.elements[i].name]);
      }
      else if(theForm.elements[i].type == "checkbox"){
	     errorMessage += validateField(theForm.elements[i].name, theForm.elements[i].checked, fieldRegexes[theForm.elements[i].name]);
      }
      else if(theForm.elements[i].type == "radio"){
	     errorMessage += validateField(theForm.elements[i].name, theForm.elements[i].checked, fieldRegexes[theForm.elements[i].name]);
      }
      else if(theForm.elements[i].type == "select-one"){
      	  errorMessage += validateField(theForm.elements[i].name, theForm.elements[i].options[theForm.elements[i].selectedIndex].value, fieldRegexes[theForm.elements[i].name]);
      }
   }
   
   //reset conditinal fields to allow for user changing his mind
   declaredConditionalFields = new Array();

   if (errorMessage == '') {
      return true;
   } else {
       showError('<ul>' + errorMessage + '</ul>');
      return false;
   }
 	      
}

//validates fields based on filters placed in body by validate function
function validateField (fieldName, fieldContent, regexContent)
{
	if ( typeof(requiredFields[fieldName]) != "undefined" ) {
		if (requiredFields[fieldName] && !fieldContent) {
		document.getElementById(fieldName).style.borderColor = "#FF0000";
			return '<li>' + requiredFields[fieldName] + '</li>';
		} 
		
	}
	
	if ( typeof(declaredConditionalFields[fieldName]) != "undefined" ) {
		if (declaredConditionalFields[fieldName] && !fieldContent) {
			document.getElementById(fieldName).style.borderColor = "#FF0000";
			return '<li>' + declaredConditionalFields[fieldName] + '</li>';
		} 
	}
	
	
	if ((typeof(fieldRegexes[fieldName]) != "undefined") && 
		 ((typeof(requiredFields[fieldName]) != "undefined") || (typeof(declaredConditionalFields[fieldName]) != "undefined" ))) {
    	if (regexContent.test(fieldContent)) {
    		return '';
    	}
    	else {
    		document.getElementById(fieldName).style.borderColor = "#F5B800";
     		return '<li>' + fieldErrorMessages[fieldName] + '</li>';
    	}
	} else {
	    return '';
	}
}




 
// displays and shows the errormsg

function validate(frmName) {
   
	makePOSTRequest('validator.php?action=validateCustomerType', poststr, 'shoppingCart', 'setPageTotal()');
}

function hideError() {
   document.getElementById('spanErrorMessage').style.visibility = "hidden";
   fadeBackgroundHide();
}

function showError(errorMessage) {
   if (errorMessage.length != 0) {
       document.getElementById('errorMessage').innerHTML = errorMessage;
   }
   document.getElementById('spanErrorMessage').style.visibility = "visible"
   fadeIn('spanErrorMessage',0);
   fadeBackgroundShow();
 }
 
 
 
 function fadeIn(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      setOpacity(obj, opacity);
      opacity += 50;
      window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}
 
 function fadeOut(objId,opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity >= 0) {
      setOpacity(obj, opacity);
      opacity -= 50;
      window.setTimeout("fadeOut('"+objId+"',"+opacity+")", 100);
    }
  }
}

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}



var winW;
var winH;

function getWindowSize() {
  winW = document.documentElement.clientWidth;
  winH = document.documentElement.clientHeight;
}


function fadeBackgroundShow(backgroundId) {
    if (typeof( backgroundId ) == 'undefined') {
   		backgroundId =  'fadeBackground';
    }
	if (document.getElementById(backgroundId)) {
	  getWindowSize();
	  
	  height = document.body.clientHeight;
	 // if (winH > height) { height = winH; }
	
	  hideDropdowns();
	  document.getElementById(backgroundId).style.width = winW+'px';
	  document.getElementById(backgroundId).style.height = height+'px';
	  document.getElementById(backgroundId).style.visibility = 'visible';
	}
}

function fadeBackgroundHide(backgroundId) {

    if (typeof( backgroundId ) == 'undefined') {
   		backgroundId =  'fadeBackground';
    }
	if (document.getElementById(backgroundId)) {
	  showDropdowns();
	  document.getElementById(backgroundId).style.visibility = 'hidden';
	}
}


function hideDropdowns() {
  if (typeof (dropdownRegistry) != "undefined" ) {
	  for(keyVar in dropdownRegistry) {
	  	if(document.getElementById(dropdownRegistry[keyVar])) {
	      	document.getElementById(dropdownRegistry[keyVar]).style.visibility = 'hidden';
	  	}
	  }
  }
}

function showDropdowns() {
 if (typeof (dropdownRegistry) != "undefined" ) {
	  for(keyVar in dropdownRegistry) {
	  	if(document.getElementById(dropdownRegistry[keyVar])) {
	      	document.getElementById(dropdownRegistry[keyVar]).style.visibility = 'visible';
	    }
	  }
 }
}


