﻿// validation number --------------------------
/*
blockNonNumbers takes the following arguments
function blockNonNumbers(obj, e, allowDecimal, allowNegative)

extractNumber takes the following arguments
function extractNumber(obj, decimalPlaces, allowNegative)
*/


//--------------------------------------------------------------
function ChangeElementVisibility(elementID, value, mustbe)
{
	var element = document.getElementById(elementID);
	if (value == mustbe)
		element.style.display = 'block';
	else
		element.style.display = 'none';
}

//--------------------------------------------------------------
function extractNumber(obj, decimalPlaces, allowNegative)
{
	var temp = obj.value;
	
	// avoid changing things if already formatted correctly
	var reg0Str = '[0-9]*';
	if (decimalPlaces > 0) {
		reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
	} else if (decimalPlaces < 0) {
		reg0Str += '\\.?[0-9]*';
	}
	reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
	reg0Str = reg0Str + '$';
	var reg0 = new RegExp(reg0Str);
	if (reg0.test(temp)) return true;

	// first replace all non numbers
	var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
	var reg1 = new RegExp(reg1Str, 'g');
	temp = temp.replace(reg1, '');

	if (allowNegative) {
		// replace extra negative
		var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
		var reg2 = /-/g;
		temp = temp.replace(reg2, '');
		if (hasNegative) temp = '-' + temp;
	}
	
	if (decimalPlaces != 0) {
		var reg3 = /\./g;
		var reg3Array = reg3.exec(temp);
		if (reg3Array != null) {
			// keep only first occurrence of .
			//  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
			var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
			reg3Right = reg3Right.replace(reg3, '');
			reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
			temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
		}
	}
	
	obj.value = temp;
}

//--------------------------------------------------------------
function blockNonNumbers(obj, e, allowDecimal, allowNegative)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	if (isNaN(key)) return true;
	
	keychar = String.fromCharCode(key);
	
	// check for enter or backspace or delete, or if Ctrl was pressed 
	if (key == 13 || key == 8 || isCtrl)
	{
		return true;
	}

	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
}

//--------------------------------------------------------------
function onKeyPressBlockNumbers(e)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	reg = /\d/;
	return !reg.test(keychar);
}

// email validation 
//--------------------------------------------------------------
function emailValidation(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){ return false }
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ return false }
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ return false }
		if (str.indexOf(at,(lat+1))!=-1){ return false }
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ return false }
		if (str.indexOf(dot,(lat+2))==-1){ return false }	
		if (str.indexOf(" ")!=-1){ return false }

	return true					
	}

// email validation --------------------


// validate frmSubmit  ----------------
//--------------------------------------------------------------
function reqFieldsIsEmpty()
{
	
	var result = false;
	
	// <INPUT TYPE="text">
	var collText;
	collText = document.body.getElementsByTagName("INPUT");
	
	if (collText!=null)
	{
		for (i=0; i<collText.length; i++) 
		{
			var inputType = collText[i].type;
				
			if (inputType == "text")
			{
				var inputDataSrc = collText[i].getAttribute("dataSrc");
				if (collText[i].value.length == 0 && inputDataSrc == "Required")
				{	
					collText[i].style.background = " #ffe0e0";
					result = true; 
				}
				else
				{
					if (inputDataSrc == "Required")
					{
						collText[i].style.background = "";
					}
				}
			}
		}
	}
	
	// <TEXTAREA>
	var collTextArea;
	collTextArea = document.body.getElementsByTagName("TEXTAREA");
	
	if (collTextArea!=null)
	{
		for (i=0; i<collTextArea.length; i++) 
		{
			var inputDataSrc = collTextArea[i].getAttribute("dataSrc");
			if (collTextArea[i].value.length == 0 && inputDataSrc == "Required")
			{	
				collTextArea[i].style.background = " #ffe0e0";
				result = true; 
			}
			else
			{
				if (inputDataSrc == "Required")
				{
					collTextArea[i].style.background = "";
				}
			}
		}
		
	}	
	
	
	// <select>
	var collSelect;
	collSelect = document.body.getElementsByTagName("SELECT");
	
	if (collSelect!=null)
	{
		for (i=0; i<collSelect.length; i++) 
		{
			var inputDataSrc = collSelect[i].getAttribute("dataSrc");
			var m_selectedIndex = collSelect[i].selectedIndex;
			if (collSelect[i].options[m_selectedIndex].value == "-1" && inputDataSrc == "Required")
			{	
				collSelect[i].style.background = " #ffe0e0";
				result = true; 
			}
			else
			{
				if (inputDataSrc == "Required")
				{
					collSelect[i].style.background = "";
				}
			}
			
		}
	}
	
	return  result; 
}


//--------------------------------------------------------------
function validateForm()
{
	
	var result = true;
	if (reqFieldsIsEmpty())
	{
		window.alert('.لطفا همه فيلدهای مورد نظر را پر کنيد');
		result = false;
	}
	return result;
} 



//--------------------------------------------------------------
function frmSubmit_Onkeydown(e, btnID)
{
	var key = window.event ? e.keyCode : e.which;
	if (key == 13 )
	{
		
		var m_btn = document.getElementById(btnID);
		if ( m_btn != null)
		{
			var m_txtSubmitClicked = document.getElementById("txtSubmitClicked");
			if (m_txtSubmitClicked != null)
			{
				m_txtSubmitClicked.value = "true";
			}
			m_btn.click();
		}
		else
		{
			window.alert('Error on saving .............!');
		}
	}
}



//--------------------------------------------------------------
function submitOnIndexPage()
{
	
	var m_txtSubmitSearchbar = document.getElementById("txtSubmitSearchbar");
	
	if (m_txtSubmitSearchbar.value == "true")
	{
		return true;
	}
	else
	{
		return false;
	}
}



