//*****************************************************************************
//Added by Victoria.Khazin client side validation
//*****************************************************************************

var phoneInstruction="";
function ValidPhoneNumer(countryvalue,phonevalue)
{	
	/*var countrid=parseInt(countryvalue);
	var required=false;
	var phonePatten;
	zipinstruction="";
	
	for(i=0;i<country_phone_count;i++)
	{	if(countrid==parseInt(country_phone[i][0]))
		{	phoneInstruction=country_phone[i][2];
			phonePatten=country_phone[i][1];
			required=true;			
		}
	}	
	//phonePatten='/^[+]{0,1}[\(]?(\d){1,3}[ ]?[\)]?([-]?((\d)|[ ]){1,12})+$/'
	//alert("phonePatten="+phonePatten)
	//alert("phonevalue="+phonevalue)
	if(required)
	{	if(phonevalue=="") 
		{	return false;
		}	
		if (!phonePatten.test(phonevalue))
			return false;
		else
			return true;
	}
	else
		return true;*/
	phonePatten=/^[+0-9()-]+$/;   // /^[+]{0,1}[\(]?(\d){1,5}[ ]?[\)]?([-]?((\d)|[ ]){1,12})+$/;
	if(phonevalue=="") 
		return false;
	//alert(phonePatten);
	phoneInstruction="Instruction:only \"number ( ) + -\" allow!"
	if (!phonePatten.test(phonevalue))
		return false;
	else
		return true;
		
} 

var zipinstruction="";
function ValidPostCode(countryvalue,postvalue)
{	
	var countrid=parseInt(countryvalue);
	var required=false;
	var PostCodePatten;
	zipinstruction="";
	
	for(i=0;i<country_count;i++)
	{	if(countrid==parseInt(country_post[i][0]))
		{	zipinstruction=country_post[i][2];
			PostCodePatten=country_post[i][1];
			required=true;			
		}
	}	
	if(required)
	{	if(postvalue=="") 
		{	//zipinstruction="Zip/postcode can not be empty!";
			return false;
		}	
		if (!PostCodePatten.test(postvalue))
			return false;
		else
			return true;
	}
	else
		return true;
}

function validate( form, fieldsArray)
{

	var j;
	var berror
	var ie = false
	var serrormessage;
	
	ie=(document.all!=null)	
	serrormessage='';	
		
	for (j=0; j < fieldsArray.length ;j++) 
	 { 
		
			
			if (!fieldsArray[j])
			{
				break;
			}
			var tempobj=fieldsArray[j][0]
			var stext =fieldsArray[j][1]
			
				if (form.elements[tempobj].type=="select-one")
				{ //combobox special case 
					var selectedIndex;
					
					selectedIndex = form.elements[tempobj].selectedIndex;
					
					if (form.elements[tempobj].options[selectedIndex].value == 0 )
					{	
					//	serrormessage = serrormessage +   stext + ' ' + 'cannot  be empty!' + '\n';	
						serrormessage = serrormessage +   stext +'\n';	
						berror = true;		
					}	
				}
				else if (form.elements[tempobj].type=="text" || form.elements[tempobj].type=="textarea")
				{
					form.elements[tempobj].value = Trim(form.elements[tempobj].value, true, true)
					
					if (form.elements[tempobj].value.length==0)
					{	
						serrormessage = serrormessage +   stext +  '\n' ;
						berror = true;		
					}	
				}
	}	
	
	
	printerror(serrormessage)
	
	return serrormessage;

}

function isInteger(arg)
{
	//parseInt returns NaN if its argument does not start with an integer
	//otherwise it returns the integer the argument starts with
	//for example parseInt("4ever") will return 4
	//If parseInts return value exactly matches its argument
	// then it contains ONLY an integer
	if ( parseInt(arg) == arg )
		return true
	else
		return false
}

function isFloat(arg)
{
	//See description of isInteger function above	
	if ( parseFloat(arg) == arg )
		return true
	else
		return false
}

function isPhoneNumber(PhoneNo)
{	
	if ( isInteger(PhoneNo) && (String(PhoneNo).length == 10) )	
		return true;
	else
		return false;	
}

//*****************************************************************************
//Added by Victoria.Khazin model year validation
//*****************************************************************************

