/*************************Header*****************************/
//number of functions=12
//1) 	checkemail				(frm,field)
//2) 	validNumeric			(thiss,format,chars,deflt)
//3)	isNaD(					thiss)
//4) 	isMonth					(thiss)
//5) 	isNumber				(obj)
//6)	isInteger				(s)
//7)	isEmpty					(strfield1)
//8)	isEmpty1				(field,mess)
//9)	checkInternationalPhone	(strPhone)
//10)	validatePhone			(thiss)
//11)	fileType				(typeName,thiss)
//12)	textCounter				(field, countfield, maxlimit)

/************************************************************/
// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 6;


function checkemail(frm,field){
	var str		= eval("document."+frm+"."+field+".value");
	var filter	= /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	str			= deleteChar(str,'');
	if((filter.test(str))||(str==''))
		testresults	= true
	else{
		alert(imf)
		eval("document."+frm+"."+field+".value=''");
		eval("document."+frm+"."+field+".focus()");
		eval("document."+frm+"."+field+".select()");
	}
}
function validNumeric(thiss,format,chars,deflt){
	var num 	= "";
	var num1 	= "";
	num			= thiss.value;
	num1 		= deleteChar(num,chars);
	var tst 	= isNaN(num1);
	var len		= format.length;
	var l 		= 0;
	l			= num.length;
	var valid	= false;
	var lo 		= "";
	lo=format.substring(format.indexOf(chars)+1);
	if(tst==false){
		num1=eval(num1);
		var ind = num.indexOf(chars);
		if(ind==-1 || ind==(num.length) -2){
			if(ind==-1){
				num=num+chars;
				lo=lo.length;
			}else lo=lo.length-1;
			if(chars!=''){
				for(i=0;i<lo;i++){
					num=num+'0';
				}
			}
			thiss.value=num;
		}
		if((l!=0)&&(l<=len)&&(num1!=0)){
			var vecF0	= "";
			var vec0	= "";
			var vecF1	= "";
			var vec1	= "";
			var vecF	= new Array();
			vecF		= format.split(chars);
			var vec		= num.split(chars);
			var len1 = vecF.length;
			var len2 =vec.length; 
			//alert(vecF.length+" "+vec.length);
			
			vecF0		= vecF[0];
			vec0		= vec[0];
			if(len1>1)
				vecF1=vecF[1];
			if(len2>1)
			 	vec1	= vec[1];
			if((vecF0.length>=vec0.length)&&(vecF1.length>=vec1.length)){
				valid=true;
			}
			//alert(vecF0+" "+vec0+"||"+vecF1+" "+vec1);
		}else{
			if(num1==0 || l==0) valid	= true;
		}
	}
	if(valid==false){
		alert(frmt+format);
		thiss.value	= deflt;
		thiss.focus();
		thiss.select();
	}
	if(thiss.value.indexOf(".")==0) thiss.value='';
}
function isNaD(thiss){
	var str = thiss.value;
	var i 	= str.indexOf('.');
	if(i!=-1 || isNaN(str)) {
		alert(mustbeintgr);
		thiss.select();
		thiss.focus();
	}
}

