﻿var strContent
strContent = ''

function ValidateForm()
{

    var controlArray = document.forms[0].elements;
    var controlArrayLength = controlArray.length - 1;
    var controlId = '';
    var controlType = '';
    var questionId = '';
    var flag = true;
    var question = '';
    var alertMessage = '';
    
        for(i = 0; i < controlArrayLength; i++)
        {
            controlId = controlArray[i].id;   
             
                if(( controlId.indexOf("isMandatory") > 0 ) || ( controlId.indexOf("isMandatory") == 0 ))
	            {
	    	            if(controlArray[i].value == 'True')
                        {
                            controlType = document.getElementById(controlId.replace('isMandatory', 'Control')).value;
                            questionId = document.getElementById(controlId.replace('isMandatory', 'QuestionId')).value;
                            
                                  if(!CheckSelection(controlType, controlId, questionId))                                            
                                    {
                                        question = document.getElementById(controlId.replace('isMandatory', 'lblQuestion')).innerHTML;
                                        
                                        alertMessage = 'Please complete required field:\n\n' + question;
                                        
                                        alert(alertMessage);
                                        //alert(controlType);
                                        return false;
                                        
                                      
                                        
                                    }//if(!CheckSelection(controlType, controlId)
                           }
                        
                   } //if(controlArray[i].value == 'False')
                    
	        }   //  if(controlId.indexOf("isMandatory") > 0)
    
    return true;
}

function CheckSelection(pControlType, pControlId, pQuestionId)
{

    var subControlId = '';
    var flag = false;    
    var pControlIdLen;
    var mandatory;   
    var replaceString;

    pControlType = pControlType.toLowerCase();      
    pControlIdLen = pControlId.length;    
    mandatory=pControlId.indexOf('isMandatory');   
    replaceString=pControlId.substring(mandatory,pControlIdLen);

    if( pControlType == "checkbox")
    {
        for(j = 0; j <= 10; j++)
        {
            subControlId = pControlId.replace("isMandatory", "cbxList");
            subControlId=subControlId + "_" + j ;
            var subControl = document.getElementById(subControlId);
            var subControlLabel = document.getElementById(subControlId.replace("cbx","lblcbx"));          
            
            if(subControl != null)
            {
                //alert(subControl.value + ' - ' + subControlLabel.innerHTML.replace("<br>",""));
                if(subControl.checked)
                {                                    
                    //strContent = strContent +  subControl.parentControl.innerHTML;                                       
                    flag = true;
                    break;
                                       
                    
                }   //  if(subControl.checked)
                
            }   //  if(subControl != null)   
            
        }   //  for(j = 0; j <= 10; j++)               
        
    } // if(controlType == "CheckBox")
    
    if(pControlType == "radiobutton")
    {
    
        for(j = 0; j <= 10; j++)
        {
            subControlId = pControlId.replace("isMandatory", "rbtn") + j;
            var subControl = document.getElementById(subControlId);
            var subControlLabel = document.getElementById(subControlId.replace("rbtn","lblrbtn"));
            //alert(subControl);
            if(subControl != null)
            {
                //alert(subControl.value + ' - ' + subControlLabel.innerHTML.replace("<br>",""));
                if(subControl.checked)
                {
                    //strContent = strContent +  subControl.parentControl.innerHTML;
                    flag = true;
                    
                }   //  if(subControl.checked)
                
            }   //  if(subControl != null)   
            
        }   //  for(j = 0; j <= 10; j++)               
        
    } // if(controlType == "CheckBox")
    
    if(pControlType == "listbox")
    {
        subControlId = pControlId.replace("isMandatory", "lbx" + pQuestionId);
        
        var subControl = document.getElementById(subControlId);
        
        //alert(subControl.selectedIndex);
        
        if(subControl.selectedIndex != -1)
        {
            flag = true;
        }
    }
    
    if(pControlType == "textbox")
    {
    
        subControlId = pControlId.replace(replaceString, "tbx" + pQuestionId);
        
        var subControl = document.getElementById(subControlId);
        
        if(subControl.value != '')
            flag = true;
    }
    
    if(pControlType == "textarea")
    {
    
       // subControlId = pControlId.replace("isMandatory", "tbxa" + pQuestionId);
        subControlId = pControlId.replace(replaceString, "tbx" + pQuestionId);
        
        var subControl = document.getElementById(subControlId);      
        
        if(subControl.value != '')
            flag = true;
    }
    
    if(pControlType == "dropdown")
    {
    
        subControlId = pControlId.replace(replaceString, "ddl" + pQuestionId);
        
        var subControl = document.getElementById(subControlId);
        
       
        if(subControl.selectedIndex != 0)
        {
            flag = true;
        }
    }
    
    return flag;
}



         function setPosition_Header(pValue)
                {
                
                }
                
                
    function trim(str) {
	
		for( lspace=0 ; lspace<str.length ; lspace++ )
			if( str.charAt(lspace)!=' ' )
				break;
		str = str.substring( lspace );
		for( rspace=str.length-1 ; rspace>=0 ; rspace-- )
			if( str.charAt(rspace)!=' ' )
				break;
		str = str.substring( 0, rspace+1 );
		return str;
	}
	
	
