var tempid = '';
var alertmaxprice ='';
var alertminprice ='';
function getPage(pageLocation){
    window.location = pageLocation;
}
function addPortfolio(companycode){
    getPage("http://portfolio.rediff.com/money/jsp/add_portfolio.jsp?companyCode="+companycode+"&equity=yes");
}

function opendropdown(id){
    if(id == 'addPortfolio')
        document.getElementById(id).style.display="block";
}
function closedropdown(id){
    document.getElementById(id).style.display="none";
}
function showAddPopup(companyId){
    tempid = companyId;
    if(document.all){
        document.getElementById("addportfoliopopup").style.display="block";
    }else{
        document.getElementById("addportfoliopopup").style.display="block";
    }
}
function closeAddPopup(){
    document.forms["add_pf_co"].reset();
    document.getElementById("addportfoliopopup").style.display="none";
}
function  doAddPortfolioSubmit(){

    var message="Please fill up carefully with following details \n\n";
    var x=0;
    var check=checkQuantity(document.add_pf_co.quantity);
        if(check==false){
            message=message+" * Please enter only integer values for Quantity.\n\n";
            x=1;
        }
        var check1=checkCost(document.add_pf_co.price);
            if(check1==false){
            message=message+" * Please enter numeral values for Cost. \n\n";
            x=1;
        }
    if(document.add_pf_co.date.value==''){
        message=message+" * Date shouldn't be blank";
        x=1;
    }else{
        var date = document.add_pf_co.date.value;
       var currentDate = new Date();
       var y = date.split("/");
       var tempdate =  y[1]+"/"+y[0]+"/"+y[2];
       dt = new Date(tempdate);
       if(dt > currentDate) {
            message=message+" * You cannot select future date.";
            x=1;
        }
    }
    if (x==1) {
        alert(message);
        return false;
    }else{
    submitPortfolioForm();
    }
}
function xshowAddPopup(companyId){
    if(tempid != '')
       xcloseAddPopup();
	tempid = companyId;
    document.getElementById(tempid).className="moneypopup";
    document.getElementById(tempid).innerHTML =  document.getElementById("addportfoliopopup").innerHTML ;
    document.getElementById("addportfoliopopup").innerHTML ="";
    document.getElementById(tempid).style.display="block";
}
function xcloseAddPopup(){
	resetDiv('co');
	document.getElementById("addportfoliopopup").innerHTML =   document.getElementById(tempid).innerHTML;
    document.getElementById(tempid).style.display="none";
    document.getElementById(tempid).innerHTML =  "";
    tempid='';
}

function showPDiv(id){
	if(id == 'portfolio'){
		document.getElementById('plink').style.display="none";
		document.getElementById('ppage').style.display="block";
		document.getElementById('swpage').style.display="none";
	}else if(id == 'sw'){
		document.getElementById('ppage').style.display="none";
		document.getElementById('plink').style.display="none";
		document.getElementById('swpage').style.display="block";
	}else if(id == 'sportfolio'){
		document.getElementById('slink').style.display="none";
		document.getElementById('spage').style.display="block";
		document.getElementById('sswpage').style.display="none";
	}else if(id == 'ssw'){
		document.getElementById('spage').style.display="none";
		document.getElementById('slink').style.display="none";
		document.getElementById('sswpage').style.display="block";
	}
}

function resetDiv(id){
	
	if (id == 'co'){
		document.getElementById('ppage').style.display="none";
		document.getElementById('plink').style.display="block";
		document.getElementById('swpage').style.display="none";	
	}else if (id == 'mf'){
		document.getElementById('spage').style.display="none";
		document.getElementById('slink').style.display="block";
		document.getElementById('sswpage').style.display="none";	
	}
}


var tempDateId ='';
var mftempDateId ='';
function dateClk(formId){
	document.domain="rediff.com";
    if(formId == 'mf'){
        tempDateId = 'mf';
        show_calendar('mf_add_form.date');
    }else{
        tempDateId = 'cm';
        show_calendar('add_pf_co.date');
    }
}
function setDate(dt, dateObj){
    if(tempDateId == 'cm')
        document.add_pf_co.date.value=dt;
    else
        document.mf_add_pf_co.date.value=dt;
}
function mfdateClk(formId){
    if(formId == 'mf'){
        tempDateId = 'mf';
        show_calendar('mf_add_form.date');
    }else{
        tempDateId = 'cm';
        show_calendar('add_pf_co.date');
    }
}
function mfsetDate(dt, dateObj){
    if(tempDateId == 'cm')
        document.add_pf_co.date.value=dt;
    else
        document.mf_add_pf_co.date.value=dt;
}
function submitPortfolioForm(){
    var cmprice=document.add_pf_co.price.value;
    var cmquantity=document.add_pf_co.quantity.value;
    var cmdate=document.add_pf_co.date.value;
    document.forms["add_pf_co"].code.value =  tempid;
    document.forms["add_pf_co"].flag.value =  'cm';
    document.forms["add_pf_co"].transDetails.value='cm|'+tempid+'|'+cmprice+'|'+cmquantity+'|'+cmdate;
    document.add_pf_co.method = "post";
    document.add_pf_co.action = "http://portfolio.rediff.com/money/jsp/select_portfolio.jsp";
    document.add_pf_co.submit();

}

