
// Initiate Ajax Call here to load secondary tasks
function recalculatePOSTotal(){
	//alert("Caling recalculatePOSTotal");
	var vegQty = document.posForm.pos_veg.value;
	var nVegQty = document.posForm.pos_nveg.value;
	var vegQtyRed = document.posForm.pos_vegR.value;
	var nVegQtyRed = document.posForm.pos_nvegR.value;	
	
	
	
	//alert("In Recalc Method: Veg-nVeg-vegR-nVegR:"+vegQty+"-"+nVegQty+"-"+vegQtyRed+"-"+nVegQtyRed+"<<");
	if(IsNumeric(vegQty) && IsNumeric(nVegQty) && IsNumeric(vegQtyRed) && IsNumeric(nVegQtyRed)) {
		var totQty = eval(Number(vegQty)+Number(nVegQty)+Number(vegQtyRed)+Number(nVegQtyRed));
		document.posForm.pos_paid.value = totQty*5;
    } else {
    	alert("ERROR: Invalid Number!");
    }
}


// Get the HTTP Object
function getHTTPObject(){
   if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
   else if (window.XMLHttpRequest) return new XMLHttpRequest();
   else {
      alert("Your browser does not support AJAX. Please Contact System Administrator");
      return null;
   }
}   
 
 
// Initiate Ajax Call here to load secondary tasks
function recalculateTotal(){
	//alert("Caling recalculateTotal");
	var vegQty = document.orderForm.vegQty.value;
	var nVegQty = document.orderForm.nonVegQty.value;
	var vegQtyRed = document.orderForm.vegQtyRed.value;
	var nVegQtyRed = document.orderForm.nonVegQtyRed.value;	
	
	//alert("In Recalc Method: Veg-nVeg-vegR-nVegR:"+vegQty+"-"+nVegQty+"-"+vegQtyRed+"-"+nVegQtyRed+"<<");
	if(IsNumeric(vegQty) && IsNumeric(nVegQty) && IsNumeric(vegQtyRed) && IsNumeric(nVegQtyRed)) {
		controlDeliveryOption(); // Call everytime change the delivery
			 
		//alert("Calling Ajax Method...#"+vegQty+"#"+nVegQty+vegQtyRed);
	    httpObject = getHTTPObject();
	    if (httpObject != null) {
	        httpObject.open("GET", "ajaxProcessing.php?action=CALC_TOTAL&Veg_Qty="+vegQty+"&NVeg_Qty="+nVegQty+"&Veg_QtyRed="+vegQtyRed+"&NVeg_QtyRed="+nVegQtyRed, true);
	        httpObject.send(null); 
	        httpObject.onreadystatechange = setTotal;
	        //--------------- enable disable delivery option 
	    }
    } else {
    	alert("ERROR: Invalid Number!");
    }
}

// Save Admin Comments on the fly
function saveAdminComments(orderId, comments) {
	//alert("Calling Ajax Method...:"+comments);
    httpObject = getHTTPObject();
    if (httpObject != null) {
        httpObject.open("GET", "ajaxProcessing.php?action=ADMIN_COMMENTS&Comments="+comments+"&OrderId="+orderId, true);
        httpObject.send(null); 
        httpObject.onreadystatechange = setComments;
        //--------------- enable disable delivery option
        
    }
}

// Save collected status on the fly
function saveCollected(orderId, collBool) {
	var collFlag = "N";
	if (collBool) {
		collFlag = "Y";
	}
	//alert("Calling Ajax Method...:"+collFlag);
    httpObject = getHTTPObject();
    if (httpObject != null) {
        httpObject.open("GET", "ajaxProcessing.php?action=COLLECTED&Collected="+collFlag+"&OrderId="+orderId, true);
        httpObject.send(null); 
        httpObject.onreadystatechange = setCollected;
        //--------------- enable disable delivery option
        
    }
}

// Save Paid Status on the fly
function savePaid(orderId, paidAmt) {
	
	//alert("Calling Ajax Method...PAID:"+paidAmt);
    httpObject = getHTTPObject();
    if (httpObject != null) {
        httpObject.open("GET", "ajaxProcessing.php?action=PAID&PaidAmt="+paidAmt+"&OrderId="+orderId, true);
        httpObject.send(null); 
        httpObject.onreadystatechange = setPaid;
		// --- Update balance amount---
        var totAmt = document.getElementById(orderId+"_TOT").innerHTML;
        var balAmt = eval(Number(totAmt)-Number(paidAmt));
        document.getElementById(orderId+"_BAL").innerHTML = balAmt;
    }
}

