// JScript File

// Validation for Login.aspx
function ValidateLogin(usercontrolId)
{
    var txtEmailId = document.getElementById(usercontrolId +  "_txtEmailId");
    var txtPassword = document.getElementById(usercontrolId +  "_txtPassword");
    
    if (Trim(txtEmailId.value) == "")
    {
        alert("Please insert Email Id.");
        txtEmailId.focus();
        return false;
    }
    else if (ValidateEmail(Trim(txtEmailId.value)) == false)
    {
        txtEmailId.value = "";
        txtEmailId.focus();
        return false;
    }
    
    if (Trim(txtPassword.value) == "")
    {
        alert("Please insert Password.");
        txtPassword.focus();
        return false;
    }
}

function ValidateHomePageLogin()
{
    var txtEmailId = document.getElementById("txtEmailId");
    var txtPassword = document.getElementById("txtPassword");
    
    if (Trim(txtEmailId.value) == "")
    {
        alert("Please insert Email Id.");
        txtEmailId.focus();
        return false;
    }
    else if (ValidateEmail(Trim(txtEmailId.value)) == false)
    {
        txtEmailId.value = "";
        txtEmailId.focus();
        return false;
    }
    
    if (Trim(txtPassword.value) == "")
    {
        alert("Please insert Password.");
        txtPassword.focus();
        return false;
    }
}