function checkQuantity(fieldName){
    var strValue=fieldName.value;
    var cnt
    if(strValue.length==0){
        return false;
    }else{
        for(cnt=0;cnt<strValue.length; cnt++){
            if(!(strValue.charAt(cnt)>='0' && strValue.charAt(cnt) <='9')) {
                return false
            }
        }
    }
    return true
}

function checkCost(fieldName){
    var strValue=fieldName.value;
    var cnt
    if(strValue.length==0){
        return false;
    }else if(strValue.charAt(0)=='.'){
        return false;
    }else{
        for(cnt=0;cnt<strValue.length; cnt++){
            if(!(strValue.charAt(cnt)>='0' && strValue.charAt(cnt) <='9') && strValue.charAt(cnt)!='(' && strValue.charAt(cnt)!=')' && strValue.charAt(cnt)!='.' && strValue.charAt(cnt)!='') {
                return false
            }
        }
    }
    return true
}

function  mfdoAddPortfolioSubmit(code){

    var message="Please fill up carefully with following details \n\n";
             var x=0;
             var check=mfcheckQuantity(document.mf_add_form.quantity);
			 if(check==false){
					message=message+" * Please enter valid values for Quantity.\n\n";
					x=1;
			}
			var check1=mfcheckCost(document.mf_add_form.price);
			 if(check1==false){
					message=message+" * Please enter numeral values for Cost. \n\n";
					x=1;
			}
			if(document.mf_add_form.date.value==''){
				message=message+" * Date shouldn't be blank";
					x=1;
			}
			if(document.mf_add_form.date.value!=''){
			   var date = document.mf_add_form.date.value;
			    var currentDate = new Date();
			    var y = date.split("/");
			    var tempdate =  y[1]+"/"+y[0]+"/"+y[2];
			    dt = new Date(tempdate);
			    if(dt > currentDate) {
			        message=message+" * You cannot select future date.";
			        //document.mf_add_form.date.value=='';
			        x=1;
			    }
			}
			if (x==1) {
				alert(message);
            	return false;
            }else{
				mfsubmitPortfolioForm(code);
			}
   }

function mfcheckQuantity(fieldName){
    var strValue=fieldName.value;
    var cnt
    var len = strValue.length;
    if(len==0){
        return false;
    }else{
         if(len == 1){
		    if(strValue.charAt(0)=='0' || strValue.charAt(0)=='.')
			    return false;
	    }else if(len == 2){
		    if(strValue.charAt(1)=='.' )
		        return false;
		    else{
		       if(strValue.charAt(0)=='.' && strValue.charAt(1)=='0')
		        return false;
		    }
	}else{
	    if(len == 3)
		    if(strValue.charAt(0)=='0' && strValue.charAt(1)=='.' && strValue.charAt(2)=='0')
		        return false;
        for(cnt=0;cnt<len; cnt++){
            if(!((strValue.charAt(cnt)>='0' && strValue.charAt(cnt) <='9')||(strValue.charAt(cnt)=='.'))) {
                return false
                }
          }
      }
    return true
    }
}

function mfcheckCost(fieldName){
    var strValue=fieldName.value;
    var cnt
    if(strValue.length==0){

    return false;
    }else if(strValue.charAt(0)=='.'){
        return false;
    }else{
    for(cnt=0;cnt<strValue.length; cnt++){
    if(!(strValue.charAt(cnt)>='0' && strValue.charAt(cnt) <='9') && strValue.charAt(cnt)!='(' && strValue.charAt(cnt)!=')' && strValue.charAt(cnt)!='.' && strValue.charAt(cnt)!='') {
        return false
                }
          }
      }
    return true
}

