//define a country combo box 
var fp;
var comboWidth = 264; // hack to override Ext... where is the parent value coming from??? --nb
function getCountryComboBox(){
	
	var countryStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: JSON + '/system/getCountries'
        }),
        reader: new Ext.data.JsonReader({
            root: 'result'
        }, ['code', 'name', 'id'])
    });
	
    var country = new Ext.form.ComboBox({
    	width: comboWidth,
        store: countryStore,
		valueField: 'code',
        displayField: 'name',
        allowBlank: false,
        typeAhead: true,
        mode: 'remote',
        triggerAction: 'all',
        emptyText: 'Select a country...',
		blankText: 'Select a country',
        selectOnFocus: true,
        forceSelection: true,
		editable:false,
        applyTo: 'country',
		id: 'country',
		fieldLabel: 'Country',
		msgTarget: 'country-error',
		msgDisplay: 'block'
    });
	
    return country;
}

function getIndustryComboBox(){
	
	var industryStore = new Ext.data.Store({
	    proxy: new Ext.data.HttpProxy({
	        url: JSON + '/account/listIndustryProfileGroup'
	    }),
	    reader: new Ext.data.JsonReader({
	        root: 'result'
	    }, ['key', 'value'])
	});
	
    var industry = new Ext.form.ComboBox({
    	width: comboWidth,    
		store: industryStore,
        displayField: 'value',
		valueField: 'key',
        allowBlank: true,
        typeAhead: true,
        mode: 'remote',
        triggerAction: 'all',
        emptyText: 'Select an industry...',
        //blankText: 'Select an industry',
        selectOnFocus: true,
        forceSelection: true,
        editable: false,
        applyTo: 'industry',
		id: 'industry',
		fieldLabel: 'Industry',
		msgTarget: 'industry-error',
		msgDisplay: 'block'
    });
    
    return industry;
}

function getSizeComboBox(){
	var sizeStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: JSON + '/account/listSizeProfileGroup'
        }),
        reader: new Ext.data.JsonReader({
            root: 'result'
        }, ['key', 'value'])
    });
	
    var size = new Ext.form.ComboBox({
    	width: comboWidth,    
        store: sizeStore,
        displayField: 'value',
		valueField: 'key',
        allowBlank: true,
        typeAhead: true,
        mode: 'remote',
        triggerAction: 'all',
        emptyText: 'Select a size...',
        //blankText: 'Select a size',
        selectOnFocus: true,
        forceSelection: true,
        editable: false,
        applyTo: 'companySize',
		id: 'companySize',
		fieldLabel: 'Company size',
		msgTarget: 'companySize-error',
		msgDisplay: 'block'
    });
    
    return size;
}

function getFoundComboBox(){
	
	var foundStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: JSON + '/account/listFoundProfileGroup'
        }),
        reader: new Ext.data.JsonReader({
            root: 'result'
        }, ['key', 'value'])
    });
	
    var found = new Ext.form.ComboBox({
    	width: comboWidth,    
		store: foundStore,
        displayField: 'value',
		valueField: 'key',
        allowBlank: false,
        typeAhead: true,
        mode: 'remote',
        triggerAction: 'all',
        emptyText: 'Select how you heard about us...',
        blankText: 'Select how you heard about us',
        selectOnFocus: true,
        forceSelection: true,
        editable: false,
        applyTo: 'hearAbout',
		id: 'hearAbout',
		fieldLabel: 'How did you hear about us?',
		msgTarget: 'hearAbout-error',
		msgDisplay: 'block'
    });
    
    return found;
}



//define a timezone combo box 
function getTimeZoneComboBox(){

    var reader = new Ext.data.JsonReader({
        root: 'result'
    }, [{
        name: 'code'
    }, {
        name: 'name'
    }]);
    var proxy = new Ext.data.HttpProxy({
        url: JSON + '/system/getTimezones'
    })
    var store = new Ext.data.Store({
        proxy: proxy,
        reader: reader
    });
    store.load();
    
    var timeZoneBox = new Ext.form.ComboBox({
    	width: comboWidth,    
        store: store,
        id: 'timeZoneBox',
        name: 'timeZoneBox',
        fieldLabel: 'Time Zone',
        valueField: "code",
        displayField: 'name',
        allowBlank: false,
        emptyText: 'Select a Time Zone',
        mode: 'remote',
        triggerAction: 'all',
        selectOnFocus: true,
        forceSelection: true,
        allowBlank: false,
        forceSelection: true,
        editable: false
    });
    
    return timeZoneBox;
}

