 function Load_Conditional_Question(QuestionID,UrlRoot,FormName){

        var ParentQuestion = document.getElementById('Question'+QuestionID);
        var ElementValues="";
        var QuestionContainer;
        var FormObject = document.getElementById(FormName);

        QuestionContainer = document.getElementById('SubQuestions'+QuestionID);

    for(var k=0; k < FormObject.elements.length; k++){
      if( FormObject.elements[k].name == 'Question'+QuestionID){ //If it's part of the same element
        //Find out which type it is and fetch it's values
        if( FormObject.elements[k].type == 'checkbox' && FormObject.elements[k].checked == true ){

                  ElementValues += FormObject.elements[k].value+',';

        }else if( ParentQuestion.type == 'radio' && FormObject.elements[k].checked ){
              ElementValues += FormObject.elements[k].value+',';
        }else if( ParentQuestion.type == 'select-one'  ){
          for(var j=0; j < FormObject.elements[k].length;j++){
            if( FormObject.elements[k].options[j].selected ){
              ElementValues += FormObject.elements[k].options[j].value+',';
            }
          }
        }else if( ParentQuestion.type == 'select-multiple'  ){
          for(var j=0; j < FormObject.elements[k].length;j++){
            if( FormObject.elements[k].options[j].selected ){
              ElementValues += FormObject.elements[k].options[j].value+',';
            }
          }
        }

     }
   }

       if( ElementValues != "" ){ //Time to load the Conditionals
        if( ElementValues.substr(ElementValues.length-1,1) == ',' ){
           ElementValues = ElementValues.substring(0,ElementValues.length-1);
        }

        loadPage(UrlRoot+'components/Form_Builder/admin/ajax/Form_Conditional_Question.php?QuestionID='+QuestionID+'&QuestionValues='+ElementValues,QuestionContainer);


       }else{
     //   QuestionContainer.innerHtml='empty';
       }
}

function Build_Rating_Boxes(QuestionID,QuestionValueID,FormName,ItemValue,NewItem){

  var FormObject = document.getElementById(FormName);
   var ItemCount=0;
   var ItemSelectedCount=0;
   var SelectedItems=',';
   var Item;


   //Find out how many items belong to this question
   for(var k=0; k < FormObject.elements.length; k++){

      if( FormObject.elements[k].name == 'Question'+QuestionID ){
          ItemCount++;
          if( FormObject.elements[k].checked ){
             ItemSelectedCount++;
             SelectedItems+=FormObject.elements[k].value+',';
          }
      }
   }

   var StrHtml = "";
   //Check if an object exits for this item if not then create
   if( document.getElementById('PrioritisedSpan'+QuestionValueID).innerHTML =='' && SelectedItems.indexOf(QuestionValueID)>-1 ){
      StrHtml = '<SELECT NAME="QuestionPriority'+QuestionValueID+'" class="Main">';
      StrHtml += '<option value="0">N/A</option>';
          for(var m=1;m<=ItemSelectedCount;m++){
                  StrHtml += '<option value="'+m+'" ';
                  if( ItemValue > 0 && ItemValue == m){
                      StrHtml += 'SELECTED';
                  }
                  StrHtml += '>'+m+'</option>';
          }

      StrHtml += '</SELECT>';


      document.getElementById('PrioritisedSpan'+QuestionValueID).innerHTML = StrHtml;

     //Need to add an option from the other existing boxes
   //  alert(ItemSelectedCount + ' : ' + ItemCount);
     SelectedItems = SelectedItems.split(',');
     for(var i=1;i<SelectedItems.length-1;i++){
      if( (QuestionValueID > SelectedItems[i] && ItemSelectedCount != ItemCount) || (NewItem==1 && QuestionValueID != SelectedItems[i])  ){
       Item = document.getElementById('QuestionPriority'+SelectedItems[i]);
     if( Item ){
       var elOptNew = document.createElement('option');
       elOptNew.text = Item.length;
       elOptNew.value = Item.length;

       try {
           Item.add(elOptNew, null); // standards compliant; doesn't work in IE
       }
       catch(ex) {
           Item.add(elOptNew); // IE only
       }
      }
      }
     }
   }else if(document.getElementById('PrioritisedSpan'+QuestionValueID).innerHTML !='' && SelectedItems.indexOf(QuestionValueID)==-1){

     //Need to remove an option from the other existing boxes
     SelectedItems = SelectedItems.split(',');
     for(var i=1;i<SelectedItems.length-1;i++){
       Item = document.getElementById('QuestionPriority'+SelectedItems[i]);
       Item.remove(Item.length-1);
     }

     //Remove the selected item as it's not applicable anymore
     document.getElementById('PrioritisedSpan'+QuestionValueID).innerHTML='';


   }

}

             function OnChangeJSQuestionnaire(ItemVar,FormID) {
                      if(ItemVar.options[ItemVar.selectedIndex].value==2){
                          document.getElementById('AdditionalSettingsQuestionnaire'+FormID).style.display='block';
                          document.getElementById('AdditionalSettingsSurvey'+FormID).style.display='none';
                      }else{
                          document.getElementById('AdditionalSettingsQuestionnaire'+FormID).style.display='none';
                          document.getElementById('AdditionalSettingsSurvey'+FormID).style.display='block';
                      }
             }

             function InitializeQuestionContent(QuestionID,QuestionTypeID,Values){

                      var item = document.getElementById('QuestionValues'+QuestionID);

                      if( QuestionTypeID == '1' || QuestionTypeID == null || QuestionTypeID == 'null' || QuestionTypeID == '' || QuestionTypeID.Length == 0 ){ //text field
                       item.innerHTML ='';
                      }else if( QuestionTypeID == '2' || QuestionTypeID == '8' || QuestionTypeID == '10' || QuestionTypeID == '11' ){  //textarea, Email, Number
                       item.innerHTML ='';
                      }else if( QuestionTypeID == '9' ){ //Date
                       item.innerHTML ='<textarea name="QuestionValue'+QuestionID+'">'+Values+'</textarea><a style="cursor:hand" title="To Specify the Year Range use 1=>FromYear,2=>ToYear">?</a>';
                      }else {
                       item.innerHTML ='<textarea name="QuestionValue'+QuestionID+'">'+Values+'</textarea><a style="cursor:hand" title="All Items except Text and Textarea uses data in the following format Value=>Text, Value is the actual value of the selected item and the Text is the text that will be displayed next to the item. For example 1=>Boy,2=>Girl... the Values Boy and Girl will be displayed, but their values will each be 1 and 2 respectively.">?</a>';
                      }

             }

             function PivotRow(RowName,QuestionID){

                      var tmpARY = RowName.split('AAAA');
                      var root=document.getElementById(RowName).parentNode;//the TBODY
                      var clone=document.getElementById(RowName).cloneNode(true);//the clone of the first row


                        clone.id=tmpARY[0]+'AAAA'+(parseInt(tmpARY[1])+1);


                        for(var i=0;i< clone.getElementsByTagName('td').length;i++){

                         if( clone.getElementsByTagName('td')[i] ){



                          var CloneHtml = clone.getElementsByTagName('td')[i].innerHTML;
                           while( CloneHtml.indexOf('AAAA'+tmpARY[1]) > -1 ){
                            CloneHtml=CloneHtml.replace(('AAAA'+tmpARY[1]).toString(),('AAAA'+(parseInt(tmpARY[1])+1)).toString());
                           }
                           clone.getElementsByTagName('td')[i].innerHTML=CloneHtml;
                           clone.getElementsByTagName('td')[i].childNodes[0].value='';
                           clone.getElementsByTagName('td')[i].childNodes[0].checked=false;
                           clone.getElementsByTagName('td')[i].childNodes[0].selectedIndex=0;
                           clone.getElementsByTagName('td')[i].childNodes[0].selected=false;
                         }
                        }

                       root.appendChild(clone);//appends the clone
                       document.getElementById('PivotCounter').value=parseInt(document.getElementById('PivotCounter').value)+1;
                       document.getElementById('PivotChanged').value=1;

             }

             function Update_Date(DateName){
               document.getElementById(DateName).value=document.getElementById(DateName+'_Year').options[document.getElementById(DateName+'_Year').selectedIndex].value+'-'+document.getElementById(DateName+'_Month').options[document.getElementById(DateName+'_Month').selectedIndex].value+'-'+document.getElementById(DateName+'_Day').options[document.getElementById(DateName+'_Day').selectedIndex].value;

             }