function isModelYearValid( nyear )
{
	
	if( nyear > 1980 ) 
		return true;
	else
		return false;	
		
	
}


//*****************************************************************************
//Added by Victoria.Khazin list of existing client side errors
//*****************************************************************************
function getError(nerrorNumber)
{

	var errors = new Array(1);

	errors[0] = 'Squirt information will be deleted';
	
	return errors[nerrorNumber];
}


//*****************************************************************************
//Added by Victoria.Khazin password validation
//will return error code
//*****************************************************************************
function validatePassword( s )
{
	var error
	
	error = 0;
	
	if (s.length < 8 )
	{
		error =   4;
	};
	
	if (s.indexOf(" ") > -1 )
	{
		error =  5;
	};
	
	if (numCheck(s))
	{
		error =  6;
	};
	
	return error;
}

//*****************************************************************************
//Added by Victoria.Khazin userid validation
//will return error code
//*****************************************************************************
function loginValidation(s)
{
	var error
	
	error = 0;
	
	if (s.length < 3 )
	{
		error =   7;
	};
	

	
	if (s.indexOf(" ") > -1 )
	{
		error =  8;
	};
	
	return error;	
}



//*****************************************************************************
//Added by Victoria.Khazin numcheck function
//will return true if arguments contains only 0-9
//*****************************************************************************

function numCheck(argvalue) 
{
  if (argvalue.length == 0)
    return false;

  for (var n = 0; n < argvalue.length; n++)
    if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
      return false;

  return true;

}

function isNumeric(strString)
   //  check for valid numeric strings	
  {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

	if (isEmpty(strString))
		blnResult = false;
	else	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	   {
	   strChar = strString.charAt(i);
	   if (strValidChars.indexOf(strChar) == -1)
	      {
	      blnResult = false;
	      }
	   }
	return blnResult;
   }

// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function printerror( serror)
{
	
	
	if(!isEmpty(serror))
		alert(serror);
	
	
}	

function  isEmailAddress( emailIn )
{
	var  numAtChars;					//Number of '@' chars 
	var  userNameIn;					//part before the '@' 
	var  domainNameIn;					//part after the '@' 
	var  addressFields = new Array();	//parts of address split at '@'s	
	var  alertString = "";	

	addressFields = emailIn.split( '@' );
	numAtChars = addressFields.length - 1;

	if ( emailIn == "" )
		alertString += "is EMPTY.";

	else if ( numAtChars  ==  0 )
		alertString += "contains no '@' character.";

	else if ( numAtChars  >  1 )
		alertString += "contains " + numAtChars + " '@' characters.";

	else if ( addressFields[0] == "" )
		alertString += "has no Username before the '@' character.";

	else if ( addressFields[1] == "" )
		alertString += "has no Domain Name after the '@' character.";

	else
		{
		userNameIn   = addressFields[0];
		domainNameIn = addressFields[1];

		if ( userNameIn.indexOf( " " ) != -1 )
			alertString += "has one or more Spaces in the Username before the '@' character.";
		else if ( domainNameIn.indexOf( " " ) != -1 )
			alertString += "has one or more Spaces in the Domain Name after the '@' character.";

		else if ( isStandardDomain( domainNameIn ) == false )
			alertString += "does not end with a '.com' style domain or two letter country code domain.";

		} // Ends else from outer string of if-then-else's.

	return alertString;
}

/// This checks the string passed in as a domain name and requires that it
///   either end in one of the '.com' style endings or ends with '.cc' where the
///   'cc' represents two alphabetic characters of a country code domain.
function  isStandardDomain( domainIn )
{

	
	var  isStandardReturn = false;													
	var  last4chars  =  domainIn.substring( domainIn.length-4, domainIn.length );   
	var  last3chars  =  domainIn.substring( domainIn.length-3, domainIn.length );  

	last4chars = last4chars.toUpperCase();	

	/// A regular expression pattern to match country code domains.
	///  In otherwords a Dot character followed by two alphabetic characters.
	var  countryCodePattern = /\.[a-zA-Z][a-zA-Z]/;

	if      ( last4chars == ".COM" ) isStandardReturn = true;
	else if ( last4chars == ".EDU" ) isStandardReturn = true;
	else if ( last4chars == ".GOV" ) isStandardReturn = true;
	else if ( last4chars == ".NET" ) isStandardReturn = true;
	else if ( last4chars == ".MIL" ) isStandardReturn = true;
	else if ( last4chars == ".ORG" ) isStandardReturn = true;

	else if ( last3chars.search( countryCodePattern )   !=  -1 )
		isStandardReturn = true;

	return  isStandardReturn;
}

