﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function isEmailValid(emailStr) { // Check if the "emailStr" is valid or not.	
	var check=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (check.test(emailStr)) return true
		 else return false      
  }
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function getFixedURL(url) { // Add "http://" to the url string if necessary .
	if (url.substr(0,7).toLowerCase()!="http://")
		url = "http://"+url;
	return url;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function isURLValid(url){ // Check if the "url" is valid or not.
   // var RegExp = /^http\:\/\/+(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
    var RegExp = /^http\:\/\/+([\w-]+(?:\.[\w-]+)*)\.((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
	if(RegExp.test(url))
        return true; 
    else
        return false;  
} 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function isIPValid (IPvalue) { // Check if the "IPValue" is a valid IP or not. 
	// Remove the port number with the ":"
	startPort = IPvalue.indexOf(":");
	if (startPort!=-1)
		IPvalue = IPvalue.substr(0,startPort);
	
	if (IPvalue.substr(0,7).toLowerCase()=="http://")
		IPvalue = IPvalue.substr(7);
	var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = IPvalue.match(ipPattern);

	if ((IPvalue == "0.0.0.0") || (IPvalue == "255.255.255.255") || (ipArray == null))
		return false;
	else {
		for (i = 0; i < 4; i++) {
			thisSegment = ipArray[i];
			if (thisSegment > 255)
				return false;
			if ((i == 0) && (thisSegment > 255))
				return false;
		}
	}
	return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function splitUrl(url) {
	if (url.substr(0,7).toLowerCase()=="http://")
		url = url.substr(7);
	splitCharPos = url.indexOf("/");
	urlWithoutIP = "";
	ip = url;
	if (splitCharPos!="-1") {
		urlWithoutIP = url.substr(splitCharPos+1);
		ip = url.substr(0,splitCharPos);
	}
	return [ip,urlWithoutIP];
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function isValidURLWithIP(url) {
	arr = splitUrl(url);
	if (isIPValid(arr[0]))
		return true;
	else
		return false;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function check_ip_format(ip_addr) { // not used.
	var valid=true;
	var rE = new RegExp("/^((127)|(192)|(10).*)$/");
	if (rE.test(ip_addr)) {
		valid = false; 
	}
	else { 
		var ip = ip_addr.split("."); 
		if(ip.length!= 4) {
			valid = false; 
		} 
		else { 
			for (i=0; i<ip.length; i++) {
				if (isNaN(ip[i])==true || ip[i] > 255 || ip[i] < 1) { 
					valid = false; 
				} 
			} 
		} 
	} 
	return valid;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function isPhotoTypeValid(photo) { // Validation for photo type.
	if(document.all) // IE
		var regExp = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.jpg|.JPG|.png|.PNG)$/;
	else // FF
		var regExp = /^((\w[\w].*))(.jpg|.JPG|.png|.PNG)$/;
	if (!regExp.test(photo)) 
		return false;
	else
		return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function isCustomTypeValid(file) { // Validation for file type.
	if (navigator.userAgent.toLowerCase().indexOf("firefox") != -1)
		var regExp = /(.txt|.TXT|.xls|.XLS)/;
	else
		var regExp = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.txt|.TXT|.xls|.XLS)$/;
	if (!regExp.test(file)) 
		return false;
	else
		return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function isFileNameValid(file) { // Basic validation for file input(photo).
	if((file.indexOf("'")!=-1) || (file.indexOf("`")!=-1) || (file.indexOf("_")!=-1) || (file.indexOf("\"")!=-1) || (file.indexOf("_")!=-1)) 
		return false;	
	return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function isFieldEmpty(str) { // Check if the "str" value is empty or not.
	return (str=="")
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function isDoubleInteger(str) { // Check if the "str" value is a positive double number or not.
	var i,pointCount;
	pointCount = 0;
    for (i = 0; i < str.length; i++)
    {   
        var c = str.charAt(i);
        if (((c < "0") || (c > "9"))) {
			if ((c != "+") || (i!=0))
				return false;
			else {
				if (pointCount > 0)
					return false;
				else
					pointCount++;
			}	
		}
    }
    return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function isPositiveNumber(str) { // Check if the "str" value is a positive number or not.
	var i;
    for (i = 0; i < str.length; i++)
    {   
        var c = str.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function IsNumeric(str) { // Check if the "str" value is a number or not(positive and nagitive).
    var ValidChars = "0123456789";
    var IsNumber=true;
    var Point=false;
    var Char;
	FirstChar = str.charAt(0);
	if ((ValidChars.indexOf(FirstChar) == -1) && (FirstChar != "-"))
		IsNumber = false;
    for (i = 1; i < str.length && IsNumber == true; i++) { 
		Char = str.charAt(i); 
		if (Char == ".") {
			if ((FirstChar=="-") && (i==1))
				IsNumber = false;
			if (Point) 
				IsNumber = false;
			Point = true;
		}
		if ((ValidChars.indexOf(Char) == -1) && (Char != "."))
			IsNumber = false;
    }
    return IsNumber;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkField(cmd,field,msg,num,strMatch) { // Check the entered "field" for some cases, and use the "msg" to display.
	switch (cmd) {
		case "0"   : if (isFieldEmpty(field.value)) { // Check if the "field" value is empty or not.	
						 if (msg!="") alert(msg);
						 field.focus();
						 return false;
					 }
					 break;
		case "1"   : if (!isEmailValid(field.value)) { // Check if the "field" is valid or not.
						 if (msg!="") alert(msg);
						 field.focus();
						 field.select();
						 return false;
					 }
					 break;
		case "2"   : 
					 if ((field.value==strMatch)) { // Compare the "field" with the "strMatch" (if not equal).
						 if (msg!="") alert(msg);
						 field.focus();
						 return false;
					 }
					 break;
		case "3"   : if (field.value.length<Math.abs(num)) { // Check the length of the "field" comparing to "num".
						 if (msg!="") alert(msg);
						 field.focus();
						 return false;
					 }
					 break;
		case "13"   : if (field.value.length>Math.abs(num)) { // Check the length of the "field" comparing to "num".
						 if (msg!="") alert(msg);
						 field.focus();
						 return false;
					 }
					 break;
		case "4"   : fixedURL = getFixedURL(field.value);
					 if ((!isURLValid(fixedURL)) && (!isValidURLWithIP(fixedURL))) { // Check if the "field" is valid URL(with IP) or not.
						 if (msg!="") alert(msg);
						 field.focus();
						 field.select();
						 return false;
					 }
					 else 
						field.value = fixedURL;
					 break;		
		case "5"   : if (!isPhotoTypeValid(field.value)) { // Check if the "field" value is valid photo url.	
						 if (msg!="") alert(msg);
						 field.focus();
						 field.select();
						 return false;
					 }
					 break;		
		case "6"   : if (!isPositiveNumber(field.value)) { // Check if the "field" value is valid number.	
						 if (msg!="") alert(msg);
						 field.focus();
						 field.select();
						 return false;
					 }
					 break;		
		case "7"   : if (Math.abs(field.value)<Math.abs(num)) { // Check if the "field" is smaller then "num".
						 if (msg!="") alert(msg);
						 field.focus();
						 field.select();
						 return false;
					 }
					 break;	
		case "8"   : if (!isDoubleInteger(field.value)) { // Check if the "field" value is valid number.	
						 if (msg!="") alert(msg);
						 field.focus();
						 field.select();
						 return false;
					 }
					 break;	
		case "9"   : if (!isFileNameValid(field.value)) { // Check if the "field" value is valid file path.	
						 if (msg!="") alert(msg);
						 field.focus();
						 field.select();
						 return false;
					 }
					 break;
		case "10"   : if (!IsNumeric(field.value)) { // Check if the "field" value is valid number.
						 if (msg!="") alert(msg);
						 field.focus();
						 field.select();
						 return false;
					 }
					 break;	
		case "11"   : if (!isCustomTypeValid(field.value)) { // Check if the "field" value is valid url(custom file type).	
						 if (msg!="") alert(msg);
						 field.focus();
						 field.select();
						 return false;
					 }
					 break;					 
	}
   return true;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////