//define a country combo box 
function getSalutationComboBox(){

	var salutationStore = new Ext.data.SimpleStore({
        fields: ['salutation'],
        data : [['Mr'], ['Mrs'], ['Ms']]
    });
	
    var salutation = new Ext.form.ComboBox({
    	width: comboWidth,    
        store: salutationStore,
        displayField: 'salutation',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        emptyText: 'Select a title...',
		blankText: 'Select a title',
        selectOnFocus: true,
		//allowBlank: false,
        forceSelection: true,
        editable: false,
        applyTo: 'salutation',
		id: 'salutation',
		fieldLabel: 'Title',
		msgTarget: 'salutation-error',
		msgDisplay: 'block'
    });
	
	return salutation;
}

function getCardTypeComboBox(){

	var cardTypeStore = new Ext.data.SimpleStore({
        fields: ['cardType'],
        data : [['Visa'], ['Mastercard'], ['American Express']]
    });
	
    var cardType = new Ext.form.ComboBox({
    	width: 100,    
        store: cardTypeStore,
        displayField: 'cardType',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        emptyText: 'Select a card type...',
        selectOnFocus: true,
        applyTo: 'cardType',
		msgTarget: 'cardType-error',
		msgDisplay: 'block'
    });
	
	cardType.setValue('Visa');
	
	return cardType;
}

function getExpMonthComboBox(){

	var expMonthStore = new Ext.data.SimpleStore({
        fields: ['expMonth'],
        data : [['01'], ['02'], ['03'], ['04'], ['05'], ['06'], ['07'], ['08'], ['09'], ['10'], ['11'], ['12']]
    });
	
    var expMonth = new Ext.form.ComboBox({
    	width: 50,    
        store: expMonthStore,
        displayField: 'expMonth',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        emptyText: 'Select a exp month...',
        selectOnFocus: true,
        applyTo: 'expMonth',
		msgTarget: 'expMonth-error',
		msgDisplay: 'block'
    });
	
	expMonth.setValue('01');
	
	return expMonth;
}

function getExpYearComboBox(){

	var expYearStore = new Ext.data.SimpleStore({
        fields: ['expYear'],
        data : [['2008'], ['2009'], ['2010'], ['2011'], ['2012'], ['2013']]
    });
	
    var expYear = new Ext.form.ComboBox({
    	width: 70,    
        store: expYearStore,
        displayField: 'expYear',
        typeAhead: true,
        mode: 'local',
        triggerAction: 'all',
        emptyText: 'Select a exp year...',
        selectOnFocus: true,
        applyTo: 'expYear',
		msgTarget: 'expYear-error',
		msgDisplay: 'block'
    });
	
	expYear.setValue('2008');
	
	return expYear;
}

/**
 *DEPRICATED
 */
function getUploadImageWindow(imageUploadHandler, params){

    if (!win) {
        win = new Ext.ux.FileUploadForm({
            el: 'upload-win',
            layout: 'fit',
            closeAction: 'hide',
            plain: false,
			params: params,
            respondHandler: imageUploadHandler,
            resizable: false,
			displayColumns: false,
            url: APP + '/campaign/images/upload.html'
        });
        
    }
    else {
        win.respondHandler = imageUploadHandler;
		win.setParams(params);
    }
	win.showClearButton();//show clear button
    return win;
}


