
function GetComboValue(pCN){
  return pCN.options[pCN.selectedIndex].value;
}

function checkWho(event) {
	obj=getEvent(event).target;
	alert(obj.nodeName)
}

function getEvent(e) {
	var event = e || window.event;
	if( ! event.target ) {
		event.target = event.srcElement
	}
	return event;
}

function hideElement(pId) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(pId).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.pId.display = 'none';
		}
		else { // IE 4
			document.all.pId.style.display = 'none';
		}
	}
}

function paintDay(pDay, pDayOfWeek, pCurrMonth, pOnclick, pSelected) {
	var str = "";
	if ( isWeekend(pDayOfWeek) ) {
		if ( pCurrMonth ) {
			if ( pSelected ) {
				str += "<td class=\"cdyhd\" onmouseover=\"className='cdyhdo'\" onmouseout=\"className='cdyhd'\"";
			} else {
				str += "<td class=\"cdyh\" onmouseover=\"className='cdyho'\" onmouseout=\"className='cdyh'\"";
			}
		} else {
			str += "<td class=\"cdyph\" onmouseover=\"className='cdypho'\" onmouseout=\"className='cdyph'\"";
		}
	} else { 
		if ( pCurrMonth ) {
			if ( pSelected ) {
				str += "<td class=\"cdyd\" onmouseover=\"className='cdydo'\" onmouseout=\"className='cdyd'\"";
			} else { 
				str += "<td class=\"cdy\" onmouseover=\"className='cdyo'\" onmouseout=\"className='cdy'\"";
			}
		} else {
			str += "<td class=\"cdyp\" onmouseover=\"className='cdypo'\" onmouseout=\"className='cdyp'\"";
		}
	}
	str += " onclick=\"" + pOnclick  + "\">"; 
	str += pDay;
	str += "</td>";
	return str;
}

function dayOfWeek(pDate) {
	if ( pDate.getDay() == 0 ) { 
		return 6;
	} else {
		return pDate.getDay() - 1;
	}
}

function rollBackAmount(pStartInWeek) {
	return pStartInWeek - 1;
}

function isWeekend(pDayOfWeek) {
	if ( pDayOfWeek == 5 || pDayOfWeek == 6 ) {
		return true;
	} else {
		return false;
	}
}

function getDateStr(pDate) {
	return formatDate(pDate) + "." + dayOfWeek(pDate); 
}

function getFormattedFromStr(pStr) {
	return getDayFromStr(pStr) + "." + getMonthFromStr(pStr) + "." + getYearFromStr(pStr);
}

function getDayFromStr(pStr) {
	return LPad(pStr.split(".")[0]);
}

function getYearFromStr(pStr) {
	return pStr.split(".")[2];
}

function getMonthFromStr(pStr) {
	return LPad(pStr.split(".")[1]);
}

function LPad(pStr) {
	if ( pStr.toString().length <= 1 ) { 
		return "0" + pStr;
	} else {
		return pStr;
	}
}

function getLang() {
	if ( window.location.href.indexOf( "lng=EN" ) >= 0) {
		return "EN";
	} else {
		return "RO";
	}
}

function getMonthName(pMonth) {
	if ( getLang() == "RO" ) { 
		return getMonthNameRO(pMonth);
	} else {
		return getMonthNameEN(pMonth);
	}
	return "Undefined";
}

function getMonthNameRO(pMonth) {
	if ( pMonth == "01" ) return "Ianuarie";
	if ( pMonth == "02" ) return "Februarie";
	if ( pMonth == "03" ) return "Martie";
	if ( pMonth == "04" ) return "Aprilie";
	if ( pMonth == "05" ) return "Mai";
	if ( pMonth == "06" ) return "Iunie";
	if ( pMonth == "07" ) return "Iulie";
	if ( pMonth == "08" ) return "August";
	if ( pMonth == "09" ) return "Septembrie";
	if ( pMonth == "10" ) return "Octombrie";
	if ( pMonth == "11" ) return "Novembrie";
	if ( pMonth == "12" ) return "Decembrie";
	return "Nedefinit";
}

function getMonthNameEN(pMonth) {
	if ( pMonth == "01" ) return "January";
	if ( pMonth == "02" ) return "February";
	if ( pMonth == "03" ) return "March";
	if ( pMonth == "04" ) return "April";
	if ( pMonth == "05" ) return "May";
	if ( pMonth == "06" ) return "June";
	if ( pMonth == "07" ) return "July";
	if ( pMonth == "08" ) return "August";
	if ( pMonth == "09" ) return "September";
	if ( pMonth == "10" ) return "October";
	if ( pMonth == "11" ) return "November";
	if ( pMonth == "12" ) return "December";
	return "Undefined";
}

