﻿// AJAX Functions /////////////////////////////

function RequestAjaxPage(url, containerid)
{
	var page_request = false
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		page_request = new XMLHttpRequest()
	else if (window.ActiveXObject)
	{
		try 
		{ page_request = new ActiveXObject("Msxml2.XMLHTTP")} 

			catch (e)
			{
				try{ page_request = new ActiveXObject("Microsoft.XMLHTTP")}
				catch (e){}
			}
		}
	else
	return false
	
	page_request.onreadystatechange=function()
	{
		LoadAjaxPage(page_request, containerid)
	}	
	page_request.open('POST', url, true)
	page_request.send(null) ;	
} // function RequestAjaxPage(url, containerid)


function LoadAjaxPage(page_request, containerid)
{
	WatingImage = "<img src='themes/images/waiting.gif'>" ;
	
	document.getElementById(containerid).innerHTML = WatingImage ;
	
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
		document.getElementById(containerid).innerHTML=page_request.responseText ;
		
} // LoadAjaxPage(page_request, containerid)

// AJAX Functions /////////////////////////////



// Cookies Functions /////////////////////////////
function setCookie( name, value, expires, path, domain, secure ) 
{
	var today = new Date();
	today.setTime( today.getTime() );
	
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
} // function setCookie( name, value, expires, path, domain, secure )


function getCookie( name ) 
{
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	
	if ( start == -1 ) return null;
	
	var end = document.cookie.indexOf( ";", len );
	
	if ( end == -1 ) end = document.cookie.length;
	
	return unescape( document.cookie.substring( len, end ) );
} // function getCookie( name ) 

// Cookies Functions /////////////////////////////


function changeTabs(tabId)
{
	ctabs = getCookie('tabId');
	

	
	if(tabId == -1 && ctabs == null) {
		tabId = 0;
	}
	else if(tabId == -1 && ctabs != null)
	{
		var crntUrl = location.href;
		
		if (crntUrl.indexOf('index.php') != -1)
			tabId = 0	
		else
			tabId = ctabs;
	}
	
	for(i=0; i<=5; i++) {
		if(i == tabId) {
			document.getElementById("tabBtn"+i).className = 'tabBtn_open';
			document.getElementById("tab"+i).className = 'tabOpened';
		}
		else {
			document.getElementById("tabBtn"+i).className = 'tabBtn_close';
			document.getElementById("tab"+i).className = 'tabClosed';
		}
	}
	
	setCookie('tabId', tabId, '', '/', '', false );
	
} // function changeTabs(tabId)


function redirect(page, option, id)
{
	var url ;
	
	url = (option == '') ? page + '.php' : page + '.php?opt=' + option ;
	//url = page + '.php?opt=' + option;
	
	url += (id != '') ? '&id=' + id : '' ;
	
	window.location = url ;
} // function redirect()


function checkDate(id)
{	
	var dvName = 'divYear' + id;
	var lstName = 'selMonthTo' + id;
	
	var divYear = document.getElementById(dvName);
	var list = document.getElementById(lstName);
	var index = list.selectedIndex;
	
	if ( index == 1) {
		if(list.length > 13)
			divYear.style.display = 'none';
	}
	else
		divYear.style.display = 'block';
} // function checkDate()


function checkDate2()
{
	var divYear = document.getElementById('divYear');
	var selctedIndex = document.getElementById('selMonthTo').selectedIndex;
	
	if ( selctedIndex == 1) {
			divYear.style.display = 'none';
	}
	else
		divYear.style.display = 'block';
}


function addTblRow(tbl)
{
	var url;
	var objTbl = document.getElementById(tbl);
	var lastRow = objTbl.rows.length;
	var row = objTbl.insertRow(lastRow);

	row.id = 'newRow' + lastRow;

	var rowId = row.id;	
	
	var rowtd= row.insertCell(0);
	rowtd.id = 'tdExp' + lastRow;
	rowtd.align = 'right';
	
	if( tbl == 'tblExp' )
		url = 'templates/exp.php?rowId=' + rowId + '&tblId=' + tbl;
	else if( tbl == 'tblQual' )
		url = 'templates/qual.php?rowId=' + rowId + '&tblId=' + tbl;
	
	RequestAjaxPage(url, rowtd.id);
	
	
	var rowsNum = rowId.substring(rowId.indexOf('ow') + 2, rowId.length);
	
	document.getElementById('RowsNum').value = rowsNum;
	
	if( tbl == 'tblExp' )
		checkExpDate('add');
} // function addTblRow()


function removeTblRow(row, tbl)
{
	var tblMeant = document.getElementById(tbl) ;
	var rowDelete = document.getElementById(row) ;
	
	var rowsNum = eval(document.getElementById('RowsNum').value) ;

	rowsNum -= 1;

	tblMeant.deleteRow(rowDelete.rowIndex) ;
	
	document.getElementById('RowsNum').value = rowsNum;
	
	if( tbl == 'tblExp') {
		resortExpControls(rowsNum);
		checkExpDate('del');
	}
	else if( tbl == 'tblQual') {
		resortQualificData(rowsNum);
	}
	
	document.getElementById('dvError').style.display = 'none';
} // function removeTblRow()


