/*

	To Do:  strengthen the checks on email to check for embedded spaces and multiple @ 
			symbols.

*/

//======================================================================
// Name:		Is Valid Date
//
// Function:	Validates a date to make sure it is valid.
//
// Arguments:	month	any number from 1 to 12.
//				day		any valid number from 1 to n (where n is the
//						appropriate number of days for month).
//				year	any number.
//======================================================================
function isValidDate(month, day, year) {

	var maxDays = new Array(31, 28, 31, 30, 31, 30, 
							31, 31, 30, 31, 30, 31);

	if (month) {
	
		if (isNumeric(month)) {
		
			if (year) {
			
				if (isNumeric(year)) {
				
					if (day) {
					
						if (isNumeric(day)) {
											
							if (parseInt(year) % 4 == 0) {
								maxDays[1] = 29;
							}

							if (parseInt(month) > 0 && parseInt(month) < 13) {
								
								if (parseInt(day) > 0 && parseInt(day) <= maxDays[parseInt(month) - 1]) {
								
									return true;
								
								} else {
									// day is invalid 
									return false;
								}	
	
							} else {
								// month is invalid
								return false;
							}
						} else {
							// day is not a number
							return false;
						}
					} else {
						// day was not provided
						return false;
					}
				} else {
					// year is not numeric
					return false;
				}
			} else {
				// year was not provided
				return false;
			}
		} else {
			// month is not numeric
			return false;
		}
	} else {
		// month was not provided
		return false;
	}						
}

//======================================================================
// Name:		Is Valid E-Mail
//
// Function:	Validates an e-mail address to make sure it follows 
//				e-mail naming conventions.  E-Mail address should contain
//				a user name, followed by an @ sign, followed by a domain
//				(i.e.: name@domain).
//
// Arguments:	E-Mail address to evaluate.
//======================================================================
function isValidEmail(emailX) {

	var pos = emailX.indexOf("@");
	
	if (pos == -1) {
		return false;
	}else if (pos == 0) {
		return false;
	}else if (pos == (emailX.length - 1) ) {
		return false;
	} else {
		return true;
	}
	
		
}


//======================================================================
// Name:		Is RFC822 Valid E-Mail
//
// Function:	Validates an e-mail address to make sure it enforce RFC822 syntax 
//		RFC822 is the document that specifies the syntax of standard internet email messages
//
// Arguments:	E-Mail address to evaluate.
//======================================================================
function isRFC822ValidEmail(sEmail) {

	var sQtext = '[^\\x0d\\x22\\x5c\\x80-\\xff]';
	var sDtext = '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
	var sAtom = '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
	var sQuotedPair = '\\x5c[\\x00-\\x7f]';
	var sDomainLiteral = '\\x5b(' + sDtext + '|' + sQuotedPair + ')*\\x5d';
	var sQuotedString = '\\x22(' + sQtext + '|' + sQuotedPair + ')*\\x22';
	var sDomain_ref = sAtom;
	var sSubDomain = '(' + sDomain_ref + '|' + sDomainLiteral + ')';
	var sWord = '(' + sAtom + '|' + sQuotedString + ')';
	var sDomain = sSubDomain + '(\\x2e' + sSubDomain + ')*';
	var sLocalPart = sWord + '(\\x2e' + sWord + ')*';
	var sAddrSpec = sLocalPart + '\\x40' + sDomain; // complete RFC822 email address spec
	var sValidEmail = '^' + sAddrSpec + '$'; // as whole string
	  
	var reValidEmail = new RegExp(sValidEmail);
	  
	if (reValidEmail.test(sEmail)) {
	  return true;
	}
	  
	return false;
}	


//======================================================================
// Name:		Is Numeric
//
// Function:	Confirms that a string contains only numbers.
//
// Arguments:	String to evaluate.
//======================================================================
function isNumeric(strValue) {
	
	
	for(var i = 0; i < strValue.length; i++) {
	
		if (strValue.substring(i, i+1) < '0' || 
			strValue.substring(i, i+1) > '9' ) {
			return false;
		}	
	}
	return true;
}


//======================================================================
// Name:		LTrim
//
// Function:	Trims leading spaces from a string.
//
// Arguments:	String to parse.
//======================================================================
function lTrim(strValue) {

	if (typeof strValue != "undefined") {
	
		for(var i = 0; i < strValue.length; i++) {
	
			if (strValue.substring(i, i+1) != " ") {
				return strValue.substring(i, strValue.length);
			}
		}
	} else {
		return "";
	}
}