function getDayOfWeekFromStr(pStr) {
	return pStr.split(".")[3];
}

function formatDate(pD) {
	vMn = LPad(parseInt(pD.getMonth(),10) + 1);
	vDy = LPad(parseInt(pD.getDate(),10));
	return vDy + "." + vMn + "." + pD.getFullYear();
}

function formattedToDate(pString) {
	vDy = pString.split(".")[0];
	vMn = parseInt(pString.split(".")[1],10) - 1;
	vYr = pString.split(".")[2];
	return new Date(vYr, vMn, vDy);
}

function isSameMonth( pMonth1, pMonth2 ) {
	if ( pMonth1 == pMonth2 ) {
		return true;
	} else {
		return false;
	}
}

function getMonth(pDate, pItemName) {
	var str = "";
	var month = new Array(6);
	for (var i=0; i <6; i++) month[i] = new Array(7);
	var d = new Date(pDate);
	var currMonth = LPad(d.getMonth()+1);
	var currMonthDsp = getMonthName(d.getMonth()+1);
	var currYear = d.getFullYear();
	var vPrevYear = formatDate(new Date(d.getFullYear()-1, d.getMonth(), d.getDate()));
	var vNextYear = formatDate(new Date(d.getFullYear()+1, d.getMonth(), d.getDate()));
	var vPrevMnth = formatDate(new Date(d.getFullYear(), d.getMonth()-1, d.getDate()));
	var vNextMnth = formatDate(new Date(d.getFullYear(), d.getMonth()+1, d.getDate()));
	var mnthStart = new Date(pDate);
	mnthStart.setDate(1);
	d.setDate(mnthStart.getDate()-rollBackAmount(dayOfWeek(mnthStart)+1));
	for (var i=0; i<42; i++) {
		month[Math.floor(i/7)][dayOfWeek(d)] = getDateStr(d);
		d.setDate(d.getDate()+1);
	}
	str += "<table>";
	str += "<tr>";
	str += "<td colspan=7 class=\"ctt\"><table width=\"%100\" border=\"0\"><tr>";
	str += "<td class=\"ctnav\"><img src=\"/ceaweb/img/img/cal_py.gif\" onclick=\"setDate('" + vPrevYear + "','" + pItemName + "', false)\"></td>";
	str += "<td class=\"ctnav\"><img src=\"/ceaweb/img/img/cal_pm.gif\" onclick=\"setDate('" + vPrevMnth + "','" + pItemName + "', false)\"></td>";
	str += "<td class=\"ctt\">" + currMonthDsp.substring(0, 3).toUpperCase() + ", " + currYear + "</td>";
	str += "<td class=\"ctnav\"><img src=\"/ceaweb/img/img/cal_nm.gif\" onclick=\"setDate('" + vNextMnth + "','" + pItemName + "', false)\"></td>";
	str += "<td class=\"ctnav\"><img src=\"/ceaweb/img/img/cal_ny.gif\" onclick=\"setDate('" + vNextYear + "','" + pItemName + "', false)\"></td>";
	str += "</table></td>";
	str += "</tr>";
	str += "<tr>";
	str += "<td class=\"ctd\">Lun</td>";
	str += "<td class=\"ctd\">Mar</td>";
	str += "<td class=\"ctd\">Mie</td>";
	str += "<td class=\"ctd\">Joi</td>";
	str += "<td class=\"ctd\">Vin</td>";
	str += "<td class=\"ctdh\">Sam</td>";
	str += "<td class=\"ctdh\">Dum</td>";
	str += "</tr>";
	for (var i=0; i<month.length; i++) {
		str += "<tr>";
		for ( var j=0; j<month[i].length; j++) {
			var sameMonth = isSameMonth(getMonthFromStr(month[i][j]), currMonth);
			var isSelected = false;
			if ( formatDate(pDate) == getFormattedFromStr(month[i][j]) )  {
				isSelected = true;
			}
			str += paintDay(getDayFromStr(month[i][j]), getDayOfWeekFromStr(month[i][j]), sameMonth, "setDate('" + month[i][j] + "','" + pItemName + "', true);", isSelected);
		}
		str += "</tr>";
	}
	str += "</table>";
	return str;
}