// Validation for Registration.aspx
function ValidateRegistration(usercontrolId, mode)
{
    var txtFirstName = document.getElementById(usercontrolId +  "_txtFirstName");
    var txtLastName = document.getElementById(usercontrolId +  "_txtLastName");
    var txtEmailId = document.getElementById(usercontrolId +  "_txtEmailId");
    var txtPassword = document.getElementById(usercontrolId +  "_txtPassword");
    var txtConfirmPassword = document.getElementById(usercontrolId +  "_txtConfirmPassword");
    var txtAddress1 = document.getElementById(usercontrolId +  "_txtAddress1");
    var ddlCountry = document.getElementById(usercontrolId +  "_ddlCountry");
    var ddlState = document.getElementById(usercontrolId +  "_ddlState");
    var txtState = document.getElementById(usercontrolId +  "_txtState");
    var ddlCity = document.getElementById(usercontrolId +  "_ddlCity");
    var txtCity = document.getElementById(usercontrolId +  "_txtCity");
    var hdnPicChar = document.getElementById(usercontrolId +  "_hdnPicChar");
    var txtCharector = document.getElementById(usercontrolId +  "_txtCharector");
    var ddlVerticle = document.getElementById(usercontrolId +  "_ddlVerticle");
    var txtComments = document.getElementById(usercontrolId +  "_txtComments");
    
    if (Trim(txtFirstName.value) == "")
    {
        alert("Please insert First Name.");
        txtFirstName.focus();
        return false;
    }
    
    if (Trim(txtLastName.value) == "")
    {
        alert("Please insert Last Name.");
        txtLastName.focus();
        return false;
    }
    
    if (Trim(ddlVerticle.value) == "" || Trim(ddlVerticle.value) == "0")
    {
        alert("Please select Verticle.");
        ddlVerticle.focus();
        return false;
    }
    
    if (Trim(txtEmailId.value) == "")
    {
        alert("Please insert Email Address.");
        txtEmailId.focus();
        return false;
    }
    else if (ValidateEmail(Trim(txtEmailId.value)) == false)
    {
        txtEmailId.value = "";
        txtEmailId.focus();
        return false;
    }
    
    if (mode == "edit")
    {
        if (Trim(txtPassword.value) != "")
        {
            if (Trim(txtPassword.value).length < 6)
            {
                alert("Password must have at least 6 char.");
                txtPassword.value = "";
                txtPassword.focus();
                return false;
            }
        }
    }
    else
    {
        if (Trim(txtPassword.value) == "")
        {
            alert("Please insert Password.");
            txtPassword.focus();
            return false;
        }
        else if (Trim(txtPassword.value).length < 6)
        {
            alert("Password must have at least 6 char.");
            txtPassword.value = "";
            txtPassword.focus();
            return false;
        }
    }
    
    if (mode == "edit")
    {
        if (Trim(txtPassword.value) != "")
        {
            if (Trim(txtConfirmPassword.value) == "")
            {
                alert("Please insert Confirm Password.");
                txtConfirmPassword.focus();
                return false;
            }
            else if (Trim(txtConfirmPassword.value).length < 6)
            {
                alert("Confirm Password must have at least 6 char.");
                txtConfirmPassword.value = "";
                txtConfirmPassword.focus();
                return false;
            }
        }
    }
    else
    {
        if (Trim(txtConfirmPassword.value) == "")
        {
            alert("Please insert Confirm Password.");
            txtConfirmPassword.focus();
            return false;
        }
        else if (Trim(txtConfirmPassword.value).length < 6)
        {
            alert("Confirm Password must have at least 6 char.");
            txtConfirmPassword.value = "";
            txtConfirmPassword.focus();
            return false;
        }
    }
    
    if (Trim(txtPassword.value) != "" && Trim(txtConfirmPassword.value) != "")
    {
        if (ConfirmPassword(txtPassword, txtConfirmPassword) == false)
            return false;
    }
    
    if (Trim(txtAddress1.value) == "")
    {
        alert("Please insert Address.");
        txtAddress1.focus();
        return false;
    }
    
    if (Trim(ddlCountry.value) == "" || Trim(ddlCountry.value) == "0")
    {
        alert("Please select Country.");
        ddlCountry.focus();
        return false;
    }
    
    if (Trim(ddlState.value) == "" || Trim(ddlState.value) == "0" || Trim(ddlState.value) == "Select")
    {
        alert("Please select State.");
        ddlState.focus();
        return false;
    }
    else if (Trim(ddlState.value) == "Other")
    {
        if (Trim(txtState.value) == "")
        {
            alert("Please insert other state name.");
            txtState.focus();
            return false;
        }
    }
    
    if (Trim(ddlCity.value) == "" || Trim(ddlCity.value) == "0"  || Trim(ddlCity.value) == "Select")
    {
        alert("Please select City.");
        ddlCity.focus();
        return false;
    }
    else if (Trim(ddlCity.value) == "Other")
    {
        if (Trim(txtCity.value) == "")
        {
            alert("Please insert other city name.");
            txtCity.focus();
            return false;
        }
    }
    
//    if (Trim(txtComments.value) == "")
//    {
//        alert("Please enter the Additional Information.");
//        txtComments.focus();
//        return false;
//    }
    
    if (Trim(txtCharector.value) == "")
    {
        alert("Please enter characters in the picture.");
        txtCharector.focus();
        return false;
    }
    else if (hdnPicChar.value != Trim(txtCharector.value))
    {
        alert("Characters are different than the picture.");
        txtCharector.focus();
        return false;
    }
    
    return true;
}

function EnableSubmit(crtl1, crtl2)
{
    var btnSubmit = document.getElementById(crtl1);
    var chkTerms = document.getElementById(crtl2);
    if (chkTerms.checked)
        btnSubmit.disabled = false;
    else
        btnSubmit.disabled = true;
}

// Validation for Registration.aspx to check the email exist or not.
function CheckEmailExistsForUser(usercontrolId, mode, email)
{   
    txtEmailId = document.getElementById(usercontrolId + "_txtEmailId");
    var hdnUserId = document.getElementById(usercontrolId + "_hdnUserId");
    if(email != Trim(txtEmailId.value))
    {
        if(Trim(txtEmailId.value)!="")
        {
            if (mode == "edit")
                CheckEmailExistsEditMode(txtEmailId.value, hdnUserId.value, ServerSideCallBack_CheckEmailExistsEditMode);
            else
                CheckEmailExists(txtEmailId.value,ServerSideCallBack_CheckEmailExists);
        }
    }
}

function ServerSideCallBack_CheckEmailExists(response)
{
    txtEmailId = document.getElementById("UcRegistration1_txtEmailId");
    if(response.Rows.length != 0 )
    {
        alert("Email Id is already in use.Please select another email");
        txtEmailId.value = "";
        txtEmailId.focus();
        return false;
    }
    else
    {
        return true;
    }
}