//======================================================================
// Name:		RTrim
//
// Function:	Trims trailing spaces from a string.
//
// Arguments:	String to parse.
//======================================================================
function rTrim(strValue) {

	if (typeof strValue != "undefined") {
	
		for(var i = strValue.length -1; i >= 0; i--) {
	
			if (strValue.substring(i, i+1) != " ") {
				return strValue.substring(0, i+1);
			}
		}
	
	} else {
		return "";
	}
}


//======================================================================
// Name:		Trim
//
// Function:	Removes leading and trailing spaces from a string.
//
// Arguments:	String to parse.
//======================================================================
function trim(strValue) {

	if (typeof strValue != "undefined") {

		var strWork = new String();
	
		strWork = lTrim(strValue);
		
		if (typeof strWork != "undefined") {
		
			strWork = rTrim(strWork);
			return strWork;
		
		} else {
			return "";
		}	
	} else {
		return "";
	}	
}

//======================================================================
// Name:		Parse Numbers From String
//
// Function:	Pulls all the numbers out of a string.  This is useful
//				for dealing with strings that may be formatted in a 
//				number of ways, but which tend to contain a fixed number
//			    of digits (such as a zip code or phone number).
//
// Arguments:	String to parse.
//======================================================================
function parseNumbersFromString(strIn) {
	
	var work = new String();
	var charX = new String();
		
	for (var i = 0; i < strIn.length; i++) {
		
		charX = strIn.substring(i, i + 1);
			
		if (charX >= '0' && charX <= '9') {
			work += charX;			
		}
	}

	return work;
}



function formatCurrency(val) {
	var dlr = new String("0");
	var dec = new String("00");
	var pos = -1;
	val = "" + val;	// Make sure "val" is treated as a string.

	if (isNumeric(val)) {
		pos = val.indexOf(".");

		if (pos != -1) {
			dlr = val.substring(0, pos);
			dec = val.substring(pos + 1, val.length);
		} else {
			dlr = val;
		}
		
		dec += "00";
		dec = dec.substring(0, 2);

		if (dlr.length == 0) {
			dlr = "0";
		} else {
			var cnt = 1;
			var wrk = new String("");

			for (var idx = dlr.length - 1; idx >= 0; idx--)	{
				if (cnt % 4 == 0) {
					wrk = "," + wrk;
				}
				wrk = dlr.charAt(idx) + wrk;
			}

			dlr = wrk;
		}
		return "$" + dlr + "." + dec;
	} else {
		return "$0.00";
	}
}


function isNumeric(val) {
	var prdFnd = false;
	val = "" + val;	// Make sure "val" is treated as a string.
	
	for (var pos = 0; pos < val.length; pos++) {
		if (val.charAt(pos) < "0" ||
			val.charAt(pos) > "9") {
			if (val.charAt(pos) == ".") {
				if (prdFnd) {
					return false;
				} else {
					prdFnd = true;
				}
			} else {
				return false;
			}
		}
	}
	return true;
}

function formatCurrencyDecimals(pnumber,decimals) 
	{
	  var strNumber = new String(pnumber);
	  var arrParts = strNumber.split('.');
	  var intWholePart = parseInt(arrParts[0],10);
	  var strResult = '';
	  if (isNaN(intWholePart))
    	intWholePart = '0';
	  if(arrParts.length > 1)
	  {
    	var decDecimalPart = new String(arrParts[1]);
	    var i = 0;
    	var intZeroCount = 0;
	    while ( i < String(arrParts[1]).length )
    	{
			if( parseInt(String(arrParts[1]).charAt(i),10) == 0 )
       		{
         		intZeroCount += 1;
         		i += 1;
       		}
       		else
		         break;
		    }
		    decDecimalPart = parseInt(decDecimalPart,10)/Math.pow(10,parseInt(decDecimalPart.length-decimals-1)); 
		    Math.round(decDecimalPart); 
		    decDecimalPart = parseInt(decDecimalPart)/10; 
		    decDecimalPart = Math.round(decDecimalPart); 

		    //If the number was rounded up from 9 to 10, and it was for 1 'decimal' 
		    //then we need to add 1 to the 'intWholePart' and set the decDecimalPart to 0. 

		    if(decDecimalPart==Math.pow(10, parseInt(decimals)))
		    { 
		      intWholePart+=1; 
		      decDecimalPart="0"; 
		    } 
		    var stringOfZeros = new String('');
		    i=0;
		    if( decDecimalPart > 0 )
		    {
		      while( i < intZeroCount)
		      {
        		stringOfZeros += '0';
		        i += 1;
		      }	
		    }
		    decDecimalPart = String(intWholePart) + "." + stringOfZeros + String(decDecimalPart); 
		    var dot = decDecimalPart.indexOf('.');
		    if(dot == -1)
		    {
		      decDecimalPart += '.'; 
		      dot = decDecimalPart.indexOf('.'); 
		    } 
		    var l=parseInt(dot)+parseInt(decimals); 
		    while(decDecimalPart.length <= l) 
		    {
			   decDecimalPart += '0'; 
		    }
		    strResult = decDecimalPart;
		  }
		  else
		  {
		    var dot; 
		    var decDecimalPart = new String(intWholePart); 

		    decDecimalPart += '.'; 
		    dot = decDecimalPart.indexOf('.'); 
		    var l=parseInt(dot)+parseInt(decimals); 
		    while(decDecimalPart.length <= l) 
		    {
		      decDecimalPart += '0'; 
		    }
		    strResult = decDecimalPart;
		  }
		  
		  strResult = '$' + strResult;
		  
		  return strResult;
	}

