
var hrvc = {
	cvalue: Array(),
	cname: "bpl_fav",
	getcv: function (){
		var getv = hr.getCookie(this.cname);
		var getG = hr.explode(';',getv);
		var tmpv = Array();
		for(var i=0; i<getG.length; i++){
			var getC = hr.explode(':',getG[i]);
			tmpv[getC[0]] = Array();
			for(var x=0; x<getC.length; x++){
				tmpv[getC[0]][x] = getC[x];
			}
		}
		this.cvalue = tmpv;
	},
	setqty: function (gid,qty){
		this.cvalue[gid][4] = qty;
	},
	set_ringsize: function (gid,size){
		var tmpget = this.cvalue[gid][2];
		var itemg = hr.explode(',',tmpget);
		var tmpa = Array();
		for(var i=0; i<itemg.length; i++ ){
			tmpa[i] = Array();
			var itemd = hr.explode('/',itemg[i]);
			for(var x=0; x<itemd.length; x++){
				tmpa[i][x] = itemd[x];
			}
		}
		
		if(tmpa[0][1]=='D' || tmpa[0][1]=='D2')
			tmpa[1][2] = size;
		else
			tmpa[0][2] = size;
			
		var str = '';
		for(var a=0; a<tmpa.length; a++){
			for(var b=0; b<tmpa[a].length; b++){
				str += tmpa[a][b];
				str += '/';
			}
			str += ',';
		}
		this.cvalue[gid][2] = str;
	},
	removeitem: function (gid,id){
		var sap = hr.explode(',',this.cvalue[gid][2]);
		if(sap.length > 1){
			var newstr = '';
			for(var b=0; b<sap.length; b++){
				var sal = hr.explode('/',sap[b]);
				if(sal[0]==id) continue;
				newstr += sap[b] + ',';
			}
			this.cvalue[gid][2] = newstr;
		}else{
			var v = Array();
			var i = 0;
			for(var a=0; a<this.cvalue.length; a++){
				if(a==gid) continue;
				v[i] = this.cvalue[a];
				v[i][0] = i;
				i++;
			}
			this.cvalue = v;
		}
	},
	getids: function (gid){
		try{
		var sap = hr.explode(',',this.cvalue[gid][2]);
		var ret = new Array();
		for(var b=0; b<sap.length; b++){
			var sal = hr.explode('/',sap[b]);
			ret[b] = sal[0]
		}
		return ret;
		}catch(e){
			//alert('getids: '+e.message)
		}
	},
	arraytostr: function (){
		var tmpstr = '';
		/*for(a in hrvc.cvalue){
			for(b in hrvc.cvalue[a]){
				alert(b);
				tmpstr += hrvc.cvalue[a][b];
				if((hrvc.cvalue[a].length-1)!=b) tmpstr += ':';
			}
			tmpstr += ';';
		}*/
		var ai=0;
		for(var a=0; a<hrvc.cvalue.length; a++){
			//alert(a);
			var bi=0;
			for(var b=0; b<hrvc.cvalue[ai].length; b++){
				tmpstr += hrvc.cvalue[ai][bi];
				if((hrvc.cvalue[ai].length-1)!=bi) tmpstr += ':';
				bi++;
			}
			ai++;
			tmpstr += ';';
		}
		return tmpstr;
	},
	update: function (){
		hr.setCookie(this.cname,this.arraytostr(),1);
	},
	onerror: function (){
		window.location = 'viewcart.pl?cfg=bpl';
	}
}