function ServerSideCallBack_CheckEmailExistsEditMode(response)
{
    txtEmailId = document.getElementById("UcRegistration1_txtEmailId");
    
    if(response == true)
    {
        alert("Email Id is already in use.Please select another email");
        txtEmailId.value = "";
        txtEmailId.focus();
        return false;
    }
    else
    {
        return true;
    }
}

function GetSelectedAnswer(iAnswerId)
{
//alert(iAnswerId);
    var hdnAnswerId = document.getElementById("UcQuestion1_hdnAnswerId");
    hdnAnswerId.value = iAnswerId;
}
function SetState(id,CatId)
{
    var hdnState = document.getElementById("UcQuestion1_hdnState")
    var hdnQID = document.getElementById("UcQuestion1_hdnQID");
    var hdnQIDPrev = document.getElementById("UcQuestion1_hdnQidPrev");
    var hdnCurrentQID = document.getElementById("UcQuestion1_hdnCurrentQID");
    var hdnCategoryId = document.getElementById("UcQuestion1_hdnCategoryId");
    if(id=="1")
    {
        hdnState = id;
        hdnQID.value = "";
        hdnQIDPrev.value = "";
        hdnCurrentQID.value ="";
    }
    else
    {
        hdnState.value = id;
    }
    if(CatId != "")    
    {
        hdnCategoryId.value = CatId;
    }
}
function SaveData(iQuestionId)
{ 
 var btnNext = document.getElementById("UcQuestion1_imgNextq");
 var hdnQID = document.getElementById("UcQuestion1_hdnQID");
 var hdnQIDPrev = document.getElementById("UcQuestion1_hdnQidPrev");
 var hdnCurrentQID = document.getElementById("UcQuestion1_hdnCurrentQID");
 var hdnState = document.getElementById("UcQuestion1_hdnState")
 hdnState.value = "";
 hdnCurrentQID.value = iQuestionId;
 if(hdnQID.value != "" && hdnQID.value != "0")
 {
   hdnQIDPrev.value = hdnQID.value;
 }   
 else
 {
  hdnQID.value = iQuestionId;
 }
}

function ValidateButton()
{
    var chkTerms = document.getElementById("chkTermsandConditions");
    var btnTerms = document.getElementById("btnTermsandConditions");
    if(chkTerms.checked == false)
    {
        btnTerms.disabled = true;
    }
    else
    {
        btnTerms.disabled = false;
    }
}

function GetQuestion(ctrlId)
{
    var hdnQuestionId = document.getElementById("UcQuestion1_hdnQuestion");
    hdnQuestionId.value = ctrlId;    
}


function passwordStrength(password)
{
	var desc = new Array();
	desc[0] = "Too Short";
	desc[1] = "Fair";
	desc[2] = "Good";
	desc[3] = "Strong";
	desc[4] = "Strongest";	

	var score   = 0;

	//if password bigger than 6 give 1 point
	if (password.length > 6) score++;

	//if password has both lower and uppercase characters give 1 point	
	if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;

	//if password has at least one number give 1 point
	if (password.match(/\d+/)) score++;

	//if password has at least one special caracther give 1 point
	if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) )	score++;

	//if password bigger than 12 give another 1 point
	if (password.length > 12) score++;

	 document.getElementById("passwordDescription").innerHTML = desc[score];
	 document.getElementById("passwordStrength").className = "strength" + score;
	 document.getElementById("passwordDescription").className = "strengthText" + score; 
}