function alignLayer(imageName, layerName) {
	var img;
	for( var i = 0; i < document.images.length; i++ ) {
		if( document.images[i].name == imageName ) {
			img = document.images[i];
		}
	}
	
	var browsername=navigator.appName;
	var browserversion= navigator.appVersion;


	if (document.layers) {
		lyr = document.layers[layerName];
		lyr.left = img.x;
		lyr.top = img.y;
	} else if (document.all){
		lyr = eval("document.all."+ layerName );
		var left = 0;
		var top = 0;

		x = img;

		while (x != null) {
			left = left + x.offsetLeft;
			top = top + x.offsetTop;
			x = x.offsetParent;
		};

		top = top + 1;
		lyr.style.visibility = "hidden";
		 
		lyr.style.left = left;
		lyr.style.top = top-48;
		lyr.style.visibility = "visible";
			
	} else {
		lyr = document.getElementById(layerName);
		lyr.style.left = img.x;
		lyr.style.top = img.y + 3;

		var left = 0;
		var top = 0;

		x = img;

		while (x != null) {
			left = left + x.offsetLeft;
			if( (browsername.indexOf("Microsoft")!=-1) 	
				&& (navigator.appVersion.indexOf("MSIE 5.0")!=-1) ) 
			{	
				top = top + x.offsetTop;
			} else {
				top = top + x.offsetTop;
			}
			x = x.offsetParent;
		};
		lyr.style.left = left;
		lyr.style.top = top;

	}
}

function replaceParamValue( startUrl, paramName, newValue ) {
	var i =	startUrl.indexOf( paramName + "=" );
	var newUrl;
	if ( i > -1 ) {
		newUrl = startUrl.substring( 0, i + paramName.length + 1 ) + newValue;
		var j = startUrl.indexOf( "&", i );
		if( j > -1 ) {
			newUrl = newUrl + startUrl.substring( j );
		}
	} else {
		if( startUrl.indexOf( '?' ) > -1 ) {
			newUrl = startUrl + "&" + paramName + "=" + newValue;
		} else {
			newUrl = startUrl + "?" + paramName + "=" + newValue;
		}
	}
	return newUrl;
}

function removeParam( startUrl, paramName ) {
	var i =	startUrl.indexOf( paramName + "=" );
	if( i > -1 ) {
		var questIndex = startUrl.indexOf( "?" );
		var questAdded = false;
		var newUrl = startUrl.substring( 0, questIndex );
		var j = startUrl.indexOf( "&", questIndex );
		if( j == -1 ) {
			return newUrl;
		}
		
		j = questIndex + 1;
		var counter = 0;
		var separator = "?";
		do {
			var endIndex = startUrl.indexOf( "&", j );
			var paramNameAndValue = ''
			if( endIndex == -1 ) {
				paramNameAndValue = startUrl.substring( j );
			} else {
				paramNameAndValue = startUrl.substring( j, endIndex );
			}
			if( paramNameAndValue.indexOf( paramName + "=" ) == -1 ) {
				newUrl = newUrl + separator + paramNameAndValue;
				if( questAdded == false ) {
					questAdded = true;
					separator = "&";
				}
			}
			
			if( endIndex == -1 ) {
				return newUrl;
			} else {
				j = endIndex + 1;
			}
			
			counter++;
			if( counter == 30 ) {
				return newUrl;
			}
		} while( j > -1 )
		
		return newUrl;
	} else {
		return startUrl;
	}
}

function clearPurchasePlanAndFilter() {
	currUrl = self.location.href;
	currUrl = removeParam( currUrl, 'pplan');
	currUrl = removeParam( currUrl, 'splan');
	currUrl = removeParam( currUrl, 'showFilterItems');
	currUrl = removeParam( currUrl, 'filter');
	self.location.href=currUrl;
}