function de(txt,element) {	// data error
	element.focus()
	element.select()
	alert(txt)
}

function de2(txt,element){
	element.focus()
	alert(txt)
}

function stripchar(str,character) {												// strips character from str
	s = str
	if (s != null) {
		while ((j=s.indexOf(character))>=0) {
			s = s.substring(0,j) + s.substring(j+1,s.length)
		}
	}
	return s
}

function stripbegspace(str) {														// strips whitespace from beginning of string
	t = str
	if (t != null) {
		for (i=0; i<t.length; i++) {
				if ( t.charAt(0)==' ' || t.charAt(0)=='	') {				//second IF is a tab
					t = t.substring(1,t.length)
					i=0;   																//restart the count 
				}else{
					return t;
				}
		}
	}
	return t;
}

function striptrailspace(str) {													// strips whitespace from end of string
	t = str
	if (t != null) {
		for (i=t.length-1; i>=0; i--) {
				if ( t.charAt(i)==' ' || t.charAt(i)=='	') {				//second IF is a tab
					t = t.substring(0,t.length-1)
				}else{
					return t;
				}
		}
	}
	return t;
}

function checknumeric(txt) {
	ok = true
	t = txt
	for (i=0; i<t.length; i++) {
		if (!isDigit(t.charAt(i)) && ok==true) { ok=false }
	}
	return ok
}

function checknumericcount(txt) {
	count=0;
	t = txt
	for (i=0; i<t.length; i++) {
		if (isDigit(t.charAt(i))){ count = count+1; }
	}
	return count;
}

function checkalphacount(txt) {
	ok = true
	count = 0;
	t = txt
	for (i=0; i<t.length; i++) {
		if ( (t.charAt(i) <= "z" && t.charAt(i) >= "a") || (t.charAt(i) <= "Z" && t.charAt(i) >= "A")) { count = count + 1; }
	}
	return count;
}

function isDigit(num) {
	if (num.length>1){return false;}
	var string="1234567890.";
	if (string.indexOf(num)!=-1){return true;}
	return false;
}

function checkEmail(email){
		length2=email.length-1;
		number=0;
		number2=0;
		number3=0;
		error=0;
		numb=0;
		flag=0;
	
		if (email.length == 0){
			alert('You must enter your email address.');
		 	return false;
		}
		if ((email.length != 0)&&(email.charAt(0) == ".")){
		  	alert('Your email address can not start with a full stop');
		  	return false;
		}
		if ((email.length != 0)&&(email.charAt(0) == "@")){
		 	alert('Your email address can not start with an @ symbol');
		   return false;
		}
		for(num=0;num<email.length;num++){
			if(email.charAt(num)=="@"){
				numb=numb+1;
			}
		}
		if(numb>1){
			alert('Your email address can only contain one @ symbols.');
			return false;
		}
		else if((numb==0)){
			alert('Your email address must contain an @ symbol.');
			return false;
		}
		else if((email.charAt(length2)=="@")){
			alert('Your email address can not end with a @ symbol.');
			return false;
		}
		else if((email.charAt(length2)==".")){
			alert('Your email address can not end with a full stop.');
			return false;
		}
		for(num=0;num<email.length;num++){
			if(email.charAt(num)==" "){
					number=number+1;
			}
		}
		if(number>0){
			alert('Your email address can not contain spaces.')
			return false;
		}
		
		for(num=0;num<email.length;num++){	
			if(email.charAt(num)=="."){
					number2=number2+1;
			}
		}
		if(number2==0){
			alert('Your email address must contain a full stop.')
			return false;
		}
		
		
		for(num=0;num<email.length;num++){
			if(email.charAt(num)=="."){
			  if(email.charAt(num+1)=="."){
				 alert('Your email address has two full stops in a row.')
				 return false;
			  }
			}
		}

		/* Invalid characters for email addresses */
		errorTypeIndex=0;
		errorType = ['exclamation marks (!)', 'double quotes', 'hash signs (#)', 
                     'dollar signs ($)','percent signs (%)','parentheses ( or )',
                     'comma\'s (,)','colons (:)','semi colons (;)',
                     'chevrons (< or >)','box brackets ([ or ])','backticks (`)',
                     'pipe symbols (|)'                     
                     ];  
                     	
		for(num=0;num<email.length;num++){	
			if(email.charAt(num)=="!"){
				number3=number3+1;
			}else if (email.charAt(num)=="\"") {
                number3=number3+1;
                errorTypeIndex=1;
            }else if (email.charAt(num)=="#") {
                number3=number3+1;
                errorTypeIndex=2;
            }else if (email.charAt(num)=="$") {
                number3=number3+1;
                errorTypeIndex=3;
            }else if (email.charAt(num)=="%") {
                number3=number3+1;
                errorTypeIndex=4;
            }else if ((email.charAt(num)=="(") || (email.charAt(num)==")")) {
                number3=number3+1;
                errorTypeIndex=5;
            }else if (email.charAt(num)==",") {
                number3=number3+1;
                errorTypeIndex=6;
            }else if (email.charAt(num)==":") {
                number3=number3+1;
                errorTypeIndex=7;
            }else if (email.charAt(num)==";") {
                number3=number3+1;
                errorTypeIndex=8;
            }else if ((email.charAt(num)=="<") || (email.charAt(num)==">")) {
                number3=number3+1;
                errorTypeIndex=9;
            }else if ((email.charAt(num)=="[") || (email.charAt(num)=="]")) {
                number3=number3+1;
                errorTypeIndex=10;
            }else if (email.charAt(num)=="`") {
                number3=number3+1;
                errorTypeIndex=11;
            }else if (email.charAt(num)=="|") {
                number3=number3+1;
                errorTypeIndex=12;
            }
            
            if (number3>0) {
                break;
            }
		}
	
		if(number3>0){
			alert('Your email address can not contain ' + errorType[errorTypeIndex] );
			return false;
		}        

		
		return true;

}


function checkURL(url) {
	var valid = false;
	
	var regex_1 = /(http:|https:)\/\/([A-z\d]+)\:([A-z\d]+)\@([A-z\d\-\.]+\.)+[A-z]/i;
	var regex_2 = /^(http:|https:)\/\/([A-z\d\-\.]+\.)+[A-z]/i;
	var regex_3 = /^(http:|https:)\/\/(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/i;
	
	if (regex_1.test(url)) { valid = true; }
	if (regex_2.test(url)) { valid = true; }
	if (regex_3.test(url)) { valid = true; }
	
	return valid;
}