var vivocart = {
	mid: "hrvcmain_itable",
	items: Array(),
	setitems: function (){
		hrvc.getcv();
		var count = hrvc.cvalue.length;
		for(var i=0; i<count; i++){
			this.items[i] = i;
		}
	},
	removeitem: function (t,g,id,d){
		try{
		if(this.items.length < 1) this.setitems();
		if(!d){
			this.loading();
			setTimeout("vivocart.removeitem("+t+","+g+","+id+",1)",1000);
			return false;
		}
		var getids;
			hrvc.getcv();
			getids = hrvc.getids(this.items[g]);
			hrvc.removeitem(this.items[g],id);
			if(getids.length <= 1){
				this.items[g] = null;
			}
			//delete(this.items[g])
		}catch(e){
			hrvc.onerror();
			return false;
		}
		setTimeout(function (){vivocart.fremove(t,getids,id)},500);
	},
	fremove: function (t,ids,id){
		hrvc.update();
		try{
		var mid = hr.$(this.mid);
		if(ids.length > 1){
			mid.removeChild(hr.$('hrvcmain_ihr_'+t));
			mid.removeChild(hr.$(this.mid+t+'_'+id));
			if(ids[0]==id) var n = hr.$(this.mid+t+'_'+ids[1]+'tr'); else var n = hr.$(this.mid+t+'_'+ids[0]+'tr');
			if(n){if(n.style.display=='none') n.style.display = '';}
		}else{
			mid.removeChild(hr.$(this.mid+t+'_'+id));
		}

		var z=0
		for(var x=0; x<this.items.length; x++){
			if(this.items[x]==null) continue;
			this.items[x] = z;
			z++;
		}

		var l = hrvc.cvalue.length;
		if(l <= 1){
			if(!hrvc.cvalue[1] && hrvc.cvalue[0][0]==0){
				hr.$('hrvcmain_emptycart').style.display='block';
				vcartform.disableCvform();
			}
		}
		vivocart.countamout();
		this.loading(1);
		}catch(e){
			hrvc.onerror();
		}
	},
	setqty: function (g,q){
		if(this.items < 1) this.setitems();
		this.loading();
		try{
			hrvc.getcv();
			hrvc.setqty(this.items[g],q);
		}catch(e){
			this.loading(1);
			return false;
		}
		setTimeout("hrvc.update()",500);
		setTimeout(function (){
			try{
				vivocart.loading(1);
				var ids = hrvc.getids(vivocart.items[g]);
				for(var z=0; z<ids.length; z++){
					vivocart.selectedqty('itemqty_'+g+'_'+ids[z],q);
					vivocart.setqtyvalue(g+'_'+ids[z],q);
				}
				vivocart.countamout();
			}catch(e){
				hrvc.onerror();
			}
		},1000);
	},
	setqtyvalue: function (g,q){
		try{
		var v = hr.$("vcart_i_g"+g);
		if(!v) return false;
		var ex = hr.explode('|',v.value);
		var emprice = number_format((ex[1]*q),2,'.','');
		ex[2] = q;
		var tmpv = '';
		for(var i=0; i<ex.length; i++){
			tmpv += ex[i];
			if(i!=(ex.length-1))tmpv += '|'; 
		}
		v.value = tmpv;
		hr.$('vcart_t_g'+g).innerHTML = emprice;
		}catch(e){
			hrvc.onerror();
		}
	},
	setringsize: function (g,q){
		if(this.items < 1) this.setitems();
		this.loading();
		try{
			hrvc.getcv();
			hrvc.set_ringsize(this.items[g],q);
		}catch(e){
			this.loading(1);
			return false;
		}
		setTimeout("hrvc.update()",500);
		setTimeout("vivocart.loading(1)",1000);
	},
	loading: function (r){
		var mid = hr.$(this.mid);
		var lid = hr.$('hrvcload');
		if(r){
			lid.style.display = 'none';
			lid.style.height = '1px';
			return false;
		}
		var midpoint = Math.ceil((mid.offsetHeight/2))-25;
		hr.$('hrvcloadimg').style.marginTop = midpoint + "px";
		lid.style.height = mid.offsetHeight + "px";
		lid.style.display = 'block';
	},
	countamout: function (){
		var m = hr.$(this.mid).getElementsByTagName('input');
		var v=0;
		for(var i=0; i<m.length; i++){
			if(m.item(i).name=='vcart_iprice[]'){
				var o = hr.explode('|',m.item(i).value);
				v = v+(o[1]*o[2]);
			}
		}
		
		hr.$('vcart_t_subtotal').innerHTML = number_format(v,2,'.','');
		
		var vt = vivocart.setGST(v);
		
		// calculate wire discount
		var getp_Discount = hr.$('vcart_i_discount').value;
		var discount = round(((vt*getp_Discount)/100),2);
		hr.$('vcart_t_wdiscount').innerHTML = number_format(discount,2,'.','');
		
		vt = vt-discount;
		hr.$('vcart_t_total').innerHTML = number_format(vt,2,'.','');
	},
	setGST: function (p){
		var tex = 1.07;
		var total = p * tex;
		var texTotal = total - p;
		
		var shiptothis = getRadioValue(document.vivocart_form,'ship_to_same_address');
		var getCountry = hr.$('shipto_billing_country').value;
		if(shiptothis=='1'){
			getCountry = hr.$('billing_country').value;
		}
		
		if(getCountry!='SG'){
			total = p;
			texTotal = '0.00';
		}
		texTotal = number_format(texTotal,2);
		hr.$('vcart_t_gst').innerHTML = texTotal;
		hr.$('vcart_i_tax').value=texTotal;
		return total;
	},
	selectedqty: function (id,q){
		var selectObj = hr.$(id);
		var opt = selectObj.getElementsByTagName('option');
		for(var i=0; i<opt.length; i++){
			if(opt.item(i).value==q)
				opt.item(i).selected = true;
			else
				opt.item(i).selected = false;
		}
	}
}

