// JavaScript Document
	/*
	# eLuminous Technologies - Copyright (C)  http://eluminoustechnologies.com 
	# This code is written by eLuminous Technologies, Its a sole property of 
	# eLuminous Technologies and cant be used / modified without license.  
	# Any changes/ alterations, illegal uses, unlawful distribution, copying is strictly
	# prohibhited 
	# Name: form_validator.js
	# Usage: included the file to validate the contents of the form. ( WRITE USAGE DETAILS ) 
	# Created : Rupal Pinge & Sham Shriwastav (30-05-2007)
	# Update  : 30-05-2007 Sham Shriwastav 
	# Status  : open
	# Purpose : make javascript code seprate from other coding
	*/
	function isFilled(str){ return (str != ""); }

	function isEmail(string) { return (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1); }

 	//function isDigital(str)	{ return(parseInt(str,10)==(str*1)); /*return(parseFloat(str,10)==(str*1));*/ }
	function isDigital(str)	{
		if(str==null){return false;}
		if (str.length==0){return false;}
		for (var i = 0; i < str.length; i++) {
			var ch = str.charAt(i);
			if (ch < "0" || ch > "9") {
			return false;
			}
		}
		return true;	
	}
	
	function isFloat(str)	{
		if(str==null){return false;}
		if (str.length==0){return false;}
		for (var i = 0; i < str.length; i++) {
			var ch = str.charAt(i);
			if(ch == '-')
				return false;
			if(ch == '.')
				continue;
			if (ch < "0" || ch > "9") {
			return false;
			}
		}
		return true;	
	}
	
 	function isCurrency(val) { 	var re = /^(\$?\d+\$?|\$?\d+\.\d+\$?)$/; return (re.test(val)); }
	
function validate_float(floats)
{
	var field, i;
	var floats_len = parseInt(floats.length);
	
	for (i=0;i<floats_len;i++)
	{

		var field = document.getElementById(floats[i]);
			field.style.border="1px solid #E5E5E5";
		if ( field.value !="")
		{
			if (!isFloat(trim(field.value))) 
			{
			alert("Please fill only digits(0-9), value must be positive.");
			field.style.border="1px solid #FF0000";
			field.focus();
			return false;
				break;
			}	
		}
	}
	
}

function validate(req,email,digits,currs)
{
	var field, i;
	var req_len = parseInt(req.length);
	var email_len = parseInt(email.length);
	var digits_len = parseInt(digits.length);
	var currs_len = parseInt(currs.length);
	
	for (i=0;i<req_len;i++)
	{
			var field = document.getElementById(req[i]);
			if (field)
			{
				//field.style.border="1px solid #000000";
				//field.style.border="1px solid #E5E5E5";
				if ((field.type == 'checkbox')||(field.type == 'radio'))
				{
					var field = document.getElementsByName(req[i]);
					var chk = false;
					for(l=0;l<field.length;l++)
					{
						if (field[l].checked) chk = true;
					}
					if (!chk) 
					{
						//alert("Field '" + field[0].title + "' is required to be checked correctly before successful submission.");
						//alert ("Please select " + field[0].title+"." );
						alert ("Please check "+field[0].title);
						field[0].style.border="1px solid #FF0000";
						return false; 
						break;
					}
				}
				else
				{
					if (!isFilled(trim(field.value)))
						{
						//alert("Field '" + field.title + "' is required to be filled in before successful submission.");
						//alert ("Please fill " + field.title+"." );
						alert (field.title);
						field.value="";
						field.focus();
						field.style.border="1px solid #FF0000";
						return false;
						// break;
						}
					else if(its_whitespace(field.value)==false)
						{
						alert (field.title);
						field.value="";
						field.focus();
						field.style.border="1px solid #FF0000";
						return false;
						// break;
						}

				}
			}
			else
				alert(req[i] + " does not exist" );		
	}


		for (i=0;i<email_len;i++)
		{
			var field = document.getElementById(email[i]);
			//field.style.border="1px solid #000000";
			field.style.border="1px solid #E5E5E5";
			if (!isEmail(trim(field.value)))
			 {
				//alert("Field '" + field.title + "' is required to be filled in with valid email addresses before successful submission.");
				alert("Please fill up valid email address.");
				field.style.border="1px solid #FF0000";
				field.value="";
				field.focus();
				return false;
				break;
			}
		}

		for (i=0;i<digits_len;i++)
		{

			var field = document.getElementById(digits[i]);
				//field.style.border="1px solid #000000";
				field.style.border="1px solid #E5E5E5";
			if ( field.value !="")
			{
				if (!isDigital(trim(field.value))) 
				{
					//alert("Field " + field.title + " is required to be filled in only with digits (0-9) and decimal point before successful submission.");
					//alert("Please fill only digits(0-9) and decimal point in "+field.title + ".");
					alert("Please fill only digits(0-9), value must be positive.");
					field.style.border="1px solid #FF0000";
					field.focus();
					return false;
					break;
				}	
			}
		}

		for (i=0;i<currs_len;i++)	{

			var field = document.getElementById(currs[i]);
				field.style.border="1px solid #000000";
			if (!isCurrency(trim(field.value))) {

				//alert("Field " + field.title + " is required to be filled in only with digits (0-9) a decimal point, or a dollar sign before successful submission.");
				alert("Please fill only digits(0-9) and decimal point in "+field.title + ".");
				field.style.border="1px solid #FF0000";
				field.focus();
				return false;
				break;

			}}

		return true;
}
	
	
//-- trim functions
// Removes Leading whitespaces
  function LTrim( value )
			{
				var re = /\s*((\S+\s*)*)/;
				return value.replace(re, "$1");
				
			}