// Enable disable the quantity Update Fields
function showHideQtyFields(orderId) {
	
	//alert("JS Methid:showHideQtyFields: OrderId:"+orderId);
	
	// Read Only Fields
	var rf1 = orderId+"_NVEG_QTY_R";
	openHideSection(rf1);
	var rf2 = orderId+"_VEG_QTY_R";
	openHideSection(rf2);
	var rf3 = orderId+"_NVEG_QTYR_R";
	openHideSection(rf3);
	var rf4 = orderId+"_VEG_QTYR_R";
	openHideSection(rf4);

	// Editable Fields
	var ef1 = orderId+"_NVEG_QTY_E";
	openHideSection(ef1);
	var ef2 = orderId+"_VEG_QTY_E";
	openHideSection(ef2);
	var ef3 = orderId+"_NVEG_QTYR_E";
	openHideSection(ef3);
	var ef4 = orderId+"_VEG_QTYR_E";
	openHideSection(ef4);
	
}

function openHideSection(obj) {
	//alert ("actionOn:"+obj+":");
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';
	} else {
		el.style.display = '';
	}
}

var typeField = "";
var qtyValue = "";

// Save Quantities by Administrator
function updateQuantity(orderId, type, qty) {
	typeField = type;
	qtyValue = qty
	//alert("Calling Ajax Method...:"+qty+"::"+type);
    httpObject = getHTTPObject();
    if (httpObject != null) {
        httpObject.open("GET", "ajaxProcessing.php?action=QTY&Type="+type+"&Qty="+qty+"&OrderId="+orderId, true);
        httpObject.send(null); 
        httpObject.onreadystatechange = setQuantity;
        //-------- Update Read Only Field	
        var lableField = orderId+"_"+type+"_R";
        document.getElementById(lableField).innerHTML = qty;
        //------- Update Total Amt due
		var ef1 = orderId+"_NVEG_QTY_R";
		var ef2 = orderId+"_VEG_QTY_R";
		var ef3 = orderId+"_NVEG_QTYR_R";
		var ef4 = orderId+"_VEG_QTYR_R";
        var total = eval(Number(document.getElementById(ef1).innerHTML)+Number(document.getElementById(ef2).innerHTML)+Number(document.getElementById(ef3).innerHTML)+Number(document.getElementById(ef4).innerHTML));
		var tolAmt = eval(Number(total)*5);
		document.getElementById(orderId+"_TOT").innerHTML = tolAmt;
		//---------Update Balance Amt 
		var balFieldId = orderId+"_BAL";
		var paidFieldId = orderId+"_PAID";
		var paidAmt = document.getElementById(paidFieldId).value;
        var balAmt = eval(Number(tolAmt)-Number(paidAmt));
        document.getElementById(balFieldId).innerHTML = balAmt;
		
        
    }
}

// Ajax call back after saving comments 
function setComments() {
	//alert("setComments Method");
    if(httpObject.readyState == 4){
    	//alert("Comments Saved: Ret Value:-->>"+httpObject.responseText);
    }	
}

// Ajax call back after saving collected Flag 
function setCollected() {
	//alert("setCollected Method");
    if(httpObject.readyState == 4){
    	//alert("setCollected Saved: Ret Value:-->>"+httpObject.responseText);
    }	
}

// Ajax call back after Paid amount 
function setPaid() {
	//alert("setPaid Method");
    if(httpObject.readyState == 4){
    	//alert("setPaid Saved: Ret Value:-->>"+httpObject.responseText);
    }	
}

// Ajax call back after update quantities 
function setQuantity() {
	//alert("setQuantity Method");
    if(httpObject.readyState == 4){
    	//alert("setPaid Saved: Ret Value:-->>"+httpObject.responseText);
    }	
}

// Change the values of subtask dropdown
function setTotal(){
	//alert("Caling setTotal");
    if(httpObject.readyState == 4){
    	//alert("Got Value from Server:-->>"+httpObject.responseText);
        document.getElementById('CALC_TOTAL').innerHTML = httpObject.responseText;
    }
}

// Change readonly option for delivery
function enableDelivery() {
    if(httpObject.readyState == 4){
    	//alert("Got Value from Server:-->>"+httpObject.responseText);
        document.getElementById('CALC_TOTAL').innerHTML = httpObject.responseText;
    }

}