function getImageUploadForm(imageUploadHandler, params){
	
	if(!fp){
		fp = new Ext.FormPanel({
	        renderTo: 'upload-form',
	        fileUpload: true,
	        width: 350,
	        html:'<div style="text-align:center;font-weight:bold;">Maximum file size 6 Mb.</div>',
	        frame: true,
	        title: 'File Upload Form',
	        autoHeight: true,
	        bodyStyle: 'padding: 10px 10px 0 10px;',
	        labelWidth: 50,
	        defaults: {
	            anchor: '95%',
	            allowBlank: false,
	            msgTarget: 'side'
	        },
	        items: [{
	            xtype: 'fileuploadfield',
	            id: 'form-file',
	            emptyText: 'Select an image',
	            fieldLabel: 'Image',
	            name: 'file',
	            buttonText: '',
	            buttonCfg: {
	                iconCls: 'upload-icon'
	            }
	        }],
	        buttons: [{
	            text: 'Upload',
	            handler: function(){
	                if(fp.getForm().isValid()){
		                fp.getForm().submit({
		                    url: APP + '/campaign/images/upload.html',
		                    waitMsg: 'Uploading your image...',
		                    success: function(fp, o){
			                	var resultQuery = Ext.util.JSON.decode(o.response.responseText);
		                        if (resultQuery.success == true) {
		                        	imageUploadHandler(resultQuery);
		                        }
		                        else {
		                            Ext.MessageBox.alert("ERROR", resultQuery.msg);
		                        }
		                        
		                    }
		                });
	                }
	            }
	        },/*{
	            text: 'Reset',
	            handler: function(){
	                fp.getForm().reset();
	            }
	        },*/{
	            text: 'Remove',
	            handler: function(){
	        		imageUploadHandler('clear');
	            }
	        },{
	            text: 'Close',
	            handler: function(){
	        		fp.destroy();
	            }
	        }]
	    });
	}
	
	fp.on("destroy",function(){fp=null;});
	
	var upload_form=Ext.get('upload-form');
	var el = Ext.get('WIDIDcmWidget');
	upload_form.setX(el.getX()+(el.getWidth()-upload_form.getWidth())/2);
	upload_form.setY(el.getY()+(el.getHeight()-upload_form.getHeight())/2);
	upload_form.setVisible(true);
	return fp;
}


//clean all spaces in the string 
function cleanSpaces(str){
    var returnString = "";
    for (i = 0; i < str.length; i++) {
        if (str.charAt(i) != " ") {
            returnString = returnString + str.charAt(i);
        }
    }
    return returnString;
}

function isUserNameValid(str){
    var specialCharacters = new Array("$", "&", "+", ",", "/", ":", ";", "=", "?", "!", " ", "\"", "<", ">", "#", "%", "{", "}", "|", "\\", "^", "~", "[", "]", "`", "'", "(", ")", "*");
    
    for (var i = 0; i < str.length; i++) {
        for (var j = 0; j < specialCharacters.length; j++) {
            if (specialCharacters[j] == str.charAt(i)) {
                return false;
            }
        }
    }
    return true;
}



function cleanSpecialCharacters(str){
    var specialCharacters = new Array("$", "&", "+", ",", "/", ":", ";", "=", "?", "@", " ", "\"", "<", ">", "#", "%", "{", "}", "|", "\\", "^", "~", "[", "]", "`");
    var specialCharactersHexa = new Array("24", "26", "2B", "2C", "2F", "3A", "3B", "3D", "3F", "40", "20", "22", "3C", "3E", "23", "25", "7B", "7D", "7C", "5C", "5E", "7E", "5B", "5D", "60");
    
    var strOut = '';
    var changed = 0;
    
    for (var i = 0; i < str.length; i++) {
        changed = 0;
        for (var j = 0; j < specialCharacters.length; j++) {
            if (specialCharacters[j] == str.charAt(i)) {
                strOut = strOut + '%' + specialCharactersHexa[j];
                changed = 1;
            }
        }
        if (changed != 1) {
            strOut = strOut + str.charAt(i);
        }
    }
    return strOut;
}

function getOthersPopup(func, animated){ //popup promt to enter others industry details
    var popup = Ext.MessageBox.show({
        title: 'Industry',
        msg: 'Please enter your industry here:',
        width: 300,
        buttons: Ext.MessageBox.OK,
        multiline: true,
        fn: func,
        animEl: animated
    });
}