// Removes ending whitespaces
		function RTrim( value )
			{
				var re = /((\s*\S+)*)\s*/;
				return value.replace(re, "$1");
				
			}

// Removes leading and ending whitespaces
	function trim( value ) 
		{
			return LTrim(RTrim(value));
		}

// Script by hscripts.com 
function checkDomain(nname)
{
var arr = new Array(
'.com','.net','.org','.biz','.coop','.info','.museum','.name',
'.pro','.edu','.gov','.int','.mil','.ac','.ad','.ae','.af','.ag',
'.ai','.al','.am','.an','.ao','.aq','.ar','.as','.at','.au','.aw',
'.az','.ba','.bb','.bd','.be','.bf','.bg','.bh','.bi','.bj','.bm',
'.bn','.bo','.br','.bs','.bt','.bv','.bw','.by','.bz','.ca','.cc',
'.cd','.cf','.cg','.ch','.ci','.ck','.cl','.cm','.cn','.co','.cr',
'.cu','.cv','.cx','.cy','.cz','.de','.dj','.dk','.dm','.do','.dz',
'.ec','.ee','.eg','.eh','.er','.es','.et','.fi','.fj','.fk','.fm',
'.fo','.fr','.ga','.gd','.ge','.gf','.gg','.gh','.gi','.gl','.gm',
'.gn','.gp','.gq','.gr','.gs','.gt','.gu','.gv','.gy','.hk','.hm',
'.hn','.hr','.ht','.hu','.id','.ie','.il','.im','.in','.io','.iq',
'.ir','.is','.it','.je','.jm','.jo','.jp','.ke','.kg','.kh','.ki',
'.km','.kn','.kp','.kr','.kw','.ky','.kz','.la','.lb','.lc','.li',
'.lk','.lr','.ls','.lt','.lu','.lv','.ly','.ma','.mc','.md','.mg',
'.mh','.mk','.ml','.mm','.mn','.mo','.mp','.mq','.mr','.ms','.mt',
'.mu','.mv','.mw','.mx','.my','.mz','.na','.nc','.ne','.nf','.ng',
'.ni','.nl','.no','.np','.nr','.nu','.nz','.om','.pa','.pe','.pf',
'.pg','.ph','.pk','.pl','.pm','.pn','.pr','.ps','.pt','.pw','.py',
'.qa','.re','.ro','.rw','.ru','.sa','.sb','.sc','.sd','.se','.sg',
'.sh','.si','.sj','.sk','.sl','.sm','.sn','.so','.sr','.st','.sv',
'.sy','.sz','.tc','.td','.tf','.tg','.th','.tj','.tk','.tm','.tn',
'.to','.tp','.tr','.tt','.tv','.tw','.tz','.ua','.ug','.uk','.um',
'.us','.uy','.uz','.va','.vc','.ve','.vg','.vi','.vn','.vu','.ws',
'.wf','.ye','.yt','.yu','.za','.zm','.zw');

var mai = nname;
var val = true;

var dot = mai.lastIndexOf(".");
var dname = mai.substring(0,dot);
var ext = mai.substring(dot,mai.length);
//alert(ext);
if(dot>2 && dot<57)
{
	for(var i=0; i<arr.length; i++)
	{
	  if(ext == arr[i])
	  {
	 	val = true;
		break;
	  }	
	  else
	  {
	 	val = false;
	  }
	}
	if(val == false)
	{
	  	 alert("Your domain extension "+ext+" is not correct");
		 return false;
	}
	else
	{
		for(var j=0; j<dname.length; j++)
		{
		  var dh = dname.charAt(j);
		  var hh = dh.charCodeAt(0);
		  if((hh > 47 && hh<59) || (hh > 64 && hh<91) || (hh > 96 && hh<123) || hh==45 || hh==46)
		  {
			 if((j==0 || j==dname.length-1) && hh == 45)	
		  	 {
		 	  	 alert("Domain name should not begin are end with '-'");
			      return false;
		 	 }
		  }
		else	{
		  	 alert("Your domain name should not have special characters");
			 return false;
		  }
		}
	}
}
else    
{
	 alert("Invalid Domain name");
	 return false;
}	

return true;
}
// Script by hscripts.com 		


