﻿function SelectedItemCount()
{
	
	var coll = document.all.tags("input");
	var result = 0;
	if(coll!=null)
	{
		for(i=0;i<coll.length;++i)
		{
			if(coll[i].type=="checkbox")
			{
				if (coll[i].checked)
					result++;
			}
		}
	}
	return result;
}	

function CheckAll(obj)
{
	
	var coll = document.all.tags("input");
	
	if(coll!=null)
	{
		for(i=0;i<coll.length;++i)
		{
			
			if(coll[i].type=="checkbox")
			{
				coll[i].checked = obj.checked;
				if (coll[i].id != obj.id)
					coll[i].attachEvent ('onclick', NoneCheckAll);
			}
		}
	}
	return true;
}								
		
function NoneCheckAll()
{
	document.getElementById("chkAll").checked = false;
	return true;
}					

function showTableOnRDOSelected(rdo, param)
{
	eval("var list=" + param);

	for (var count = 0; count < list.length; count++)
	{
		if (rdo[list[count].index].checked)
			document.getElementById(list[count].table).style.display = 'block';
		else
			document.getElementById(list[count].table).style.display = 'none';
	}
	
}
	
function refreshMain()
{	
	var mainFrame = window.parent.frames.item('main').document;
	mainFrame.execCommand('Refresh');
}


/*

///////////////////////////////////////////////////////////////////////////////
//   ..:: right click menu
///////////////////////////////////////////////////////////////////////////////

 menuItems = new Array();
 menuItemNum = 0;

 function addMenuItem(text, url, img){
  if(img) menuItems[menuItemNum] = new Array(text, url, img);
  else if(text) {menuItems[menuItemNum] = new Array(text, url);}
  else menuItems[menuItemNum] = new Array();
  menuItemNum++;
 }

 menuWidth = 169; //menu width
 menuHeight = 109; //menu height
 menuDelay = 100; //delay before menu appears
 menuSpeed = 8; //speed which menu appears (lower=faster)
 menuOffset = 2; //offset of menu from mouse pointer

 addMenuItem("<center>اتوماسيون انستيتو ايزايران</center>","#");
 addMenuItem();
 addMenuItem("ويرايش اطلاعات شخصي","http://comp1/InstituteAutomation/CP/Users/Users/chang_pwd.aspx");
 addMenuItem("خروج از برنامه","http://comp1/InstituteAutomation/CP/login.aspx?out=yes");
 addMenuItem();
 addMenuItem("راهنما","#");
 addMenuItem("درباره نرم افزار","http://comp1/InstituteAutomation/CP/top.htm");

 ///////////////////////////////////////////////////////////////////////////////
 // do not edit the code below, it is required for the menu to work correctly
 ///////////////////////////////////////////////////////////////////////////////


 if(window.navigator.appName == "Microsoft Internet Explorer" && window.navigator.appVersion.substring(window.navigator.appVersion.indexOf("MSIE") + 5, window.navigator.appVersion.indexOf("MSIE") + 8) >= 5.5)
  isIe = 1;
 else
  isIe = 0;

 if(isIe){
  menuContent = '<table id="rightMenu" width="0" height="0" cellspacing="0" cellpadding="0" style="font:menu;color:menutext;"><tr height="1"><td style="background:threedlightshadow" colspan="4"></td><td style="background:threeddarkshadow"></td></tr><tr height="1"><td style="background:threedlightshadow"></td><td style="background:threedhighlight" colspan="2"></td><td style="background:threedshadow"></td><td style="background:threeddarkshadow"></td></tr><tr height="10"><td style="background:threedlightshadow"></td><td style="background:threedhighlight"></td><td style="background:threedface"><table cellspacing="0" cellpadding="0" nowrap style="font:menu;color:menutext;cursor:default;">';
  for(m=0;m<menuItems.length;m++){
   if(menuItems[m][0] && menuItems[m][2])
    menuContent += '<tr height="17" onMouseOver="this.style.background=\'highlight\';this.style.color=\'highlighttext\';" onMouseOut="this.style.background=\'threedface\';this.style.color=\'menutext\';" onClick="parent.window.location.href=\'' + menuItems[m][1] + '\'"><td style="background:threedface" width="1" nowrap></td><td width="21" nowrap><img src="' + menuItems[m][2] + '"></td><td nowrap>' + menuItems[m][0] + '</td><td width="21" nowrap></td><td style="background:threedface" width="1" nowrap></td></tr>';
   else if(menuItems[m][0])
    menuContent += '<tr height="17" onMouseOver="this.style.background=\'highlight\';this.style.color=\'highlighttext\';" onMouseOut="this.style.background=\'threedface\';this.style.color=\'menutext\';" onClick="parent.window.location.href=\'' + menuItems[m][1] + '\'"><td style="background:threedface" width="1" nowrap></td><td width="21" nowrap></td><td nowrap>' + menuItems[m][0] + '</td><td width="21" nowrap></td><td style="background:threedface" width="1" nowrap></td></tr>';
   else
    menuContent += '<tr><td colspan="5" height="4"></td></tr><tr><td colspan="5"><table cellspacing="0"><tr><td width="2" height="1"></td><td width="0" height="1" style="background:threedshadow"></td><td width="2" height="1"></td></tr><tr><td width="2" height="1"></td><td width="100%" height="1" style="background:threedhighlight"></td><td width="2" height="1"></td></tr></table></td></tr><tr><td colspan="5" height="3"></td></tr>';
  }
  menuContent += '</table></td><td style="background:threedshadow"></td><td style="background:threeddarkshadow"></td></tr><tr height="1"><td style="background:threedlightshadow"></td><td style="background:threedhighlight"></td><td style="background:threedface"></td><td style="background:threedshadow"></td><td style="background:threeddarkshadow"></td></tr><tr height="1"><td style="background:threedlightshadow"></td><td style="background:threedshadow" colspan="3"></td><td style="background:threeddarkshadow"></td></tr><tr height="1"><td style="background:threeddarkshadow" colspan="5"></td></tr></table>';

  menuPopup = window.createPopup();
  menuPopup.document.body.innerHTML = menuContent;
 }

 function showMenu(){
  menuXPos = event.clientX + menuOffset;
  menuYPos = event.clientY + menuOffset;

  menuXIncrement = menuWidth / menuSpeed;
  menuYIncrement = menuHeight / menuSpeed;

  menuTimer = setTimeout("openMenu(0,0)", menuDelay);

  return false;
 }


 function openMenu(height, width){
  iHeight = height;
  iWidth = width;

  menuPopup.show(menuXPos, menuYPos, iWidth, iHeight, document.body);

  if(iHeight < menuHeight)
   menuTimer = setTimeout("openMenu(iHeight + menuYIncrement, iWidth + menuXIncrement)", 1);
  else
   clearTimeout(menuTimer);
 }


 if(isIe) document.oncontextmenu = showMenu;


//-->

*/