function mfsubmitPortfolioForm(code){
	
	if(code == null || code.length <1){
	var code = newSchemeId;
	if(code.indexOf("a") != -1){
            code =code.replace("a",",");
        }
    }
	var mfprice=document.mf_add_form.price.value;
    var mfquantity=document.mf_add_form.quantity.value;
    var mfdate=document.mf_add_form.date.value;
    document.mf_add_form.transDetails.value='mf|'+code+'|'+mfprice+'|'+mfquantity+'|'+mfdate;
    document.mf_add_form.method = "post";
    document.mf_add_form.action = "http://portfolio.rediff.com/money/jsp/select_portfolio.jsp";
    document.mf_add_form.submit();
}

var newId='';
var newSchemeId ='';
function mfshowAddPopup(schemeId,planId){
	
	if (newId != '')
		mfcloseAddPopup();
    newId = schemeId+planId;
	newSchemeId = schemeId+'a'+planId;
    document.getElementById(newId).className="moneypopup";
    document.getElementById(newId).innerHTML =  document.getElementById("mfaddportfoliopopup").innerHTML ;
    document.getElementById("mfaddportfoliopopup").innerHTML ="";
    document.getElementById(newId).style.display="block";
}
function mfcloseAddPopup(){
	
	resetDiv('mf');
    document.getElementById("mfaddportfoliopopup").innerHTML =   document.getElementById(newId).innerHTML;
    document.getElementById(newId).style.display="none";
    document.getElementById(newId).innerHTML =  "";
    newId='';
	newSchemeId='';
}

function checkalertvalidate(){
	var stockmessage="";
    var x=0;
    var maxprice=Trim(document.stockwatch_popup.maxprice.value);
	var dotIndex = maxprice.indexOf(".");
	var tempCount = 0;
	while(dotIndex != -1){
		tempCount++;
		dotIndex = maxprice.indexOf(".",dotIndex+1);
	}
	if(tempCount <2){ 
		while (maxprice.indexOf(",") != -1)
			maxprice= maxprice.replace(/,/,"");
		if(maxprice.length >=1){
			var checkmax=checkalertPrice(maxprice);
			if(checkmax==false){
				stockmessage="Please enter numbers only";
				x=1;
			}
		}
	}else{
		stockmessage="Please enter only one decimal";
		x=1;
	}
    var minprice=Trim(document.stockwatch_popup.minprice.value);
	dotIndex = minprice.indexOf(".");
	tempCount = 0;
	while(dotIndex != -1){
		tempCount++;
		dotIndex = minprice.indexOf(".",dotIndex+1);
	}
	if(tempCount <2){ 
		while (minprice.indexOf(",") != -1)
			minprice= minprice.replace(/,/,"");
		if(minprice.length>=1){
			var checkmin=checkalertPrice(minprice);
			if(checkmin==false){ 
				stockmessage="Please enter numbers only";
				x=1;
			}
		}
	}else{
		stockmessage="Please enter only one decimal";
		x=1;
	}
    if(x==1){
        alert(stockmessage);
		return false;
    }else{
		alertmaxprice =maxprice;
		alertminprice = minprice;
		submitAlertStockForm('co');
    }
}
function checkmfalertvalidate(){
	var stockmessage="";
    var x=0;
    var maxprice=Trim(document.mfstockwatch_popup.maxprice.value);
	var dotIndex = maxprice.indexOf(".");
	var tempCount = 0;
	while(dotIndex != -1){
		tempCount++;
		dotIndex = maxprice.indexOf(".",dotIndex+1);
	}
	if(tempCount <2){
		while (maxprice.indexOf(",") != -1)
			maxprice= maxprice.replace(/,/,"");
		if(maxprice.length >=1){
			var checkmax=checkalertPrice(maxprice);
			if(checkmax==false){
				stockmessage="Please enter numbers only";
				x=1;
			}
		}
	}else{
		stockmessage="Please enter only one decimal";
		x=1;
	}
    var minprice=Trim(document.mfstockwatch_popup.minprice.value);
	dotIndex = minprice.indexOf(".");
	tempCount = 0;
	while(dotIndex != -1){
		tempCount++;
		dotIndex = maxprice.indexOf(".",dotIndex+1);
	}
	if(tempCount <2){
		while (minprice.indexOf(",") != -1)
			minprice= minprice.replace(/,/,"");
		if(minprice.length>=1){
			var checkmin=checkalertPrice(minprice);
			if(checkmin==false){
				stockmessage="Please enter numbers only";
				x=1;
			}
		}
	}else{
		stockmessage="Please enter only one decimal";
		x=1;
	}
    if(x==1){
        alert(stockmessage);
		return false;
    }else{
		alertmaxprice = maxprice;
		alertminprice = minprice;
		submitAlertStockForm('mf');
    }
}
function checkmfalertvalidate1(){
	var stockmessage="";
    var x=0;
    var maxprice=Trim(document.mfstockwatch_popup.maxprice.value);
	var dotIndex = maxprice.indexOf(".");
	var tempCount = 0;
	while(dotIndex != -1){
		tempCount++;
		dotIndex = maxprice.indexOf(".",dotIndex+1);
	}
	if(tempCount <2){
		while (maxprice.indexOf(",") != -1)
			maxprice= maxprice.replace(/,/,"");
		if(maxprice.length >=1){
			var checkmax=checkalertPrice(maxprice);
			if(checkmax==false){
				stockmessage="Please enter numbers only";
				x=1;
			}
		}
	}else{
		stockmessage="Please enter only one decimal";
		x=1;
	}
    var minprice=Trim(document.mfstockwatch_popup.minprice.value);
	dotIndex = minprice.indexOf(".");
	tempCount = 0;
	while(dotIndex != -1){
		tempCount++;
		dotIndex = maxprice.indexOf(".",dotIndex+1);
	}
	if(tempCount <2){
		while (minprice.indexOf(",") != -1)
			minprice= minprice.replace(/,/,"");
		if(minprice.length>=1){
			var checkmin=checkalertPrice(minprice);
			if(checkmin==false){
				stockmessage="Please enter numbers only";
				x=1;
			}
		}
	}else{
		stockmessage="Please enter only one decimal";
		x=1;
	}
    if(x==1){
        alert(stockmessage);
		return false;
    }else{
		alertmaxprice = maxprice;
		alertminprice = minprice;
		submitAlertStockForm('mf1');
    }
}

