﻿/// <reference name="MicrosoftAjax.js"/>


Type.registerNamespace("Controls");

Controls.ContactForm = function(element) {

    
    Controls.ContactForm.initializeBase(this, [element]);
    
}

//var ClientID;

Controls.ContactForm.prototype = {
    initialize: function() {
        Controls.ContactForm.callBaseMethod(this, 'initialize');
        ClientID = this.get_id();
       
    },
    dispose: function() {        
        //Add custom dispose actions here
        Controls.ContactForm.callBaseMethod(this, 'dispose');
    }
    
    
}
Controls.ContactForm.registerClass('Controls.ContactForm', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


// scriptable client object
function ContactForm(ClientID) {

  this.ClientID = ClientID;  
  this.SiteID = window.document.getElementById(ClientID + "_SiteID").value;
  
  /*this.SetMailbox = function (MailboxID) 
  {
    window.document.getElementById(ClientID + "_Subject").value = AdminUserID;
  };*/
  
  // subject
  this.get_subject = function () 
  {
    return window.document.getElementById(ClientID + "_Subject").value;
  };
  
  this.set_subject = function (subject) 
  {
    window.document.getElementById(ClientID + "_Subject").value = subject;
  };
  
  // message body
  this.get_message = function () 
  {
    return window.document.getElementById(ClientID + "_Message").value;
  };
  
  this.set_message = function (message) 
  {
    window.document.getElementById(ClientID + "_Message").value = message;
  };
  
  // admin userid
  this.get_mailboxID = function () 
  {
    return window.document.getElementById(ClientID + "_MailboxID").value;
  };
  
  this.set_mailboxID = function (mailboxID) 
  {
    window.document.getElementById(ClientID + "_MailboxID").value = mailboxID;
    // load mailbox details
    this.load_mailbox();
  };

  // load mailbox
  this.load_mailbox = function () 
  {
    var wRequest = new Sys.Net.WebRequest();
    wRequest.set_url("idxproxy.aspx");
    wRequest.set_httpVerb("POST");
    var body = "ID=2&SiteID=" + this.SiteID + "&MailboxID=" + this.get_mailboxID();
    wRequest.set_body(body);
    wRequest.get_headers()["Content-Length"] = body.length;
    try { wRequest.add_completed(load_mailboxComplete); } catch(err) {}
    wRequest.invoke();
  };
  
  var load_mailboxComplete = function(executor, eventArgs)
  {
    window.document.getElementById(ClientID + "_To").value = executor.get_responseData();
  }
  
  
  // customer userid
  this.get_userID = function () 
  {
    return window.document.getElementById(ClientID + "_FromUserID").value;
  };
  
  this.set_userID = function (userID) 
  {
    window.document.getElementById(ClientID + "_FromUserID").value = userID;
    // load users details
    this.load_user();
  };  
  
  // load user
  this.load_user = function () 
  {
    if (this.SiteID!="")
    {
      var wRequest = new Sys.Net.WebRequest();
      wRequest.set_url("idxproxy.aspx");
      wRequest.set_httpVerb("POST");
      var body = "ID=3&SiteID=" + this.SiteID + "&UserID=" + this.get_userID();
      wRequest.set_body(body);
      wRequest.get_headers()["Content-Length"] = body.length;
      try { wRequest.add_completed(load_userComplete); } catch(err) {}
      wRequest.invoke();
    }
    else
    {
        alert("Error (ContactForm): You must supply a SiteID");
    }
  };
  
  var load_userComplete = function(executor, eventArgs)
  {
    var Results = executor.get_responseData().split("¬");
  
    window.document.getElementById(ClientID + "_FromName").value = Results[0];
    window.document.getElementById(ClientID + "_FromEmail").value = Results[1];
  }
  
  this.cancel = function()
  {
    // hide form
    window.document.getElementById(ClientID + "_Window").style.display = "none";
    window.document.getElementById(ClientID + "_Border").style.display = "none";
    window.document.getElementById(ClientID + "_Iframe").style.display = "none";
    
  }
  
  this.show = function()
  {
    // show form
    window.document.getElementById(ClientID + "_Window").style.display = "inline";
    window.document.getElementById(ClientID + "_Border").style.display = "inline";
    window.document.getElementById(ClientID + "_Iframe").style.display = "inline";
    
  }
  
  this.send = function()
  {
  
    // check from email
    if (!this.checkEmailValid())
    {
        alert("Please supply a valid email address");
        return;
    }
  
    if (this.get_subject()!="" && this.get_message()!="")
    {
    window.document.getElementById(ClientID + "_SendingMessage").style.display = "block";  
        
    var mailboxID = window.document.getElementById(ClientID + "_MailboxID").value;
    var fromUserID = window.document.getElementById(ClientID + "_FromUserID").value;
    var fromName = window.document.getElementById(ClientID + "_FromName").value;
    var fromEmail = window.document.getElementById(ClientID + "_FromEmail").value;
    var mailboxName = window.document.getElementById(ClientID + "_To").value;
    var subject = window.document.getElementById(ClientID + "_Subject").value;
    var message = window.document.getElementById(ClientID + "_Message").value;
    var wRequest = new Sys.Net.WebRequest();
    wRequest.set_url("idxproxy.aspx");
    wRequest.set_httpVerb("POST");
    var body = "ID=1&SiteID=" + this.SiteID + "&MailboxID=" + mailboxID + "&FromUserID=" + fromUserID + "&FromName=" + fromName + "&FromEmail=" + fromEmail + "&Subject=" + subject + "&Message=" + message + "&MailboxName=" + mailboxName;
    wRequest.set_body(body);
    wRequest.get_headers()["Content-Length"] = body.length;
    try { wRequest.add_completed(sendComplete); } catch(err) {}
    wRequest.invoke();
    }
    else
    {
        alert("Error (ContactForm): You must supply a subject and message");
    }
  }
  
  this.hideSentMessage = function()
  {
    window.document.getElementById(ClientID + "_MessageSent").style.display = "none";
    window.document.getElementById(ClientID + "_Send").disabled = true;
    
    window.document.getElementById(ClientID + "_Window").style.display = "none";
    window.document.getElementById(ClientID + "_Border").style.display = "none";
    window.document.getElementById(ClientID + "_Iframe").style.display = "none";
  }
  
  var sendComplete = function(executor, eventArgs)
  {
    window.document.getElementById(ClientID + "_SendingMessage").style.display = "none";
    window.document.getElementById(ClientID + "_MessageSent").style.display = "block";
    window.setTimeout("var " + ClientID + " = new ContactForm('" + ClientID + "'); " + ClientID + ".hideSentMessage();",2000);   
  }
   
  this.setSendButtonState = function()
  {
    if(window.document.getElementById(ClientID + "_Subject").value=="" || window.document.getElementById(ClientID + "_Message").value=="" || window.document.getElementById(ClientID + "_FromName").value=="" || window.document.getElementById(ClientID + "_FromEmail").value=="")
    {
        window.document.getElementById(ClientID + "_Send").disabled = true;
    }
    else
    {
        window.document.getElementById(ClientID + "_Send").disabled = false;
    }
  }
  
  this.checkEmailValid = function()
  {
        var str = window.document.getElementById(ClientID + "_FromEmail").value; 
		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 myob() {
  this.property1 = 'value1'; //this creates a public property
  var property2 = 'value2';  //this creates a private property
  this.method1 = function () { alert( property2 ); };
}
var oneOb = new myob();
alert(oneOb.property1); //alerts 'value1'
alert(oneOb.property2); //alerts undefined (private property)
oneOb.method1();        //alerts 'value2'
*/
Type.registerNamespace('Controls');Controls.Resource={};
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();