﻿var common = {
    resolveBoolean: function(value){
        if (value == 'True'){
            return 'checked';
        Else
            return '';
        }
        
    }
}

var customer = {
    myCon:   		false,
	control:		false,
	body:    		false,
	loader:  		false,
	target: 		false,
	content:		false,
	pageURL:		false,
	params: 		false,
	myFuncWhenDone: false,
    
    lightBox: function() {
		customer.myCon = new AjaxDell();
		if(!customer.myCon) return;
		customer.target = document.getElementById('loginnotice')
		customer.params = 'action=LIGHTBOXTEST';
		customer.params += '&logemail=' + document.getElementById('logemail').value;
		customer.params += '&logpassword=' + document.getElementById('logpassword').value;
        
		customer.pageURL = "../remotes/action_customer.aspx";
		customer.buildPageLoader('loginloader');
		customer.myFuncWhenDone = function(xmlObj) {
            customer.killPageLoader()
            customer.target.innerHTML = xmlObj.responseText
            customer.target.style.visibility = "visible";
		}
		
		customer.retrieveContent();
	},
	
	gup: function(name)
    {
      name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regexS = "[\\?&]"+name+"=([^&#]*)";
      var regex = new RegExp( regexS );
      var results = regex.exec( window.location.href );
      if( results == null )
        return "";
      else
        return results[1];
    },

    login: function() {
        var logemail = document.getElementById('logemail').value;
        var logpassword = document.getElementById('logpassword').value;
        var logautorun = "";
        
        if(logemail == "")
        {
            try
            {
                logemail = $('#logemailRedirect').val();
                logpassword = $('#logpasswordRedirect').val();
                logautorun = customer.gup('autorun');
            }
            catch(e)
            {}
        }
        
        if(logemail != "")
        {
		    customer.myCon = new AjaxDell();
		    if(!customer.myCon) return;
		    customer.target = document.getElementById('loginnotice')
		    customer.params = 'action=LOGIN';
		    customer.params += '&logemail=' + logemail;
		    customer.params += '&logpassword=' + logpassword;
		    customer.pageURL = "../remotes/action_customer.aspx";
		    customer.buildPageLoader('loginloader');
		    customer.myFuncWhenDone = function(xmlObj) {
		        //alert(xmlObj.responseText);
                customer.killPageLoader()
                if (xmlObj.responseText == 'SUCCESS'){
                    if(logautorun != "")
	                {
	                    window.location = "../modules/home.aspx?autorunl=" + logautorun
	                }
	                else
	                {
	                    window.location = "../modules/home.aspx"
	                }
                }else{
                    customer.target.innerHTML = xmlObj.responseText
                    customer.target.style.visibility = "visible";
                }
		    }
		    customer.retrieveContent();        
        }

	},
	
	
	logout: function() {
		customer.myCon = new AjaxDell();
		if(!customer.myCon) return;
		customer.target = document.getElementById('status')
		customer.params = 'action=LOGOUT';
		
		customer.pageURL = "../remotes/action_customer.aspx";
		//customer.buildPageLoader('Populating');
		customer.myFuncWhenDone = function(xmlObj) {            
		    //customer.killPageLoader()
		    window.location = "../modules/home.aspx"
		    
		}
		
		customer.retrieveContent();
	},
	
	insertCustomer: function() {
	    var arrPort = [];
	    arrPort = getElementsByClassName(document, "*", 'port');
	    
	    var arrCity = [];
	    arrCity = getElementsByClassName(document, "*", 'city');
	    
		customer.myCon = new AjaxDell();
		if(!customer.myCon) return;
		customer.target = document.getElementById('status')
		customer.params = 'action=INSERT';
		customer.params += '&companyname=' + document.getElementById('companyname').value;
		customer.params += '&completename=' + document.getElementById('completename').value;
		customer.params += '&address=' + document.getElementById('address').value;
		customer.params += '&zip=' + document.getElementById('zip').value;
		customer.params += '&city=' + arrCity[0].value;
		customer.params += '&country=' + document.getElementById('country').value;
		customer.params += '&port=' + arrPort[0].value;
		customer.params += '&workphone=' + document.getElementById('workphone').value;
		customer.params += '&homehone=' + document.getElementById('homehone').value;
		customer.params += '&mobilephone=' + document.getElementById('mobilephone').value;
		customer.params += '&fax=' + document.getElementById('fax').value;
		customer.params += '&email=' + document.getElementById('email').value;
		customer.params += '&emailusername=' + document.getElementById('emailusername').value;
		customer.params += '&emailpassword=' + document.getElementById('emailpassword').value;
		
		customer.pageURL = "../remotes/action_customer.aspx";
		customer.buildPageLoader('loader');
		customer.myFuncWhenDone = function(xmlObj) {            
		    customer.killPageLoader()
		    customer.target.innerHTML = xmlObj.responseText
		   
		}
		
            var requiredString,statusFlag;
            requiredString="";
            
            if(document.getElementById('completename').value==""){
               statusFlag=1;
               requiredString = requiredString + "<br> Must enter your name";
            }
            
            if(document.getElementById('companyname').value==""){
               statusFlag=1;
               requiredString = requiredString + "<br> Must input a compnany name";
            }
            
            if(document.getElementById('country').value==""){
                statusFlag=1;
                requiredString = requiredString + "<br> Must select a country field"
            } 
	        
	        if(arrPort[0].value==""){
	           statusFlag=1;
               requiredString = requiredString + "<br> Must select a port field";
            }
               	 
            if(arrCity[0].value==""){
               statusFlag=1;
               requiredString = requiredString + "<br> Must select a city field";
            } 
		   
		   
		    if(document.getElementById('workphone').value==""){
	           statusFlag=1;
               requiredString = requiredString + "<br> Must input your work phone number";
            }
               	 
            if(document.getElementById('homehone').value==""){
               statusFlag=1;
               requiredString = requiredString + "<br> Must input your home phone number";
            } 
            
            var emailStr;
            emailStr=document.getElementById('email').value;
           if(emailStr.match("@")==null){
             statusFlag=1;
             requiredString = requiredString + "<br> Must enter a valid email";
           }
		    
		    if(document.getElementById('emailpassword').value==""){
               statusFlag=1;
               requiredString = requiredString + "<br> Must enter your password";
            } 
		   var logPass = document.getElementById('emailpassword').value
           
           
		   if(logPass.length>0){
		        statusFlag=1;
		        if(document.getElementById('retypepassword').value!=document.getElementById('emailpassword').value){
                   statusFlag=1;
                   requiredString = requiredString + "<br> Please retype your pasword";
                }
           }
             
            if(requiredString!=""){
                customer.killPageLoader();
                customer.target.innerHTML = requiredString;
            }else{
                customer.retrieveContent();
            }
            
            
        
		
	},
	
	updateCustomer: function() {
	    var key='ctl00_contentholder_';
		customer.myCon = new AjaxDell();
		if(!customer.myCon) return;
		customer.target = document.getElementById('status')
		customer.params = 'action=UPDATE';
		customer.params += '&companyname=' + document.getElementById(key + 'companyname').value;
		customer.params += '&completename=' + document.getElementById(key + 'completename').value;
		customer.params += '&address=' + document.getElementById(key + 'address').value;
		customer.params += '&city=' + document.getElementById(key + 'city').value;
		customer.params += '&country=' + document.getElementById(key + 'country').value;
		customer.params += '&port=' + document.getElementById(key + 'port').value;
		customer.params += '&workphone=' + document.getElementById(key + 'workphone').value;
		customer.params += '&homehone=' + document.getElementById(key + 'homehone').value;
		customer.params += '&mobilephone=' + document.getElementById(key + 'mobilephone').value;
		customer.params += '&fax=' + document.getElementById(key + 'fax').value;
		customer.params += '&email=' + document.getElementById(key + 'email').value;
		customer.params += '&emailusername=' + document.getElementById(key + 'emailusername').value;
		customer.params += '&emailpassword=' + document.getElementById(key + 'newpassword').value;

		customer.pageURL = "../remotes/action_customer.aspx";
		customer.buildPageLoader('loader');
		customer.myFuncWhenDone = function(xmlObj) {            
		    customer.killPageLoader()
		    customer.target.innerHTML = xmlObj.responseText
		    
		}
		customer.retrieveContent();
	},
	
    buildPageLoader: function(target){
	    customer.loader = document.getElementById(target);
	    //customer.loader.innerHTML = 'loading data..'
		customer.loader.innerHTML = '<img src="../images/common/ajax-loader.gif" alt="loading">'
 	},
	
	killPageLoader: function() {
		customer.loader.style.visibility	= 'hidden';
	},

	retrieveContent: function() {
		customer.myCon.connect(customer.pageURL, "GET", customer.params, customer.myFuncWhenDone);
	},
	
	retrieveTContent: function() {
		window.location = customer.pageURL + '?' + customer.params;
	}
}