// center popup opener --------------------------
function PopupCenter(pageURL, title,w,h) {

	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	var targetWin = window.open (pageURL,title,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width='+w+',height='+h+',top='+top+',left='+left);
} 

// Get data from student quick search --------------------------
function getStudentFromOldDB(selectedTR)
{

	var studentRow = selectedTR.title.split(',');
	var name     = studentRow[0];
	var family   = studentRow[1];
	var idno     = studentRow[2];
	var father   = studentRow[3];
	var tel_home = studentRow[4];
	var address  = studentRow[5];
	var id  = studentRow[6];
	
	var txtFirstName   = window.opener.document.all.item("txtFirstName");
	var txtLastName    = window.opener.document.all.item("txtLastName");
	var txtHomeAddress = window.opener.document.all.item("txtHomeAddress");
	var txtHomePhone   = window.opener.document.all.item("txtHomePhone");
	var txtStudentID = window.opener.document.all.item("txtStudentID");	
	if(window.opener.document.all.item("txtBirthCertNumber") != null)
	{	
		var txtBirthCertNumber = window.opener.document.all.item("txtBirthCertNumber");
		txtBirthCertNumber.value = idno;
	}
	if(window.opener.document.all.item("txtFather") != null)
	{
		var txtFather = window.opener.document.all.item("txtFather");		
		txtFather.value = father;
	}
			
	txtFirstName.value = name;
	txtLastName.value = family;
	txtHomeAddress.value = address;
	txtHomePhone.value = tel_home;
	txtStudentID.value = id;
	
	window.close();
}


// Get data from student quick search --------------------------
function getStudentNo(selectedTR)
{
	var student = selectedTR.title.split(',');
	var studentNo = student[0];
	var studentFullName = student[1];
	var txtStudentNo = window.opener.document.all.item("txtStudentNo");
	var lblStudentFullName = window.opener.document.all.item("lblStudentFullName");
	txtStudentNo.value = studentNo;
	lblStudentFullName.innerText = studentFullName;
	window.close();
}

// Get data from Teacher quick search --------------------------
function getTeacher(selectedTR)
{
	var teacher = selectedTR.title.split(',');
	var id = teacher[0];
	var ddlTeacher = window.opener.document.all.item("ddlTeacher");
	for(var i = 0; i < ddlTeacher.length; i++){
		if(ddlTeacher.options[i].value == id){
			ddlTeacher.selectedIndex = i;			
			break;
		}
	}
	window.close();
}

// Get data from Contract quick search --------------------------
function getContractCode(selectedTR)
{
	var contract = selectedTR.title.split(',');
	var contractCode = contract[0];
	var contractOrganization = contract[1];
	var txtContractCode = window.opener.document.all.item("txtContractCode");
	var lblOrganization = window.opener.document.all.item("lblOrganization");
	txtContractCode.value = contractCode;
	lblOrganization.innerText = contractOrganization;
	lblOrganization.style.color = 'green';
	window.close();
}


// Get data from IntroductionLetter quick search --------------------------
function getIntroductionLetterNo(selectedTR)
{
	var introLetter = selectedTR.title.split(',');
	var introLetterCode = introLetter[0];
	var introLetterOrganization = introLetter[1];
	var introLetterID = introLetter[2];
	
	var txtLetterNo = window.opener.document.all.item("txtLetterNo");
	var txtLetterID = window.opener.document.all.item("txtLetterID");
	var lblOrganization = window.opener.document.all.item("lblOrganization2");
	txtLetterNo.value = introLetterCode;
	txtLetterID.value = introLetterID;
	lblOrganization.innerText = introLetterOrganization;
	lblOrganization.style.color = 'green';
	window.close();
}

// Get data from Organization quick search --------------------------
function getOrganizationCode(selectedTR)
{
	var organization = selectedTR.title.split(',');
	var organizationCode = organization[0];
	var organizationName = organization[1];
	var txtOrganizationCode = window.opener.document.all.item("txtOrganizationCode");
	var lblOrganizationTitie = window.opener.document.all.item("lblOrganizationTitie");
	txtOrganizationCode.value = organizationCode;
	lblOrganizationTitie.innerText = organizationName;
	lblOrganizationTitie.style.color = 'green';
	window.close();
}

// Get data from Organization quick search --------------------------
function getOrganizationInformation(selectedTR)
{
	var organization = selectedTR.title.split(',');
	var organizationCode = organization[0];
	var organizationName = organization[1];
	var organizationAddress = organization[2];
	var organizationPhone = organization[3];
	var organizationPostalCode = organization[4];
	
	var txtOrganizationCode = window.opener.document.all.item("txtOrganizationCode");
	var txtCompanyName = window.opener.document.all.item("txtCompanyName");
	var txtAddress = window.opener.document.all.item("txtAddress");
	var txtPhone = window.opener.document.all.item("txtPhone");
	var txtPostalCode = window.opener.document.all.item("txtPostalCode");
	
	txtCompanyName.value = organizationName;	
	txtAddress.value = organizationAddress;	
	txtPhone.value = organizationPhone;	
	txtPostalCode.value = organizationPostalCode;
		
	//txtOrganizationCode.value = organizationCode;
	//lblOrganizationTitie.innerText = organizationName;
	//lblOrganizationTitie.style.color = 'green';
	window.close();
}

// Get data from Factor quick search --------------------------
function getFactorNo(selectedTR)
{
	var factor = selectedTR.title.split(',');
	var factorCode = factor[0];
	var factorOrganization = factor[1];
	var txtFactorNo = window.opener.document.all.item("txtFactorNo");
	var lblFactorOrganization = window.opener.document.all.item("lblFactorOrganization");
	txtFactorNo.value = factorCode;
	lblFactorOrganization.innerText = factorOrganization;
	lblFactorOrganization.style.color = 'green';
	window.close();
}

// Get data from IClass quick search --------------------------
function getClassCode(selectedTR)
{
	var classObj = selectedTR.title.split(',');
	var classCode = classObj[0];
	var classTitle = classObj[1];
	var txtClassCode = window.opener.document.all.item("txtClassCode");
	var lblClassTitle = window.opener.document.all.item("lblClassTitle");
	txtClassCode.value = classCode;
	lblClassTitle.innerText = classTitle;
	lblClassTitle.style.color = 'green';
	window.close();
}

// 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" || inputDataSrc == "URequired"))
				{	
					collText[i].style.background = " #ffe0e0";
					result = true; 
				}
				else
				{
					if (inputDataSrc == "Required" || inputDataSrc == "URequired")
					{
						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" || inputDataSrc == "URequired"))
			{	
				collTextArea[i].style.background = " #ffe0e0";
				result = true; 
			}
			else
			{
				if (inputDataSrc == "Required" || inputDataSrc == "URequired")
				{
					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;
	}
}



//--------------------------------------------------------------
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return null;
}


//--------------------------------------------------------------
function getCurrentPageName()
{
	var sPath = window.location.pathname;
	//var sPage = sPath.substring(sPath.lastIndexOf('\\') + 1);
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	return sPage;
}

//--------------------------------------------------------------
function resetDDL(ddl)
{
	for (var count = ddl.options.length-1; count >-1; count--)
	{
		ddl.options[count] = null;
	}
	ddl.options[0] = new Option( "انتخاب کنيد", "-1", false, false);
}


//--------------------------------------------------------------
function btnExcel_Click()
{
	var txtExcelName =  document.getElementById('txtExcelName');
	if (txtExcelName)
		txtExcelName.value = window.prompt('لطفا نام فايل اکسل را وارد کنيد','');
	else
		return false;			
}