// function to check valid ip address
// Original:  Jay Bienvenu 
// Web Site:  http://www.bienvenu.net 
	
function verifyIP (IPvalue) {
	errorString = "";
	theName = "IPaddress";
	
	var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = IPvalue.match(ipPattern);
	
	if (IPvalue == "0.0.0.0")
	errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	else if (IPvalue == "255.255.255.255")
	errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	if (ipArray == null)
	errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
	else {
		for (i = 0; i < 5; i++) {
			thisSegment = ipArray[i];
			if (thisSegment > 255) {
				errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
				i = 5;
			}
			if ((i == 0) && (thisSegment > 255)) {
				errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
				i = 5;
			}
		}
	}
	extensionLength = 3;
	if (errorString != "")
	alert (errorString);
}	
	


// check the url for validation 
function checkUrl(field)
{
	//-- field is object
  	/* This is the main function which was used in crocads...
	theUrl=field.value;
	//if(theUrl.match(/^(http)\:\/\/\w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i))
	if(theUrl.match(/http:\/\/[A-Za-z0-9_\.\[\]\?-]{3,}\.[A-Za-z]{3}/))
		{
		   //alert("valid address.");
			return true;
		} 
	 else 
		{
			//alert("Wrong address.");
			alert("Please fill in valid URL.");
			field.style.border="1px solid #FF0000";
			field.focus();
			return false;
		}
	*/	
	return true;
}	

//Function to check valid numeric value
function is_numeric(num)
	{
		var exp = new RegExp("^[0-9-.]*$","g");
		return exp.test(num);
	}
	
	

//-- function added by Darshana  Aher on 10th Dec 2007---//
	
//function to check whether only spaces are entered
function its_whitespace(string_value) 
{
	  // These are the whitespace characters
	  var whitespace = " \n\r\t"
	
	  // Run through each character in the string
	  for (var counter = 0; counter < string_value.length; counter++) 
	  {
			// Get the current character
			current_char = string_value.charAt(counter)
			
			// If it's not in the whitespace characters string
			if (whitespace.indexOf(current_char) == -1) 
			{
			  return true;
			}
	 }
  // Otherwise, the string has nothing but whitespace characters, so return false
  return false;
}//function its_whitespace(string_value)

// Function to check start date > curr date
function CompareContestDates(str1,str2)
{
    var dt1   = parseInt(str1.substring(8,10),10);
    
    var mon1  = parseInt(str1.substring(6,8),10);
 
    var yr1   = parseInt(str1.substring(0,5),10);
   
    var dt2   = parseInt(str2.substring(8,10),10);
 
    var mon2  = parseInt(str2.substring(6,8),10);
 
    var yr2   = parseInt(str2.substring(0,5),10);
 
    var start_date = new Date(yr1, mon1, dt1);
 
    var end_date = new Date(yr2, mon2, dt2);
    if(start_date < end_date)
    {
		//alert("Please fill in valid date.");
		return false; 
    } 
	 return true;  
}

/*
//Function to check phone no
function checkPhone(field)
{
	var ph=field.value;
	var ch=/^([\(]?\s?\d+\s?[\)]?\s?[-]?\s?)+$/;
	if(!ch.test(ph))
	{
		alert ("Please fill up valid phone number.");
		field.value="";
		field.focus();
		field.style.border="1px solid #FF0000";
		return false
	}
	else
	 	return true;	
}
*/


