window.defaultStatus = "METRO Cash & Carry";

function findObj(n) { 
    return document.getElementById(n); 
}

function topMIover(obj){		
	obj.className = "topMIover";
}
function topMInormal(obj){				
	obj.className = "topMInormal";	
}

function lmOver(obj) {
	obj.className = "lmHover";
}
function lmNormal(obj) {
	obj.className = "lmNormal";
}


function goTo(page) {
	location.href = 'index.php?screen='+page;
}

function openPost(page) {
    window.open(page,'Mail','height=600,width=800,scrollbars=no');
}
function openWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

function SwitchMenu(objName) {
	obj = findObj(objName);
	
	if (obj.className == "show") {
		obj.className = "hide";
	} else {
		obj.className = "show";
	}
}


/***********************************************
*Start Client Validation
***********************************************/
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0); 
}

function isNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
   }
   return IsNumber;   
}

function isEmpty(s)
{ 
	return ((s == null) || (s.length == 0)) 
}

// It works for both types of cards
function ValidateCard(field1, field2, field3, minCode, maxCode)
{   
    
    f1 = findObj(field1);
    f2 = findObj(field2);
    f3 = findObj(field3);      
    
    // take out possible 0-s from the begining
    for (i = 0; i < f1.length; i++)
    {
        if (f1.substring(i, 1) != "0")
        {
            f1.value = f1.substring(i);
        }
    }
      
    if(isEmpty(f1.value) || isEmpty(f2.value) || isEmpty(f3.value) || !isNumeric(f1.value) || !isNumeric(f2.value) || !isNumeric(f3.value) || f1.value.length < 2 || f2.value.length < 6 || f3.value.length == 2 || parseInt(f1.value, 10) < parseInt(minCode, 10) || parseInt(f1.value, 10) > parseInt(maxCode, 10))
    {        
        return false;
    }    
    return true;
}

// It works for the 1st type of card (old type)
function ValidateCardV1(field1, field2, field3, field4, minCode, maxCode)
{    
    f1 = findObj(field1);
    f2 = findObj(field2);
    f3 = findObj(field3);
    f4 = findObj(field4);    
    if(isEmpty(f1.value) || isEmpty(f2.value) || isEmpty(f3.value) || isEmpty(f4.value) || !isNumeric(f1.value) || !isNumeric(f2.value) || !isNumeric(f3.value) || !isNumeric(f4.value) || f1.value.length < 2 || f2.value.length < 6 || f3.value.length < 2 || parseInt(f1.value) < parseInt(minCode) || parseInt(f1.value) > parseInt(maxCode))
    {        
        return false;
    }    
    return true;
}

// It works for the 2nd type of card (new type)
function ValidateCardV2(field1, field2, field3, minCode, maxCode)
{    
    f1 = findObj(field1);
    f2 = findObj(field2);
    f3 = findObj(field3);
    f4 = findObj(field4);    
    if(isEmpty(f1.value) || isEmpty(f2.value) || isEmpty(f3.value) || !isNumeric(f1.value) || !isNumeric(f2.value) || !isNumeric(f3.value) || f1.value.length < 2 || f2.value.length < 6 || f3.value.length < 2 || parseInt(f1.value) < parseInt(minCode) || parseInt(f1.value) > parseInt(maxCode))
    {        
        return false;
    }    
    return true;
}

function ValidateRegOfCommerce(field1, field2, field3)
{    
    f1 = findObj(field1);
    f2 = findObj(field2);
    f3 = findObj(field3);     
    if(isEmpty(f1.value) || isEmpty(f2.value) || isEmpty(f3.value) || !isNumeric(f2.value) || !isNumeric(f3.value) || f1.value.length < 3 || f2.value.length < 3 || f3.value.length < 4)
    {   
        return false;
    }    
    return true;
}

function Form_OnSubmit(formObj) {
	//var errMsgObj = findObj('loginErrMsg');
	//errMsgObj.style.visibility = 'hidden';
	if (! ValidateCard('c1', 'c2', 'c3', 10, 35)) {
		//errMsgObj.style.visibility = 'visible';
		alert('Introdu un numar de card valid!')
		return false;
	}
	formObj.submit();
	return true;
}

function Newsletter_OnSubmit(formObj) {
	if (findObj('newsletter_email')) {
		findObj('newsletter_email_err').className = 'formFieldError_hidden';
		var newsletter_email = findObj('newsletter_email');
		if (isEmpty(newsletter_email.value)) {
			findObj('newsletter_email_err').className = 'formFieldError';
			findObj('newsletter_email_err').innerHTML = 'Va rugam introduceti adresa de email';
			return false;
		} else if (!isEmail(newsletter_email.value)) {
			findObj('newsletter_email_err').className = 'formFieldError';
			findObj('newsletter_email_err').innerHTML = 'Adresa de email introdusa nu este corecta';
			return false;
		}
	} else {
		var errMsgObj = findObj('newsletter_loginErrMsg');
		errMsgObj.style.visibility = 'hidden';
		if (! ValidateCard('newsletter_c1', 'newsletter_c2', 'newsletter_c3', 10, 34)) {
			errMsgObj.style.visibility = 'visible';
			return false;
		}
	}
	formObj.submit();
	return true;
}

