/*
 * qform.js: This file contains the script required at the render survey
 * Version: 	2.2 		1-MAY-2006
 * @author: 	Stephan Robberts
 * Copyright (c) 1995-2006 3M Worldwide.
 * All Rights Reserved.
 *
 * *********************************************************************
 * CONTENTS:
 * disableEnterKey() 					Disables the Enter Key
 * isblank(s)							Check for blank input values
 * checkOnSubmit(emailID)				Check format of e-mail if it is required
 * checkOnSubmit2(emailID)				Check format of e-mail if it is not required
 * removeSpace(src)						Remove spaces from the passed value
 * emailBadChars(string)				Check for bad characters in the passed valeu
 * isEmailOkay(valueCheck) 				Checks the format of e-mail address
 * toggle(cb)							Checks and unchecks a checkbox when passed
 * checkCoupons(capture)				Checks all radio & checkboxes for blanks
 * extractParam()						Pulls rp variable from header
 * parseURL(varName,URLEncodedTrigger)	Parses the URL for custom variables and URLdecoes the value
 * 
 * *********************************************************************


/* *********************************************************************
 * Quickform E-mail Functions 
 * *********************************************************************
*/


function disableEnterKey() { 
	if (window.event.keyCode == 13) {
	 window.event.keyCode = 0; 
	 }
} 