function checkPhone(cnt_id)
{  
	
   //var phone =trim(document.getElementById(cnt_id).value);
   var phone =trim(cnt_id.value);
   
    var len  =phone.length;
   
    var str_accept ="0123456789+-()";
    if (phone != "")
    {
     if (phone <= 0)
     {
      	alert ("Please enter valid phone number."); 
		cnt_id.focus();
		cnt_id.style.border="1px solid #FF0000";
	    return false; 
     } 
 	 else if (len < 6)
     { 
      alert ("Please enter minimum 6 digits for phone number."); 
		cnt_id.focus();
		cnt_id.style.border="1px solid #FF0000";
      return false;
  }
  else if (phone.charAt(0) == "-" || phone.charAt(0) == "(" || phone.charAt(0) == ")" )  
  { 
    alert ( "Please enter the proper phone number.");  
		cnt_id.focus();
		cnt_id.style.border="1px solid #FF0000";
    return false; 
   }
  else
     {
      for(j=0;j<len;j++)
      {
         current_char = phone.charAt(j);
        
         if(str_accept.indexOf(current_char) == -1)
              {  
               alert ( "Please enter the proper phone number.");  
				cnt_id.focus();
				cnt_id.style.border="1px solid #FF0000";
               return false; 
              }
           
          }  
   }     
   return true;// final return for the phone validate 
   } 
 } 


//Function to check postal code
function checkZip(field)
{
	var zip=field.value;
	var ch=/^([a-zA-Z0-9]+)$/;
	if(zip.length < 3 || zip.length > 10 || !ch.test(zip))
	{
		alert ("Please fill up valid Zip/Postal code.");
		field.value="";
		field.focus();
		field.style.border="1px solid #FF0000";
		return false
	}
	else
	 	return true;	
} 

function usernameCheck (cnt_id)
{ 
 
 var str_username = document.getElementById(cnt_id).value;
 
 var len = document.getElementById(cnt_id).value.length;
 for(j=0;j<len;j++)
    {
       current_char = str_username.charAt(j);
      
       var testval = str_username.charCodeAt(j);
  if( !( (testval >= 65 && testval <= 90)  || (testval >= 97 && testval <= 122 ) || (testval >= 48 && testval <= 57) || (testval >= 48 && testval <= 57 ) ))
  {
   if (current_char != "-" && current_char != "_" )
   { 
		return false;
   } 
  }
    }  
 return true; 
}  

function isValidName(cnt_id,nametitle) 
{
	var str = document.getElementById(cnt_id).value;
	for (var i = 0; i < str.length; i++) 
	{
		var ch = str.substring(i, i + 1);
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ' && ch != ',' && ch != '-') 
		{
			alert("\nPlease fill up valid "+nametitle);
			document.getElementById(cnt_id).value="";
			document.getElementById(cnt_id).focus();
			document.getElementById(cnt_id).style.border="1px solid #FF0000";

			return false;
	   }
	}
	return true;
}



// Dispaly the word counter for the textb0x
function word_counter(obj_text,obj_show,maxval)
{
		var strMsg = document.getElementById(obj_text).value;
		var strMsgLen = strMsg.length
        if(maxval=='')
			{ maxval=strMsgLen;	}
		
		var strMsgMax = strMsg.substring(0, maxval);
        
		
        if(strMsgLen<=maxval)
            document.getElementById(obj_show).innerHTML = (maxval - strMsgLen).toString();
        else
        {
            document.getElementById(obj_text).value = strMsgMax;
            //event.returnValue = false;
        }
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function

function validateFeild(str_Feild)
{
	var obj = document.getElementById(str_Feild);
	
	if(trim(obj.value)=="")
	{
		//alert (obj.title); //commented by swati patel on 1-Nov-08 as it was showing blank alertbox by pressing enter in textfield
		obj.value="";
		obj.focus();
		obj.style.border="1px solid #FF0000";
		return false;
	}
	
	return true;
	
}
function isNumericalInput(str)
{ 
	return(parseInt(str,10)==(str*1)); 
}
function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function wordCountLimit( str, maxWords )
{
 var minLen	= 1;
 var re = new RegExp('\\b\\w{'+minLen+',}\\b', 'g'), count = 0;  
   
 while( re.exec( str ) )
  count++;  
  
 if( count > maxWords)
  alert( "Word count (" + maxWords +") exceeded!" ); 
 
 return count <= maxWords; 
}

//Function to check postal code
function checkZip(field)
{
	var zip=field.value;
	var ch=/^([a-zA-Z0-9]+)$/;
	if(zip.length < 3 || zip.length > 10 || !ch.test(zip))
	{
		alert ("Please fill in valid Zip Code/Postal Code.");
		field.value="";
		field.focus();
		field.style.border="1px solid #FF0000";
		return false
	}
	else
	 	return true;	
}