function checkExpDate(op)
{
	var rowsNum = document.getElementById('RowsNum').value;
	var lstId;
	var mnthList;

	if( op == 'add') {
		if ( rowsNum > 1 ) {
			lstId = 'selMonthTo' + (rowsNum - 1);
			mnthList = document.getElementById(lstId);
			
			if( mnthList.selectedIndex == 1 ) {
				mnthList.selectedIndex = 0;
				checkDate(rowsNum - 1) ;
				mnthList.remove(1);
			}
			else
				mnthList.remove(1);
		} // if ( rowsNum > 1 )
	} // if( op == 'add')
	else if( op == 'del') {
		if ( rowsNum != 0 ) {
			
			lstId = 'selMonthTo' + rowsNum;
			mnthList = document.getElementById(lstId);
			
			if(mnthList.options.length < 14) {
				var newOption = document.createElement('option');
				newOption.value = 'Now';
				newOption.text = 'حتى الآن' ;
				
				var oldOption = mnthList.options[1];
				
				try {
					mnthList.add(newOption, oldOption);
				}
				catch(ex){
					mnthList.add(newOption, 1);
				}
			} // if(mnthList.options.length < 14)
		} // if ( rowsNum != 0 )		
	} // else if( op == 'del')
} // function checkExpDate()


function resortExpControls(rowsNum)
{
	var j;
	var isExist = false;
	
	for(i=1; i<=rowsNum; i++) {
		j = i ;
		isExist = false;
		
		while(isExist == false) {
			if (document.getElementById('txtCompName' + j) != null )
				isExist = true;
			else
				j += 1;
		} // while(isExist = false)

		if(isExist == true) {
			var fldCompName = document.getElementById('txtCompName' + j);
			var fldCompMajor = document.getElementById('selCompMajor' + j);
			var fldCompSect = document.getElementById('selCompSection' + j);
			var fldPosition = document.getElementById('txtPosition' + j);
			
			var fldCountry = document.getElementById('selCountry' + j);
			var fldAddress = document.getElementById('txtAddress' + j);
			var fldEmail = document.getElementById('txtEmail' + j);
			
			var fldMonthFrom = document.getElementById('selMonthFrom' + j);
			var fldYearFrom = document.getElementById('selYearFrom' + j);
			var fldMonthTo = document.getElementById('selMonthTo' + j);
	
			fldCompName.id = 'txtCompName' + i;
			fldCompMajor.id = 'selCompMajor' + i;
			fldCompSect.id = 'selCompSection' + i;
			fldPosition.id = 'txtPosition' + i;
			
			fldCountry.id = 'selCountry' + i;
			fldAddress.id = 'txtAddress' + i;
			fldEmail.id = 'txtEmail' + i;
			
			fldMonthFrom.id = 'selMonthFrom' + i;
			fldYearFrom.id = 'selYearFrom' + i;
			fldMonthTo.id = 'selMonthTo' + i;
			
			if( document.getElementById('selYearTo' + j) != null )
				document.getElementById('selYearTo' + j).id = 'selYearTo' + i;
		}
	} // for(i=1; i<rowsNum; i++)
} // function resortExpControls()


function resortQualificData(rowsNum)
{
	var j;
	var isExist = false;
	
	for(i=1; i<=rowsNum; i++) {
		j = i ;
		isExist = false;
		
		while(isExist == false) {
			if (document.getElementById('txtMajor' + j) != null )
				isExist = true;
			else
				j += 1;
		} // while(isExist = false)

		if(isExist == true) {
			var fldQualific = document.getElementById('selQual' + j);
			var fldGrade = document.getElementById('selGrade' + j);
			var fldMajor = document.getElementById('txtMajor' + j);
			var fldMonth = document.getElementById('selMonth' + j);
			var fldYear = document.getElementById('selYear' + j);
			
			fldQualific.id = 'selQual' + i;
			fldGrade.id = 'selGrade' + i;
			fldMajor.id = 'txtMajor' + i;
			fldMonth.id = 'selMonth' + i;
			fldYear.id = 'selYear' + i;
		}
	} // for(i=1; i<rowsNum; i++)
} // function resortQualificData()


function changeTextBox(textBox, opt, txtVal)
{
	if (opt == 1) {
		if (textBox.value == txtVal)
			textBox.value = '';
	}
	else {
		if (textBox.value == '')
			textBox.value = txtVal ;
	}
} // function changeTextBox()


function Confirm(mesg,page,parm,otherParm)
{	
	if(confirm(mesg)){
		redirect(page, parm, otherParm)	;
	}else{
		return false;	
	}
}


function checkCvDelete(frmName)
{
	var msg = 'هل أنت متأكد من رغبتك بالحذف؟';
	
	if( confirm(msg) == true)
		submitForm(frmName);
	else
		return false;
} // function checkCvDelete()


function checkCvType(radioCvType)
{
	if(radioCvType.value == 'edit')
		document.getElementById('filCV').disabled = 'disabled' ;
	else
		document.getElementById('filCV').disabled = '' ;
} // function checkCvType()


function executeCvLink(frm, itemId, opt)
{	
	if(opt == 'cvdel' || opt == 'expdel' || opt == 'quadel' || opt == 'appdel') {
		var msg = 'هل أنت متأكد من رغبتك بالحذف؟';
		if( confirm(msg) == false)
			return false;
	}
	
	var action = 'action.php?opt=' + opt;
	var objForm = document.getElementById(frm) ;

	objForm.itemId.value = itemId ;
	objForm.action = action;
	submitForm(frm) ;
} // function executeCvLink()


function changeVacmarDirection(dir)
{
	document.getElementById('marVac').direction = dir ;
} // function changeVacmarDirection()


function vacMarkMove(move)
{
	var objMarq = document.getElementById('marVac') ;

	objMarq.scrollAmount = (move == 0) ? 0 : 2 ;
} // function vacMarkMove()


function trimString(sInString)
{
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
} // function Trim(sInString)


function doDownload(file)
{
	window.open('download.php?file='+file) ;
} // function doDownload()