// - JavaScript Document

function verifyArticle( formToValidate )
{
	var categoryID = document.getElementById( formToValidate ).elements["cmbCategory"] ;
	var title      = document.getElementById( formToValidate ).elements["txtTitle"] ;
	var abstract   = document.getElementById( formToValidate ).elements["txtAbstract"] ;
	var article    = document.getElementById( formToValidate ).elements["txtArticle"] ;
	var keyword    = document.getElementById( formToValidate ).elements["txtKeyword"] ;

	// Article Category
	if ( !categoryID.value )
	{
		alert('-| You must select an Article Category |-');
		categoryID.focus ( ) ;
		return false ;
	}
	// Article Title
	if ( title.value.length < 15 )
	{
		alert('-| Minimum Article Title length is 15 characters |-');
		title.focus ( ) ;
		return false ;
	}
	if ( title.value.length > 100 )
	{
		alert('-| Maximum Article Title length is 100 characters |-');
		title.focus ( ) ;
		return false ;
	}

	// Article Abstract
	if ( abstract.value.length < 50 )
	{
		alert('-| Minimum Article Abstract length is 50 characters |-');
		abstract.focus ( ) ;
		return false ;
	}
	
	// Article 
	/*if ( article.value.length < 100 )
	{
		alert('-| Minimum Article length is 100 characters |-');
		article.focus ( ) ;
		return false ;
	}*/
	
	// ** Word Length Check ***********************************

	// title Word Lengths
	/*
	if ( validateWordLength( title.value, 30 ) )
	{
		title.focus ( ) ;
		return false ;
	}


	// keyword Word Lengths
	if ( validateWordLength( keyword.value, 30 ) )
	{
		keyword.focus ( ) ;
		return false ;
	}

	// abstract Word Lengths
	if ( validateWordLength( abstract.value, 30 ) )
	{
		abstract.focus ( ) ;
		return false ;
	}
	
	// Article Word Lengths
	if ( validateWordLength( article.value, 30 ) )
	{
		article.focus ( ) ;
		return false ;
	}
*/
	return true;
}
// * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * -
function verifyUser( formToValidate )
{
	var emailID   = document.getElementById( formToValidate ).elements["txtEmailAddress"] ;
	var emailID2  = document.getElementById( formToValidate ).elements["txtEmailAddress2"] ;
	var paswd     = document.getElementById( formToValidate ).elements["txtPassword"] ;
	var paswd2    = document.getElementById( formToValidate ).elements["txtPassword2"] ;
	var answer    = document.getElementById( formToValidate ).elements["txtAnswer"] ;
	var firstName = document.getElementById( formToValidate ).elements["txtFirstName"] ;
	var lastName  = document.getElementById( formToValidate ).elements["txtLastName"] ;
	var phone     = document.getElementById( formToValidate ).elements["txtPhone"] ;
	var cell      = document.getElementById( formToValidate ).elements["txtCell"] ;
	var fax       = document.getElementById( formToValidate ).elements["txtFax"] ;
	var address   = document.getElementById( formToValidate ).elements["txtAddress"] ;
	var country   = document.getElementById( formToValidate ).elements["cmbCountry"] ;
	var city      = document.getElementById( formToValidate ).elements["cmbCity"] ;
	var state     = document.getElementById( formToValidate ).elements["cmbState"] ;
	
	// Email Validation
	if ( emailcheck ( emailID.value ) == false )
	{
		emailID.value = "" ;
		emailID.focus ( ) ;
		return false ;
	}
	// Email and Retype Email is not Equal
	if ( emailID.value != emailID2.value )
	{
		alert('-| Email and retype Email address are not equal |-');
		emailID.value = "" ;
		emailID2.value = "" ;
		emailID.focus () ;
		return false ;
	}

	// Password Length
	if ( paswd.value.length < 6 )
	{
		alert('-| Minimum password length is 6 characters |-');
		paswd.value = "" ;
		paswd.focus () 
		return false ;
	}

	// Password and Retype Password is not Equal
	if ( paswd.value != paswd2.value )
	{
		alert('-| Password and retype Password are not equal |-');
		paswd.value = "" ;
		paswd2.value = "" ;
		paswd.focus () 
		return false ;
	}

	// Answer Length
	if ( answer.value.length < 10 )
	{
		alert('-| Minimum answer length is 10 characters |-');
		answer.value = "" ;
		answer.focus () 
		return false ;
	}

	// First Name Length
	if ( firstName.value.length < 3 )
	{
		alert('-| Minimum First Name length is 3 characters |-');
		firstName.value = "" ;
		firstName.focus () 
		return false ;
	}
	
	// Last Name Length
	if ( lastName.value.length < 3 )
	{
		alert('-| Minimum Last Name length is 3 characters |-');
		lastName.value = "" ;
		lastName.focus () 
		return false ;
	}

	// 3 Contacts - atleast one be entered
	if ( phone.value == "" && fax.value == "" && cell.value == "" )
	{
		alert('-| Atleast one contact must be entered |-');
		phone.focus () 
		return false ;
	}

	// Address Length
	if ( address.value.length < 10 )
	{
		alert('-| Minimum Address length is 10 characters |-');
		address.focus () 
		return false ;
	}

	// Country Length
	if ( country.value <= 0 )
	{
		alert('-| Country Not Selected |-');
		country.focus () 
		return false ;
	}

	// State Length
	if ( state.value <= 0 )
	{
		alert('-| State Not Selected |-');
		state.focus () 
		return false ;
	}

	// City Length
	if ( city.value <= 0 )
	{
		alert('-| City Not Selected |-');
		city.focus () 
		return false ;
	}

	// if no error found
	return true;
}

