//if (top.frames.length == 0) 
//top.document.location = "/sorry.cfm"; 

var phone_field_length=0;
function TabNext(obj,event,len,next_field) {
	if (event == "down") {
		phone_field_length=obj.value.length;
		}
	else if (event == "up") {
		if (obj.value.length != phone_field_length) {
			phone_field_length=obj.value.length;
			if (phone_field_length == len) {
				next_field.focus();
			}
		}
	}
}

function getLL(address_field, city_field, province_field, zipcode_field, latitude_field, longitude_field)	{
	
	var address = document.getElementById(address_field);
	var city = document.getElementById(city_field);
	var province = document.getElementById(province_field);
	var zipcode = document.getElementById(zipcode_field);
	
	if( !_CF_hasValue(address, "TEXT", true ) )
    {
        alert("Please enter Address 1.");
        return false;
    }
	
	if( !_CF_hasValue(city, "TEXT", true ) )
    {
        alert("Please enter City.");
        return false;
    }
	
	if( _CF_hasValue(zipcode, "TEXT", false ) )
    {
        if (!_CF_checkzip(zipcode.value, true))
        {
            alert("Please enter valid Zip Code.");
            return false;
        }
    }
	
	// send data to CF
	DWRUtil.useLoadingMessage();
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'ax_getLL', address.value, city.value, province.value, zipcode.value, latitude_field, longitude_field, getLLResult);
}

// call back function
function getLLResult(r) {

	var latitude = document.getElementById(r.latitude_field);
	var longitude = document.getElementById(r.longitude_field);
	
    if(r.error == 0) {
		latitude.value = r.latitude;
		longitude.value = r.longitude;
	} else {
		alert(r.error_detail);
	}
}

function storeLookup(form, formName, address, city, province, zipCode, datereq, timeReq, delStore, picStore, orderID) {
	if( _CF_hasValue(form['date_req'], "TEXT", false ) )
    {
        //form element date_req 'DATE' validation checks
        if (!_CF_checkdate(form['date_req'].value, true))
        {
            alert("Please enter valid date required");
            return false;
        }

    }else {
        alert("Please enter date required");
        return false;
    }
		
	if(timeReq == 0) {
		alert("Please enter select time required.");
		return false;
	}
	
	if( !_CF_hasValue(form['Add1'], "TEXT", true ) )
    {
        alert("Please enter Address 1.");
        return false;
    }
	
	if( !_CF_hasValue(form['city'], "TEXT", true ) )
    {
        alert("Please enter City.");
        return false;
    }
	
	if( _CF_hasValue(form['postal'], "TEXT", false ) )
    {
        if (!_CF_checkzip(form['postal'].value, true))
        {
            alert("Please enter valid Zip Code.");
            return false;
        }
    }
	
	newWindow = window.open("main.cfm?app_action=order&action=store_lookup&form=" + formName + "&address=" + address + "&city=" + city + "&province=" + province + "&zipcode=" + zipCode + "&datereq=" + datereq + "&timereq=" + timeReq + "&del_store=" + delStore + "&pic_store=" + picStore + "&order_id=" + orderID, "NewWindow", "toolbar=no,width=775,height=500;,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no" );
	
	return true;
}

function storePick(pickup, value) {
	if(pickup == true) {
		document.getElementById("pic_store").value = value;
	} else {
		document.getElementById("del_store").value = value;
	}
}

function GeneratePassword() {
    if (parseInt(navigator.appVersion) <= 3) {
        alert("Sorry this only works in 4.0+ browsers");
        return true;
    }
    var length=8;
    var sPassword = "";
    length = 6;
    var noPunction = "checked";
    var randomLength = "";
    if (randomLength) {
        length = Math.random();
        length = parseInt(length * 100);
        length = (length % 7) + 6
    }

    for (i=0; i < length; i++) {
        numI = getRandomNum();
        if (noPunction) { while (checkPunc(numI)) { numI = getRandomNum(); } }
        sPassword = sPassword + String.fromCharCode(numI);
		
    }
    document.client.client_password.value = sPassword
    return true;
}

function getRandomNum() {
    // between 0 - 1
    var rndNum = Math.random()
    // rndNum from 0 - 1000
    rndNum = parseInt(rndNum * 1000);
    // rndNum from 33 - 127
    rndNum = (rndNum % 94) + 33;
    return rndNum;
}

function checkPunc(num) {
    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }
    return false;
}

function checkAll(elementName){
     var boolValue = elementName.checked;
     for (var i=0;i<document.orderList.elements.length;i++)
     {
       var e = document.orderList.elements[i];
       if (e.name != elementName.name)
         e.checked = boolValue;
     }
}

function formatCurrency(num) {

	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	//num = num.substring(0,num.length-(4*i+3))+','+	//Min removed Jun 01, 2004
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}


function show(obj){	
	if(!document.all && document.getElementById) {
		if(document.getElementById(obj)) document.getElementById(obj).style.display='';
	}else {
        	if(document.all[obj]) document.all[obj].style.display='';
	}
}