document.getElementsByClassName = function(className)
{   var outArray = new Array();
    var item;
    try {
        var xpathResult = document.evaluate('//*[@class = "' + className + '"]', document, null, 0, null);
        while (item = xpathResult.iterateNext())
            outArray[outArray.length] = item;
    }
    catch(err) {
        // ie fix
        var currentIndex = 0;
        var allElements = document.getElementsByTagName('*');
        for(var i=0; i < allElements.length; i++)
        {   if(allElements[i].className.match(className))
            {   outArray[currentIndex] = allElements[i];
                currentIndex++;
            }
        }
    }
    return outArray;
}

function toggleDetails(obj)
{
	var block = findObj(obj);
	if(block.className=='hide')
	{
		block.className = 'show';
	}
	else
	{
		block.className = 'hide';
	}
}

/***********************************************
* Start Installation Calculus
***********************************************/
function installmentCalc(){
	var produs_financiar = findObj("produs_financiar").value;
	
	if(produs_financiar > 0){
	
		var arrOptions = new Array(11);
		arrOptions[0] = '101.3100';
		arrOptions[1] = '59.5300';
		arrOptions[2] = '45.6700';
		arrOptions[3] = '38.7900';
		arrOptions[4] = '34.7000';
		arrOptions[5] = '95.8100';
		arrOptions[6] = '54.0300';
		arrOptions[7] = '40.1700';
		arrOptions[8] = '33.2900';
		arrOptions[9] = '29.2000';
		arrOptions[10] = '100.0000';
		arrOptions[11] = '61.5700';
		arrOptions[12] = '47.6800';
		arrOptions[13] = '40.7400';
		arrOptions[14] = '36.5700';
		arrOptions[15] = '45.6700';
		arrOptions[16] = '38.7900';
		arrOptions[17] = '34.7000';
		
		var arrDAE = new Array(11);
		arrDAE[0] = '44.9157292818607';
		arrDAE[1] = '43.8326858067042';
		arrDAE[2] = '42.2459990872829';
		arrDAE[3] = '40.7798009107064';
		arrDAE[4] = '39.4987946576882';
		arrDAE[5] = '30.0416187210779';
		arrDAE[6] = '29.7173002691422';
		arrDAE[7] = '29.0410422233429';
		arrDAE[8] = '28.3637739190584';
		arrDAE[9] = '27.7406532393228';
		arrDAE[10] = '0';
		arrDAE[11] = '40.3360692395397';
		arrDAE[12] = '40.7226658170671';
		arrDAE[13] = '40.147816447452';
		arrDAE[14] = '39.2985536803602';
		arrDAE[15] = '33.4301472886255';
		arrDAE[16] = '34.8207804826136';
		arrDAE[17] = '34.9666642588933';

		
		var invoice = findObj("invoice").value;
		
		findObj("valoare_finantata").innerHTML = roundOff(invoice,2);
		findObj("rata").innerHTML = Math.round(roundOff((invoice*arrOptions[produs_financiar-1]/1000),13)*100)/100;
		
		if(produs_financiar > 0 && produs_financiar < 18){
			findObj("dobanda_anuala").innerHTML = produs_financiar == 18 ? "00.00" : "11.90";
			findObj("dobanda_efectiva").innerHTML = roundOff(arrDAE[produs_financiar-1],2);
		} else {
			findObj("dobanda_anuala").innerHTML = "00.00";
			findObj("dobanda_efectiva").innerHTML = "00.00";
		}
		//findObj("rata").innerHTML = Pmt(interest,perioada,valoare_finantata,0);
		
		
		findObj("error").style.display='none';
		findObj("result").style.display='';
		
	} else {
		findObj("error").innerHTML='<font color=red><br>Alegeti produsul financiar.</font>';
		findObj("error").style.display='';
	}
}

/*
function installmentCalc(){
	var invoice = parseInt(findObj("invoice").value);
	var period = parseInt(findObj("perioada").value);
	var result = findObj("result");
	var interest = 18;//dobanda anuala
	
	result.value = Pmt(interest,period,invoice+invoice*3/100,0);
	}
	
*/	

function Pmt(r,np,pv,fv) {
	r = r/1200
	if (!fv) fv = 0;
	pmt=(r * (fv+Math.pow((1+r),np)*pv)/(-1+Math.pow((1+r),np)));
	finalPmt=roundOff(pmt,2);
	return finalPmt;
	}

function roundOff(value,dplaces){
	value=value.toString();	
	if((value.indexOf(".")!=-1)&&(value.length>(value.indexOf(".")+dplaces))){	 
		three=value.substring(value.indexOf(".")+dplaces+1,value.indexOf(".")+dplaces+2)
		one=value.substring(0,value.indexOf(".")+dplaces)
		two=value.substring(value.indexOf(".")+dplaces,value.indexOf(".")+dplaces+1)	 
		if(parseInt(three)>=5){value=one+(parseInt(two)+1);value=parseFloat(value)}
		else{value=one+two;value=parseFloat(value)}
		}
	return value;
	} 

/***********************************************
* Stop Installation Calculus
***********************************************/

