// JavaScript Document
var opened = false;
var AVATARSEL = 
{
	
	show: function(ctrId)
	{
	    path='EmoticonOng/';
	    numberoficons= 52;
	    opened = false;
		var html = '', index = '';
		for(var i=0;i<numberoficons;i++)
		{
			index = (i<10) ? ('0' + i) : (i + '');
			html += '<div style="float:left; width:40px; height:40px; padding:10px; overflow:hidden;"><div align="center" style="filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+ path + index +'.png\'); width:30px; height:30px; cursor:pointer" onclick="AVATARSEL.insert(' + index + ', this, \'' + ctrId + '\');return false;"><a href="javascript:void(0);" title="' + index + '" ><img src="' + path + index + '.png" style="filter: progid:DXImageTransform.Microsoft.Alpha(opacity=0));" /></a></div></div>';
		}
//		var popup = document.getElementById('popupbox');
//		popup.style.display="block";
//		popup.style.zIndex="100";
showPopup('popupbox');
		document.getElementById('popupbox_content').innerHTML = html;
		window.setTimeout("GanLaiOpened()",500);
	},
	showAvatar: function(ctrId)
	{
	    path= 'Cmavatar/';
	    numberoficons= 45;
	    opened = false;
		var html = '', index = '';
		for(var i=0;i<numberoficons;i++)
		{
			index = (i<10) ? ('0' + i) : (i + '');
			html += '<div style="float:left; width:55px; height:55px; overflow:hidden;padding:5px"><div align="center" style="width:55px; height:55px; cursor:pointer" ><a href="javascript:void(0);" title="' + index + '" ><img onclick="AVATARSEL.insertAvatar(\'' + path + index + '.jpg\', this, \'' + ctrId + '\');return false;" src="' + path + index + '.jpg" style="width:55px; height:55px;" /></a></div></div>';
		}
		//var popup = document.getElementById('popupbox');
		//popup.style.display="block";
		//popup.style.zIndex="100";
		showPopup('popupbox');
		
		document.getElementById('popupbox_content').innerHTML = html;
		window.setTimeout("GanLaiOpened()",500);
	},
	hide: function()
	{
	    if(opened != false && document.getElementById('popupbox')!=null)
	    {
	        document.getElementById('popupbox').style.display="none";
	    }
	},
	insert: function(index, a, ctrId)
	{
		index = '<' + index + '>';
			try
    		{
				var ctr = document.getElementById(ctrId);	            
				if (ctr != null && ctr.createTextRange && ctr.caretPos) 
				{
					var caretPos = ctr.caretPos;
					caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? index + ' ' : index;	
				}
				else 
				{
					ctr.value += index;
				}
			}
			catch(e)
			{
        		alert(e.message);
			}
		
		AVATARSEL.hide();
			
			
	},
	insertAvatar: function(index, a, ctrId)
	{
		try
    		{
				var ctr = document.getElementById(ctrId);	
				           
				var URLAvatar = document.getElementById("hidenURLAvatar");
			   
				if (ctr != null && URLAvatar !=null) 
				{
				   
					ctr.src = index;
					URLAvatar.value  = index;
					
				}
				//alert("ctr :"+index);
				
			}
			catch(e)
			{
        		alert(e.message);
			}
		
		AVATARSEL.hide();
	}
}

function GanLaiOpened()
{
    opened = true;
}

function showPopup(controlID)
{
	var control = document.getElementById(controlID);
	if (control)
	{
		control.style.display = 'block';
		control.style.zIndex = '100';
		var top = (window.screen.availHeight - control.offsetHeight)/2 - 100;
        var left = (document.documentElement.offsetWidth - control.offsetWidth)/2;
        left += (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
        top += (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;

		control.style.left= left + 'px';
		control.style.top=  top + 'px';
		
		return true;
	}
	return false;
}
// check email
function checkValidation(formInput) {

    /*if (typeof(formInput) != "object") {
        alert("Validation not supported on this browser.");
        return(false);
    }*/

    var message = 'Chổ * bị bỏ trống hoặc địa chỉ mail không chính xác';

    if (stringEmpty(formInput.value)) {        
        alert(message);
    } else if (noAtSign( formInput.value )) {        
        alert(message);
    } else if (nothingBeforeAt(formInput.value)) {        
        alert(message);
    } else if (noLeftBracket(formInput.value)) {        
        alert(message);
    } else if (noRightBracket(formInput.value)) {        
        alert( message);
    } else if (noValidPeriod(formInput.value)) {        
        alert(message);
    } else if (noValidSuffix(formInput.value)) {        
        alert(message);
    }else {
		return(true);	
	}     
}

function checkValid (act) {
	if(act < 1) {
		var yourmail = document.getElementById('yourmail');
		var yourname = document.getElementById('yourname').value;	
    	if ( checkValidation ( yourmail ) == true && yourname != '') {        
			document.getElementById('file_upload_form').submit();
    	}else {
		return;		
		}
	}else {
		document.getElementById('file_upload_form').submit();
	}
}

function stringEmpty (formField) {
    // CHECK THAT THE STRING IS NOT EMPTY
    if ( formField.length < 1 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function noAtSign (formField) {
    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
    if (formField.indexOf ('@', 0) == -1) {
        return ( true )
    } else {
        return ( false );
    }
}

function nothingBeforeAt (formField) {
    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
    if ( formField.indexOf ( '@', 0 ) < 1 ) {
        return ( true )
    } else {
        return ( false );
    }
}

function noLeftBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
    if ( formField.indexOf ( '[', 0 ) == -1 && formField.charAt (formField.length - 1) == ']') {
        return ( true )
    } else {
        return ( false );
    }
}

function noRightBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
    if (formField.indexOf ( '[', 0 ) > -1 && formField.charAt (formField.length - 1) != ']') {
        return ( true );
    } else {
        return ( false );
    }
}

function noValidPeriod (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf ( '@', 0 ) > 1 && formField.charAt (formField.length - 1 ) == ']')
        return ( false );

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
    if (formField.indexOf ( '.', 0 ) == -1)
        return ( true );

    return ( false );
}

function noValidSuffix(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']') {
        return ( false );
    }

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
    var len = formField.length;
    var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;
    if ( ( len - pos ) < 2 || ( len - pos ) > 4 ) {
        return ( true );
    } else {
        return ( false );
    }
}