function checkalertPrice(strValue){
	
    strValue = Trim(strValue);
	var tempValue='';
    if(strValue.length>0){
		
		if(isNaN(strValue)){
			return false;
		}
	}
    return true
}

function Trim(str){
	return str.replace(/^\s*|\s*$/g,"");
}

function submitAlertStockForm(id){
	var maxprice = alertmaxprice;
	var minprice = alertminprice;
	if (id =='co'){
		document.forms["stockwatch_popup"].code.value=tempid;
		document.forms["stockwatch_popup"].flag.value='sw_add';
		document.forms["stockwatch_popup"].transDetails.value='sw_edit|1|'+tempid+'|'+maxprice+'|'+minprice+"|edit|SW";
		document.stockwatch_popup.method="post";
		document.stockwatch_popup.action="http://portfolio.rediff.com/money/jsp/stockwatch_edit.jsp";
		document.stockwatch_popup.submit();
	}else if(id == 'mf'){
		document.forms["mfstockwatch_popup"].code.value=newSchemeId;
		document.forms["mfstockwatch_popup"].flag.value='sw_add';
		document.forms["mfstockwatch_popup"].transDetails.value='sw_edit|1|'+newSchemeId+'|'+maxprice+'|'+minprice+"|edit|SW";
		document.mfstockwatch_popup.method="post";
		document.mfstockwatch_popup.action="http://portfolio.rediff.com/money/jsp/stockwatch_edit.jsp";
		document.mfstockwatch_popup.submit();
	}else if(id == 'mf1'){
		//document.forms["mfstockwatch_popup"].code.value=newSchemeId;
		document.forms["mfstockwatch_popup"].flag.value='sw_add';
		document.forms["mfstockwatch_popup"].transDetails.value='sw_edit|1|'+document.forms["mfstockwatch_popup"].code.value+'|'+maxprice+'|'+minprice+"|edit|SW";
		document.mfstockwatch_popup.method="post";
		document.mfstockwatch_popup.action="http://portfolio.rediff.com/money/jsp/stockwatch_edit.jsp";
		document.mfstockwatch_popup.submit();
	}
}
function openMfPopup(){
    document.getElementById("mfAddPortfolio").style.display="block";
}
function closeMfPopup(){
    document.forms["mf_add_form"].reset();
    document.getElementById("mfAddPortfolio").style.display="none";
}
function openMfPopup(id){
    document.getElementById(id).style.display="block";
}
function closeMfPopup(id){
	if(id == 'mfAddPortfolio'){
		document.forms["mf_add_form"].reset();
	}else if(id == 'mfAddStockwatch'){
		document.forms["mfstockwatch_popup"].reset();
	}
	document.getElementById(id).style.display="none";
}