function round(val,precision,mode) {
    var retVal=0,v='',integer='',decimal='',decp=0,negative=false;
    var _round_half_oe = function (dtR,dtLa,even){
        if (even === true) {
            if (dtLa == 50) {
                if ((dtR % 2) === 1) {
                    if (dtLa >= 5) {
                        dtR+=1;
                    } else {
                        dtR-=1;
                    }
                }
            }else if (dtLa >= 5) {
                dtR+=1;
            }
        }else{
            if (dtLa == 5) {
                if ((dtR % 2) === 0) {
                    if (dtLa >= 5) {
                        dtR+=1;
                    }else{
                        dtR-=1;
                    }
                }
            }else if (dtLa >= 5) {
                dtR+=1;
            }
        }

        return dtR;
    };
    var _round_half_ud = function (dtR,dtLa,up) {
        if (up === true) {
            if (dtLa>=5) {
                dtR+=1;
            }
        }else{
            if (dtLa>5) {
                dtR+=1;
            }
        }
        return dtR;
    };
    var _round_half = function (val,decplaces,mode){
    /*Declare variables
         *V       - string representation of Val
         *Vlen    - The length of V - used only when rounding intgerers

         *VlenDif - The difference between the lengths of the original V
         *          and the V after being truncated
         *decp    - character in index of . [decimal place] in V
         *integer - Integr protion of Val
         *decimal - Decimal portion of Val
         *DigitToRound - The digit to round

         *DigitToLookAt- The digit to comapre when rounding
         *
         *round - A function to do the rounding
         */
        var v = val.toString(),vlen=0,vlenDif=0;
        var decp = v.indexOf('.');

        var digitToRound = 0,digitToLookAt = 0;
        var integer='',decimal='';
        var round = null,bool=false;
        switch (mode) {
            case 'up':
                bool = true;
                // Fall-through
            case 'down':
                round = _round_half_ud;
                break;
            case 'even':
                bool = true;
            case 'odd':
                round = _round_half_oe;
                break;
        }
        if (decplaces < 0){ //Int round
            vlen=v.length;

            decplaces = vlen + decplaces;
            digitToLookAt = Number(v.charAt(decplaces));
            digitToRound  = Number(v.charAt(decplaces-1));
            digitToRound  = round(digitToRound,digitToLookAt,bool);
            v = v.slice(0,decplaces-1);
            vlenDif = vlen-v.length-1;

            if (digitToRound == 10){
                v = String(Number(v)+1)+"0";
            }else{
                v+=digitToRound;
            }

            v = Number(v)*(Math.pow(10,vlenDif));
        }else if (decplaces > 0){
            integer=v.slice(0,decp);
            decimal=v.slice(decp+1);
            digitToLookAt = Number(decimal.charAt(decplaces));

            digitToRound  = Number(decimal.charAt(decplaces-1));
            digitToRound  = round(digitToRound,digitToLookAt,bool);
            decimal=decimal.slice(0,decplaces-1);
            if (digitToRound==10){
                v=Number(integer+'.'+decimal)+(1*(Math.pow(10,(0-decimal.length))));
            }else{
                v=Number(integer+'.'+decimal+digitToRound);
            }
        }else{
            integer=v.slice(0,decp);
            decimal=v.slice(decp+1);
            digitToLookAt = Number(decimal.charAt(decplaces));

            digitToRound  = Number(integer.charAt(integer.length-1));
            digitToRound  = round(digitToRound,digitToLookAt,bool);
            decimal='0';
            integer = integer.slice(0,integer.length-1);
            if (digitToRound==10){
                v=Number(integer)+1;
            }else{
                v=Number(integer+digitToRound);
            }
        }
        return v;
    };


    if (typeof precision == 'undefined') {
        precision = 0;
    }
    if (typeof mode == 'undefined') {
        mode = 'PHP_ROUND_HALF_UP';
    }

    if (val < 0){
        negative = true;
    }else{
        negative = false;
    }

    v = Math.abs(val).toString();
    decp = v.indexOf('.');
    if ((decp == -1) && (precision >=0)){
        return val;
    }else{
        if (decp == -1){
            integer = v;
            decimal = '0';
        }else{

            integer = v.slice(0,decp);
            if (precision >= 0){
                decimal = v.substr(decp+1,precision+1);
            }else{
                decimal = '0';
            }
        }
        if ((precision > 0) && (precision >= decimal.length)){
            return val;
        }else if ((precision < 0) && (Math.abs(precision) >= integer.length)){
            return 0;
        }
        val = Number(integer+'.'+decimal); 
    }


    switch (mode){
        case 0:
        case 'PHP_ROUND_HALF_UP':
            retVal = _round_half(val,precision,'up');
            break;
        case 1:
        case 'PHP_ROUND_HALF_DOWN':
            retVal = _round_half(val, precision,'down');
            break;
        case 2:
        case 'PHP_ROUND_HALF_EVEN':
            retVal = _round_half(val,precision,'even');
            break;
        case 3:
        case 'PHP_ROUND_HALF_ODD':
            retVal = _round_half(val,precision,'odd');
            break;
    }
    if (negative){
        return 0-retVal;
    }else{
        return retVal;
    }
}