function ValidateUserRegistration()
{
    var txtFirstName = document.getElementById("UcQuestion1_txtFirstName");
    var txtLastName = document.getElementById("UcQuestion1_txtLastName");
    var txtEmailId = document.getElementById("UcQuestion1_txtEmailId");
    var txtOtherCity = document.getElementById("UcQuestion1_txtOtherCity");
    var ddlCountry = document.getElementById("UcQuestion1_ddlCountry");
    
    if (Trim(txtFirstName.value) == "")
    {
        alert("Please insert First Name.");
        txtFirstName.focus();
        return false;
    }
    
    if (Trim(txtLastName.value) == "")
    {
        alert("Please insert Last Name.");
        txtLastName.focus();
        return false;
    }
    
    if (Trim(txtEmailId.value) == "")
    {
        alert("Please insert Email Address.");
        txtEmailId.focus();
        return false;
    }
    else if (ValidateEmail(Trim(txtEmailId.value)) == false)
    {
        txtEmailId.value = "";
        txtEmailId.focus();
        return false;
    }
    
    if (ddlCountry.value == "0")
    {
        alert("Please select country.");
        ddlCountry.focus();
        return false;
    }
    
    if (Trim(txtOtherCity.value) == "")
    {
        alert("Please insert city.");
        txtOtherCity.focus();
        return false;
    }
    return true;
}

function SetCorrectCount(ctrl)
{
    var check1 = document.getElementById("UcQuestion1_" + ctrl);
    var hdnTotalCorrectCount = document.getElementById("UcQuestion1_hdnTotalCorrectCount");
    var hdnSelectedCount = document.getElementById("UcQuestion1_hdnSelectedCount");
    
    if (check1.checked == true)
    {
        if (parseInt(hdnTotalCorrectCount.value) == parseInt(hdnSelectedCount.value))
        {
            alert("You can select only " + hdnTotalCorrectCount.value + " options.");
            check1.checked = false;
            return false;
        }
        else
        {
            hdnSelectedCount.value = parseInt(hdnSelectedCount.value) + 1;
        }
    }
    else if (check1.checked == false)
    {
        if (parseInt(hdnSelectedCount.value) != 0)
        {
            hdnSelectedCount.value = parseInt(hdnSelectedCount.value) - 1;
        }
    }
}

//This is to get confirmation before single or multiple user(s)/assessment(s) deletion
function ConfirmDeleteMessage(ctrl)
{

              if(GetSelectedRowsCount()<1)//count==0)
              {
                    alert("Please select " + ctrl + "(s) to be deleted.");
                    return false;
              }
              else 
              {
                    if(GetSelectedRowsCount()>1)//count>1)
                    {
                        if(ctrl=='user')
                             return confirm("Are you sure you want to delete these " + ctrl + "s?");
                        else if(ctrl=='assessment')
                             return confirm('Are you sure you want to delete these assessments?\r\n\nOn delete of the assessment you may loose all data and responses completely.')
                        else if(ctrl=='category')
                             return confirm('Are you sure you want to delete these categories?\r\n\nOn delete of the category you may loose all data and responses completely.')
                        else if(ctrl=='question')
                            return confirm('Are you sure you want to delete these questions? If yes, then all the Answers and Answer Reports related to the questions get deleted.');
                    }
                    else
                    {
                        if(ctrl=='user')
                              return confirm("Are you sure you want to delete this " + ctrl + "?");
                        else if(ctrl=='assessment' || ctrl=='category')
                              return confirm("Are you sure you want to delete this " + ctrl + "?\r\n\nOn delete of the " + ctrl + " you may loose all data and responses completely.")
                        else if(ctrl=='question')
                            return confirm('Are you sure you want to delete this question? If yes, then all the Answers and Answer Reports related to the question get deleted.');
                    }
              }
}

function ConfirmSingleQuestionDelete()
{
	if(!confirm('Are you sure you want to delete this question? If yes, then all the Answers and Answer Reports related to the question get deleted.'))
    {
        return false;
    }
    else
    {
        return true;
    }
}

//This is to validate Category selection
function ValidateCategorySelect()
{
    if(GetSelectedRowsCount()<1)
    {
        alert("Please select at least one category.");
        return false;
    }
    return true;
}

function ValidateForAssign(control)
{
    var hiddenCatIDs = document.getElementById(control + "_hiddenCatIDs");
    if (hiddenCatIDs.value == "" && GetSelectedRowsCount() < 1)
    {
        alert("Please select atleast one record to assign.");
        return false;
    }
}

//This is to validate question selection
function ValidateCategorySelect()
{
    if(GetSelectedRowsCount()<1)
    {
        alert("Please select at least one question.");
        return false;
    }
    return true;
}

