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) || 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', 1, 3)) {
		//errMsgObj.style.visibility = 'visible';
		alert(LOGIN_ERR_MSG)
		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', 1, 3)) {
			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 invoice = parseInt(findObj("invoice").value);
	var period = parseInt(findObj("perioada").value);
	var result = findObj("result");
	var interest = 24.33;//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
***********************************************/