function controlDeliveryOption(){
	//alert("Caling Control Delivery");
	var vegQty = document.orderForm.vegQty.value;
	var nVegQty = document.orderForm.nonVegQty.value;
	var vegQtyRed = document.orderForm.vegQtyRed.value;
	var nVegQtyRed = document.orderForm.nonVegQtyRed.value;	
	var totQty = eval(Number(vegQty)+Number(nVegQty)+Number(vegQtyRed)+Number(nVegQtyRed));
	var min4Delivery = Number(document.orderForm.min4del.value);
	
	//alert("TOT QTY:"+totQty+"MIN4DEL:"+min4Delivery);
	if(totQty >= min4Delivery) {
		document.getElementById("DELIVERY").style.display = ''; // Show
	} else {
		document.getElementById("DELIVERY").style.display = 'none';// Hide
	}
}
 
var httpObject = null;			  


function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

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 isValidPostalcode(postalcode) {
	if (postalcode.length == 6 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z]\d[a-zA-Z]\d$/) != -1) return true;
	else if (postalcode.length == 7 && postalcode.search(/^[a-zA-Z]\d[a-zA-Z](-|\s)\d[a-zA-Z]\d$/) != -1) return true;
	else return false;
}

//--------------------------------------------------------

var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 13;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object){
	phonevalue1 = ParseChar(object.value, zChar);
}

function ParseForNumber2(object){
	phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object,e) { 
	if(e){ 
		e = e 
	} else {
		e = window.event 
	} 

	if(e.which){ 
		var keycode = e.which 
	} else {
		var keycode = e.keyCode 
	}

	ParseForNumber1(object)

	if(keycode >= 48){
		ValidatePhone(object)
	}
}


function backspacerDOWN(object,e) { 
	if(e){ 
		e = e 
	} else {
		e = window.event 
	} 
	if(e.which){ 
		var keycode = e.which 
	} else {
		var keycode = e.keyCode 
	}
	ParseForNumber2(object)
} 

function GetCursorPosition(){

	var t1 = phonevalue1;
	var t2 = phonevalue2;
	var bool = false
	for (i=0; i<t1.length; i++) {
		if (t1.substring(i,1) != t2.substring(i,1)) {
			if(!bool) {
				cursorposition=i
				bool=true
			}
		}
	}
}

function ValidatePhone(object){

	var p = phonevalue1

	p = p.replace(/[^\d]*/gi,"")

	if (p.length < 3) {
		object.value=p
	} else if(p.length==3){
		pp=p;
		d4=p.indexOf('(')
		d5=p.indexOf(')')
		if(d4==-1){
			pp="("+pp;
		}
		if(d5==-1){
			pp=pp+")";
		}
		object.value = pp;
	} else if(p.length>3 && p.length < 7){
		p ="(" + p; 
		l30=p.length;
		p30=p.substring(0,4);
		p30=p30+")"
		
		p31=p.substring(4,l30);
		pp=p30+p31;
		
		object.value = pp; 

	} else if(p.length >= 7){
		p ="(" + p; 
		l30=p.length;
		p30=p.substring(0,4);
		p30=p30+")"
		
		p31=p.substring(4,l30);
		pp=p30+p31;
		
		l40 = pp.length;
		p40 = pp.substring(0,8);
		p40 = p40 + "-"
		
		p41 = pp.substring(8,l40);
		ppp = p40 + p41;
		
		object.value = ppp.substring(0, maxphonelength);
	}

	GetCursorPosition()

if(cursorposition >= 0){
if (cursorposition == 0) {
cursorposition = 2
} else if (cursorposition <= 2) {
cursorposition = cursorposition + 1
} else if (cursorposition <= 5) {
cursorposition = cursorposition + 2
} else if (cursorposition == 6) {
cursorposition = cursorposition + 2
} else if (cursorposition == 7) {
cursorposition = cursorposition + 4
e1=object.value.indexOf(')')
e2=object.value.indexOf('-')
if (e1>-1 && e2>-1){
if (e2-e1 == 4) {
cursorposition = cursorposition - 1
}
}
} else if (cursorposition < 11) {
cursorposition = cursorposition + 3
} else if (cursorposition == 11) {
cursorposition = cursorposition + 1
} else if (cursorposition >= 12) {
cursorposition = cursorposition
}

var txtRange = object.createTextRange();
txtRange.moveStart( "character", cursorposition);
txtRange.moveEnd( "character", cursorposition - object.value.length);
txtRange.select();
}

}

function ParseChar(sStr, sChar)
{
if (sChar.length == null) 
{
zChar = new Array(sChar);
}
else zChar = sChar;

for (i=0; i<zChar.length; i++)
{
sNewStr = "";

var iStart = 0;
var iEnd = sStr.indexOf(sChar[i]);

while (iEnd != -1)
{
sNewStr += sStr.substring(iStart, iEnd);
iStart = iEnd + 1;
iEnd = sStr.indexOf(sChar[i], iStart);
}
sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

sStr = sNewStr;
}

return sNewStr;
}


