﻿// Ajax Client Script File

function FindElement(id)
{
    return document.getElementById(id);
}

function Value(id)
{
    var ctrl = FindElement(id);
    return ctrl.value;
}

 // Put this information into the HTML and hence screen
 // 0|error message
 // location|HTML
 function Place(res)
 {
    if (res.error == null)
    {
	    var result = res.value
	    var listTokens = result.split('|')

	    if (listTokens[0] == '0')
	    {
		    alert(listTokens[1]);
	    }
	    else {
	        try {
	            FindElement(listTokens[0]).innerHTML = listTokens[1];
	        }
	        catch (e) {
	            document.getElementById("txtName").value = "";
	            document.getElementById("txtEmail").value = "";
	            document.getElementById("txtCompany").value = "";
	            document.getElementById("phonenumber").value = "";
	            document.getElementById("txtComments").value = "";
	            document.getElementById("MessageConfirmation").innerHTML = "<br/><p style=\"color:red;\">Your message has been sent, someone will contact you shortly.</p>";
	        }
	    }
	}
	else
	{
	    alert(res.error);
	}
 }
 
 function Ok(res)
 {
	var result = res.value
	var listTokens = result.split('|')
					
	return (listTokens[0] != '0');
 }
 
 function AlertOnError(res)
 {
	var result = res.value
	var listTokens = result.split('|')
					
	if (listTokens[0] == '0')
	{
		alert(listTokens[1])
    }		
 }


/*
function SubmitApplication()
{
    FindElement('review').innerHTML="<img src='../app_themes/standard/images/ajaxloader.gif'/>";
    document.body.style.cursor = "wait";
    ApplicationReviewControl.FireSubmit(FireSubmit_Callback);
}

function FireSubmit_Callback(response)
{
    FindElement('review').innerHTML='<p>Application submitted.</p>';
    document.body.style.cursor = "default";
}
*/

function SendContactUsEmail()
{
    var name = Value("txtName");
    var email = Value("txtEmail");
    var company = Value("txtCompany");
    var comments = Value("txtComments");
    
    AjaxMethods.SendContactUsEmail("contactus",name,email,company,comments,SendContactUsEmail_CallBack);
}

function ForwardContactUsForm(formFields)
{
    if (formFields != null && formFields.length>0)
    {
        var formFieldValues = '';
        
        var formFieldArray = formFields.split("|");
        
        for(index = 0; index<formFieldArray.length;index++)
        {
            var elem = document.getElementById(formFieldArray[index]);
            
            if (elem != null)
            {               
                if ((elem.tagName == "INPUT" && elem.type == "text") || (elem.tagName == "TEXTAREA"))
                {
                    if (formFieldValues.length>0)
                    {
                        formFieldValues += '|';
                    }
                                
                    formFieldValues += formFieldArray[index] + '~' + elem.value;
                }
            }
        }
           
        Place(AjaxMethods.ForwardContactUsForm("contactus",formFieldValues));
    }
}

function SendContactUsEmail_CallBack(response)
{
    Place(response);
}

function ValidateQuestion_TextBox(elemId, questionId)
{   
    var val = document.getElementById(elemId).value;   
    Place(ApplicationControl.FireValidateQuestion(questionId,val));
}

function ValidateQuestion_RadioButtonList(radioButtonGroup, questionId)
{   
    var val = '';
    
    for (i=0;i<radioButtonGroup.length;i++) 
    {
	    if (radioButtonGroup[i].checked) 
	    {
		    val = radioButtonGroup[i].value;
		    break;
        }		    
	}
	
	Place(ApplicationControl.FireValidateQuestion(questionId,val));
}

function ValidateQuestion_RadioButton(elemId, questionId)
{   
    var val = document.getElementById(elemId).value;   
    Place(ApplicationControl.FireValidateQuestion(questionId,val));
}

function PreviousClientSelect_OnClientSelect(clientDetails)
{    
    // Make server side call to setup previous client details
    var result = AjaxMethods.SetPreviousClient(clientDetails);
    AlertOnError(result);
    
    if (Ok(result))
    {
        // Close the popup window. Passing true ensures that the submodal
        // return function gets called (which refreshes the page and refreshes the responses
        // held by the  question controls)
        window.top.hidePopWin(true);
    }        
}