function isMonth(thiss){
	
	var str 	= thiss.value;
	var flag  	= 0;
	var zero 	= str.substring(0,1);
	isNaD(thiss);
	if((str<1 || str>12) && (str != "")){
		alert(month1b12);
		thiss.select();
		thiss.focus();
		flag 	= 1;
	}
	if((flag!=1) && ((str>=1) && (str<=9)) && (zero !="0")){
		str1		= "0"+str;
		thiss.value	= str1;
	}
}
function isNumber(obj){
	var varChar = obj.value;
	//alert(varChar);
	//alert(varChar.length+" "+varChar.indexOf("0"));
	if (isNaN(obj.value)||((varChar.length>1)&&(varChar.indexOf("0")==0))){
		alert(fieldnumber);
		obj.value="";
		obj.select();
		obj.focus();
	}
}
function isInteger(s){   
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function isEmpty(strfield1) {
	//name field
    if (strfield1 == "" || strfield1 == null || strfield1.charAt(0) == ' '){
       	return false;
    }else 
		return true;
}
function isEmpty1(field,mess) {
	var vecfield 	= field.split('|');
	var vecmess  	= mess.split('!');
	var result 		= true;
	var j			= 0;
	for(i=0;i<vecfield.length;){
		if(result==true){
			result = isEmpty(vecfield[i]);
			i++;
		}else {
			j	= i-1;
			i	= vecfield.length+100;
		}
	}
	if(result==false){
	 	alert(vecmess[j]); 
	 	return false;
	}else 
		return true;
}
function checkInternationalPhone(strPhone){
	s		= stripCharsInBag(strPhone,validWorldPhoneChars);
	var ret	= isInteger(s) && s.length >= minDigitsInIPhoneNumber
	if(strPhone.replace(" ","")!="") 
		return ret;
	else 
		return true;
}

function validatePhone(thiss){
	var Phone=thiss;
	
	if (checkInternationalPhone(Phone.value)==false){
		alert(validphone)
		Phone.value	= "";
		Phone.focus();
		return false;
	}
	return true;
 }

var alertNum=0
function fileType(typeName,thiss){
		var fname = "";
		eval("fname 	  = "+thiss+".value");
		fname	  =	fname.toLowerCase();
		typeName  = typeName.toLowerCase();
		if(fname.indexOf("'")!=-1){
			if(alertNum%2==0)
				alert(singleQt)
			alertNum++	
			alert(alertNum)
			return -1
		}
		//var blackList = ".exe.zip.src.vbs.bat.com.pif.asp.doc.xls";//TCH : this's the original list from net.left it here because of future file Scanning 
		var blackList = ".exe.zip.src.vbs.bat.com.pif.asp.mp3.mpg.wmv.cda.wav.mov";
		var typeAllowed	= "";
		if(typeName == 'text') 	typeAllowed	= ".txt";
		if(typeName == 'image') typeAllowed	= ".jpg/.gif/.png/.bmp/.jpeg";
		if(typeName == 'doc') 	typeAllowed	= ".doc/.docx";
		
 		if((fname!='')&&(typeName!='')){
			//var ext = fname.substring(fname.length-4);
			var ext = fname.substring(fname.lastIndexOf("."),fname.length);
			
			if(blackList.indexOf(ext)!=-1) return -1;
			else{
					if((typeName=='text' && ext=='.txt')||(typeName=='image' && typeAllowed.indexOf(ext)!=-1) || typeName=='file' || (typeName=="doc" && (ext==".doc" || ext==".docx")) )
						 return 1;
					else {
							alert(selectfile+typeAllowed);
							return -2;
					}
			}
		}else{
			return -3;
		}
}

function textCounter(field, countfield, maxlimit){
	if (field.value.length > maxlimit){ // if too long...trim it!
		if(field.value.length - maxlimit>1)
		alert(limitExc+'\n'+field.value.substring(maxlimit,field.value.length));
			//alert('Limit exceeded.This part will be chopped:\n'+field.value.substring(maxlimit,field.value.length));
		field.value = field.value.substring(0, maxlimit);
	}
	else // otherwise, update 'characters left' counter
		countfield.value = maxlimit - field.value.length;
}

function isYear(thiss){
	var str 	= thiss.value;
	isNaD(thiss);
	if((str<1900 || str>2100) && (str != "")){
		alert(validYear);
		thiss.select();
		thiss.focus();
	}
	
}
function setMaxLength() {
	var x = document.getElementsByTagName('textarea');
	var counter = document.createElement('div');
	
	counter.className = 'counter';
	for (var i=0;i<x.length;i++) {
		
		if (x[i].getAttribute('maxlength')) {
			
			var counterClone = counter.cloneNode(true);
			counterClone.relatedElement = x[i];
			counterClone.innerHTML = '<span class="counter">0</span>/'+x[i].getAttribute('maxlength');
			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);
			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];
	
			x[i].onkeyup = x[i].onchange = checkMaxLength;
			x[i].onkeyup();
		}
	}
}

function checkMaxLength(i) {
	var maxLength = this.getAttribute('maxlength');
	var currentLength = this.value.length;
	if (currentLength > maxLength){
		var str=this.getAttribute('value');
		if(str==null) str=this.value;
		//alert('Limit exceeded.This part will be chopped:\n'+str.substring(maxLength,currentLength+1));
		alert(limitExc+'\n'+str.substring(maxLength,currentLength+1));
		var str1=this.getAttribute('value');
		if(str1==null) this.value=str.substring(0, maxLength);
		else this.setAttribute('value',str.substring(0, maxLength)) ;
		currentLength =maxLength;
		this.relatedElement.className = 'toomuch';
	}else
		this.relatedElement.className = '';
	this.relatedElement.firstChild.nodeValue = currentLength;
	// not innerHTML
}
function checkHacker(object)
{
	
		 var fault = false;
		 var text=object.value;
		 var len=text.length;
		 var testresults =false;
		 var text1=text;
		// var str='0123456789abcdefghijklmnopqrstuxywz\.@,';
		 var filter	= /^([a-z0-9A-Z]*[\.@,]*)$/i
		
		 if((filter.test(text))||(text==''))
				testresults	= true
		else{
				for(var i=0;i<len;i++){
						char = text.charAt(i);
						
 						if (!(((char <= "z") && (char >= "a")) || ((char <= "Z") && (char >= "A")) || ((char <= "9") && (char >= "0"))||(char=='@' || char=='.' ||char==',' ||char==' '))){
							var asci =ascii_value (char);
							if(asci<128 && asci!=32){
							//alert(char.charCodeAt());
							text1=text1.replace(char,"");
							}
							
					}
 							
	    		}
				if(object.type!='password')
				object.value=text1;
				else
				object.value='';
		}
	 	 
}

function ascii_value (c)
{
	// restrict input to a single character
	c = c . charAt (0);

	// loop through all possible ASCII values
	var i;
	for (i = 0; i < 256; ++ i)
	{
		// convert i into a 2-digit hex string
		var h = i . toString (16);
		if (h . length == 1)
			h = "0" + h;

		// insert a % character into the string
		h = "%" + h;

		// determine the character represented by the escape code
		h = unescape (h);

		// if the characters match, we've found the ASCII value
		if (h == c)
			break;
	}
	return i;
}