function findPosition( oElement ) {
    if( typeof( oElement.offsetParent ) != 'undefined' ) {
        for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) {
            posX += oElement.offsetLeft;
            posY += oElement.offsetTop;
        }
        return [ posX, posY ];
    } else {
        return [ oElement.x, oElement.y ];
    }
}

function setDate(pDate, pName, pClose) {
	var gbPopUp = document.getElementById("div_" + pName);
	vLongTxt = getMonthNameEN(getMonthFromStr(pDate)) + " " + getDayFromStr(pDate) + ", " + getYearFromStr(pDate);
	vCurrDate = new Date(vLongTxt);
	gbPopUp.innerHTML = getMonth(vCurrDate, pName);
	document.getElementsByName(pName)[0].value = formatDate(vCurrDate);
	if (pClose) gbPopUp.style.visibility = "hidden";
}


function showCal(posElement, pName) { 
	var gbPopUp = document.getElementById("div_" + pName);
	var vPos = findPosition(posElement);
	w = 180;
	h = 215;
	t = (vPos[1] + 25);
	if ( t < 0 ) { t = 0; }
	l = (vPos[0] - w);
    if ( l < 0 ) { l = 0; }
	gbPopUp.style.top = t + "px";
	gbPopUp.style.left = l + "px";
	gbPopUp.style.width = w + "px";
	gbPopUp.style.height = h + "px";
	if ( document.getElementsByName(pName)[0].value == "undefined" || document.getElementsByName(pName)[0].value == "" || document.getElementsByName(pName)[0].value == null ) { 
		gbPopUp.innerHTML = getMonth(new Date(), pName);
	} else { 
		gbPopUp.innerHTML = getMonth(formattedToDate(document.getElementsByName(pName)[0].value), pName);
	}
	gbPopUp.style.visibility = "visible";
}


function SubmitForm() {
	alert("Submiting Form");
}

function formatNumber(pN){
  vN = Math.abs(cleanNumber(pN))
  vNM = Math.floor(vN)
  vND = Math.round((vN - vNM) * 100).toString()
  vCC = Math.ceil(vNM.toString().length/3)-1
  vN = Math.floor(vNM/Math.pow(10,vCC*3)).toString()
  vF = vN
  for (i=0; i<vCC; i++) {
    vF = vF + "," + vNM.toString().substring(vN.length+3*i, vN.length+3*i+3)
  }
  if ( vND =="0" ) { vND = "00" }
  if ( vND.toString().length == 1 ) vND = "0" + vND; 
  vN = vF + "." + vND
  if (pN.charAt(0) == "-") {
    return "-" + vN;
  } else {
    return vN;
  }
}

function cleanNumber(pN){
  vN = ""
  for (i=0; i<=pN.length; i++) {
	if (pN.charAt(i) in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"] || pN.charAt(i) == ".") {
      vN += pN.charAt(i)
    }
  }
  vN = Math.round(vN*100)/100
  if (pN.charAt(0) == "-") {
    return "-" + vN;
  } else {
    return vN;
  }
}

function cleanDate(pN){
    vN = ""
    for (i=0; i<=pN.length; i++) {
	    if (pN.charAt(i) in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]) {
        vN += pN.charAt(i)
      }
    }
    return vN;
}

function formatDateText(pStr) { 
	vD = cleanDate(pStr);
	if (vD == "" || vD == null || vD.charAt(0) == ' ' || vD == undefined ) { 
		return "";
	} else { 
	  return vD.substring(0,2) + "." + vD.substring(2,4) + "." + vD.substring(4);
	}
}

function textChange(pTextItem, pRequired) { 
	vVal = pTextItem.value;
	if (vVal == "" || vVal == null || vVal.charAt(0) == ' ' || vVal == undefined ) { 
		if ( pRequired ) { 
			pTextItem.className = "txr";
		} else { 
			pTextItem.className = "txn";
		} 
	} else { 
		pTextItem.className = "txo";
	}
}

function tareaChange(pTextItem, pRequired) { 
	vVal = pTextItem.value;
	if (vVal == "" || vVal == null || vVal.charAt(0) == ' ' || vVal == undefined ) { 
		if ( pRequired ) { 
			pTextItem.className = "tarear";
		} else { 
			pTextItem.className = "tarean";
		} 
	} else { 
		pTextItem.className = "tareao";
	}
}

function numberChange(pNumberItem, pRequired) { 
	vVal = formatNumber(pNumberItem.value);
	if (vVal == "" || vVal == null || vVal.charAt(0) == ' ' || vVal == undefined ) { 
		if ( pRequired ) { 
			pNumberItem.className = "nmr";
		} else { 
			pNumberItem.className = "nmn";
		} 
	} else { 
		pNumberItem.className = "nmo";
	}
	pNumberItem.value = vVal;
}

