(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;

if(typeof control.isGSMASCIIMsg == 'boolean')
	this.isGSMASCIIMsg = control.isGSMASCIIMsg;
else
	this.isGSMASCIIMsg = true;

if(typeof control.stripCRFromCount == 'boolean')
	this.stripCRFromCount = control.stripCRFromCount;
else
	this.stripCRFromCount = false;

/* 
   sampleValidator =
   {
   	notifEleId : 'notifEleId',
   	validatorF: validatorFunction,
   	showNotifOn : ['onblur', 'onkeyup'],
   	isInline: true, (false by default)
   	args : {
   		validatorFArgs : val
   	}
    style : {
  		superClass : "notification" , 
		success : "notificationGreen" , 
		failure : "notificationRed",
		warning : "notificationOrange"
   	}
   }
*/
if(typeof(control.validator) == 'object')
	this.validator = control.validator;
else
	this.validator = null;

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.validate = function(o) {
	var result = null;
	if (this.validator != null)
	{
		result = this.validator.validatorF(this, this.validator.args);
		var styleClasses = '';
		if (this.validator.style)
		{
			var style = this.validator.style;
			if (typeof(style.superClass) == 'string')
				styleClasses = style.superClass;

			if(result.success)
				styleClasses += " " + style.success;
			else if(result.failure)
				styleClasses += " " + style.failure;
			else if(result.warning)
				styleClasses += " " + style.warning;				
		}	
		GupShup.html.util.setClass(this.validator.notifEleId, styleClasses);
		
		if (result.success)
		{
			if (typeof(result.message) == 'string')
			{
				GupShup.util.setInnerHTML(this.validator.notifEleId, result.message, this.doc);
				GupShup.html.util.showElement(this.validator.notifEleId, this.validator.isInline, this.doc);	
			}
			else
			{
				GupShup.util.setInnerHTML(this.validator.notifEleId, '', this.doc);
				GupShup.html.util.hideElement(this.validator.notifEleId, this.doc);				
			}			
		}
		else if (result.failure || result.warning)
		{
			GupShup.util.setInnerHTML(this.validator.notifEleId, result.message, this.doc);
			GupShup.html.util.showElement(this.validator.notifEleId, this.validator.isInline, this.doc);
		}
	}
	return result;
}

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();
				if (this.stripCRFromCount)
					val = GupShup.util.removeCarriageReturn(val);
				return val.length;
		case 'gsmASCIIMsgLength' :
				var val = this.getValue();
				if (this.stripCRFromCount)
					val = GupShup.util.removeCarriageReturn(val);
				return GupShup.util.getGSMASCIIMsgLength(val);
		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.setOnChange = function(){
	try
	{
		if(typeof this.ele.onchange == 'function')
			this.ele.onchange();
	}
	catch(e){}
}

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('gsmASCIIMsgLength');
			displayBox.innerHTML = count;
			if(count <= 0)
			{
				o.trimLength();
				displayBox.innerHTML = 0;
			}
		});
		break;
		
		case 'usedCounter' : this.addEventListner('onkeyup,onfocus', function(){
			var displayBox  = document.getElementById(attr.holder);
			displayBox.innerHTML = o.get('gsmASCIIMsgLength');
		});
		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.trimLength();
				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 firstMsgLimit = 160; // Hint from SMPPUtils.PDU_LENGTH.SMS_PDU_LENGTH
			if(typeof attr.startMsgLimit == "number")
				firstMsgLimit = parseInt(attr.startMsgLimit);
			
			var multMsgLimit = 153; // Hint from SMPPUtils.PDU_LENGTH.CONCATENATED_SMS_FRAG_LEN
			if(typeof attr.smsLimit == "number")
				multMsgLimit = parseInt(attr.smsLimit);
			
			var contentLength = o.get('gsmASCIIMsgLength');

			displayBox.innerHTML = GupShup.util.getSMSCount(contentLength, firstMsgLimit, attr.smsLimit);
		});
		break;
		
		case 'sms-counterUnicode' : this.addEventListner('onkeyup,onfocus', function(){
			var displayBox  = document.getElementById(attr.holder);

			var firstMsgLimit = 70; // Hint from (SMPPUtils.PDU_LENGTH.UCS2_PDU_LEN/4)
			if(typeof attr.startMsgLimit == "number")
				firstMsgLimit = parseInt(attr.startMsgLimit);
			
			var multMsgLimit = 66; // Hint from (SMPPUtils.PDU_LENGTH.CONCATENATED_UCS2_SMS_FRAG_LEN/4)
			if(typeof attr.smsLimit == "number")
				multMsgLimit = parseInt(attr.smsLimit);
			
			var contentLength = o.get('length');

			displayBox.innerHTML = GupShup.util.getSMSCount(contentLength, firstMsgLimit, attr.smsLimit);
		});
		break;
		
		case 'copy-to' : this.addEventListner('onkeyup,onfocus', function(){
			var displayBox  = document.getElementById(attr.holder);
			switch(displayBox.type) 
			{ 
				case 'text': 
				case 'hidden':
				case 'textarea': 
					displayBox.value = o.getValue();
					break;
			}
		});
		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' :
		case 'copy-to' :
			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.trimLength = function () {
	if (this.isGSMASCIIMsg)
		this.ele.value = GupShup.util.limitGSMASCIIMsg(this.get('value'), this.get('limit'));
	else
		this.ele.value = this.get('value').substring(0, this.get('limit'));
}

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(){
			if (o.isGSMASCIIMsg)
				o.set(GupShup.util.limitGSMASCIIMsg(o.get('value'), o.get('limit')));
			else
				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);
	
	if (o.validator != null)
	{
		if (typeof(o.validator.showNotifOn) == 'object')
		{
			for (var i=0; i<o.validator.showNotifOn.length; i++)
				o.addEventListner(o.validator.showNotifOn[i], function(){o.validate();});
		}
	}
};

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;
			this.ele.setAttribute("maxLength",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, divId, notifId, img){
	if (!GupShup.util.isInitialized(notifId)){
		notifId ='notification';
		}
	var notifEle = GupShup.html.util.createNotificationElement(text,function(){GupShup.html.util.destroyElement(notifId)}, divId, notifId, img);
	/*
		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);
	
	if (!GupShup.util.isInitialized(divId)){
		divId = GupShup.forms.controls.DEFAULT_NOTIFICATION_CONTAINER;
		}
	var notifHolder = document.getElementById(divId);
	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();
}
}
})();