function echeck(str) {

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)
    if (str.indexOf(at)==-1){
       return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        return false
    }

     if (str.indexOf(at,(lat+1))!=-1){
        return false
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false
     }

     if (str.indexOf(dot,(lat+2))==-1){
        return false
     }
    
     if (str.indexOf(" ")!=-1){
        return false
     }

      return true          
  }

function calcHeight(FrameName)
{
  //find the height of the internal page
  var the_height=
    document.getElementById(FrameName).contentWindow.
      document.body.scrollHeight;

  var the_width=
    document.getElementById(FrameName).contentWindow.
      document.body.scrollWidth;

  //change the height of the iframe
  document.getElementById(FrameName).height=
      the_height;

  document.getElementById(FrameName).width=
      the_width;
}

function isValidNumber(inpString) {
   return /^[-+]?\d+(\.\d+)?$/.test(inpString);
}

function isValidSMS(Item){
  Item.value = Item.value.replace(" ","");
  if( Item.value.substring(0,1)!="0"){
      return false;
  }else if(isValidNumber(Item.value) == false){
    return false;
  }else if(Item.value.length!=10){
        return false;
  }
  return true;
}

function Validate_CheckBoxes(Item){
         var FoundValue=false;
         Item = document.getElementsByName(Item.name);
         for(var i=0; i < Item.length;i++){
            if(Item[i].checked==true){
                FoundValue=true;
                break;
            }
        }

        return FoundValue;
}

function Validate_RadioBoxes(Item){
         var FoundValue=false;
         Item = document.getElementsByName(Item.name);
         for(var i=0; i < Item.length;i++){

            if(Item[i].checked==true){
                FoundValue=true;
                break;
            }
        }

        return FoundValue;         
}

function Validate_MultipleDropdown(Item){
         var FoundValue=false;
         for(var i=0; i < Item.length;i++){
            if(Item[i].selected==true){
                FoundValue=true;
                break;
            }
        }

        return FoundValue;          
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function ResetForm(Form,ExcludeList){
    var ItemARY = Form.getElementsByTagName("*");
    for(i=0;i<ItemARY.length;i++){
      if( ExcludeList.indexOf(ItemARY[i].name) == -1){
                          ItemARY[i].value='';
                          ItemARY[i].checked=false;
                          ItemARY[i].selectedIndex=0;
                         ItemARY[i].selected=false;
      }
    }
}