// Used to check for blank input fields
function isblank(s) {
	if ((s == null) || (s == "")) return true;
	for(var i = 0; i<s.length; i++)
	{
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}


/* Script to check the email - this utilizes the functions below it 
 * emailID is the e-mail input name (F*)
 */
function checkOnSubmit(emailID) {
	// Remove white space from the e-mail F input
	emailID.value = removeSpace(emailID.value);
	
	// Set input email to the F value
	document.qform.email.value = emailID.value;
	
	// Checks the E-mail field for Invalid Characters & Length
	if(isEmailOkay(emailID.value) == false){	
		emailID.focus();
		return false; 
	 }
	 
	// ****************************************************
	// CustomValidateFunction call added 12/30/04 by SM. 
	// To use: include lines like these in the PAGE_MSG.entry.header (enclosed in script tags):
	//	function myCustomValidate() {
	//		if(document.qform.F5.value == '') { 
	//			alert("Please enter a product type.");
	//			return false;
	//		}
	//	}	
	//	var customValidateFunction = "myCustomValidate()";
	// ****************************************************
	if(window.customValidateFunction) {
		customValidation = eval(customValidateFunction);
	}
	return customValidation;
	
	return true;
}

/* Script to check fields if e-mail is not required 
 * this utilizes the functions below it 
 * emailID is the e-mail input name (F*) 
 */ 
function checkOnSubmit2(emailID) {
	if (emailID.value != "") {
		emailID.value = removeSpace(emailID.value);
		document.qform.email.value = emailID.value;
		// Checks the E-mail field for Invalid Characters & Length
		if(isEmailOkay(emailID.value) == false){	
			emailID.focus();
			return false; 
		}
	} else {
		document.qform.email.value = "noreply@3m.com";
	}
	// CustomValidateFunction Check
	if(window.customValidateFunction) {
		customValidation = eval(customValidateFunction);
	}
	return customValidation;
	
	return true;
}


/* Script to remove the spaces in the string 
 * @param src ---string in which the spaces are to be removed
 */
function removeSpace(src) {
	var result="";
	var inPtr=0;
	// Loop to check the white spaces
	for(inPtr=0; inPtr < src.length; inPtr++) {
		if(src.charAt(inPtr) != ' ') {//check if the character is white space
			result+=src.charAt(inPtr);
		} // end of check for the character is white space
	} // End of loop
	return result;
}


/* Script to check the bad characters in the mail
 * @param string ----contains the mail string in which bad charaters 
                     are to be checked */  
function emailBadChars(string) {
	if (!string) return false;
	var iChars = "*|,\":<>[]{}`\';()&$#%";
	// Loop to check the string
	for (var i = 0; i < string.length; i++) {
		if (iChars.indexOf(string.charAt(i)) != -1) { //condition to check for bad chars
			return true;
		} // End of condition
	} // End of loop
	return false;
} // End of emailBadChars()                 



// Script to check whether the the email format is ok
function isEmailOkay(valueCheck) {
	var result = true;
	if (emailBadChars(valueCheck) == true) {	//Check for badcharacters
		   alert("We're sorry, but the email address you entered contains invalid characters.\r\n"+
			"Please re-enter your email address.");
		result = false;
	}	// End of check for bad characters
	else {	// Else for not having bad characeters
		if(valueCheck.length < 7) {				//check length of email
				alert("We're sorry, but the email address you entered does not contain enough characters to be valid.\r\n"+
				"Please re-enter your email address.");
				result = false;
		} // End of check length
		else {
		  if(valueCheck.indexOf('@') < 1
				|| valueCheck.lastIndexOf('.') < valueCheck.indexOf('@')+2
				|| valueCheck.lastIndexOf('.') == valueCheck.length - 1
				|| valueCheck.lastIndexOf('@') == valueCheck.length - 1)  {	 	//Check for  @, . in the email
			alert("We're sorry, but the email address you entered does not follow the pattern x@x.x\r\n"+
			"Please re-enter your email address.");
			result = false;
		  }// End of check for @ . in email
		}
	} // End of else for not having bad characters
	return result;
} // End of isEmailOkay()




/* *********************************************************************
 * Quickform Coupons Functions 
 * *********************************************************************
*/


/* Used in Coupon Malls - When the user clicks on a 
	 coupon image, this function changes the checkbox */
function toggle(cb) {
	var togglevar = document.getElementById(cb);
	if(togglevar.checked == true) { togglevar.checked  = false; }
	else if(togglevar.checked == false){ togglevar.checked = true; }
}

/* Takes a snapshot of the entire form and returns an
	alert if all check and radio boxes are blank */
function checkCoupons(capture) {
	var toNullEle = 0;
	for(i=0; i<document.qform.elements.length; i++) {
		var elm = document.qform.elements[i];
		// Check Checkbox & Radio Elements
		var toNullEle = ((elm.type == 'checkbox' || elm.type == 'radio') && (elm.checked == true)) ? toNullEle+1 : toNullEle;
	}
	// Any empty values - return an alert
	if(toNullEle == 0){
			alert('Please check at least one option before continuing.');
			return false;
	}
	// ****************************************************
	// To use: include lines like these in the PAGE_MSG.entry.header (enclosed in script tags):
	//	function myCustomValidate() {
	//		if(document.qform.F5.value == '') { 
	//			alert("Please enter a product type.");
	//			return false;
	//		}
	//	}	
	//	var customValidateFunction = "myCustomValidate()";
	// ****************************************************
	if(window.customValidateFunction) {
		customValidation = eval(customValidateFunction);
		return customValidation;
	}
	
	return true;

}


/* *********************************************************************
 * Other Functions 
 * *********************************************************************
*/

// Extract rp value from the URL for capture
function extractParam()  {
	var rp="";
	var parm="";
	var parmindex=0;
	if( document.location.search != "" ) {
		str = document.location.search;
		
		parmindex=str.indexOf("rp=");
		if(parmindex != '-1') {
		keyval=str.substr(parmindex).split("=");
		parm=keyval[1];
		} else {
		parm = "noparm";
		}
	} else {
		parm = "noparm";
	}
	return parm;
}


/* Parses the entire URL and returns the value of the specified variable
   format:  parseURL('variableName',URLEncoded?(1=yes, 0=no));
   example URL: ...?storeName=3M%20Company&otherParam=value
   parseURL('storeName',1) = 3M Company
   parseURL('storeName',0) = 3M%20Company

 */
function parseURL(varName,URLEncodedTrigger) {
	varValue = '';
	// The first line and first if statement ensure that the variable name isn't part of another variable
	var pageURI = document.location.href.split('?'+varName+"=");
	if (pageURI.length <= 1) {
		pageURI = document.location.href.split('&'+varName+"=")
	}
	
	// If it passes, find the value and URLDecode it
	if (pageURI.length > 1) {
		varValue = pageURI[1].split("&");
	}
	if (varValue[0] && URLEncodedTrigger) {
		return unescape(varValue[0]);
	} else if (varValue[0] && !URLEncodedTrigger) {
		return varValue[0];
	} else {
		return '';
	}
}






// End of qform2.js