// Returns False on first non numeric is found
function checkNumeric( checkStr )
{
	for (i = 0; i < checkStr.length; i++)
		if ( checkStr.charAt(i) > "9" || checkStr.charAt(i) < "0" )
			return false ;
	return true ;
}

// explode a string in Javascript
function explode( delimiter, string ) {
 
    var emptyArray = { 0: '' };
 
    if ( arguments.length != 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function' || typeof delimiter == 'object' || typeof string == 'function' || typeof string == 'object' )
	 {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
 
    return string.toString().split ( delimiter.toString() );
}

// Checking word's length a string
function validateWordLength( stringToCheck, wordLength )
{
	 myArray = stringToCheck.split(' ');	 
	 for ( i=0; i<myArray.length ; i++ ){
			if ( myArray[i].length >= wordLength ) {
				alert ( "-| error in word # " + (i+1) + " length |- ( " + myArray[i] + " )") ;
				
				return i+1;
			}
	 }
	return 0;
}

// Email Address Validation 
function emailcheck ( str ) 
{
	var at   = "@" ;
	var dot  = "." ;
	var lat  = str.indexOf( at ) ;
	var lstr = str.length ;
	var ldot = str.indexOf( dot );
	if ( str.indexOf ( at ) == -1 )
	{
		alert( "No @ found in E-mail ID" ) ;
		return false ;
	}

	if (str.indexOf ( at ) == -1 || str.indexOf ( at ) == 0 || str.indexOf ( at ) == lstr )
	{
		alert( "Invalid use of @ in E-mail ID" ) ;
		return false ;
	}

	if ( str.indexOf ( dot ) == -1 || str.indexOf ( dot ) == 0 || str.indexOf ( dot ) == lstr )
	{
		 alert( "Invalid use of DOT in E-mail ID" ) ;
		 return false ;
	}

	 if ( str.indexOf ( at , ( lat+1 ) ) != -1 ){
		 alert( "Invalid use of @ in E-mail ID" ) ;
		 return false ;
	 }

	 if ( str.substring ( lat-1 , lat ) == dot || str.substring ( lat + 1 , lat+2 ) == dot )
	 {
		 alert( "Invalid use of DOT in E-mail ID" ) ;
		 return false ;
	 }

	 if ( str.indexOf ( dot , ( lat+2 ) ) == -1 )
	 {
		 alert ( "Invalid use of DOT in E-mail ID" ) ;
		 return false ;
	 }
	
	 if ( str.indexOf ( " " ) != -1 )
	 {
		 alert ( "E-mail ID can not contain spaces." ) ;
		 return false ;
	 }

	 return true ;			
}

// Verify Passwords
function verifyPassword( formToValidate )
{
	var password = document.getElementById( formToValidate ).elements["txtPassword"] ;
	var password2 = document.getElementById( formToValidate ).elements["txtPassword2"] ;
	var password1 = document.getElementById( formToValidate ).elements["txtPassword1"] ;
	
	if ( password.value != password2.value )
	{
		alert ("New password and retype password are not equal") ;
		password.focus() ;
		return false;
	}
	
	return true;
}

// Word Count
function CountWords ( textString, showField ) 
{
	
	var charCount = textString.length;
	var fullStr = textString + " ";
	
	fullStr = fullStr.replace(/\&\w+;/ig,"");
	
	var stripped = fullStr.replace(/(<([^>]+)>)/ig,"");
	
	var initialWhiteSpaceExp = /^[^A-Za-z-']+/gi;
	var leftTrimmedStr = stripped.replace( initialWhiteSpaceExp, "" );
	var nonAlphaNumericExp = rExp = /[^A-Za-z-']+/gi;
	var cleanedStr = leftTrimmedStr.replace( nonAlphaNumericExp, " " );
	var splitString = cleanedStr.split( " " );
	var wordCount = splitString.length -1 ;

	if ( fullStr.length <2) { wordCount = 0; }
	
	if ( wordCount == 1) { wordOrWords = " word"; } else { wordOrWords = " words"; }
	if ( charCount == 1) { charOrChars = " character"; } else { charOrChars = " characters"; }
	
	showField.innerHTML = wordCount + wordOrWords + "\n" + "    " + charCount + charOrChars; 
	
}
	//return wordCount;

// Check Valid URL
function isValidURL(url)
{
    var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
    if(RegExp.test(url)){
        return true;
    }else{
        return false;
    }
}
// Check Valid Email Address
function isValidEmail(email){
    var RegExp = /^((([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.))*([a-z]|[0-9])([a-z]|[0-9]|\-){0,61}([a-z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/
    if(RegExp.test(email)){
        return true;
    }else{
        return false;
    }
}

// Validate Article Comments
function validateComments( formToValidate )
{
	var emailAddress = document.frmComments.elements['txtEmailAddress'] ;
	var urlAddress = document.frmComments.elements['txtURL'] ;
	var commentsText = document.frmComments.elements['txtComments'] ;
	var visitorName	= document.frmComments.elements['txtName'] ;

	// Check if Name is antered
    if( !visitorName.value ) 
	{
        alert ( 'Please enter a name ' ) ;
		visitorName.focus ( ) ;
		return false;
    }

	// Check if Comments are antered
    if( !commentsText.value ) 
	{
        alert ( 'Please enter a something in Comments' ) ;
		//commentsText.focus ( ) ;
		return false;
    }

	// Check if EmailAddress is Valid
    if( !isValidEmail( emailAddress.value ) ) 
	{
        alert ( 'Please enter a valid Email Address' ) ;
		emailAddress.focus ( ) ;
		return false;
    }


	// Check if entered URL is Valid
	if( !isValidURL( urlAddress.value ) )
	{
        alert ( "Please enter a valid URL" ) ;
		urlAddress.focus ( ) ;
		return false;
    }

	return true;	
}


// Validate User Information at Signup
function validateSignup( formToValidate )
{
	var emailAddress = document.frmSignUp.elements['txtEmailAddress'] ;
	var firstName = document.frmSignUp.elements['txtFirstName'] ;
	var lastName = document.frmSignUp.elements['txtLastName'] ;
	var password	= document.frmSignUp.elements['txtPassword'] ;
	var password2	= document.frmSignUp.elements['txtPassword2'] ;

	// Check if firstName is antered
    if( !firstName.value ) 
	{
        alert ( 'Please enter First Name ' ) ;
		firstName.focus ( ) ;
		return false;
    }

	// Check if lastName is antered
    if( !lastName.value ) 
	{
        alert ( 'Please enter Last Name ' ) ;
		lastName.focus ( ) ;
		return false;
    }

	// Check if password are antered
    if( password.value != password2.value ) 
	{
        alert ( 'Password and retype Password does not match' ) ;
		password.value = "" ;
		password2.value = "" ;
		password.focus ( ) ;
		return false;
    }

	// Check if password are antered
    if( !password.value ) 
	{
        alert ( 'Please enter a password' ) ;
		password.focus ( ) ;
		return false;
    }

	// Check if EmailAddress is Valid
    if( !isValidEmail( emailAddress.value ) ) 
	{
        alert ( 'Please enter a valid Email Address' ) ;
		emailAddress.focus ( ) ;
		return false;
    }

	return true;	
}