function ValidateForgotPassword()
{
    var txtEmailId = document.getElementById("txtEmailId");
    if (Trim(txtEmailId.value) == "")
    {
        alert("Please insert Email Id.");
        txtEmailId.focus();
        return false;
    }
    else if (ValidateEmail(Trim(txtEmailId.value)) == false)
    {
        txtEmailId.value = "";
        txtEmailId.focus();
        return false;
    }
}

function ValidateSubscription()
{
    var hdnSelectedPlanId = document.getElementById("UcMyAccount1_hdnSelectedPlanId");
    var rblSubscriptionPlans = document.getElementById("UcMyAccount1_rblSubscriptionPlans");
    var isSelected = false;
    if (hdnSelectedPlanId.value != "")
        isSelected = true;
//    for (var i = 0; i < rblSubscriptionPlans.cells.length; i++)
//    {
//        if (rblSubscriptionPlans.cells[i].childNodes[0].checked)
//        {
//            isSelected = true;
//        }
//    }
    if (isSelected == false)
    {
        alert("Please select Subscription Plan.");
        //rblSubscriptionPlans.focus();
        return false;
    }
}

function ShowPlanDetails(input)
{
    var hdnSelectedPlanId = document.getElementById("UcMyAccount1_hdnSelectedPlanId");
    if (input != "")
    {
        SelectedPlanDetails(input,ServerSidePlanDetails_CallBack);
        hdnSelectedPlanId.value = input;
    }
}

function ServerSidePlanDetails_CallBack(response)
{
    var lblPlanDetails = document.getElementById("UcMyAccount1_lblPlanDetails");
    if (response != "")
    {
        lblPlanDetails.innerHTML = response;
    }
    else
    {
        lblPlanDetails.innerHTML = "Please select the plan.";
    }
}

function ValidateContactUs()
{
    var txtEmail = document.getElementById("txtEmail");
    var txtSubject = document.getElementById("txtSubject");
    var txtMessage = document.getElementById("txtMessage");
    
    if (Trim(txtEmail.value) == "")
    {
        alert("Please insert your Email Address.");
        txtEmail.focus();
        return false;
    }
    else if (ValidateEmail(Trim(txtEmail.value)) == false)
    {
        txtEmail.value = "";
        txtEmail.focus();
        return false;
    }
    
    if (Trim(txtSubject.value) == "")
    {
        alert("Please insert Subject.");
        txtSubject.focus();
        return false;
    }
    
    if (Trim(txtMessage.value) == "")
    {
        alert("Please insert your Questions/Comments.");
        txtMessage.focus();
        return false;
    }
}

function ValidateSupport()
{
    var txtName = document.getElementById("txtName");
    var txtEmailId = document.getElementById("txtEmailId");
    var ddlAccountType = document.getElementById("ddlAccountType");
    var ddlInquiryType = document.getElementById("ddlInquiryType");
    var txtMessage = document.getElementById("txtMessage");
    
    if (Trim(txtName.value) == "")
    {
        alert("Please insert Name.");
        txtName.value="";
        txtName.focus();
        return false;
    }
    
    if (Trim(txtEmailId.value) == "")
    {
        alert("Please insert Email Address.");
        txtEmailId.focus();
        return false;
    }
    else if (ValidateEmail(Trim(txtEmailId.value)) == false)
    {
        txtEmailId.value = "";
        txtEmailId.focus();
        return false;
    }
    
    if (Trim(ddlAccountType.value) == "" || Trim(ddlAccountType.value) == "0")
    {
        alert("Please select Current Account Type.");
        ddlAccountType.focus();
        return false;
    }
    
    if (Trim(ddlInquiryType.value) == "" || Trim(ddlInquiryType.value) == "0")
    {
        alert("Please select Type of Inquiry.");
        ddlInquiryType.focus();
        return false;
    }
    
    if (Trim(txtMessage.value) == "")
    {
        alert("Please insert Message or Question.");
        txtMessage.value="";
        txtMessage.focus();
        return false;
    }
}