﻿

function getDivSibling(source)
{
	var sibling = null;	
	var currentElement = source;
	
	try
	{
	    //go forward until you find the sibling
	    while (sibling == null)
	    {
	     //if there is no text in the node
	     if(currentElement.innerHTML == null || currentElement.innerHTML == "")
	     {
		    //move forward
		    currentElement = currentElement.nextSibling;
    		
		    //if we are at the end
		    if(currentElement == null)
		    {	
			    //reset currentElement to move backward	
			    currentElement = source;
    			
			        //go backward until you find the sibling
			        while (sibling == null)
			        {
			         //if there is no text in the node
			         if(currentElement.innerHTML == null || currentElement.innerHTML == "")
			         {
				        //move backward
				        currentElement = currentElement.previousSibling;
            		
				        //if we are at the end
				        if(currentElement == null)
				        {
				            //we couldnt find the node
    		                //throw "DIV ELEMENT NOT FOUND";
				        }
			         }
			         else
			         {
				        sibling = currentElement;
			         }
			        }
    			
		    }
	     }
	     else
	     {
		    sibling = currentElement;
	     }
	    }
	
	}
	catch(E)
	{
	    sibling = null;
	}
	
	return sibling;
}

//Validates the control data using the regex statement provided
function validateControl(source, args)
{ 
    args.IsValid = false;

    var sibling = getDivSibling(source);
 
 if(sibling != null)
 {
     var innerHTML = sibling.innerHTML;
     var errorMessage = ' is invalid.';
     //var customValidationFunction = source.attributes["CustomValidationFunction"];
     var customErrorMessage = source.attributes["CustomErrorMessage"];
     
     var fieldRequired = '';
      
     if(customErrorMessage != null)
     {
	    errorMessage = customErrorMessage.value;
     }
     
//     if(customValidationFunction != null)
//     {
//	    window[customValidationFunction.value](source, args);
//     }
//     else 
//     {
	    var regex = source.attributes["Regex"].value;
	    try
	    {
	        fieldRequired  = source.attributes["FieldRequired"].value;
	    }
	    catch(E)
	    {
	        fieldRequired = "True";
	    }
    	
	    if(fieldRequired == "False" && args.Value == "")
	    {
	        args.IsValid = true;
	    }
	    else
	    {	
	        if(args.Value.match(regex))
	        {
	            args.IsValid = true;
	        }
	        else
	        { 
	            args.IsValid = false;
	        }
	    }
//    }
//    //check date
//    var validateDate = source.attributes["ValidateDate"];
//    if (validateDate != null) {
//        //check to valid date
//        ValidateDateByValue(args, 'True');
//    }

     //Do text modification
     if(args.IsValid)
     {
	    innerHTML = innerHTML.replace(errorMessage, "");
	    
	    sibling.innerHTML = innerHTML;
	    sibling.style.color = "#000000"; 
    	
     }
     else
     {
	    innerHTML = innerHTML.replace(errorMessage, "");
	    innerHTML = innerHTML + errorMessage;
    	 
	    sibling.innerHTML = innerHTML;
	    sibling.style.color = "#FF0000"; 	
    	
     }
 }
 else
 {
    args.IsValid = true;
 }
 
}
