(function(){
GupShup.forms.controls = function(control){
var doc = control.doc;

if(typeof doc == 'undefined')
		doc = document;

if(typeof control.id != 'undefined')
{
	this.ele = doc.getElementById(control.id);
}
else
{
	var eleName = control.name.split(".")[1];
	var form = doc.forms[control.name.split(".")[0]];
	if(typeof form == 'object')
		this.ele = form[eleName];  
}

if((typeof this.ele == 'undefined') || (this.ele == null))
	return;
	
this.id = this.ele.id;
this.eleName = this.ele.name;
this.style = this.ele.style;

if(typeof control.dimText == 'string')
	this.addDimText(control.dimText);
else
	this.addDimText('');
	
this.pattern = control.pattern;

if(typeof control.toolTip == 'string')
	this.toolTip = control.toolTip;
else
	this.toolTip = '';

if(typeof control.limit == 'number')
	this.limit = control.limit;
else
	this.limit = -1;
this.err = false;
}

GupShup.forms.controls.DEFAULT_INPUT_TEXT_COLOR = "#000000";

GupShup.forms.controls.DEFAULT_DIM_TEXT_COLOR = "#CCCCCC";

GupShup.forms.controls.DEFAULT_NOTIFICATION_CONTAINER = "middle-col";

GupShup.forms.controls.DEFAULT_NOTIFICATION_PROPERTIES = {
						style : { 
									superClass : "notification" , 
									success : "notificationGreen" , 
									failure : "notificationRed",
									warning : "notificationOrange"
								}
						};
 
GupShup.forms.controls.prototype.isValidText = function(){
 		return this.pattern.test(this.getValue());
}

GupShup.forms.controls.prototype.isValid = function(){
	if((typeof this.ele == 'undefined') || (this.ele == null))
		return false;
	else
		return true;	
}

GupShup.forms.controls.prototype.get = function(attr){
	switch(attr)
	{
		case 'value' : return this.getValue();
		case 'length' :
				var val = this.getValue();
				var len = 0;
				for(var i=0; i<val.length; i++)
				{
					if (val.charCodeAt(i) != 13)
						len++;
				}
				return len;
		case 'object' : return this.ele;
		default : return this[attr] ? this[attr] :'' ;
	}
}

GupShup.forms.controls.prototype.getValue = function(){
switch(this.ele.type) 
 { 
	case 'text': 
	case 'password': 
	case 'hidden': 
	case 'textarea': 
	case 'file': 
		return GupShup.util.trim(this.ele.value);
	case 'select-one': 
		if (this.ele.selectedIndex>=0)
			return GupShup.util.trim(this.ele.options[this.ele.selectedIndex].value);
		else
			return ''; 
  }
}

GupShup.forms.controls.prototype.addEventListner = function(types, newF, keepPrev)
{
	var typeArr = types.split(",");
	for(var i = 0; i < typeArr.length; i ++)
	{
		var type = GupShup.util.trim(typeArr[i]);
		switch(type)
		{
			case 'onchange' :	var prevF = this.ele.onchange;
								if(typeof prevF == 'function')
									this.ele.onchange = function(){ prevF(); newF();}
								else
									this.ele.onchange = function(){ newF();}
								 break;
			case 'onblur' : 	var prevF = this.ele.onblur;
								if(typeof prevF == 'function')
									this.ele.onblur = function(){ prevF(); newF();}
								else
									this.ele.onblur = function(){ newF();}
								 break;
			case 'onfocus' : 	var prevF = this.ele.onfocus;
								if(typeof prevF == 'function')
									this.ele.onfocus = function(){ prevF(); newF();}
								else
									this.ele.onfocus = function(){ newF();}
								break;
			case 'onkeyup' : 	var prevF = this.ele.onkeyup;
								if(typeof prevF == 'function')
									this.ele.onkeyup = function(){ prevF(); newF();}
								else
									this.ele.onkeyup = function(){ newF();}
								break;
		}
	} 
}

GupShup.forms.controls.prototype.removeEventListner = function(types)
{
	var typeArr = types.split(",");
	for(var i = 0; i < typeArr.length; i ++)
	{
		var type = GupShup.util.trim(typeArr[i]);
		switch(type)
		{
			case 'onchange' :
				this.ele.onchange = function(){};
				break;
			case 'onblur' : 	
				this.ele.onblur = function(){};
			 	break;
			case 'onfocus' :	
				this.ele.onfocus = function(){}
				break;
			case 'onkeyup' : 	
				this.ele.onkeyup = function(){}
				break;
		}
	} 
}

GupShup.forms.controls.prototype.addListner = function(type, attr)
{
	var o = this;
	switch(type)
	{
		case 'counter' : this.addEventListner('onkeyup,onfocus', function(){
			var displayBox  = document.getElementById(attr.holder);
			var count = o.limit - o.get('length');
			displayBox.innerHTML = count;
			if(count <= 0)
			{
				o.ele.value = o.getValue().substring(0,o.limit);
				displayBox.innerHTML = 0;
			}
		});
		break;
		
		case 'usedCounter' : this.addEventListner('onkeyup,onfocus', function(){
			var displayBox  = document.getElementById(attr.holder);
			displayBox.innerHTML = o.get('length');
		});
		break;
				
		case 'counterUnicode' : this.addEventListner('onkeyup,onfocus', function(){
			var displayBox  = document.getElementById(attr.holder);
			var count = o.limit - o.get('length');  
			displayBox.innerHTML = count;
			if(count <= 0)
			{
				o.ele.value = o.getValue().substring(0,o.limit);
				displayBox.innerHTML = 0;
			}
		});
		break;
						
		case 'usedCounterUnicode' : this.addEventListner('onkeyup,onfocus', function(){
			var displayBox  = document.getElementById(attr.holder);
			displayBox.innerHTML = o.get('length');
		});
		break;
		
		case 'sms-counter' : this.addEventListner('onkeyup,onfocus', function(){
			var displayBox = document.getElementById(attr.holder);
			var count = 0;
			var contentLength = o.get('length');
			if(contentLength != 0)
			{
				var firstMsgLimit = 160;
				
				if(typeof attr.startMsgLimit == "number")
					firstMsgLimit = parseInt(attr.startMsgLimit);
					
				if (contentLength <= firstMsgLimit)
					count = 1;
				else
				{
					count = Math.floor(contentLength/attr.smsLimit);
					if ((contentLength % attr.smsLimit) > 0)
						count++;
				}
			}
			displayBox.innerHTML = count;
		});
		break;
		
		case 'sms-counterUnicode' : this.addEventListner('onkeyup,onfocus', function(){
			var displayBox  = document.getElementById(attr.holder);
			var count = 0;
			var contentLength = o.get('length');
			if(contentLength != 0)
			{
				count = Math.floor(contentLength/attr.smsLimit);
				if ((contentLength % attr.smsLimit) > 0)
					count++;				
			}
			displayBox.innerHTML = count;
		});
		break;
	}
}

GupShup.forms.controls.prototype.removeListner = function(type)
{
	var o = this;
	switch(type)
	{
		case 'counter' : 
		case 'usedCounter' : 
		case 'counterUnicode' : 
		case 'usedCounterUnicode' : 
		case 'sms-counter' : 
		case 'sms-counterUnicode' : 
			this.removeEventListner('onkeyup,onfocus');
		break;
	}
}

GupShup.forms.controls.prototype.isDefaultText = function(){
	return (this.get('value') == this.get('dimText'));
}

GupShup.forms.controls.prototype.addDimText = function(text){
	var o = this;
	
	if(typeof o.dimText == 'string' && o.dimText.length > 0)
	{
		o.dimText = text;	//No need to reset onblur/onfocus functions  coz they are already set
		return;
	}
	else if(text.length == 0)
	{
		o.dimText = '';
		return;
	}
	else
		o.dimText = text;
		
	if(o.get('value').length < 1)	//Do not overrride default value
		o.set(o.get('dimText'));
	o.ele.style.color = GupShup.forms.controls.DEFAULT_DIM_TEXT_COLOR;
	
	o.addEventListner('onfocus' , function(){
		var val = GupShup.util.trim(o.get('value'));
		if(val == o.get('dimText'))
			o.set("");
		o.ele.style.color = GupShup.forms.controls.DEFAULT_INPUT_TEXT_COLOR;
	});
	
	o.addEventListner('onblur',function(){
		var val = GupShup.util.trim(o.get('value'));
		if(val == "")
			o.set(o.get('dimText'));
		o.ele.style.color = GupShup.forms.controls.DEFAULT_DIM_TEXT_COLOR;
	});

}

GupShup.forms.controls.prototype.init = function(){
	
	var o = this;
	
	o.addDimText(o.get('dimText'));
		
	if(o.limit > 0)
	{
		o.ele.setAttribute("maxLength", o.limit);
		o.addEventListner('onchange', function(){
			o.set(o.get('value').substring(0,o.get('limit')));
		});
	}
	
	//Add default funct for clering tool tip and err
	o.addEventListner('onblur',function(){
			o.err = false;
			GupShup.html.util.destroyElement('errorBox');
			GupShup.html.util.destroyElement('toolTip');
		});
	
	this.addToolTip(this.toolTip);
};
GupShup.forms.controls.prototype.addToolTip = function(text){
	this.toolTip = text;
	var o = this;
	if(o.get('toolTip').length > 0)
	{
		o.addEventListner('onfocus' , function(){
			if(o.err == false)
			{
					var toolTip = GupShup.html.util.createToolTipElement(o.toolTip);
					GupShup.html.util.overlay(o.ele,toolTip,"right", 0, 20);
			}			
		});
	}
}

GupShup.forms.controls.prototype.setFocus = function(){
	try
	{
		if(typeof this.ele.onfocus == 'function')
			this.ele.focus();
	}
	catch(e){}
}

GupShup.forms.controls.prototype.setAttr = function(name, value){
	switch(name) 
	{
		case 'limit' : this.limit = value; break;
		case 'style' : this.style = this.ele.style = value; break;
		case 'dimText' : this.dimText = value; break;
	}
}

GupShup.forms.controls.prototype.set = function(text){
switch(this.ele.type) 
 { 
	case 'text': 
	case 'password': 
	case 'hidden': 
	case 'textarea': 
		this.ele.value = GupShup.util.trim(text);
		if(typeof this.ele.onkeyup == 'function')
			this.ele.onkeyup();
  }
}
/*
*	notifStyle  : { failure: true,
			  sucess:false,
			  warning: false,
			  position : 'top',
			  position : 'right'
			}
*/
GupShup.forms.controls.prototype.showNotification = function(text,notifStyle){
	switch(notifStyle.position)
	{
		case 'right':
					var overlayObj = null;
					if(notifStyle.failure == true)
					{
						overlayObj = GupShup.html.util.createErrElement(text);
						this.err = true;
						if(this.ele.type != 'file')
							this.ele.focus();
						GupShup.html.util.overlay(this.ele,overlayObj,"right", 0, 0);
					}
					else if(notifStyle.success == true)
					{
						overlayObj = GupShup.html.util.createToolTipElement(text);
						GupShup.html.util.overlay(this.ele,overlayObj,"right", 0, 20);
					}
					
					break;
		case 'top': 
		default : GupShup.forms.showNotification(text, notifStyle);break;			
		
	}
}

GupShup.forms.showNotification = function(text, notifStyle){
	
	var notifEle = GupShup.html.util.createNotificationElement(text,function(){GupShup.html.util.destroyElement('notification')});
	/*
		If need animation
		var notifEle = GupShup.util.createNotificationElement(text,GupShup.forms.controls.hideNotification);
	*/
	var style = GupShup.forms.controls.DEFAULT_NOTIFICATION_PROPERTIES.style;
	var styleClasses = style.superClass;
	
	if(typeof notifStyle == 'undefined')
		notifStyle = {success:true};
		
	if(notifStyle.success == true)
		styleClasses += " " + style.success;
	else if(notifStyle.failure == true)
		styleClasses += " " + style.failure;
	else if(notifStyle.warning == true)
		styleClasses += " " + style.warning;
		
	notifEle = GupShup.html.util.setClass(notifEle,styleClasses);
	
	var notifHolder = document.getElementById(GupShup.forms.controls.DEFAULT_NOTIFICATION_CONTAINER);
	notifHolder.insertBefore(notifEle,notifHolder.firstChild);
	//Move window scroll to top
	window.scroll(0,0);
}

GupShup.forms.hideNotification = function(){
GupShup.html.util.destroyElement('notification');
}
GupShup.forms.controls.hideNotification = function(){
	GupShup.html.util.destroyElement('errorBox');
}

GupShup.forms.controls.initControls = function(controlArray){
for(var i = 0 ; i < controlArray.length ; i++)
{
		var ctrl = controlArray[i];
		if(ctrl.isValid())
			ctrl.init();
}
}
})();