/**
* @package common.js
* @desc Most Commonly Used JavaScript Functions
* @copyright ITExchange, a Division of BrainBay Consulting.
*/

/**
* @name validateEmail
* @desc Validates E-Mail Address. Can be used with TextBox.
*
* @param element element Reference to the TextBox to be validated. e.g. document.form1.emailid
* @param string msg Name/Label of the Field. e.g "E-Mail Address"
* @return bool Returns true if the e-mail address is valid; false otherwise.
*
*/
function validateEmail( element, msg )
{
	if(element.value != "")
	{
		var reg = /^[-a-zA-Z0-9!#$%&'*+\/=?^_`{|}~]+(\.[-a-zA-Z0-9!#$%&'*+\/=?^_`{|}~]+)*@(([a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9]+)?){1,63}\.)+([a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9]+)?){2,63}$/;
		if(!reg.test( element.value ))
		{
			alert("Please enter valid "+msg);
			element.focus();	
			return false;
		}
		else
			return true;		
	}
	else
	{
 		alert("Please enter "+msg);
		element.focus();	
		return false;
	}	
}

/**
* @name validateName
* @desc Validates Human Names. Can be used with TextBox.
*
* @param element element Reference to the TextBox to be validated. e.g. document.form1.firstname
* @param string msg Name/Label of the Field. e.g "Firstname"
* @return bool Returns true if the name is valid; false otherwise.
*
*/
function validateName( element, msg )
{
	
	if(element.value != "")
	{
 		var reg = /[a-zA-Z\x2D.\s]$/;
		if(!reg.test( element.value ))
		{
			alert("Please enter valid "+msg);
			element.focus();	
			return false;
		}
		else
			return true;
	}
	else
	{
 		alert("Please enter "+msg);
		element.focus();
		return false;
	}
}

/**
* @name validatePassword
* @desc Validates Passwords. Can be used with Password fields/TextBoxes. This is portal specific.
*
* @param element element Reference to the TextBox/Password field to be validated. e.g. document.form1.password
* @param string msg Name/Label of the Field. e.g "Password"
* @return bool Returns true if the password is valid; false otherwise.
*
*/
function validatePassword( element, msg )
{
	if(element.value != "")
	{
		var reg = /[\da-zA-Z\x2D_,.!?]+$/;
		if(!reg.test( element.value ))
		{
			alert("Please enter valid "+msg);
			element.focus();	
			return false;
		}
		else
			return true;
	}
	else
	{
 		alert("Please enter "+msg);
		element.focus();
		return false;
	}
}

/**
* @name validateCity
* @desc Validates Citi/State names. Can be used with TextBoxes/Combo Boxes. This is portal specific.
*
* @param element element Reference to the TextBox to be validated. e.g. document.form1.billing_city
* @param string msg Name/Label of the Field. e.g "City"
* @return bool Returns true if the City/State name is valid; false otherwise.
*
*/
function validateCity(element,msg)
{
	if(isBlank(element.value))
	{
		alert("Please enter the "+ msg);
		element.focus();
		return 0;
	}
	else
	{
	var alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";		
				for (var i=0;i<element.value.length;i++)
				{
					temp=element.value.substring(i,i+1);
					if (alp.indexOf(temp)==-1)
					{
						alert("No special characters \nValid entries are [a-z][A-Z]");
						element.focus();
						return 0;
					}
				}
	}
				
	return 1;
}

function validCity(element,msg)
{
	if(isBlank(element.value))
	{
		alert("Please enter the "+ msg);
		element.focus();
		return 0;
	}
	else
	{
	var alp = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ, ";		
				for (var i=0;i<element.value.length;i++)
				{
					temp=element.value.substring(i,i+1);
					if (alp.indexOf(temp)==-1)
					{
						alert("No special characters \nValid entries are [a-z][A-Z]");
						element.focus();
						return 0;
					}
				}
	}
				
	return 1;
}

/**
* @name getSelRadIndex
* @desc Returns the selected index of a given Radio Button Group.
*
* @param element radgroup Reference to the Radiogroup. e.g. document.form1.gender
* @return signed Returns index of the selected radio button; -1 otherwise.
*
*/
function getSelRadIndex(radgroup)
{
	/* Returns the id of selected radio button in a radio button group  */
	var re = -1;
 	if(radgroup.length)   
	{
		for( pe=0; pe < radgroup.length; pe++ )
		{
			if(radgroup[pe].checked)
			{
				re = pe;
			}
		}
	}
	return re;
}

/**
* @name isBlank
* @desc Checks if the given string is blank. This function doesn't count spaces and newslines.
*
* @param string txt Text to be checked. e.g. document.form1.comments.value
* @param int minlen Minimum length of the string.
* e.g isBlank(document.form1.comments.value, 4)
* This call tells the function to check if comments.value has atleast 4 alphanumeric characters in it.
* @return bool Returns true if the given string is blank; false otherwise.
*
*/
function isBlank(txt, minlen)
{
	if( txt.length == getCountOf('\n', txt) )
	{
		// This condition avoids the entry of just newlines in text areas.
		return true;
	}
	if( txt.length == getCountOf(' ', txt) || txt.length == 0 )
	{
		return true;
	}
	else if( minlen > 0 )
	{
		if( txt.length < minlen )
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	else
	{
		return false;
	}
	return true;
}

/**
* @name isBlank
* @desc Counts the number of occurances of a given character in a string.
*
* @param char vChr Character to be counted.
* @param string txt String in which vChr has to be checked.
* e.g getCountOf('b', "brainbay") would return 2
*
* @return int number of vChr's occurances in txt; 0 if not found.
*
*/
function getCountOf(vChr, txt)
{
	var i = 0;
	var iCount = 0;
	for( i=0; i < txt.length; i++ )
	{
		if( txt.charAt(i) == vChr )
		{
			iCount++;
		}
	}
	return iCount;
}

/**
* @name showStatus
* @desc Show txt in Browser's status bar
*
* @param string txt Status text to be shown.
* e.g showStatus('Processing...')
*
* @return none.
*
*/
function showStatus(txt)
{
	window.status = txt;
}

function isValidAddress(element,msg) 
{
	if(element.value.length == 0)
	{
		alert("Please enter the "+ msg);
		element.focus();
		return false;
	}
	else if(isBlank(element.value))
	{
		alert("Please enter the "+ msg);
		element.focus();
		return false;
	}
	return true;
}

function checkInCharSet(txt, charset)
		{
			/*
				This function checks if the characters in a given text are part of a given character set.
		
				INPUT:	Text ti be verified [txt]
							String of character that forms the reference [charset]
		
				OUTPUT: Returns true if all of the characters in txt are part of charset, else false.
		
				USAGE:
							for example:
		
								checkInCharSet( "guru", "aeiouAEIOU" ) this fucntion returns false as "guru" contains 'g' and 'r'
								whcih are not part of "aeiouAEIOU".
		
								checkInCharSet( "abC", "abcdefABCDEF" ) this statement returns true as all "abC" contains characters
								that are present in "abcdefABCDEF"
			*/
		
			var b = true;
		
			for(i = 0; i < txt.length; i++ )
			{
				if( charset.indexOf(txt.charAt(i)) == -1 )
				{
					b = false;
				}
			}
		
			return b;
		}


function creditcardValidation(element,msg)
{
  if(element.value.length == 0)
  {
         alert("Please enter the "+ msg);
		element.focus();
		return false;
   }
  else if((checkInCharSet( element.value, "0123456789" ))== 0)
	{
        alert("Enter Only Numeric Data");   
		element.focus();
		return false;
	 }
   
	 else if(element.value.length < 13)
	  {
	 alert("Creditcard number should have minimum 13 digits");
	 element.focus();
	 return false;
	 }
	 else  if(element.value.length  > 20)
	 {
	 alert("Creditcard number should have maximum 20 digits");
	 element.focus();
	 return false;
	 }
}//closing for creditcard number validation
     

function creditcardholdernameValidation(element,msg)
{
if(element.value.length == 0)
	{
		alert("Please enter the "+ msg);
		element.focus();
		return false;
	}
	else if((checkInCharSet( element.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 " ))== 0)
	{
    alert("Please enter valid "+ msg);   
		element.focus();
		return false;
	 }
	else if(isBlank(element.value,4))
	{
		alert(msg +" must be atleast 4 characters");
		element.focus();
		return false;
	}
	return true;
}//closing for creditcardholdername validation


function cvvValidation(element,msg)
{
  if(element.value.length == 0)
  {
    alert("Please enter the "+ msg);
		element.focus();
		return false;
   }
  else if((checkInCharSet( element.value, "0123456789" ))== 0)
	{
    alert("Enter Only Numeric Data");   
		element.focus();
		return false;
	 }
	 else if(element.value.length < 3)
	  {
	 alert("CVV should have minimum 3 digits");
	 element.focus();
	 return false;
	 }
	 else  if(element.value.length  > 5)
	 {
	 alert("CVV should have maximum 5 digits");
	 element.focus();
	 return false;
	 }
}//closing for creditcard number validation


function TextareaValidation(elem,msg) 
{
	
		 	if(isBlank(elem.value)) 
			{
				alert("Please enter"+msg);
				elem.focus();
				return 0;
			}
		
} // closing the function TextareaValidation()

function validName(element,msg)
{
  if(element.value.length == 0)
  {
         alert("Please enter the "+ msg);
		 element.focus();
		return false;
   }
  else if((checkInCharSet( element.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' " ))== 0)
	{
        alert("Please enter valid "+ msg);   
		element.focus();
		return false;
	 }
}//closing the function validName

function validZip(element,msg)
{
  if(element.value.length == 0)
  {
         alert("Please enter the "+ msg);
		 element.focus();
		return false;
   }
  else if((checkInCharSet( element.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- " ))== 0)
	{
        alert("Please enter valid "+ msg);   
		element.focus();
		return false;
	 }
	else if(element.value.length > 10)
	 {
	 alert("Zipcode cannot be morethan 10 characters");
	 element.focus();
	 return false;
	 }
}//closing the validZip function