function validateTime( ihour, iminutes)
{
	
	var ireturn = '';
	
	if (isEmpty(ihour)) 
	{
		ireturn = getError(24);
		
	}
	else
		if (isEmpty(iminutes)) 
		{
			
			ireturn = getError(25);
		}
		else
		
			if ( ihour < 1 || ihour >12)
				
				ireturn = getError(24);
			else
				if 	(iminutes < 0 || iminutes > 59 )
					ireturn = getError(25);
			
	
	return ireturn;	
}


function validateDates(ddate1, ddate2)
{
	
	if (Date.parse(ddate1) <= Date.parse(ddate2))
		return true;		
	else
		return false;

}

// validates Date in the US format (MM/DD/YYYY)					
function validateDate(strDate)
{ 
	if (strDate.match(/^(\d\d?)\/(\d\d?)\/(\d\d\d\d)$/)) 
	{ 
		var daMonth = parseInt(RegExp.$1) - 1 
		var daDay = parseInt(RegExp.$2) 
		var daYear = parseInt(RegExp.$3) 
		var checkDate = new Date(daYear, daMonth, daDay) 
		
		if ((checkDate.getMonth() == daMonth) && 
			(checkDate.getDate() == daDay ) && 
			(checkDate.getFullYear() == daYear ) )
			
			return true; 
		else 
			return false; 
	}
	else
		return false;  
}

function ReplaceAll(str,strFind,strReplace)
{
  var returnStr = str;
  var start = returnStr.indexOf(strFind);
  while (start>=0)
  {
    returnStr = returnStr.substring(0,start) + strReplace + returnStr.substring(start+strFind.length,returnStr.length);
    start = returnStr.indexOf(strFind,start+strReplace.length);
  }
  return returnStr;
} 

function Trim(value,bRight,bLeft) 
{ 
   var element=new String(value);
   var len=element.length;
   var remove=" ";
   var first=element.substring(0,1);
   var next=element.substring(1,2);
   var last="";
   var previous="";
   
   if(bLeft)
   {
      while(first.indexOf(remove)==0)
      {
         first=next;
         element=next+element.substring(2);
         len=len-1;
         next=element.substring(1,2);        
      }
   }
   
   if(bRight)
   { 
      last=element.substring(len-1);
      previous=element.substring(len-2,len-1);
      
      while(last.indexOf(remove)==0)
      {
         last=previous;
         element=element.substring(0,len-2)+previous;
         len=len-1;
         previous=element.substring(len-2,len-1);
      }
   }
   return element;       
}

function isNickName(strString)
{
	var validCharPattern = "[0-9A-Za-z_]";
	var blnResult;
	var i,strChar;
	
	blnResult = true;
	strString = Trim(strString, true, true);
	
	for (i = 0; i < strString.length && blnResult == true; i++)
	   {
	   strChar = strString.charAt(i);
	   if (strChar.search(validCharPattern) == -1)
	      {
	      blnResult = false;
	      }
	   }
	  if(blnResult)
	{	  var adminreg = new RegExp("editor|admin");	   
  if ( strString.toLowerCase().match(adminreg)) {
	 blnResult=false;
  }
  }
	return blnResult;
}

function isAdminNickName(strString)
{
	var validCharPattern = "[0-9A-Za-z_]";
	var blnResult;
	var i,strChar;
	
	blnResult = true;
	strString = Trim(strString, true, true);
	
	for (i = 0; i < strString.length && blnResult == true; i++)
	   {
	   strChar = strString.charAt(i);
	   if (strChar.search(validCharPattern) == -1)
	      {
	      blnResult = false;
	      }
	   }	 
	return blnResult;
}

var isIE = true;
var isNS = false;

var	oDocument;
var	oStyle;

if ("object" == typeof(document.all))
{
	isIE = true;
	isNS = false;
	
	oDocument = "document.all.";
	oStyle = ".style";
}
else
{
	isIE = false;
	isNS = true;

	oDocument = "document.";
	oStyle = "";	
}