var vcartform = {
	disable: false,
	submit: function (f){
		if(this.disable) return false;
		switch(this.focus){
			case "formB":
				this.saveB();
				break;
			case "formC":
				this.saveC();
				break;
			case "formD":
				this.saveD();
				break;
			case "formA":
			default:
				this.saveA();
				break;
		}
		return false;
	},
	focus:'formA',
	changepmtd:function (v){
		hr.$('pmtd_visa').style.display='none';
		hr.$('pmtd_bank_wire').style.display='none';
		hr.$('pmtd_pay_by_phone').style.display='none';
		switch(v){
			case "Visa":
			case "Mastercard":
				hr.$('pmtd_visa').style.display='block';
				break;
			case "Bank Wire":
				hr.$('pmtd_bank_wire').style.display='block';
				break;
			case "Credit Cart Installment":
			case "Pay by phone":
				hr.$('pmtd_pay_by_phone').style.display='block';
				break;
		}
		switch(v){
			case "Bank Wire":
				hr.$('vcart_i_discount').value='1.5';
				vivocart.countamout();
				break;
			default:
				hr.$('vcart_i_discount').value='0';
				vivocart.countamout();
				break;
		}
	},
	wstart: false,
	saveA: function (){
		var validA = new Array();
		validA[0] = Array('required','first_name');
		validA[1] = Array('required','last_name');
		validA[2] = Array('email','billto_email');
		validA[3] = Array('match','billto_email','confirm_email');
		validA[4] = Array('required','daytime_phone');
		if(!form_validation(document.vivocart_form,validA)) return false;
		var cvform1 = hr.$('cvform1');
		ck(cvform1,1);
		this.wstart = true;
		hr.$('cvform1_ac').style.backgroundImage = 'url(/i/tick.gif)';
		this.focus = 'formB';
		hr.$('cvform1_ac_oc').onclick = function (){
			if(vcartform.focus=='formA') return false;
			hr.$('cvform1_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			cvform1.style.display = '';
			ck(cvform1);
			vcartform.focus = 'formA';
			hr.$('cvform2_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			ck(hr.$('cvform2'),1);
			hr.$('cvform3_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			ck(hr.$('cvform3'),1);
			hr.$('cvform4_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			ck(hr.$('cvform4'),1);			
		}
		setTimeout(function (){
		 	hr.$('cvform2').style.display = '';
			ck(hr.$('cvform2'));
		},800);
	},
	saveB: function (){
		var i = 0;
		var validB = new Array();
		validB[i++] = Array('dropdown','payment_method');
		switch(document.vivocart_form.payment_method.value){
			case "Visa":
			case "Mastercard":
				validB[i++] = Array('required','card_number');
				validB[i++] = Array('dropdown','exp_month');
				validB[i++] = Array('dropdown','exp_year');
				validB[i++] = Array('required','cvv_number');
			break;
		}
		hr.$('type_of_address_err').style.color='';
		validB[i++] = Array('radio','type_of_address','exe='+"hr.$('type_of_address_err').style.color='red';");
		validB[i++] = Array('required','address1');
		validB[i++] = Array('required','city');
		validB[i++] = Array('required','postal_code');
		validB[i++] = Array('dropdown','billing_country');
		if(!form_validation(document.vivocart_form,validB)) return false;
		var sameaddress = getRadioValue(document.vivocart_form,'ship_to_same_address');
		ck(hr.$('cvform2'),1);
		hr.$('cvform2_ac').style.backgroundImage = 'url(/i/tick.gif)';
		
		hr.$('cvform2_ac_oc').onclick = function (){
			if(vcartform.focus=='formB' || vcartform.focus=='formA') return false;
			hr.$('cvform2_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			hr.$('cvform2').style.display = '';
			ck(hr.$('cvform2'));
			vcartform.focus = 'formB';
			hr.$('cvform3_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			ck(hr.$('cvform3'),1);
			hr.$('cvform4_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			ck(hr.$('cvform4'),1);			
		}
		
		if(sameaddress=='1'){
			hr.$('cvform3_ac').style.backgroundImage = 'url(/i/tick.gif)';
			this.focus = 'formD';
			setTimeout(function (){hr.$('cvform4').style.display = ''; ck(hr.$('cvform4'));},800);
		}else{
			this.focus = 'formC';
			setTimeout(function (){hr.$('cvform3').style.display = ''; ck(hr.$('cvform3'));},800);
		}
	},
	saveC: function (){
		var i = 0;
		var validC = new Array();
		hr.$('shipto_shipaddress_err').style.color='';
		validC[i++] = Array('radio','shipto_shipaddress','exe='+"hr.$('shipto_shipaddress_err').style.color='red';");
		validC[i++] = Array('required','shipto_address1');
		validC[i++] = Array('required','shipto_city');
		validC[i++] = Array('required','shipto_postal_code');
		validC[i++] = Array('dropdown','shipto_billing_country');
		if(!form_validation(document.vivocart_form,validC)) return false;
		ck(hr.$('cvform3'),1);
		hr.$('cvform3_ac').style.backgroundImage = 'url(/i/tick.gif)';
		this.focus = 'formD';
		
		hr.$('cvform3_ac_oc').onclick = function (){
			var sameaddress = getRadioValue(document.vivocart_form,'ship_to_same_address');
			if(vcartform.focus=='formC' || vcartform.focus=='formB' || vcartform.focus=='formA' || sameaddress =='1') return false;
			hr.$('cvform3_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			hr.$('cvform3').style.display = '';
			ck(hr.$('cvform3'));
			vcartform.focus = 'formC';
			hr.$('cvform4_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			ck(hr.$('cvform4'),1);			
		}
		
		setTimeout(function (){hr.$('cvform4').style.display = ''; ck(hr.$('cvform4'));},800);
	},
	saveD: function (){
		hrvc.getcv();
		var str = hrvc.arraytostr();
		if(document.vivocart_form.cartstr){
			document.vivocart_form.cartstr.value = str;
		}else{
			hr.$('vc_cartstr').value = str;
		}
		document.vivocart_form.submit();
	},
	disableCvform: function (){
		var cvform1 = hr.$('cvform1');
		this.disable = true;
		if(this.wstart){
			vcartform.focus = 'formA';
			cvform1.style.display = '';
			hr.$('cvform1_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			ck(cvform1);
			hr.$('cvform2_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			ck(hr.$('cvform2'),1);
			hr.$('cvform3_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			ck(hr.$('cvform3'),1);
			hr.$('cvform4_ac').style.backgroundImage = 'url(/i/tick_blank.gif)';
			ck(hr.$('cvform4'),1);
		}
		setTimeout(function (){
			hr.$('hrvcload_form').style.height = (cvform1.offsetHeight-1) + "px";
			hr.$('hrvcload_form').style.display = '';
		},800);
	},
	changeCountry: function (c){
		vivocart.countamout();
	}
}

function saved(){
	this.play = true;
	this.timeout = 10;
	this.idadd = 0;
	this.totalheight = 0;
	this.getheight = 0;
}

function ck(obj,p){
	if(navi.play == false) return false;
	var getul = obj;
	var getil = getul.getElementsByTagName('div').item(0);
	var totalheight = 0;
	totalheight	= getil.offsetHeight;
	
	if(!p){
		pl(getul.id,0,totalheight);
	}else{
		plb(getul.id,getul.offsetHeight,0);
	}
}

function pl(id,gh,gt){
	var obj = document.getElementById(id);
	gt = (gt-0);
	gh = (gh-0);
	if(gh >= gt){
		obj.style.height = gt + "px";
		obj.style.height = "";
		return false;
	}
	gh = (gh-0) + navi.idadd;
	obj.style.height = gh + "px";

	setTimeout("pl('"+id+"','"+gh+"','"+gt+"')",navi.timeout);
}

function plb(id,gh,gt){
	try{
	var obj = document.getElementById(id);
	gt = (gt-0);
	gh = (gh-0);
	gh = (gh-0) - navi.idadd; 
	if(gh <= gt){
		obj.style.height = "0px";
		obj.style.display = 'none';
		return false;
	}
	obj.style.height = gh + "px";

	setTimeout("plb('"+id+"','"+gh+"','"+gt+"')",navi.timeout);
	}catch(e){
		alert(e.message);
	}
}

// Navigation settings
var navi = new saved();
	navi.timeout = 25;
	navi.idadd = 30;