function hide(obj){
	if(!document.all && document.getElementById) {
		if(document.getElementById(obj)) document.getElementById(obj).style.display='none';
	}
	else {	
		if(document.all[obj]) document.all[obj].style.display='none';
	}
}

function ccwin(url){
	newWindow = window.open(url,"NewWindow","toolbar=no,width=230,height=180;,directories=no,status=no,scrollbars=no,resize=no,menubar=no" )
}
function newin1(url){
	newWindow = window.open(url,"NewWindow","toolbar=no,width=500,height=350,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no" )
}
function newin2(url){
	newWindow = window.open(url,"NewWindow","toolbar=no,width=700,height=600,directories=no,status=no,scrollbars=yes,resizable=no,menubar=no" )
}
function newin2_noScrollBar(url){
	newWindow = window.open(url,"NewWindow","toolbar=no,width=700,height=600,directories=no,status=no,scrollbars=no,resizable=no,menubar=no" )
}
function newin3(url){
	newWindow = window.open(url,"NewWindow","toolbar=no,width=500,height=200,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no" )
}
function newin4(url){
	newWindow = window.open(url,"NewWindow","toolbar=no,width=270,height=120,left=790,top=200,directories=no,status=no,scrollbars=no,resizable=no,menubar=no" )
}
function newin5(url){
	newWindow = window.open(url,"NewWindow","toolbar=no,width=500,height=100,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no" )
}
function newin6(url){
	newWindow = window.open(url,"NewWindow","toolbar=no,width=500,height=600,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no" )
}
function newin7(url){
	newWindow = window.open(url,"NewWindow","toolbar=no,width=695,height=650,directories=no,status=no,scrollbars=yes,resizable=no,menubar=no" )
}

// Set the horizontal and vertical position for the popup
PositionX = 400;
PositionY = 100;

// Set these value approximately 20 pixels greater than the
// size of the largest image to be used (needed for Netscape)
defaultWidth  = 500;
defaultHeight = 500;

// Set autoclose true to have the window close automatically
// Set autoclose false to allow multiple popup windows
var AutoClose = true;

if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
function popImage(imageURL,imageTitle){
	if (isNN){imgWin=window.open('about:blank','',optNN);}
	if (isIE){imgWin=window.open('about:blank','',optIE);}
	with (imgWin.document){
		writeln('<html><head><title>Loading...</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
		writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
		writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
		writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
		writeln('width=100-(document.body.clientWidth-document.images[0].width);');
		writeln('height=100-(document.body.clientHeight-document.images[0].height);');
		writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
		writeln('window.innerWidth=document.images["Min"].width;');writeln('window.innerHeight=document.images["Min"].height;}}');
		writeln('function doTitle(){document.title="'+imageTitle+'";}');writeln('</sc'+'ript>');
		if (!AutoClose) writeln('</head><body bgcolor=ffffff scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
		else writeln('</head><body bgcolor=ffffff scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
		writeln('<img name="Min" src="'+imageURL+'" style="display:block"></body></html>');
		close();		
	}
}

// Disables button and replaces text with 'Proceessing...' ***********************************************
var speed=500;
var counter=0;
function Flash() {
	document.myForm.Go.disabled = true;
	if (counter == 0) {
		document.myForm.Go.value="Processing      ";counter=1;
	}else if(counter == 1){
		document.myForm.Go.value="Processing .    ";counter=2;
	}else if(counter == 2){
		document.myForm.Go.value="Processing . .  ";counter=3;
	}else if(counter == 3){
		document.myForm.Go.value="Processing . . .";counter=0;
	}
	window.setTimeout('Flash()', speed);
}

function popUpWindow(url, width, height){
	newWindow = window.open(url,"NewWindow","toolbar=no,width="+width+",height="+height+",directories=no,status=no,scrollbars=yes,resizable=yes,location=no,menubar=no" )
}

function choice(arg){
	//return random index number in valid range of arg array
	return Math.floor(Math.random()*arg.length);
}

function randstr(arg){
	//return random argument of arg array
	var str = '';
	var seed = choice(arg);
	str = arg[seed];
	return str;
}

function mkpass(){
	//password length
	var pass_len=8;

	var cons_lo = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z'];
	var cons_up = ['B','C','D','F','G','H','J','K','L','M','N','P','Q','R','S','T','V','W','X','Y','Z'];
	var hard_cons_lo = ['b','c','d','f','g','h','k','m','p','s','t','v','z'];
	var hard_cons_up = ['B','C','D','F','G','H','K','M','P','S','T','V','Z'];
	var link_cons_lo = ['h','l','r']; 
	var link_cons_up = ['H','L','R']; 
	var vowels_lo = ['a','e','i','o','u'];
	var vowels_up = ['A','E','I','U']; //O (letter o) and 0 (number zero) get confused
	var digits = ['1','2','3','4','5','6','7','8','9'];

	//change at will how many times digits appears in names array. Order doesn't matter
	var names = [cons_lo, cons_up, digits, hard_cons_lo, hard_cons_up, digits, link_cons_up, digits];

	var newpass= '';
	for(i=0; i<pass_len; i++)
    newpass = newpass + randstr(names[choice(names)]);

	return newpass;
}

