﻿// JScript File
var flagState = 0;
var toExpertID="";
var divName="";
function openAskMenu(strOpen) {	
	if(this.flagState == 0)
	    AEShow(strOpen);
	else
	    AEHide(strOpen);	  
}
function AEHide(str) {
    x = document.getElementById(str);
    x.style.display = 'none'; // hide the element
    this.flagState = 0; // now it's off, keeping track of its state
    x.innerHTML="";
}
function AEShow(str) { 
    x = document.getElementById(str);
    x.style.display = 'block'; // show the element
    this.flagState = 1; // now it's on, keeping track of its state
}     
var askmeState = 0;
var isKwality = 0;
function open1(tDiv,nickName, askername)
{
       isKwality = 0;
       askmeState = 0;
       if(document.getElementById(divName)!=null)
       {           
            AEHide(divName);
       }       
       var x=
"<div id='askwin' class='popupBg' style='position:absolute;'><div class='pad5' align='left'>"
+"<div style='width:216px; float:left;'><h4 class='flLt white'>Ask " + nickName.split("|")[0] +  "</h4> <a href='javascript:return false;' onclick=\"AEHide('" + tDiv + "'); return false;\"><img src='" + appPath + "images/close-icon.gif' alt='Close'  class='flRt' /></a></div>"
+"<div class='clearAll spacer10'></div>"    
+"<div id='askMainContent' align='left'>"
+"<div  align='left'>"
+"<textarea id='taSubject' name='taSubject' rows='3' class='globalInput' style='color: #c9c9c9; width:216px; overflow:auto;' onfocus=\"if (this.value == 'e.g., Is it better to join IIM straight after college or after a few years of work?') {this.value = '';this.style.color = '#333';}\"" 
+"onblur=\"if (this.value == '') {this.value = 'e.g., Is it better to join IIM straight after college or after a few years of work?';this.style.color = '#c9c9c9';}\" value='e.g., Is it better to join IIM straight after college or after a few years of work?' max='160' onkeypress=\"return AECheckMaxLength(event,this)\" onkeyup=\"textCounter(this,'spnRemaining',160);\">e.g., Is it better to join IIM straight after college or after a few years of work?</textarea>"
+"<div class='clearAll spacer5'> </div>"
+"<small class='flLt white'><!--Characters allowed: 160<br />-->"
+"Characters remaining: <span id=\"spnRemaining\">160</span></small>"
+"</div>"
+"<div class='clearAll spacer5'></div>"
+ createCats(tDiv)   
+"<div class='clearAll spacer5'></div><input type='radio'  id='isP1' name='isP' value='p' checked>"
+"<label for='public' class='white'>"
+" Public	     </label> <input type='radio'  id='isP2' name='isP'  class='marginLeft10' value='r'>"
+"<label for='private' class='white'>Private </label>"
+"<!--<br />  <input type='checkbox' id='chkEmail' value='1' name='r2'>"
+"<label for='email' class='white'>    Email me the answer</label>-->"
+"<div class='clearAll spacer10'></div>"
+"<div id='divSStatus' style='display:none;font-weight:bold;' class='white flLt'><img src='" + appPath + "images/load.gif' /> Saving...</div>"
+"<div id='divButtons' class='flLt'><img id='imgAskExpertNow' src='" + appPath + "images/buttons/asknow.gif' alt='Ask' style='cursor:hand;cursor:pointer;' onclick='saveQuestion(\"" + tDiv + "\",\"" + askername + "\");' />" 
+"&nbsp;&nbsp;<input type='image' src='" + appPath + "images/buttons/clear.gif' alt='Clear'" +  "onclick= 'AEResetAll(); return false;' />"
+"</div>"
+"<div class='clearAll'></div><small id='errorQue' class='clearAll redTxt' >&nbsp;</small> </div> "
+ "<div id='askMsg' align='left' style='display:none; width:216px;'><div class='clearAll spacer18'></div><div class='white flLt' style='font-size:14px;font-weight:bold;text-align:center'>Your question has been sent to " + nickName.split("|")[0] + "</div><div class='clearAll spacer18'><div class='clearAll spacer18'></div></div><div style='font-size:14px;text-align:center'><strong><a href='javascript:return false;' onclick=\"AEResetAnother();return false;\">Ask another question</a></strong> | <strong><a  href='javascript:return false;' onclick=\"AEHide('" + tDiv + "'); return false;\">Close</a></strong></div></div>"
+ " </div> </div><div class='arr'></div>";         
         
       divName=tDiv;         
       var aa=document.getElementById(tDiv);   
       aa.innerHTML=x;      
       openAskMenu(tDiv);      
 }
 function AEResetAnother()
 {
    document.getElementById('askMsg').style.display = 'none';
    document.getElementById('askMainContent').style.display = 'block';
    AEResetAll();
    askmeState = 0;
 }
 function AEResetAll()
 {  
    var s=document.getElementById("taSubject");    
    if(isKwality == 0)
        s.value='e.g., Is it better to join IIM straight after college or after a few years of work?';
    else
        s.value='e.g., What is love? Why do people fall in love?';
    s.style.color = '#c9c9c9';
    if(document.getElementById("ddlCat") != null)
    {
        var ct=document.getElementById("ddlCat");           
        ct.selectedIndex=0;
    }
    document.getElementById("isP1").checked=true; 
    document.getElementById('errorQue').innerHTML='';
    document.getElementById("divSStatus").style.display = 'none';
    document.getElementById("divButtons").style.display = 'block';
    document.getElementById('spnRemaining').innerHTML='160';
 }
 function createCats(tDiv)
 {
    var tb=document.getElementById("hdn"+tDiv);
    var tbVal=tb.value;
    var tbValArr=tbVal.split("^");
    toExpertID=tbValArr[0];
    var arCatIDs=tbValArr[1].split("|");
    var arCatNames=tbValArr[2].split("|");
    var opt="";        
    for(var i=0;i<arCatNames.length;i++)
    {
        opt= opt +"<option value=" + arCatIDs[i] + ">" + arCatNames[i]  + "</option>";            
    }
    return "<select id='ddlCat' style='width:216px'><option value='-99'>Select Category</option>" + opt + "</select>";
 }
 function close(tDiv)
 {
    var aa=document.getElementById(tDiv);       
    aa.innerHTML="";
    openAskMenu(tDiv);
 }
 function AETrimAll(sString)
 {
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
 }
 var askmeState = 0;
 function saveQuestion(tDiv, nickname)
 {          
    var s=AETrimAll(document.getElementById("taSubject").value);           
    s = s.replace(/[\n\r]/g, "");	
    if(s.length == 0 || s == 'e.g., Is it better to join IIM straight after college or after a few years of work?')
    {
        document.getElementById('errorQue').innerHTML="Enter question";
        return false;
    }    
    if(s.length < 20)
    {
        document.getElementById('errorQue').innerHTML="Enter long question";
        return false;	
    }            
    var ct=document.getElementById("ddlCat");           
    if(ct.selectedIndex<=0)
    {
        document.getElementById('errorQue').innerHTML="Select Category";
        return false; 
    } 
    //var isP=document.getElementById("isP"); 
    var em="0";
    var pr="p";
    
    var user=  document.getElementById("hdnUser").value; 
    if(document.getElementById("isP2").checked)
        pr="r";  
    em="1";       
    if(askmeState == 0)
    {
        askmeState = 1;
        AutoComplete.SaveQuestionToExpert(s,ct.value,ct.options[ct.selectedIndex].text,pr,em,user.split("|")[0],user.split("|")[1],toExpertID, nickname,saveQuestion_Callback);
        document.getElementById('errorQue').innerHTML='';
        document.getElementById("divSStatus").style.display = 'block';
        document.getElementById("divButtons").style.display = 'none';
    }  
 }
 function saveQuestion_Callback(res,eventArgs)
 {  
    var arr=res.value.split("/");
    if(arr[0]=="1")
    {
        document.getElementById('askMainContent').style.display = 'none';
        document.getElementById('askMsg').style.display = 'block';
    }    
    else
    {
        document.getElementById("divSStatus").style.display = 'none';
        document.getElementById("divButtons").style.display = 'block';
        if(arr[0]=="2")
        {
            document.getElementById('errorQue').innerHTML = 'Please login to ask question';
        }
        else if(arr[0]=="3")
        {
            document.getElementById('errorQue').innerHTML = 'Enter question';
            document.getElementById("taSubject").focus();
        }
        else if(arr[0]=="5")
        {
            document.getElementById('errorQue').innerHTML = 'Enter long question';
            document.getElementById("taSubject").focus();
        }
        else if(arr[0]=="6")
        {
            document.getElementById('errorQue').innerHTML = '160 characters allowed';
            textCounter(document.getElementById("taSubject"),'spnRemaining',160);
            document.getElementById("taSubject").focus();
        }
        else 
            alert("There is a problem in posting the question"); 
    } 
    askmeState = 0;       
 }
 function textCounter(field,cntfield,maxlimit)
 {
    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);    
    else    // otherwise, update 'characters left' counter
        document.getElementById(cntfield).innerHTML = maxlimit - field.value.length;
}
function AECheckMaxLength(e,el) 
{  
    switch(e.keyCode) 
    {
        case 37: // left
        return true;
        case 38: // up
        return true;
        case 39: // right
        return true;
        case 40: // down
        return true;
        case 8: // backspace
        return true;
        case 46: // delete
        return true;
        case 27: // escape
        el.value='';
        return true;
    }  
    var str = AETrimAll(document.getElementById('taSubject').value);  
    str = str.replace(/[\n\r]/g, "");     
    var len = str.length;
    if(len == 0)
        document.getElementById('taSubject').value = str;
    if(len<160)
    {          
      if(e.keyCode==13) 
      {
            if(len<=160 && len != 0)             
           {     
               //getURLParameters();
           }
           else
           {                  
                if(len == 0)            
                {
                    document.getElementById('taSubject').value = '';
                    document.getElementById('errorQue').innerHTML='';
                    return;
                }
                else
                    alert("Characters allowed: 160");
           }   
      }
      document.getElementById('errorQue').innerHTML='';
      return true;
    }       
    else
    {      
     if(e.keyCode==13)   
        if(len==160)
        {          
          getURLParameters();
        }    
      return false;
    }
}
function AETrimAll(sString)
{
    while (sString.substring(0,1) == ' ')
    {
    sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
    sString = sString.substring(0,sString.length-1);
    }
    return sString;
}
function openKwality(tDiv,nickName,askedtoid,cid,cname)
{
       isKwality = 1;
       askmeState = 0;
       if(document.getElementById(divName)!=null)
       {           
            AEHide(divName);
       }       
       var x=
"<div id='askwin' class='kwPopupBg' style='position:absolute;'><div class='pad5 kwPopInnerBg' align='left'>"
+"<div style='width:222px; float:left; *padding-bottom:10px;'><h4 class='flLt white'>Ask " + nickName +  "</h4> <a href='javascript:return false;' onclick=\"AEHide('" + tDiv + "'); return false;\"><img src='" + appPath + "images/kw-close-icon.gif' alt='Close'  class='flRt' /></a></div>"
+"<div class='clearAll spacer10'></div>"    
+"<div id='askMainContent' align='left'>"
+"<div  align='left'>"
+"<textarea id='taSubject' name='taSubject' rows='3' class='globalInput' style='color: #c9c9c9; width:216px; overflow:auto;' onfocus=\"if (this.value == 'e.g., What is love? Why do people fall in love?') {this.value = '';this.style.color = '#333';}\"" 
+"onblur=\"if (this.value == '') {this.value = 'e.g., What is love? Why do people fall in love?';this.style.color = '#c9c9c9';}\" value='e.g., What is love? Why do people fall in love?' max='160' onkeypress=\"return AECheckMaxLength(event,this)\" onkeyup=\"textCounter(this,'spnRemaining',160);\">e.g., What is love? Why do people fall in love?</textarea>"
+"<div class='clearAll spacer5'> </div>"
+"<small class='flLt'>"
+"Characters remaining: <span id=\"spnRemaining\">160</span></small>"
+"</div>"
+"<div class='clearAll spacer5'></div>"
+"<div class='clearAll spacer5'></div><input type='radio'  id='isP1' name='isP' value='p' checked>"
+"<label for='public' class=''>"
+" Public	     </label> <input type='radio'  id='isP2' name='isP'  class='marginLeft10' value='r'>"
+"<label for='private' class=''>Private </label>"
+"<!--<br />  <input type='checkbox' id='chkEmail' value='1' name='r2'>"
+"<label for='email' class='white'>    Email me the answer</label>-->"
+"<div class='clearAll spacer10'></div>"
+"<div id='divSStatus' style='display:none;font-weight:bold;' class='white flLt'><img src='" + appPath + "images/load.gif' /> Saving...</div>"
+"<div id='divButtons' class='flLt' style='width:135px;'><img id='imgAskExpertNow' src='" + appPath + "images/kw-ask-now-btn.gif' alt='Ask' style='cursor:hand;cursor:pointer;' onclick='saveKwalityQue(\"" + tDiv + "\",\"" + askedtoid + "\",\"" + cid + "\",\"" + cname + "\");' />" 
+"&nbsp;&nbsp;<input type='image' src='" + appPath + "images/kw-clear-btn.gif' alt='Clear'" +  "onclick= 'AEResetAll(); return false;' />"
+"<small id='errorQue' class='redTxt' style='float:left;'>&nbsp;</small> </div>"
+"</div> "
+ "<div id='askMsg' align='left' style='display:none; width:216px; height:137px; _height:150px; padding-top:25px;'><div class='clearAll spacer18'></div><div class='white flLt' style='font-size:14px;font-weight:bold;text-align:center; color:#DA251C !important;'>Your question has been sent to " + nickName + "</div><div class='clearAll spacer18'><div class='clearAll spacer18'></div></div><div style='font-size:12px;text-align:center;'><strong><a style='color:#000;' href='javascript:return false;' onclick=\"AEResetAnother();return false;\">Ask another question</a></strong> | <strong><a style='color:#000;' href='javascript:return false;' onclick=\"AEHide('" + tDiv + "'); return false;\">Close</a></strong></div></div>"
+ " </div> </div><div class='kwArr'></div>";         
         
       divName=tDiv;         
       var aa=document.getElementById(tDiv);   
       aa.innerHTML=x;      
       openAskMenu(tDiv);      
 }
 function saveKwalityQue(tDiv, toExpertID,cid,cname)
 {          
    var s=AETrimAll(document.getElementById("taSubject").value);           
    s = s.replace(/[\n\r]/g, "");	
    if(s == 'e.g., What is love? Why do people fall in love?')
    {
        document.getElementById('errorQue').innerHTML="Enter question";
        return false;
    }    
    if(s.length < 20)
    {
        document.getElementById('errorQue').innerHTML="Enter long question";
        return false;	
    }           
    var em="0";
    var pr="p";
    var user=  document.getElementById("hdnUser").value; 
    if(document.getElementById("isP2").checked)
        pr="r";  
    em="1";       
    if(askmeState == 0)
    {
        askmeState = 1;
        AutoComplete.SaveQuestionToExpert(s,cid,cname,pr,em,user.split("|")[0],user.split("|")[1],toExpertID, user.split("|")[2],saveQuestion_Callback);
        document.getElementById('errorQue').innerHTML='';
        document.getElementById("divSStatus").style.display = 'block';
        document.getElementById("divButtons").style.display = 'none';
    }  
 }