function dateChange(pDateItem, pRequired) { 
	vVal = formatDateText(pDateItem.value);
	if (vVal == "" || vVal == null || vVal.charAt(0) == ' ' || vVal == undefined ) { 
		if ( pRequired ) { 
			pDateItem.className = "dtr";
		} else { 
			pDateItem.className = "dte";
		} 
	} else { 
		pDateItem.className = "dto";
	}
	pDateItem.value = vVal;
}

function comboChange(pCombo) { 
	pCombo.className = "txo";
}

function gbLOV(pName, pDefVal, pTitle) {
  this.vArr = [20]
  this.Name = pName;
  this.Title = pTitle;
  this.isVisible = 0
  this.ISource = '<table class="lbl"><tr><td width="150px">Required LOV</td><td>';
  this.ISource += '<input type="text" class="txr" style="text-align:left" size=22 name="l_' + pName + '" value="' + pDefVal + '">';
  this.ISource += '</td><td><img style="cursor:pointer" src="lov.gif" onclick="' + this.Name + '.lovClick(this);">';
  this.ISource += '</td></tr></table><div class="cal" id="i_' + this.Name + '"></div>';
  this.LSource = ""
  this.lovClick = lovClick;
  this.Show = lovShow;
  this.Hide = lovHide;
  this.initData = LovInitData;
  this.genLSource = genLSource;
  this.initData();
  this.genLSource();
}

function lovClick(pLSI){
  if ( this.isVisible == 0 ) { this.Show(pLSI); }
  else if ( this.isVisible == 1 ) { this.Hide(); }
} 

function lovShow(pSI){
  var gbPopUp = document.getElementById("i_" + this.Name);
  var vPos = findPosition(pSI);
  w = 340;
  h = 250;
  t = (vPos[1] + 25);
  if ( t < 0 ) { t = 0; }
  l = (vPos[0] - w);
  if ( l < 0 ) { l = 0; }
  gbPopUp.style.top = t + "px";
  gbPopUp.style.left = l + "px";
  gbPopUp.style.width = w + "px";
  gbPopUp.style.height = h + "px";
  gbPopUp.innerHTML = this.LSource;
  gbPopUp.style.visibility = "visible";
  this.isVisible = 1;
}

function lovHide() {
  var gbPopUp = document.getElementById("i_" + this.Name);
  gbPopUp.innerHTML = ""
  gbPopUp.style.visibility = "hidden";
  this.isVisible = 0;
}

function LovInitData () {
  this.vArr[0] = "Gokhan|Baydar|1000"
  this.vArr[1] = "Gokhan|Baydar|2000"
  this.vArr[2] = "Gokhan|Baydar|3000"
  this.vArr[3] = "Gokhan|Baydar|4000"
  this.vArr[4] = "Gokhan|Baydar|5000"
  this.vArr[5] = "Gokhan|Baydar|6000"
  this.vArr[6] = "Gokhan|Baydar|7000"
  this.vArr[7] = "Gokhan|Baydar|8000"
  this.vArr[8] = "Gokhan|Baydar|9000"
  this.vArr[9] = "Gokhan|Baydar|9900"
}

function genLSource () {
  var vRow = [3]
  this.LSource = '<table class="lovtb">'
  this.LSource += '<tr class="lovhd"><td align="center" colspan=3>' + this.Title + '</td></tr>' 
  for (i = 0; i < this.vArr.length; i++) {
    if ( i/2 == Math.round(i/2) ) { this.LSource += '<tr class="lovtre">' }
    else { this.LSource += '<tr class="lovtro">' }
    vRow = this.vArr[i].split("|")
    for (j = 0; j < vRow.length; j++) {
      this.LSource += '<td>' + vRow[j] + '</td>'
    }
    this.LSource += '</tr>'
  }
  this.LSource += '</table>' 
}

function onCheckBoxClick(pBox) {
	if (pBox.checked) { 
		pBox.value = 'Y';
	} else { 
		pBox.value = 'N';
	}
}

function paymentTypeChange(pCombo){
	pCombo.className = "txo";
	if ( GetComboValue(pCombo) != "0" ) { 
		document.getElementById('p_card_payment_inst').disabled = true; 
	} else { 
		document.getElementById('p_card_payment_inst').disabled = false;
	}
	document.forms["PaymentFormPre"].submit();
}