function setToolTip(tbl)
{ 
    var DropDowns = tbl.getElementsByTagName('SELECT'); 
    
//    for ( var i = 0; i < DropDowns.length; i++ ) 
//    {
//            for ( j = 0; j < DropDowns[i].options.length;j++)
//            {
//              DropDowns[i].options[j].setAttribute('title',DropDowns[i].options[j].text); 
//              
//            } 
//    } 
}


    function OpenProfile()
        {
        
                    var rr  = window.open('UpdateProfile.aspx',"" ,'height=700, width=700, top=150, left=250, scrollbars=1, resizable=1');           
        
        }


function ValidateFormCustom()
{
    var totalElementCount = document.forms[0].elements.length - 1;
    var controlArray = document.forms[0].elements;        
    var questionCount = totalElementCount;    
    var hdnQuestionNumber;
    var controlRef;
    var controlId;    
    
    for(i = 1; i <= totalElementCount; i++)
    {
        controlId = controlArray[i].id;
                
        if(controlId != null)
        {
            if(controlId != '')
            {
                // alert(controlId);
                
                if( controlId.indexOf("hdn") > -1 )
                {
                    var hdnQuestion = document.getElementById(controlId);
                    // alert(hdnQuestion.value);
                    
                    var configArray = hdnQuestion.value.split('§');;
                    
                    var ctlAppend;
                    var controlRef;
                    
                    ctlAppend = controlId.replace('hdn','');
                    
                    // alert(ctlAppend);
                    
                    if (configArray[0]=='1')
                    {
                        if((configArray[1] == 'TEXTBOX') || (configArray[1] == 'TEXTAREA'))
                        {
                            controlRef = document.getElementById('tbx' + ctlAppend);
                            
                            if (controlRef != null)
                            {
                                //alert(controlRef);
                                if(controlRef.value == '')
                                {
                                    alert('Please fill all mandatory fields.');
                                    controlRef.focus();
                                    return false;
                                }
                            }
                        }
                        if(configArray[1] == 'DROPDOWN')
                        {
                            controlRef = document.getElementById('ddl' + ctlAppend);
                            
                            if (controlRef != null)
                            {    
                                //alert(controlRef);
                                if(controlRef.selectedIndex == 0)
                                {
                                    alert('Please fill all mandatory fields.');
                                    controlRef.focus();
                                    return false;
                                }
                            }
                        }                        
                    }
                }
            } // if(controlId != '')
        } // if(controlId != null)       
    } //for(i = 1; i <= totalElementCount; i++)
    return true;
}
